[llvm] [AArch64][GlobalISel] Add custom legalization for v4s8 = G_TRUNC v4s16 (PR #85610)
Thorsten Schütt via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 18 01:42:58 PDT 2024
================
@@ -1262,11 +1262,45 @@ bool AArch64LegalizerInfo::legalizeCustom(
return legalizeDynStackAlloc(MI, Helper);
case TargetOpcode::G_PREFETCH:
return legalizePrefetch(MI, Helper);
+ case TargetOpcode::G_TRUNC:
+ return legalizeTrunc(MI, Helper);
}
llvm_unreachable("expected switch to return");
}
+bool AArch64LegalizerInfo::legalizeTrunc(MachineInstr &MI,
+ LegalizerHelper &Helper) const {
+ assert(MI.getOpcode() == TargetOpcode::G_TRUNC);
+
+ // Handle <4 x s8> = G_TRUNC <4 x s16> by widening to <8 x s16> first.
+ // So the sequence is:
+ // %orig_val(<4 x s16>) = ...
+ // %wide = G_MERGE_VALUES %orig_val, %undef:_(<4 x s16>)
+ // %wide_trunc:_(<8 x s8>) = G_TRUNC %wide
+ // %bc:_(<2 x s32>) = G_BITCAST %wide_trunc
+ // %eve:_(s32) = G_EXTRACT_VECTOR_ELT %bc, 0
+ // %final:_(<4 x s8>) = G_BITCAST %eve
+
+ MachineIRBuilder &MIB = Helper.MIRBuilder;
+
+ auto [DstReg, DstTy, SrcReg, SrcTy] = MI.getFirst2RegLLTs();
+ assert(DstTy == LLT::fixed_vector(4, LLT::scalar(8)) &&
+ SrcTy == LLT::fixed_vector(4, LLT::scalar(16)));
+
+ auto WideTy = LLT::fixed_vector(8, LLT::scalar(16));
+ auto Undef = MIB.buildUndef(SrcTy);
+ auto Merge = MIB.buildMergeLikeInstr(WideTy, {SrcReg, Undef});
+ auto Trunc = MIB.buildTrunc(LLT::fixed_vector(8, LLT::scalar(8)), Merge);
+ auto BC = MIB.buildBitcast(LLT::fixed_vector(2, LLT::scalar(32)), Trunc);
+ auto Extract = MIB.buildExtractVectorElement(
+ LLT::scalar(32), BC, MIB.buildConstant(LLT::scalar(32), 0));
----------------
tschuett wrote:
The index should be s64, see mir test.
https://github.com/llvm/llvm-project/pull/85610
More information about the llvm-commits
mailing list