-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Update transmute_copy to ub_checks and ?Sized
#155989
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
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 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
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,65 @@ | ||
| //@ compile-flags: -Copt-level=1 | ||
| //@ only-64bit | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| // Check that we don't have runtime alignment checks, just a safe alignment. | ||
| // (Rust emits a branch, but LLVM merges the loads from the arms.) | ||
|
|
||
| use std::mem::transmute_copy; | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i16_from_i32_slice | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: i64{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i16_from_i32_slice(x: &[i32]) -> [i16; 7] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 2{{.+}}%_0, | ||
| // CHECK-SAME: align 4{{.+}}%x.0, | ||
| // CHECK-SAME: i64 14, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i32_from_i16_slice | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: i64{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i32_from_i16_slice(x: &[i16]) -> [i32; 3] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 4{{.+}}%_0, | ||
| // CHECK-SAME: align 2{{.+}}%x.0, | ||
| // CHECK-SAME: i64 12, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: i32 @transmute_copy_i32_from_dyn | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: ptr{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i32_from_dyn(x: &dyn std::fmt::Debug) -> i32 { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: [[TEMP:%.+]] = load i32, ptr %x.0, align 1 | ||
| // CHECK-NEXT: ret i32 [[TEMP]] | ||
| transmute_copy(x) | ||
| } | ||
|
|
||
| // CHECK-LABEL: @transmute_copy_i16_array_from_dyn | ||
| // CHECK-SAME: ptr{{.+}}%_0, | ||
| // CHECK-SAME: ptr{{.+}}%x.0, | ||
| // CHECK-SAME: ptr{{.+}}%x.1) | ||
| #[no_mangle] | ||
| pub unsafe fn transmute_copy_i16_array_from_dyn(x: &dyn std::fmt::Debug) -> [i16; 7] { | ||
| // CHECK: start: | ||
| // CHECK-NEXT: @llvm.memcpy | ||
| // CHECK-SAME: align 2{{.+}}%_0, | ||
| // CHECK-SAME: align 1{{.+}}%x.0, | ||
| // CHECK-SAME: i64 14, | ||
| // CHECK-NEXT: ret void | ||
| transmute_copy(x) | ||
| } |
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,9 @@ | ||
| //@ run-crash | ||
| //@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes | ||
| //@ error-pattern: unsafe precondition(s) violated: cannot transmute_copy if Dst is larger than Src | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| let _unused: u64 = std::mem::transmute_copy(&1_u8); | ||
| } | ||
| } |
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.
For unsized
Srcthis comparison might not be optimized out. Is it even worth having both branches then?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.
Hmm, interesting question. I guess I could condition it on
is_val_statically_known...(We can't specialize on Sized-ness, right?)
EDIT later: Note to self, include some codegen tests to demonstrate one way or the other.
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.
@RalfJung For my own edification, can you say more about why this comparison might not be optimized out in the unsized case?
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
align_of_valmight not be a constant if the unsized tail isdyn Trait. So this compiles toif some_const > value_loaded_from_vtable ...and I don't see how LLVM could optimize this.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.
Yeah I think that makes sense. What is the alternative you're suggesting? You mention not having both branches, but which branch are you suggesting be eliminated?
(To be clear, I am nearly certain that these questions are coming from a lack of understanding on my part.)
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.
Originally I meant to suggest that we should just always use
read_unalignedto avoid codegen'ing two read paths.But as @scottmcm mentioned there's an alternative: use
is_val_statically_known.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.
Good news, even at low opt-levels LLVM is smart and merges the loads or the memcpys so even with the branch in the rust code the right thing happens 🎉 Test added.
(I still have doc fixes to do, though, so this PR is not ready yet.)Done.