[llvm] 6a77c91 - [VPlan] Don't cost scalar IV steps if the vector body runs at most once. (#221974)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 8 12:45:55 PDT 2026
Author: Florian Hahn
Date: 2026-09-08T19:45:49Z
New Revision: 6a77c9113211f81804455102823b996e01283ffc
URL: https://github.com/llvm/llvm-project/commit/6a77c9113211f81804455102823b996e01283ffc
DIFF: https://github.com/llvm/llvm-project/commit/6a77c9113211f81804455102823b996e01283ffc.diff
LOG: [VPlan] Don't cost scalar IV steps if the vector body runs at most once. (#221974)
If the vector loop executes exactly once, each scalar IV step folds to a
constant, and there's nothing to cost.
Similarly to BranchOnCount and VPRegionBlock::cost, treat
VPScalarIVSteps as free in that case.
This fixes a slight inaccuracy introduced when moving to computing
VPScalarIVSteps costs in VPlan directly.
PR: https://github.com/llvm/llvm-project/pull/221974
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
llvm/test/Transforms/LoopVectorize/AArch64/scalar-steps-cost.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index ff212663a3fac..7d671e6cd29f1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -3184,6 +3184,11 @@ InstructionCost VPScalarIVStepsRecipe::computeCost(ElementCount VF,
if (vputils::onlyFirstLaneUsed(this))
return 0;
+ // If the vector body executes at most once, the canonical IV is a constant
+ // and every lane's step folds away with it.
+ if (VPCostContext::executesAtMostOnce(*getParent()->getPlan(), VF))
+ return 0;
+
// Typically the operations are:
// 1. Add the start index to each lane value.
// 2. Multiply the start index by the step.
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/scalar-steps-cost.ll b/llvm/test/Transforms/LoopVectorize/AArch64/scalar-steps-cost.ll
index 5c12dc366b43c..00b50c5d327b6 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/scalar-steps-cost.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/scalar-steps-cost.ll
@@ -54,7 +54,7 @@ exit:
define i32 @scalar_steps_all_lanes_tc_eq_vf(ptr %start) {
; CHECK-LABEL: LV: Checking a loop in 'scalar_steps_all_lanes_tc_eq_vf'
; CHECK: Cost of 1 for VF 2: {{.*}} = SCALAR-STEPS {{.*}}, ir<1>, {{.*}}
-; CHECK: Cost of 1 for VF 4: {{.*}} = SCALAR-STEPS {{.*}}, ir<1>, {{.*}}
+; CHECK: Cost of 0 for VF 4: {{.*}} = SCALAR-STEPS {{.*}}, ir<1>, {{.*}}
entry:
br label %loop
More information about the llvm-commits
mailing list