-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Ensure ptr::read
gets all the same LLVM load
metadata that dereferencing does
#109035
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
Changes from 1 commit
b2c717f
0b96fee
1f70bb8
87696fd
e7c6ad8
dfc3377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2020,6 +2020,20 @@ extern "rust-intrinsic" { | |||||||||||
#[rustc_safe_intrinsic] | ||||||||||||
pub fn saturating_sub<T: Copy>(a: T, b: T) -> T; | ||||||||||||
|
||||||||||||
/// This is a *typed* read, `copy *p` in MIR. | ||||||||||||
/// | ||||||||||||
/// The stabilized form of this intrinsic is [`crate::ptr::read`], so | ||||||||||||
/// that can be implemented without needing to do an *untyped* copy | ||||||||||||
/// via [`copy_nonoverlapping`], and thus can get proper metadata. | ||||||||||||
|
/// The stabilized form of this intrinsic is [`crate::ptr::read`], so | |
/// that can be implemented without needing to do an *untyped* copy | |
/// via [`copy_nonoverlapping`], and thus can get proper metadata. | |
/// The stabilized form of this intrinsic is [`crate::ptr::read`], so that | |
/// it is easier for the compiler to generate a load with proper metadata. |
Outdated
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 confused me a bit — at first I though that read_via_copy
only works for pointers to locals; While it seems like actually it can only be called with a pointer which is itself a local (i.e. read_via_copy(x)
✅, read_via_copy(s.f)
❎).
Maybe the docs can be clarified a bit.
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 seems like a strange (and very syntactic) restriction? Isn't there a high risk that some other program transformation might, for instance, turn
let x = s.f;
read_via_copy(x)
into
read_via_copy(s.f)
?
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.
That can be fixed via introducing a temporary if it becomes a problem (and Jakob mentioned that on... Zulip?), but it seems the current tendency in the MIR is to aggressively desugar everything and introduce temporaries everywhere, then roll them back up in opt passes. If the implementation works and produces less MIR than the alternative, I think there's a merit in not introducing One More Temporary for the crab to claw through.
Maybe we should note why this "bug" was not "fixed", though, so that if anyone comes by and it needs to be fixed, they can immediately change 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.
Yes, this is about as big an optimization footgun as can exist. What saves us is that this runs before optimizations, so they don't have to care
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.
Still seems rather fragile, and needs at least a comment explaining the situation.
WaffleLapkin marked this conversation as resolved.
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1137,25 +1137,33 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T { | |
pub const unsafe fn read<T>(src: *const T) -> T { | ||
// We are calling the intrinsics directly to avoid function calls in the generated code | ||
// as `intrinsics::copy_nonoverlapping` is a wrapper function. | ||
#[cfg(bootstrap)] | ||
extern "rust-intrinsic" { | ||
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] | ||
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); | ||
} | ||
|
||
let mut tmp = MaybeUninit::<T>::uninit(); | ||
// SAFETY: the caller must guarantee that `src` is valid for reads. | ||
// `src` cannot overlap `tmp` because `tmp` was just allocated on | ||
// the stack as a separate allocated object. | ||
// | ||
// Also, since we just wrote a valid value into `tmp`, it is guaranteed | ||
// to be properly initialized. | ||
unsafe { | ||
assert_unsafe_precondition!( | ||
"ptr::read requires that the pointer argument is aligned and non-null", | ||
[T](src: *const T) => is_aligned_and_not_null(src) | ||
); | ||
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1); | ||
tmp.assume_init() | ||
|
||
#[cfg(bootstrap)] | ||
{ | ||
let mut tmp = MaybeUninit::<T>::uninit(); | ||
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1); | ||
tmp.assume_init() | ||
} | ||
#[cfg(not(bootstrap))] | ||
{ | ||
// This uses a dedicated intrinsic, not `copy_nonoverlapping`, | ||
// so that it gets a *typed* copy, not an *untyped* one. | ||
crate::intrinsics::read_via_copy(src) | ||
|
||
} | ||
scottmcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// compile-flags: -O -Z merge-functions=disabled | ||
// no-system-llvm | ||
// ignore-debug (the extra assertions get in the way) | ||
|
||
#![crate_type = "lib"] | ||
|
||
// Ensure that various forms of reading pointers correctly annotate the `load`s | ||
// with `!noundef` metadata to enable extra optimization. The functions return | ||
// `MaybeUninit` to keep it from being inferred from the function type. | ||
scottmcm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
use std::mem::MaybeUninit; | ||
|
||
// CHECK-LABEL: define i8 @copy_byte( | ||
#[no_mangle] | ||
pub unsafe fn copy_byte(p: *const u8) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(*p) | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte( | ||
#[no_mangle] | ||
pub unsafe fn read_byte(p: *const u8) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(p.read()) | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte_maybe_uninit( | ||
#[no_mangle] | ||
pub unsafe fn read_byte_maybe_uninit(p: *const MaybeUninit<u8>) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-NOT: noundef | ||
// CHECK-NOT: load | ||
p.read() | ||
} | ||
|
||
// CHECK-LABEL: define i8 @read_byte_assume_init( | ||
#[no_mangle] | ||
pub unsafe fn read_byte_assume_init(p: &MaybeUninit<u8>) -> MaybeUninit<u8> { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
MaybeUninit::new(p.assume_init_read()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- // MIR for `read_via_copy_primitive` before LowerIntrinsics | ||
+ // MIR for `read_via_copy_primitive` after LowerIntrinsics | ||
|
||
fn read_via_copy_primitive(_1: &i32) -> i32 { | ||
debug r => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:32: +0:33 | ||
let mut _0: i32; // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:44: +0:47 | ||
let mut _2: *const i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
scope 1 { | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
_2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<i32>(move _2) -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:85:14: 85:45 | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const i32) -> i32 {read_via_copy::<i32>}, val: Value(<ZST>) } | ||
+ _0 = (*_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
+ goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
} | ||
|
||
bb1: { | ||
StorageDead(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:47: +1:48 | ||
return; // scope 0 at $DIR/lower_intrinsics.rs:+2:2: +2:2 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- // MIR for `read_via_copy_uninhabited` before LowerIntrinsics | ||
+ // MIR for `read_via_copy_uninhabited` after LowerIntrinsics | ||
|
||
fn read_via_copy_uninhabited(_1: &Never) -> Never { | ||
debug r => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:34: +0:35 | ||
let mut _0: Never; // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:48: +0:53 | ||
let mut _2: *const Never; // in scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
scope 1 { | ||
} | ||
|
||
bb0: { | ||
StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
_2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 | ||
- _0 = read_via_copy::<Never>(move _2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
- // mir::Constant | ||
- // + span: $DIR/lower_intrinsics.rs:90:14: 90:45 | ||
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const Never) -> Never {read_via_copy::<Never>}, val: Value(<ZST>) } | ||
+ _0 = (*_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
+ unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,11 +148,11 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; | |
HEX_DUMP | ||
} | ||
|
||
error: accessing memory with alignment 1, but alignment 4 is required | ||
error[E0080]: evaluation of constant value failed | ||
|
||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616> | ||
= note: accessing memory with alignment 1, but alignment 4 is required | ||
| | ||
note: inside `std::ptr::read::<u32>` | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
note: inside `ptr::const_ptr::<impl *const u32>::read` | ||
|
@@ -162,25 +162,7 @@ note: inside `UNALIGNED_READ` | |
| | ||
LL | ptr.read(); | ||
| ^^^^^^^^^^ | ||
= note: `#[deny(invalid_alignment)]` on by default | ||
|
||
error: aborting due to 15 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. | ||
Future incompatibility report: Future breakage diagnostic: | ||
error: accessing memory with alignment 1, but alignment 4 is required | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #68585 <https://github.com/rust-lang/rust/issues/104616> | ||
note: inside `std::ptr::read::<u32>` | ||
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
note: inside `ptr::const_ptr::<impl *const u32>::read` | ||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | ||
note: inside `UNALIGNED_READ` | ||
--> $DIR/ub-ref-ptr.rs:67:5 | ||
| | ||
LL | ptr.read(); | ||
| ^^^^^^^^^^ | ||
= note: `#[deny(invalid_alignment)]` on by default | ||
|
Uh oh!
There was an error while loading. Please reload this page.