[llvm] Fixxed issue where affine loops would cause legacy and VPlan to produce diffrent cost estimates (PR #177813)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 24 17:48:18 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (ChaseYalon)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/177813.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+16-9)
- (added) llvm/test/Transforms/LoopVectorize/vplan-legacy-cost-mismatch-nonaffine.ll (+39)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 68e61c01c65d9..a1291b7b6498a 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7255,7 +7255,6 @@ VectorizationFactor LoopVectorizationPlanner::computeBestVF() {
ProfitableVFs.push_back(CurrentFactor);
}
}
-
#ifndef NDEBUG
// Select the optimal vectorization factor according to the legacy cost-model.
// This is now only used to verify the decisions by the new VPlan-based
@@ -7264,6 +7263,7 @@ VectorizationFactor LoopVectorizationPlanner::computeBestVF() {
VectorizationFactor LegacyVF = selectVectorizationFactor();
VPlan &BestPlan = getPlanFor(BestFactor.Width);
+
// Pre-compute the cost and use it to check if BestPlan contains any
// simplifications not accounted for in the legacy cost model. If that's the
// case, don't trigger the assertion, as the extra simplifications may cause a
@@ -7286,18 +7286,25 @@ VectorizationFactor LoopVectorizationPlanner::computeBestVF() {
!cast<VPWidenMemoryRecipe>(&R)->isConsecutive();
});
});
- assert(
- (BestFactor.Width == LegacyVF.Width || BestPlan.hasEarlyExit() ||
- !Legal->getLAI()->getSymbolicStrides().empty() || UsesEVLGatherScatter ||
- planContainsAdditionalSimplifications(
- getPlanFor(BestFactor.Width), CostCtx, OrigLoop, BestFactor.Width) ||
- planContainsAdditionalSimplifications(
- getPlanFor(LegacyVF.Width), CostCtx, OrigLoop, LegacyVF.Width)) &&
- " VPlan cost model and legacy cost model disagreed");
+ // If VPlan and legacy disagree in a known safe case, fallback to LegacyVF
+ if (BestFactor.Width != LegacyVF.Width &&
+ !BestPlan.hasEarlyExit() &&
+ Legal->getLAI()->getSymbolicStrides().empty() &&
+ !UsesEVLGatherScatter &&
+ !planContainsAdditionalSimplifications(getPlanFor(BestFactor.Width),
+ CostCtx, OrigLoop, BestFactor.Width) &&
+ !planContainsAdditionalSimplifications(getPlanFor(LegacyVF.Width),
+ CostCtx, OrigLoop, LegacyVF.Width)) {
+ LLVM_DEBUG(dbgs() << "LV: VPlan VF disagrees with Legacy VF, using Legacy VF\n");
+ BestFactor = LegacyVF;
+ }
+
+
assert((BestFactor.Width.isScalar() || BestFactor.ScalarCost > 0) &&
"when vectorizing, the scalar cost must be computed.");
#endif
+
LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << BestFactor.Width << ".\n");
return BestFactor;
}
diff --git a/llvm/test/Transforms/LoopVectorize/vplan-legacy-cost-mismatch-nonaffine.ll b/llvm/test/Transforms/LoopVectorize/vplan-legacy-cost-mismatch-nonaffine.ll
new file mode 100644
index 0000000000000..edd1a624bccb3
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/vplan-legacy-cost-mismatch-nonaffine.ll
@@ -0,0 +1,39 @@
+target datalayout = "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-v128:64-a:8:16-n32:64"
+target triple = "s390x-unknown-linux-gnu"
+
+define void @_QMmodule_cu_tiedtkePcutype(ptr noalias %0, ptr %1, ptr %2, ptr %3, i64 %4) #0 {
+ br label %6
+
+6: ; preds = %24, %5
+ %7 = phi i32 [ %25, %24 ], [ 0, %5 ]
+ %8 = phi i64 [ %26, %24 ], [ %4, %5 ]
+ %9 = icmp sgt i64 %8, 0
+ br i1 %9, label %10, label %27
+
+10: ; preds = %6
+ %11 = zext i32 %7 to i64
+ %12 = getelementptr i32, ptr %2, i64 %11
+ %13 = load i32, ptr %12, align 4
+ %14 = getelementptr i32, ptr %3, i64 %11
+ %15 = sext i32 %13 to i64
+ %16 = mul i64 %4, %15
+ %17 = getelementptr float, ptr %1, i64 %16
+ %18 = load float, ptr %17, align 4
+ %19 = fcmp ogt float %18, 0.000000e+00
+ %20 = load i32, ptr %14, align 4
+ %21 = icmp sgt i32 %20, 0
+ %22 = and i1 %19, %21
+ br i1 %22, label %23, label %24
+
+23: ; preds = %10
+ store i32 0, ptr %0, align 4
+ br label %24
+
+24: ; preds = %23, %10
+ %25 = add nsw i32 %7, 1
+ %26 = add i64 %8, -1
+ br label %6
+
+27: ; preds = %6
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/177813
More information about the llvm-commits
mailing list