forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#109035 - scottmcm:ptr-read-should-know-undef,…
… r=WaffleLapkin,JakobDegen Ensure `ptr::read` gets all the same LLVM `load` metadata that dereferencing does 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>`. Trying to narrow it down, it seems that was because `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. The root cause is that `ptr::read` is currently implemented via the *untyped* `copy_nonoverlapping`, and thus the `load` doesn't get any type-aware metadata: no `noundef`, no `!range`. This PR solves that by lowering `ptr::read(p)` to `copy *p` in MIR, for which the backends already do the right thing. Fortuitiously, this also improves the IR we give to LLVM for things like `mem::replace`, and fixes a couple of long-standing bugs where `ptr::read` on `Copy` types was worse than `*`ing them. Zulip conversation: <https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Move.20array.3A.3AIntoIter.20to.20ManuallyDrop/near/341189936> cc `@erikdesjardins` `@JakobDegen` `@workingjubilee` `@the8472` Fixes rust-lang#106369 Fixes rust-lang#73258
- Loading branch information
Showing
17 changed files
with
358 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// compile-flags: -O | ||
// ignore-debug (the extra assertions get in the way) | ||
|
||
#![crate_type = "lib"] | ||
|
||
// From <https://github.com/rust-lang/rust/issues/106369#issuecomment-1369095304> | ||
|
||
// CHECK-LABEL: @issue_106369( | ||
#[no_mangle] | ||
pub unsafe fn issue_106369(ptr: *const &i32) -> bool { | ||
// CHECK-NOT: icmp | ||
// CHECK: ret i1 true | ||
// CHECK-NOT: icmp | ||
Some(std::ptr::read(ptr)).is_some() | ||
} |
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,38 @@ | ||
// compile-flags: -O | ||
// ignore-debug (the extra assertions get in the way) | ||
|
||
#![crate_type = "lib"] | ||
|
||
// Adapted from <https://github.com/rust-lang/rust/issues/73258#issue-637346014> | ||
|
||
#[derive(Clone, Copy)] | ||
#[repr(u8)] | ||
pub enum Foo { | ||
A, B, C, D, | ||
} | ||
|
||
// CHECK-LABEL: @issue_73258( | ||
#[no_mangle] | ||
pub unsafe fn issue_73258(ptr: *const Foo) -> Foo { | ||
// CHECK-NOT: icmp | ||
// CHECK-NOT: call | ||
// CHECK-NOT: br | ||
// CHECK-NOT: select | ||
|
||
// CHECK: %[[R:.+]] = load i8 | ||
// CHECK-SAME: !range ! | ||
|
||
// CHECK-NOT: icmp | ||
// CHECK-NOT: call | ||
// CHECK-NOT: br | ||
// CHECK-NOT: select | ||
|
||
// CHECK: ret i8 %[[R]] | ||
|
||
// CHECK-NOT: icmp | ||
// CHECK-NOT: call | ||
// CHECK-NOT: br | ||
// CHECK-NOT: select | ||
let k: Option<Foo> = Some(ptr.read()); | ||
return k.unwrap(); | ||
} |
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,36 @@ | ||
// This test ensures that `mem::replace::<T>` only ever calls `@llvm.memcpy` | ||
// with `size_of::<T>()` as the size, and never goes through any wrapper that | ||
// may e.g. multiply `size_of::<T>()` with a variable "count" (which is only | ||
// known to be `1` after inlining). | ||
|
||
// compile-flags: -C no-prepopulate-passes -Zinline-mir=no | ||
// ignore-debug: the debug assertions get in the way | ||
|
||
#![crate_type = "lib"] | ||
|
||
#[repr(C, align(8))] | ||
pub struct Big([u64; 7]); | ||
pub fn replace_big(dst: &mut Big, src: Big) -> Big { | ||
// Before the `read_via_copy` intrinsic, this emitted six `memcpy`s. | ||
std::mem::replace(dst, src) | ||
} | ||
|
||
// NOTE(eddyb) the `CHECK-NOT`s ensure that the only calls of `@llvm.memcpy` in | ||
// the entire output, are the direct calls we want, from `ptr::replace`. | ||
|
||
// CHECK-NOT: call void @llvm.memcpy | ||
|
||
// For a large type, we expect exactly three `memcpy`s | ||
// CHECK-LABEL: define internal void @{{.+}}mem{{.+}}replace{{.+}}sret(%Big) | ||
// CHECK-NOT: alloca | ||
// CHECK: alloca %Big | ||
// CHECK-NOT: alloca | ||
// CHECK-NOT: call void @llvm.memcpy | ||
// CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) | ||
// CHECK-NOT: call void @llvm.memcpy | ||
// CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) | ||
// CHECK-NOT: call void @llvm.memcpy | ||
// CHECK: call void @llvm.memcpy.{{.+}}({{i8\*|ptr}} align 8 %{{.*}}, {{i8\*|ptr}} align 8 %{{.*}}, i{{.*}} 56, i1 false) | ||
// CHECK-NOT: call void @llvm.memcpy | ||
|
||
// CHECK-NOT: call void @llvm.memcpy |
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,96 @@ | ||
// 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` and `!range` metadata to enable extra optimization. | ||
|
||
use std::mem::MaybeUninit; | ||
|
||
// CHECK-LABEL: define noundef i8 @copy_byte( | ||
#[no_mangle] | ||
pub unsafe fn copy_byte(p: *const u8) -> u8 { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
*p | ||
} | ||
|
||
// CHECK-LABEL: define noundef i8 @read_byte( | ||
#[no_mangle] | ||
pub unsafe fn read_byte(p: *const u8) -> u8 { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
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 noundef i8 @read_byte_assume_init( | ||
#[no_mangle] | ||
pub unsafe fn read_byte_assume_init(p: &MaybeUninit<u8>) -> u8 { | ||
// CHECK-NOT: load | ||
// CHECK: load i8, ptr %p, align 1 | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
p.assume_init_read() | ||
} | ||
|
||
// CHECK-LABEL: define noundef i32 @copy_char( | ||
#[no_mangle] | ||
pub unsafe fn copy_char(p: *const char) -> char { | ||
// CHECK-NOT: load | ||
// CHECK: load i32, ptr %p | ||
// CHECK-SAME: !range ![[RANGE:[0-9]+]] | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
*p | ||
} | ||
|
||
// CHECK-LABEL: define noundef i32 @read_char( | ||
#[no_mangle] | ||
pub unsafe fn read_char(p: *const char) -> char { | ||
// CHECK-NOT: load | ||
// CHECK: load i32, ptr %p | ||
// CHECK-SAME: !range ![[RANGE]] | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
p.read() | ||
} | ||
|
||
// CHECK-LABEL: define i32 @read_char_maybe_uninit( | ||
#[no_mangle] | ||
pub unsafe fn read_char_maybe_uninit(p: *const MaybeUninit<char>) -> MaybeUninit<char> { | ||
// CHECK-NOT: load | ||
// CHECK: load i32, ptr %p | ||
// CHECK-NOT: range | ||
// CHECK-NOT: noundef | ||
// CHECK-NOT: load | ||
p.read() | ||
} | ||
|
||
// CHECK-LABEL: define noundef i32 @read_char_assume_init( | ||
#[no_mangle] | ||
pub unsafe fn read_char_assume_init(p: &MaybeUninit<char>) -> char { | ||
// CHECK-NOT: load | ||
// CHECK: load i32, ptr %p | ||
// CHECK-SAME: !range ![[RANGE]] | ||
// CHECK-SAME: !noundef ! | ||
// CHECK-NOT: load | ||
p.assume_init_read() | ||
} | ||
|
||
// CHECK: ![[RANGE]] = !{i32 0, i32 1114112} |
Oops, something went wrong.