-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add assume
s to slice length calls
#122926
Closed
Closed
Changes from all commits
Commits
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 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 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 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 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 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 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,55 @@ | ||
//@ revisions: 32BIT 64BIT | ||
//@ compile-flags: -O | ||
//@ min-llvm-version: 18.0 | ||
//@ [32BIT] only-32bit | ||
//@ [64BIT] only-64bit | ||
|
||
#![crate_type = "lib"] | ||
|
||
// Confirm that the `assume` calls from the length allows LLVM to know that some | ||
// math on the indices can be done without overflow risk. | ||
|
||
// CHECK-LABEL: @slice_length_demo | ||
#[no_mangle] | ||
pub unsafe fn slice_length_demo(x: &[u16]) { | ||
// 32BIT: %[[LIMIT:.+]] = icmp ult [[USIZE:i32]] %x.1, [[#0x40000000]] | ||
// 64BIT: %[[LIMIT:.+]] = icmp ult [[USIZE:i64]] %x.1, [[#0x4000000000000000]] | ||
// CHECK: tail call void @llvm.assume(i1 %[[LIMIT]]) | ||
|
||
// CHECK: %[[Y:.+]] = phi [[USIZE]] | ||
// CHECK-SAME: [ 0, %start ] | ||
// CHECK: %[[PLUS_ONE:.+]] = add nuw nsw [[USIZE]] %[[Y]], 1 | ||
// CHECK: call void @do_something([[USIZE]] noundef %[[PLUS_ONE]]) | ||
// CHECK: %[[TIMES_TWO:.+]] = shl nuw nsw [[USIZE]] %[[Y]], 1 | ||
// CHECK: call void @do_something([[USIZE]] noundef %[[TIMES_TWO]]) | ||
for y in 0..x.len() { | ||
do_something(y + 1); | ||
do_something(y * 2); | ||
} | ||
} | ||
|
||
// CHECK-LABEL: @nested_slice_length | ||
#[no_mangle] | ||
pub unsafe fn nested_slice_length(x: &[f32], y: &[f32]) { | ||
// 32BIT: %[[LIMIT:.+]] = icmp ult [[USIZE:i32]] %x.1, [[#0x20000000]] | ||
// 64BIT: %[[LIMIT:.+]] = icmp ult [[USIZE:i64]] %x.1, [[#0x2000000000000000]] | ||
// CHECK: tail call void @llvm.assume(i1 %[[LIMIT]]) | ||
// 32BIT: %[[LIMIT:.+]] = icmp ult [[USIZE]] %y.1, [[#0x20000000]] | ||
// 64BIT: %[[LIMIT:.+]] = icmp ult [[USIZE]] %y.1, [[#0x2000000000000000]] | ||
// CHECK: tail call void @llvm.assume(i1 %[[LIMIT]]) | ||
|
||
// CHECK: %[[J:.+]] = phi [[USIZE]] | ||
// CHECK: %[[I:.+]] = phi [[USIZE]] | ||
// CHECK-NOT: phi | ||
// CHECK: %[[T:.+]] = add nuw nsw [[USIZE]] %[[I]], %[[J]] | ||
// CHECK: call void @do_something([[USIZE]] noundef %[[T]]) | ||
for i in 0..x.len() { | ||
for j in 0..y.len() { | ||
do_something(i + j); | ||
} | ||
} | ||
} | ||
|
||
extern { | ||
fn do_something(x: usize); | ||
} |
This file contains 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 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
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.
I first tried doing this with
assume(sge(len * elem_bytes, 0))
, but got worse results -- I think LLVM is confused by the SGE limit compared with other positive things, even though it actually simplifiesule(x, isize::MAX)
intosgt(x, -1)
.