Skip to content

Commit 917dd82

Browse files
cjgillotRalfJung
authored andcommitted
Do not unify dereferences in GVN.
1 parent 2cc0ee6 commit 917dd82

31 files changed

+351
-389
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
638638
let proj = match proj {
639639
ProjectionElem::Deref => {
640640
let ty = place.ty(self.local_decls, self.tcx).ty;
641-
if let Some(Mutability::Not) = ty.ref_mutability()
641+
// unsound: https://github.com/rust-lang/rust/issues/130853
642+
if self.tcx.sess.opts.unstable_opts.unsound_mir_opts
643+
&& let Some(Mutability::Not) = ty.ref_mutability()
642644
&& let Some(pointee_ty) = ty.builtin_deref(true)
643645
&& pointee_ty.is_freeze(self.tcx, self.typing_env())
644646
{

tests/mir-opt/const_prop/read_immutable_static.main.GVN.diff

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,19 @@
1414

1515
bb0: {
1616
StorageLive(_1);
17-
- StorageLive(_2);
17+
StorageLive(_2);
1818
- StorageLive(_3);
19-
+ nop;
2019
+ nop;
2120
_3 = const {ALLOC0: &u8};
22-
- _2 = copy (*_3);
23-
+ _2 = const 2_u8;
21+
_2 = copy (*_3);
2422
StorageLive(_4);
2523
StorageLive(_5);
2624
_5 = const {ALLOC0: &u8};
2725
- _4 = copy (*_5);
28-
- _1 = Add(move _2, move _4);
29-
+ _4 = const 2_u8;
30-
+ _1 = const 4_u8;
26+
+ _4 = copy (*_3);
27+
_1 = Add(move _2, move _4);
3128
StorageDead(_4);
32-
- StorageDead(_2);
33-
+ nop;
29+
StorageDead(_2);
3430
StorageDead(_5);
3531
- StorageDead(_3);
3632
+ nop;

tests/mir-opt/const_prop/read_immutable_static.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ static FOO: u8 = 2;
66
fn main() {
77
// CHECK-LABEL: fn main(
88
// CHECK: debug x => [[x:_.*]];
9-
// CHECK: [[x]] = const 4_u8;
9+
// CHECK-NOT: [[x]] = const 4_u8;
1010
let x = FOO + FOO;
1111
}

tests/mir-opt/const_prop/ref_deref.main.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
StorageLive(_2);
1717
_4 = const main::promoted[0];
1818
_2 = &(*_4);
19-
- _1 = copy (*_2);
20-
+ _1 = const 4_i32;
19+
_1 = copy (*_2);
2120
StorageDead(_2);
2221
_0 = const ();
2322
StorageDead(_1);

tests/mir-opt/const_prop/ref_deref_project.main.GVN.diff

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
StorageLive(_2);
1717
_4 = const main::promoted[0];
1818
_2 = &((*_4).1: i32);
19-
- _1 = copy (*_2);
20-
+ _1 = const 5_i32;
19+
_1 = copy (*_2);
2120
StorageDead(_2);
2221
_0 = const ();
2322
StorageDead(_1);

tests/mir-opt/const_prop/ref_deref_project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
fn main() {
66
// CHECK-LABEL: fn main(
77
// CHECK: debug a => [[a:_.*]];
8-
// CHECK: [[a]] = const 5_i32;
8+
// CHECK-NOT: [[a]] = const 5_i32;
99
let a = *(&(4, 5).1);
1010
}

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-abort.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind unreachable];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind unreachable];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.32bit.panic-unwind.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind continue];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind continue];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-abort.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind unreachable];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind unreachable];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind unreachable];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.GVN.64bit.panic-unwind.diff

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@
3030
StorageDead(_3);
3131
StorageLive(_6);
3232
_6 = const 1_usize;
33-
- _7 = Len((*_2));
33+
_7 = Len((*_2));
3434
- _8 = Lt(copy _6, copy _7);
3535
- assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, copy _6) -> [success: bb1, unwind continue];
36-
+ _7 = const 3_usize;
37-
+ _8 = const true;
38-
+ assert(const true, "index out of bounds: the length is {} but the index is {}", const 3_usize, const 1_usize) -> [success: bb1, unwind continue];
36+
+ _8 = Lt(const 1_usize, copy _7);
37+
+ assert(move _8, "index out of bounds: the length is {} but the index is {}", move _7, const 1_usize) -> [success: bb1, unwind continue];
3938
}
4039

4140
bb1: {
4241
- _1 = copy (*_2)[_6];
43-
+ _1 = const 2_u32;
42+
+ _1 = copy (*_2)[1 of 2];
4443
StorageDead(_6);
4544
StorageDead(_4);
4645
StorageDead(_2);

0 commit comments

Comments
 (0)