Skip to content

Commit c9f36f8

Browse files
committed
Only update Eq operands in GVN if you can update both sides
Otherwise the types might not match Fixes 127089
1 parent e9e6e2e commit c9f36f8

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10741074
{
10751075
lhs = *lhs_value;
10761076
rhs = *rhs_value;
1077-
if let Some(op) = self.try_as_operand(lhs, location) {
1078-
*lhs_operand = op;
1079-
}
1080-
if let Some(op) = self.try_as_operand(rhs, location) {
1081-
*rhs_operand = op;
1077+
if let Some(lhs_op) = self.try_as_operand(lhs, location)
1078+
&& let Some(rhs_op) = self.try_as_operand(rhs, location)
1079+
{
1080+
*lhs_operand = lhs_op;
1081+
*rhs_operand = rhs_op;
10821082
}
10831083
}
10841084

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- // MIR for `main` before GVN
2+
+ // MIR for `main` after GVN
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: bool;
7+
let mut _2: *mut u8;
8+
scope 1 (inlined dangling_mut::<u8>) {
9+
let mut _3: usize;
10+
scope 2 (inlined align_of::<u8>) {
11+
}
12+
scope 3 (inlined without_provenance_mut::<u8>) {
13+
}
14+
}
15+
scope 4 (inlined Foo::<u8>::cmp_ptr) {
16+
let mut _4: *const u8;
17+
let mut _5: *mut u8;
18+
let mut _6: *const u8;
19+
scope 5 (inlined std::ptr::eq::<u8>) {
20+
}
21+
}
22+
23+
bb0: {
24+
StorageLive(_1);
25+
StorageLive(_2);
26+
StorageLive(_3);
27+
- _3 = AlignOf(u8);
28+
- _2 = _3 as *mut u8 (Transmute);
29+
+ _3 = const 1_usize;
30+
+ _2 = const {0x1 as *mut u8};
31+
StorageDead(_3);
32+
StorageLive(_4);
33+
StorageLive(_5);
34+
- _5 = _2;
35+
- _4 = _2 as *const u8 (PtrToPtr);
36+
+ _5 = const {0x1 as *mut u8};
37+
+ _4 = const {0x1 as *const u8};
38+
StorageDead(_5);
39+
StorageLive(_6);
40+
- _6 = const Foo::<u8>::SENTINEL as *const u8 (PtrToPtr);
41+
- _1 = Eq(_4, _6);
42+
+ _6 = const {0x1 as *const u8};
43+
+ _1 = const true;
44+
StorageDead(_6);
45+
StorageDead(_4);
46+
StorageDead(_2);
47+
StorageDead(_1);
48+
_0 = const ();
49+
return;
50+
}
51+
}
52+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// skip-filecheck
2+
//@ test-mir-pass: GVN
3+
//@ only-64bit
4+
//@ compile-flags: -Z mir-enable-passes=+Inline
5+
6+
// Regression for <https://github.com/rust-lang/rust/issues/127089>
7+
8+
#![feature(strict_provenance)]
9+
10+
struct Foo<T>(std::marker::PhantomData<T>);
11+
12+
impl<T> Foo<T> {
13+
const SENTINEL: *mut T = std::ptr::dangling_mut();
14+
15+
fn cmp_ptr(a: *mut T) -> bool {
16+
std::ptr::eq(a, Self::SENTINEL)
17+
}
18+
}
19+
20+
// EMIT_MIR gvn_ptr_eq_with_constant.main.GVN.diff
21+
pub fn main() {
22+
Foo::<u8>::cmp_ptr(std::ptr::dangling_mut());
23+
}

0 commit comments

Comments
 (0)