-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[LV] Return Invalid from getLegacyCost when instruction cost forced. #154543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LV] Return Invalid from getLegacyCost when instruction cost forced. #154543
Conversation
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.
|
@llvm/pr-subscribers-vectorizers Author: Kerry McLaughlin (kmclaughlin-arm) ChangesLoopVectorizationCostModel::expectedCost will only override the cost Full diff: https://github.com/llvm/llvm-project/pull/154543.diff 2 Files Affected:
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) }
|
|
@llvm/pr-subscribers-llvm-transforms Author: Kerry McLaughlin (kmclaughlin-arm) ChangesLoopVectorizationCostModel::expectedCost will only override the cost Full diff: https://github.com/llvm/llvm-project/pull/154543.diff 2 Files Affected:
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) }
|
|
This PR fixes another issue I found whilst testing #147535 with LNT. |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| for.end: | ||
| ret i32 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this 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 | |||
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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.