-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MaybeUninit::assume_init_read
should have noundef
load metadata
I was looking into `array::IntoIter` optimization, and noticed that it wasn't annotating the loads with `noundef` for simple things like `array::IntoIter<i32, N>`. Turned out to be a more general problem as `MaybeUninit::assume_init_read` isn't marking the load as initialized (<https://rust.godbolt.org/z/Mxd8TPTnv>), which is unfortunate since that's basically its reason to exist. This PR lowers `ptr::read(p)` to `copy *p` in MIR, which fortuitiously also improves the IR we give to LLVM for things like `mem::replace`.
- Loading branch information
Showing
14 changed files
with
214 additions
and
39 deletions.
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
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,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. | ||
|
||
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()) | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/mir-opt/lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff
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,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 | ||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
tests/mir-opt/lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff
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,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 | ||
} | ||
} | ||
|
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