[llvm] Recommit "[LV][VPlan] Remove any-of reduction from precomputeCost. NFC (#117109)" (PR #117289)

Elvis Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 21:30:40 PST 2024


https://github.com/ElvisWang123 created https://github.com/llvm/llvm-project/pull/117289

Update the test cases contains `any-of` printings from the precomputeCost().

Origin message: 

The any-of reduction contains phi and select instructions.

The select instruction might be optimized and removed in the vplan which may cause VF difference between legacy and VPlan-based model. But if the select instruction be removed, planContainsAdditionalSimplifications() will catch it and disable the assertion.

Therefore, we can just remove the ayn-of reduction calculation in the precomputeCost().



Recommit  "[LV][VPlan] Remove any-of reduction from precomputeCost. NFC (#117109)"

>From a1867ae488cb521c3264fb9fdaf3dddd37a2132b Mon Sep 17 00:00:00 2001
From: Elvis Wang <elvis.wang at sifive.com>
Date: Wed, 20 Nov 2024 00:24:11 -0800
Subject: [PATCH 1/2] [LV][VPlan] Remove any-of reduction from precomputeCost.
 NFC

The any-of reduction contains phi and select instructions. The select
instruction might be optimized and removed in the vplan which may cause
VF difference between legacy and VPlan-based model. But if the select
instruction be removed, `planContainsAdditionalSimplifications()` will
catch it and disable the assertion.

Therefore, we can just remove the ayn-of reduction calculation in the
precomputeCost().
---
 .../Transforms/Vectorize/LoopVectorize.cpp    | 24 ++-----------------
 1 file changed, 2 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index d13770a35c108f..5b556058cc762c 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7303,34 +7303,14 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
 
   // The legacy cost model has special logic to compute the cost of in-loop
   // reductions, which may be smaller than the sum of all instructions involved
-  // in the reduction. For AnyOf reductions, VPlan codegen may remove the select
-  // which the legacy cost model uses to assign cost. Pre-compute their costs
-  // for now.
+  // in the reduction.
   // TODO: Switch to costing based on VPlan once the logic has been ported.
   for (const auto &[RedPhi, RdxDesc] : Legal->getReductionVars()) {
     if (ForceTargetInstructionCost.getNumOccurrences())
       continue;
 
-    if (!CM.isInLoopReduction(RedPhi) &&
-        !RecurrenceDescriptor::isAnyOfRecurrenceKind(
-            RdxDesc.getRecurrenceKind()))
-      continue;
-
-    // AnyOf reduction codegen may remove the select. To match the legacy cost
-    // model, pre-compute the cost for AnyOf reductions here.
-    if (RecurrenceDescriptor::isAnyOfRecurrenceKind(
-            RdxDesc.getRecurrenceKind())) {
-      auto *Select = cast<SelectInst>(*find_if(
-          RedPhi->users(), [](User *U) { return isa<SelectInst>(U); }));
-      assert(!CostCtx.SkipCostComputation.contains(Select) &&
-             "reduction op visited multiple times");
-      CostCtx.SkipCostComputation.insert(Select);
-      auto ReductionCost = CostCtx.getLegacyCost(Select, VF);
-      LLVM_DEBUG(dbgs() << "Cost of " << ReductionCost << " for VF " << VF
-                        << ":\n any-of reduction " << *Select << "\n");
-      Cost += ReductionCost;
+    if (!CM.isInLoopReduction(RedPhi))
       continue;
-    }
 
     const auto &ChainOps = RdxDesc.getReductionOpChain(RedPhi, OrigLoop);
     SetVector<Instruction *> ChainOpsAndOperands(ChainOps.begin(),

>From b5712477b0b62f714d42b57db47142f4c2388541 Mon Sep 17 00:00:00 2001
From: Elvis Wang <elvis.wang at sifive.com>
Date: Fri, 22 Nov 2024 13:15:32 +0800
Subject: [PATCH 2/2] Update AArch64 test since we will no longer print
 any-of...

---
 llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
index 6165a73e77f238..403fc9f316d35c 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
@@ -50,10 +50,6 @@ for.cond.cleanup:                                 ; preds = %for.cond.cleanup.lo
 
 define i32 @multi_user_cmp(ptr readonly %a, i64 noundef %n) {
 ; CHECK: LV: Checking a loop in 'multi_user_cmp'
-; CHECK: Cost of 1 for VF 16:
-; CHECK:  any-of reduction   %all.off = select i1 %cmp1, i1 %all.off.next, i1 false
-; CHECK: Cost of 1 for VF 16:
-; CHECK:  any-of reduction   %.any.0.off0 = select i1 %cmp1, i1 true, i1 %any.0.off09
 ; CHECK: Cost of 4 for VF 16: WIDEN ir<%cmp1> = fcmp olt ir<%load1>, ir<0.000000e+00>
 ; CHECK: LV: Selecting VF: 16.
 entry:



More information about the llvm-commits mailing list