[llvm] [VPlan] Impl VPlan-based pattern match for ExtendedRed and MulAccRed (PR #113903)
Sander de Smalen via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 23 07:14:38 PDT 2025
================
@@ -2548,6 +2630,172 @@ void VPlanTransforms::handleUncountableEarlyExit(
LatchExitingBranch->eraseFromParent();
}
+/// This function tries convert extended in-loop reductions to
+/// VPExtendedReductionRecipe and clamp the \p Range if it is beneficial and
+/// valid. The created recipe must be lowered to concrete
+/// recipes before execution.
+static VPExtendedReductionRecipe *
+tryToMatchAndCreateExtendedReduction(VPReductionRecipe *Red, VPCostContext &Ctx,
+ VFRange &Range) {
+ using namespace VPlanPatternMatch;
+
+ Type *RedTy = Ctx.Types.inferScalarType(Red);
+ VPValue *VecOp = Red->getVecOp();
+
+ // Test if using extended-reduction is profitable and clamp the range.
+ auto IsExtendedRedValidAndClampRange = [&](unsigned Opcode, bool isZExt,
+ Type *SrcTy) -> bool {
+ return LoopVectorizationPlanner::getDecisionAndClampRange(
+ [&](ElementCount VF) {
+ auto *SrcVecTy = cast<VectorType>(toVectorTy(SrcTy, VF));
+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
+ InstructionCost ExtRedCost = Ctx.TTI.getExtendedReductionCost(
+ Opcode, isZExt, RedTy, SrcVecTy, Red->getFastMathFlags(),
+ CostKind);
+ InstructionCost ExtCost =
+ cast<VPWidenCastRecipe>(VecOp)->computeCost(VF, Ctx);
+ InstructionCost RedCost = Red->computeCost(VF, Ctx);
+ return ExtRedCost.isValid() && ExtRedCost < ExtCost + RedCost;
+ },
+ Range);
+ };
+
+ VPValue *A;
+ // Match reduce(ext)).
+ if (match(VecOp, m_ZExtOrSExt(m_VPValue(A))) &&
+ IsExtendedRedValidAndClampRange(
+ RecurrenceDescriptor::getOpcode(Red->getRecurrenceKind()),
+ cast<VPWidenCastRecipe>(VecOp)->getOpcode() ==
+ Instruction::CastOps::ZExt,
+ Ctx.Types.inferScalarType(A)))
+ return new VPExtendedReductionRecipe(Red, cast<VPWidenCastRecipe>(VecOp));
+
+ return nullptr;
+}
+
+/// This function tries convert extended in-loop reductions to
+/// VPMulAccumulateReductionRecipe and clamp the \p Range if it is beneficial
+/// and valid. The created VPExtendedReductionRecipe must be lower to concrete
+/// recipes before execution. Patterns of MulAccumulateReduction:
+/// reduce.add(mul(...)),
+/// reduce.add(mul(ext(A), ext(B))),
+/// reduce.add(ext(mul(ext(A), ext(B)))).
+static VPMulAccumulateReductionRecipe *
+tryToMatchAndCreateMulAccumulateReduction(VPReductionRecipe *Red,
+ VPCostContext &Ctx, VFRange &Range) {
+ using namespace VPlanPatternMatch;
+
+ Type *RedTy = Ctx.Types.inferScalarType(Red);
+
+ // Test if using mulutiply-accumulate-reduction is beneficial and clamp the
+ // range.
----------------
sdesmalen-arm wrote:
nit:
```suggestion
// Clamp the range if using mulutiply-accumulate-reduction is beneficial.
```
https://github.com/llvm/llvm-project/pull/113903
More information about the llvm-commits
mailing list