[llvm-branch-commits] [llvm] [LV] Only create partial reductions when profitable. (PR #181706)

Benjamin Maxwell via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Mar 12 07:27:13 PDT 2026


================
@@ -6216,20 +6197,30 @@ getScaledReductions(VPReductionPHIRecipe *RedPhiR, VPCostContext &CostCtx,
     if (!PHISize.hasKnownScalarFactor(ExtSrcSize))
       return std::nullopt;
 
-    VPPartialReductionChain Chain(
+    /// Check if a partial reduction chain is supported by the target (i.e.
+    /// does not have an invalid cost) for the given VF range. Clamps the range
+    /// and returns true if feasible for any VF.
+    VPPartialReductionChain Link(
         {UpdateR, *ExtendedOp,
          static_cast<unsigned>(PHISize.getKnownScalarFactor(ExtSrcSize)), RK});
-    if (!isValidPartialReduction(Chain, PhiType, CostCtx, Range))
+    if (!LoopVectorizationPlanner::getDecisionAndClampRange(
+            [&](ElementCount VF) {
+              InstructionCost Cost =
+                  getPartialReductionLinkCost(CostCtx, Link, VF);
+              Link.PartialReductionCost[VF] = Cost;
+              return Cost.isValid();
+            },
+            Range))
----------------
MacDue wrote:

Do we need to compute this cost here and add the `DenseMap` of costs to each link? Could the cost of a Link be computed in `IsProfitablePartialReductionChainForVF` instead? Which would allow this function to only handle matching chains (and avoid computing costs of links in chains that ultimately are not matched).

https://github.com/llvm/llvm-project/pull/181706


More information about the llvm-branch-commits mailing list