-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Use unchecked mul to compute slice sizes #98078
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This test case checks that LLVM is aware that computing the size of a slice cannot wrap. | ||
// The possibility of wrapping results in an additional branch when dropping boxed slices | ||
// in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218 | ||
|
||
// compile-flags: -O | ||
// min-llvm-version: 14.0 | ||
|
||
#![crate_type="lib"] | ||
|
||
// CHECK-LABEL: @simple_size_of_nowrap | ||
#[no_mangle] | ||
pub fn simple_size_of_nowrap(x: &[u32]) -> usize { | ||
// Make sure the shift used to compute the size has a nowrap flag. | ||
|
||
// CHECK: [[A:%.*]] = shl nsw {{.*}}, 2 | ||
// CHECK-NEXT: ret {{.*}} [[A]] | ||
core::mem::size_of_val(x) | ||
} | ||
|
||
// CHECK-LABEL: @drop_write | ||
#[no_mangle] | ||
pub fn drop_write(mut x: Box<[u32]>) { | ||
// Check that this write is optimized out. | ||
// This depends on the size calculation not wrapping, | ||
// since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0. | ||
|
||
// CHECK-NOT: store i32 42 | ||
x[1] = 42; | ||
} |
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.
Any specific reason to use signed multiplication here?
Both length and element size are unsigned.
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.
Because
mul
/unchecked_smul
/unchecked_umul
are all "half width" multiplies (the result is the same number of bits as the input), there is no behavior difference between signed and unsigned multiplication--for the same bitpattern in the inputs, they return the same result. This is why the previous code was justbx.mul
, and notbx.smul
orbx.umul
(which don't exist).The difference between
unchecked_umul
/unchecked_smul
, then, is thatunchecked_umul
tells the backend that the multiplication can't wrap around the unsigned boundary (0
/usize::MAX
), andunchecked_smul
tells the backend that it can't wrap around the signed boundary (isize::MIN
/isize::MAX
). Ideally, we would have both (resulting inmul nsw nuw
in LLVM IR), since we know both inputs are non-negative. But there's no existingunchecked_nsw_nuw_mul
, and it's not necessary for this use case, so I don't think it's worth adding.Because both
unchecked_umul
andunchecked_smul
are correct here, the choice is arbitrary. I pickedunchecked_smul
here because the upper bound on the result is lower (isize::MAX
instead ofusize::MAX
), though I have no particular reason to prefer that over a tighter lower bound. The added test case passes with either.Added a comment.