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

+3-1
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

+5-9
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

+1-1
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

+1-2
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

+1-2
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

+1-1
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

+4-5
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

+4-5
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

+4-5
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

+4-5
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.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
// CHECK-LABEL: fn main(
99
// CHECK: debug a => [[a:_.*]];
1010
// CHECK: [[slice:_.*]] = copy {{.*}} as &[u32] (PointerCoercion(Unsize, AsCast));
11-
// CHECK: assert(const true,
12-
// CHECK: [[a]] = const 2_u32;
11+
// CHECK-NOT: assert(const true,
12+
// CHECK-NOT: [[a]] = const 2_u32;
1313
let a = (&[1u32, 2, 3] as &[u32])[1];
1414
}

tests/mir-opt/gvn.borrowed.GVN.panic-abort.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
}
1919

2020
bb2: {
21-
- _0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind unreachable];
22-
+ _0 = opaque::<T>(copy _1) -> [return: bb3, unwind unreachable];
21+
_0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind unreachable];
2322
}
2423

2524
bb3: {

tests/mir-opt/gvn.borrowed.GVN.panic-unwind.diff

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
}
1919

2020
bb2: {
21-
- _0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind continue];
22-
+ _0 = opaque::<T>(copy _1) -> [return: bb3, unwind continue];
21+
_0 = opaque::<T>(copy (*_3)) -> [return: bb3, unwind continue];
2322
}
2423

2524
bb3: {

tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff

+10-20
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,18 @@
107107
StorageLive(_18);
108108
_18 = &(*_1);
109109
StorageLive(_19);
110-
- StorageLive(_20);
111-
+ nop;
110+
StorageLive(_20);
112111
_20 = copy (*_18);
113-
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind unreachable];
114-
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind unreachable];
112+
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind unreachable];
115113
}
116114

117115
bb7: {
118-
- StorageDead(_20);
119-
+ nop;
116+
StorageDead(_20);
120117
StorageDead(_19);
121118
StorageLive(_21);
122119
StorageLive(_22);
123-
- _22 = copy (*_18);
124-
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind unreachable];
125-
+ _22 = copy _20;
126-
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind unreachable];
120+
_22 = copy (*_18);
121+
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind unreachable];
127122
}
128123

129124
bb8: {
@@ -157,23 +152,18 @@
157152
StorageDead(_28);
158153
StorageDead(_27);
159154
StorageLive(_29);
160-
- StorageLive(_30);
161-
+ nop;
155+
StorageLive(_30);
162156
_30 = copy ((*_3).0: u32);
163-
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind unreachable];
164-
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind unreachable];
157+
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind unreachable];
165158
}
166159

167160
bb12: {
168-
- StorageDead(_30);
169-
+ nop;
161+
StorageDead(_30);
170162
StorageDead(_29);
171163
StorageLive(_31);
172164
StorageLive(_32);
173-
- _32 = copy ((*_3).0: u32);
174-
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind unreachable];
175-
+ _32 = copy _30;
176-
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind unreachable];
165+
_32 = copy ((*_3).0: u32);
166+
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind unreachable];
177167
}
178168

179169
bb13: {

tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff

+10-20
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,18 @@
107107
StorageLive(_18);
108108
_18 = &(*_1);
109109
StorageLive(_19);
110-
- StorageLive(_20);
111-
+ nop;
110+
StorageLive(_20);
112111
_20 = copy (*_18);
113-
- _19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
114-
+ _19 = opaque::<u32>(copy _20) -> [return: bb7, unwind continue];
112+
_19 = opaque::<u32>(move _20) -> [return: bb7, unwind continue];
115113
}
116114

117115
bb7: {
118-
- StorageDead(_20);
119-
+ nop;
116+
StorageDead(_20);
120117
StorageDead(_19);
121118
StorageLive(_21);
122119
StorageLive(_22);
123-
- _22 = copy (*_18);
124-
- _21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
125-
+ _22 = copy _20;
126-
+ _21 = opaque::<u32>(copy _20) -> [return: bb8, unwind continue];
120+
_22 = copy (*_18);
121+
_21 = opaque::<u32>(move _22) -> [return: bb8, unwind continue];
127122
}
128123

129124
bb8: {
@@ -157,23 +152,18 @@
157152
StorageDead(_28);
158153
StorageDead(_27);
159154
StorageLive(_29);
160-
- StorageLive(_30);
161-
+ nop;
155+
StorageLive(_30);
162156
_30 = copy ((*_3).0: u32);
163-
- _29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
164-
+ _29 = opaque::<u32>(copy _30) -> [return: bb12, unwind continue];
157+
_29 = opaque::<u32>(move _30) -> [return: bb12, unwind continue];
165158
}
166159

167160
bb12: {
168-
- StorageDead(_30);
169-
+ nop;
161+
StorageDead(_30);
170162
StorageDead(_29);
171163
StorageLive(_31);
172164
StorageLive(_32);
173-
- _32 = copy ((*_3).0: u32);
174-
- _31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
175-
+ _32 = copy _30;
176-
+ _31 = opaque::<u32>(copy _30) -> [return: bb13, unwind continue];
165+
_32 = copy ((*_3).0: u32);
166+
_31 = opaque::<u32>(move _32) -> [return: bb13, unwind continue];
177167
}
178168

179169
bb13: {

tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
let mut _3: fn(u8) -> u8;
99
let _5: ();
1010
let mut _6: fn(u8) -> u8;
11-
let mut _9: {closure@$DIR/gvn.rs:614:19: 614:21};
11+
let mut _9: {closure@$DIR/gvn.rs:620:19: 620:21};
1212
let _10: ();
1313
let mut _11: fn();
14-
let mut _13: {closure@$DIR/gvn.rs:614:19: 614:21};
14+
let mut _13: {closure@$DIR/gvn.rs:620:19: 620:21};
1515
let _14: ();
1616
let mut _15: fn();
1717
scope 1 {
1818
debug f => _1;
1919
let _4: fn(u8) -> u8;
2020
scope 2 {
2121
debug g => _4;
22-
let _7: {closure@$DIR/gvn.rs:614:19: 614:21};
22+
let _7: {closure@$DIR/gvn.rs:620:19: 620:21};
2323
scope 3 {
2424
debug closure => _7;
2525
let _8: fn();
@@ -62,16 +62,16 @@
6262
StorageDead(_6);
6363
StorageDead(_5);
6464
- StorageLive(_7);
65-
- _7 = {closure@$DIR/gvn.rs:614:19: 614:21};
65+
- _7 = {closure@$DIR/gvn.rs:620:19: 620:21};
6666
- StorageLive(_8);
6767
+ nop;
68-
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
68+
+ _7 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
6969
+ nop;
7070
StorageLive(_9);
7171
- _9 = copy _7;
7272
- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
73-
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
74-
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
73+
+ _9 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
74+
+ _8 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
7575
StorageDead(_9);
7676
StorageLive(_10);
7777
StorageLive(_11);
@@ -88,8 +88,8 @@
8888
StorageLive(_13);
8989
- _13 = copy _7;
9090
- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
91-
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21};
92-
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:614:19: 614:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
91+
+ _13 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21};
92+
+ _12 = const ZeroSized: {closure@$DIR/gvn.rs:620:19: 620:21} as fn() (PointerCoercion(ClosureFnPointer(Safe), AsCast));
9393
StorageDead(_13);
9494
StorageLive(_14);
9595
StorageLive(_15);

0 commit comments

Comments
 (0)