[llvm] [LV] Return Invalid from getLegacyCost when instruction cost forced. (PR #154543)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 20 06:54:37 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Kerry McLaughlin (kmclaughlin-arm)

<details>
<summary>Changes</summary>

LoopVectorizationCostModel::expectedCost will only override the cost
returned by getInstructionCost when valid. This patch ensures we do
the same in VPCostContext::getLegacyCost, avoiding the "VPlan cost
model and legacy cost model disagreed" assert in the included test.

---
Full diff: https://github.com/llvm/llvm-project/pull/154543.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+3-2) 
- (added) llvm/test/Transforms/LoopVectorize/AArch64/force-inst-cost-invalid.ll (+24) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 70f884016d08c..2d86b2b27f5f6 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6760,9 +6760,10 @@ void LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
 
 InstructionCost VPCostContext::getLegacyCost(Instruction *UI,
                                              ElementCount VF) const {
-  if (ForceTargetInstructionCost.getNumOccurrences())
+  InstructionCost Cost = CM.getInstructionCost(UI, VF);
+  if (Cost.isValid() && ForceTargetInstructionCost.getNumOccurrences())
     return InstructionCost(ForceTargetInstructionCost.getNumOccurrences());
-  return CM.getInstructionCost(UI, VF);
+  return Cost;
 }
 
 bool VPCostContext::isLegacyUniformAfterVectorization(Instruction *I,
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/force-inst-cost-invalid.ll b/llvm/test/Transforms/LoopVectorize/AArch64/force-inst-cost-invalid.ll
new file mode 100644
index 0000000000000..52a72014392d7
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/force-inst-cost-invalid.ll
@@ -0,0 +1,24 @@
+; REQUIRES: asserts
+; RUN: opt < %s -passes=loop-vectorize -force-target-instruction-cost=1 -debug-only=loop-vectorize -S -disable-output 2>&1 | FileCheck %s
+target triple = "aarch64-linux-gnu"
+
+define i32 @invalid_legacy_cost(i64 %N) #0 {
+; CHECK: LV: Checking a loop in 'invalid_legacy_cost
+; CHECK: LV: Found an estimated cost of Invalid for VF vscale x 2 For instruction: %0 = alloca i8, i64 0, align 16
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+  %0 = alloca i8, i64 0, align 16
+  %arrayidx = getelementptr ptr, ptr null, i64 %iv
+  store ptr %0, ptr %arrayidx, align 8
+  %iv.next = add i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv, %N
+  br i1 %exitcond.not, label %for.end, label %for.body
+
+for.end:
+  ret i32 0
+}
+
+attributes #0 = { "target-features"="+neon,+sve" vscale_range(1,16) }

``````````

</details>


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


More information about the llvm-commits mailing list