Skip to content

Conversation

@kmclaughlin-arm
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-vectorizers

Author: Kerry McLaughlin (kmclaughlin-arm)

Changes

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 --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) }

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Kerry McLaughlin (kmclaughlin-arm)

Changes

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 --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) }

@kmclaughlin-arm
Copy link
Contributor Author

This PR fixes another issue I found whilst testing #147535 with LNT.
When passing -force-target-instruction-cost=1, oggenc fails to build as it hits the "VPlan cost model and legacy cost model disagreed" assert.

Copy link
Contributor

@lukel97 lukel97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

InstructionCost Cost = CM.getInstructionCost(UI, VF);
if (Cost.isValid() && ForceTargetInstructionCost.getNumOccurrences())
return InstructionCost(ForceTargetInstructionCost.getNumOccurrences());
return CM.getInstructionCost(UI, VF);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a fault with your patch, but it looks like it was already broken due to this:

  return InstructionCost(ForceTargetInstructionCost.getNumOccurrences());

I'm pretty sure it should be:

  return InstructionCost(ForceTargetInstructionCost);

similar to LoopVectorizationCostModel::expectedCost

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, it should be ForceTargetInstructionCost. I've updated this in the latest commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, to test this we need a test that passes a large value, e.g. -force-target-instruction-cost=100 in llvm/test/Transforms/LoopVectorize/AArch64/force-target-instruction-cost.ll would do it


for.body:
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
%0 = alloca i8, i64 0, align 16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this without an in-loop alloca?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alloca came from the original loop in oggenc where I found this issue and it's this instruction which leads to the assert.
I've tried to rewrite the loop using another instruction which will also trigger it, but I haven't found another way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, it needs to be in the legacy path. Nevermind, thanks for checking

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid storing to nullptr, with is UB.

Comment on lines 20 to 21
for.end:
ret i32 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for.end:
ret i32 0
for.end:
ret void

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the test to llvm/test/Transforms/LoopVectorize/AArch64/force-target-instruction-cost.ll, which aloready has similar tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the test into force-target-instruction-cost.ll and changed the flag in that test to -force-target-instruction-cost=100 as suggested.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking the legacy cost model print I think, you want to check the VPlan printing or just check the generated IR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm now just checking the generated IR after moving the test.

InstructionCost Cost = CM.getInstructionCost(UI, VF);
if (Cost.isValid() && ForceTargetInstructionCost.getNumOccurrences())
return InstructionCost(ForceTargetInstructionCost.getNumOccurrences());
return CM.getInstructionCost(UI, VF);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, to test this we need a test that passes a large value, e.g. -force-target-instruction-cost=100 in llvm/test/Transforms/LoopVectorize/AArch64/force-target-instruction-cost.ll would do it

- Set -force-target-instruction-cost=100
Copy link
Contributor

@david-arm david-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -p loop-vectorize -force-target-instruction-cost=1 -S %s | FileCheck %s
; RUN: opt -p loop-vectorize -force-target-instruction-cost=100 -S %s | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I originally suggested 100, but if a smaller value also works that would probably be good


for.body:
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
%0 = alloca i8, i64 0, align 16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, it needs to be in the legacy path. Nevermind, thanks for checking

@kmclaughlin-arm kmclaughlin-arm merged commit 884c03e into llvm:main Aug 26, 2025
9 checks passed
@kmclaughlin-arm kmclaughlin-arm deleted the legacy-cost-invalid branch October 7, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants