-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[InstCombine] Fold @llvm.experimental.get.vector.length when cnt <= max_lanes #169293
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
Merged
+110
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c61d4f2
Precommit tests
lukel97 4da2081
[InstCombine] Fold @llvm.experimental.get.vector.length when cnt <= m…
lukel97 2f71e8a
Use createZExtOrTrunc, use isOne()
lukel97 db24622
Perform in larger of two bitwidths
lukel97 68b9cb6
Merge branch 'main' into instcombine/getvectorlength
lukel97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: opt < %s -passes=instcombine,verify -S | FileCheck %s | ||
|
|
||
| define i32 @cnt_known_lt() { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt() { | ||
| ; CHECK-NEXT: ret i32 1 | ||
| ; | ||
| %x = call i32 @llvm.experimental.get.vector.length(i32 1, i32 2, i1 false) | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @cnt_not_known_lt() { | ||
| ; CHECK-LABEL: define i32 @cnt_not_known_lt() { | ||
| ; CHECK-NEXT: [[X:%.*]] = call i32 @llvm.experimental.get.vector.length.i32(i32 2, i32 1, i1 false) | ||
| ; CHECK-NEXT: ret i32 [[X]] | ||
| ; | ||
| %x = call i32 @llvm.experimental.get.vector.length(i32 2, i32 1, i1 false) | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @cnt_known_lt_scalable() vscale_range(2, 4) { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt_scalable( | ||
| ; CHECK-SAME: ) #[[ATTR0:[0-9]+]] { | ||
| ; CHECK-NEXT: ret i32 2 | ||
| ; | ||
| %x = call i32 @llvm.experimental.get.vector.length(i32 2, i32 1, i1 true) | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @cnt_not_known_lt_scalable() { | ||
| ; CHECK-LABEL: define i32 @cnt_not_known_lt_scalable() { | ||
| ; CHECK-NEXT: [[X:%.*]] = call i32 @llvm.experimental.get.vector.length.i32(i32 2, i32 1, i1 true) | ||
| ; CHECK-NEXT: ret i32 [[X]] | ||
| ; | ||
| %x = call i32 @llvm.experimental.get.vector.length(i32 2, i32 1, i1 true) | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @cnt_known_lt_runtime(i32 %x) { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt_runtime( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[ICMP:%.*]] = icmp ult i32 [[X]], 4 | ||
| ; CHECK-NEXT: call void @llvm.assume(i1 [[ICMP]]) | ||
| ; CHECK-NEXT: ret i32 [[X]] | ||
| ; | ||
| %icmp = icmp ule i32 %x, 3 | ||
| call void @llvm.assume(i1 %icmp) | ||
| %y = call i32 @llvm.experimental.get.vector.length(i32 %x, i32 3, i1 false) | ||
| ret i32 %y | ||
| } | ||
|
|
||
| define i32 @cnt_known_lt_runtime_trunc(i64 %x) { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt_runtime_trunc( | ||
| ; CHECK-SAME: i64 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[ICMP:%.*]] = icmp ult i64 [[X]], 4 | ||
| ; CHECK-NEXT: call void @llvm.assume(i1 [[ICMP]]) | ||
| ; CHECK-NEXT: [[Y:%.*]] = trunc nuw nsw i64 [[X]] to i32 | ||
| ; CHECK-NEXT: ret i32 [[Y]] | ||
| ; | ||
| %icmp = icmp ule i64 %x, 3 | ||
| call void @llvm.assume(i1 %icmp) | ||
| %y = call i32 @llvm.experimental.get.vector.length(i64 %x, i32 3, i1 false) | ||
| ret i32 %y | ||
| } | ||
|
|
||
| ; FIXME: We should be able to deduce the constant range from AssumptionCache | ||
| ; rather than relying on KnownBits, which in this case only knows x <= 3. | ||
| define i32 @cnt_known_lt_runtime_assumption(i32 %x) { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt_runtime_assumption( | ||
| ; CHECK-SAME: i32 [[X:%.*]]) { | ||
| ; CHECK-NEXT: [[ICMP:%.*]] = icmp ult i32 [[X]], 3 | ||
| ; CHECK-NEXT: call void @llvm.assume(i1 [[ICMP]]) | ||
| ; CHECK-NEXT: [[Y:%.*]] = call i32 @llvm.experimental.get.vector.length.i32(i32 [[X]], i32 2, i1 false) | ||
| ; CHECK-NEXT: ret i32 [[Y]] | ||
| ; | ||
| %icmp = icmp ule i32 %x, 2 | ||
| call void @llvm.assume(i1 %icmp) | ||
| %y = call i32 @llvm.experimental.get.vector.length(i32 %x, i32 2, i1 false) | ||
| ret i32 %y | ||
| } | ||
|
|
||
|
|
||
| define i32 @cnt_known_lt_i16() { | ||
| ; CHECK-LABEL: define i32 @cnt_known_lt_i16() { | ||
| ; CHECK-NEXT: ret i32 1 | ||
| ; | ||
| %x = call i32 @llvm.experimental.get.vector.length(i16 1, i32 2, i1 false) | ||
| ret i32 %x | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why doesn't this work?
llvm-project/llvm/lib/Analysis/ValueTracking.cpp
Lines 10303 to 10328 in 1580f4b
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
computeConstantRangeIncludingKnownBitsdoesn't pass in the AssumptionCache tocomputeConstantRangecurrently. I presume it's a simple fix in another PR?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.
Btw the reason for using
computeConstantRangeIncludingKnownBitsand not justcomputeConstantRangedirectly is because for the motivating case I think we need to be able handle a simple PHI recurrence, which onlycomputeKnownBitsseems to be able to handle at the moment.Uh oh!
There was an error while loading. Please reload this page.
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 get.vector.length being in the recurrence makes it no longer "simple", doesn't it?
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.
From what I can tell that is correct, I didn't see anywhere in
computeKnownBitswhereget.vector.lengthis handled currently. Here's a test for this that could get added toget_vector_length.ll:Uh oh!
There was an error while loading. Please reload this page.
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.
matchSimpleRecurrence doesn't care what the step is so it can still handle get.vector.length in the recurrence. The only issue is that we currently don't deduce anything apart from the low zero bits in computeKnownBitsFromOperator.
Yes, we should separately add support for that too.
But for the reduced example in #164762 (comment) I think we only need to teach computeKnownBitsFromOperator that sub recurrences with nuw should only decrease, so the number of leading zeros should increase. I gave this a super quick go and it seems to work, haven't checked if its generally correct yet:That diff above in combination with this patch is enough to flatten the loop.Actually Known2 and Known3 weren't what I thought they were, I don't think the above is correct.