[llvm] [VectorCombine] Fold reduce(trunc(x)) -> trunc(reduce(x)) iff cost effective (PR #81852)
Phoebe Wang via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 17 00:26:09 PST 2024
================
@@ -1526,6 +1528,60 @@ bool VectorCombine::foldShuffleFromReductions(Instruction &I) {
return foldSelectShuffle(*Shuffle, true);
}
+/// Determine if its more efficient to fold:
+/// reduce(trunc(x)) -> trunc(reduce(x)).
+bool VectorCombine::foldTruncFromReductions(Instruction &I) {
+ auto *II = dyn_cast<IntrinsicInst>(&I);
+ if (!II)
+ return false;
+
+ Intrinsic::ID IID = II->getIntrinsicID();
+ switch (IID) {
+ case Intrinsic::vector_reduce_add:
+ case Intrinsic::vector_reduce_mul:
+ case Intrinsic::vector_reduce_and:
+ case Intrinsic::vector_reduce_or:
+ case Intrinsic::vector_reduce_xor:
+ break;
+ default:
+ return false;
+ }
+
+ unsigned ReductionOpc = getArithmeticReductionInstruction(IID);
+ Value *ReductionSrc = I.getOperand(0);
+
+ Value *TruncSrc;
+ if (!match(ReductionSrc, m_OneUse(m_Trunc(m_Value(TruncSrc)))))
+ return false;
+
+ auto *Trunc = cast<CastInst>(ReductionSrc);
+ auto *TruncSrcTy = cast<VectorType>(TruncSrc->getType());
+ auto *ReductionSrcTy = cast<VectorType>(ReductionSrc->getType());
+ Type *ResultTy = I.getType();
+
+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
+ InstructionCost OldCost =
+ TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
+ TTI::CastContextHint::None, CostKind, Trunc) +
+ TTI.getArithmeticReductionCost(ReductionOpc, ReductionSrcTy, std::nullopt,
+ CostKind);
+ InstructionCost NewCost =
+ TTI.getArithmeticReductionCost(ReductionOpc, TruncSrcTy, std::nullopt,
+ CostKind) +
+ TTI.getCastInstrCost(Instruction::Trunc, ResultTy,
+ ReductionSrcTy->getScalarType(),
+ TTI::CastContextHint::None, CostKind);
+
+ if (OldCost < NewCost || !NewCost.isValid())
----------------
phoebewang wrote:
`<=`?
https://github.com/llvm/llvm-project/pull/81852
More information about the llvm-commits
mailing list