Skip to content

Commit f3ec12a

Browse files
committed
Preemptively invalidate derefs when entering a bb.
1 parent 8d9586d commit f3ec12a

File tree

4 files changed

+70
-63
lines changed

4 files changed

+70
-63
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,13 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18511851
self.tcx
18521852
}
18531853

1854+
fn visit_basic_block_data(&mut self, block: BasicBlock, bbdata: &mut BasicBlockData<'tcx>) {
1855+
// We do not track which block we would execute between the last and this one.
1856+
// In doubt, consider one of them may contain an indirect assignment and invalidate derefs.
1857+
self.invalidate_derefs();
1858+
self.super_basic_block_data(block, bbdata);
1859+
}
1860+
18541861
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
18551862
self.simplify_place_projection(place, location);
18561863
if context.is_mutating_use() && place.is_indirect() {

tests/mir-opt/gvn_clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ test-mir-pass: GVN
2-
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline
2+
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-inline,+SimplifyCfg-after-unreachable-enum-branching
33

44
// Check if we have transformed the default clone to copy in the specific pipeline.
55

tests/mir-opt/gvn_clone.{impl#0}-clone.GVN.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
_3 = copy _4;
2424
- _2 = copy (*_3);
2525
+ _2 = copy ((*_1).0: i32);
26-
goto -> bb1;
27-
}
28-
29-
bb1: {
3026
StorageDead(_3);
3127
StorageLive(_5);
3228
StorageLive(_6);
@@ -36,10 +32,6 @@
3632
_6 = copy _7;
3733
- _5 = copy (*_6);
3834
+ _5 = copy ((*_1).1: u64);
39-
goto -> bb2;
40-
}
41-
42-
bb2: {
4335
StorageDead(_6);
4436
StorageLive(_8);
4537
StorageLive(_9);
@@ -49,10 +41,6 @@
4941
_9 = copy _10;
5042
- _8 = copy (*_9);
5143
+ _8 = copy ((*_1).2: [i8; 3]);
52-
goto -> bb3;
53-
}
54-
55-
bb3: {
5644
StorageDead(_9);
5745
- _0 = AllCopy { a: move _2, b: move _5, c: move _8 };
5846
- StorageDead(_10);

tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,89 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
55
let mut _3: &(usize, usize, usize, usize);
66
let mut _6: bool;
77
let mut _9: bool;
8-
let mut _10: bool;
9-
let _13: &usize;
10-
let _14: &usize;
8+
let mut _12: bool;
119
let _15: &usize;
1210
let _16: &usize;
13-
let mut _17: &&usize;
14-
let mut _18: &&usize;
11+
let _17: &usize;
12+
let _18: &usize;
1513
let mut _19: &&usize;
1614
let mut _20: &&usize;
1715
let mut _21: &&usize;
1816
let mut _22: &&usize;
1917
let mut _23: &&usize;
2018
let mut _24: &&usize;
19+
let mut _25: &&usize;
20+
let mut _26: &&usize;
2121
scope 1 {
22-
debug a => _13;
23-
debug b => _14;
24-
debug c => _15;
25-
debug d => _16;
22+
debug a => _15;
23+
debug b => _16;
24+
debug c => _17;
25+
debug d => _18;
2626
scope 2 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
27-
debug self => _17;
28-
debug other => _18;
27+
debug self => _19;
28+
debug other => _20;
2929
scope 3 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
30-
debug self => _13;
31-
debug other => _15;
30+
debug self => _15;
31+
debug other => _17;
3232
let mut _4: usize;
3333
let mut _5: usize;
3434
}
3535
}
3636
scope 4 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
37-
debug self => _19;
38-
debug other => _20;
37+
debug self => _21;
38+
debug other => _22;
3939
scope 5 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
40-
debug self => _16;
41-
debug other => _14;
40+
debug self => _18;
41+
debug other => _16;
4242
let mut _7: usize;
4343
let mut _8: usize;
4444
}
4545
}
4646
scope 6 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
47-
debug self => _21;
48-
debug other => _22;
47+
debug self => _23;
48+
debug other => _24;
4949
scope 7 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
50-
debug self => _15;
51-
debug other => _13;
50+
debug self => _17;
51+
debug other => _15;
52+
let mut _10: usize;
53+
let mut _11: usize;
5254
}
5355
}
5456
scope 8 (inlined std::cmp::impls::<impl PartialOrd for &usize>::le) {
55-
debug self => _23;
56-
debug other => _24;
57+
debug self => _25;
58+
debug other => _26;
5759
scope 9 (inlined std::cmp::impls::<impl PartialOrd for usize>::le) {
58-
debug self => _14;
59-
debug other => _16;
60-
let mut _11: usize;
61-
let mut _12: usize;
60+
debug self => _16;
61+
debug other => _18;
62+
let mut _13: usize;
63+
let mut _14: usize;
6264
}
6365
}
6466
}
6567

6668
bb0: {
6769
_3 = copy (*_2);
68-
// DBG: _13 = &((*_3).0: usize);
69-
// DBG: _14 = &((*_3).1: usize);
70-
// DBG: _15 = &((*_3).2: usize);
71-
// DBG: _16 = &((*_3).3: usize);
70+
// DBG: _15 = &((*_3).0: usize);
71+
// DBG: _16 = &((*_3).1: usize);
72+
// DBG: _17 = &((*_3).2: usize);
73+
// DBG: _18 = &((*_3).3: usize);
7274
StorageLive(_6);
73-
// DBG: _17 = &_13;
74-
// DBG: _18 = &?;
75+
// DBG: _19 = &_15;
76+
// DBG: _20 = &?;
77+
StorageLive(_4);
7578
_4 = copy ((*_3).0: usize);
79+
StorageLive(_5);
7680
_5 = copy ((*_3).2: usize);
77-
_6 = Le(copy _4, copy _5);
81+
_6 = Le(move _4, move _5);
82+
StorageDead(_5);
83+
StorageDead(_4);
7884
switchInt(move _6) -> [0: bb2, otherwise: bb1];
7985
}
8086

8187
bb1: {
8288
StorageLive(_9);
83-
// DBG: _19 = &_16;
84-
// DBG: _20 = &?;
89+
// DBG: _21 = &_18;
90+
// DBG: _22 = &?;
8591
StorageLive(_7);
8692
_7 = copy ((*_3).3: usize);
8793
StorageLive(_8);
@@ -93,11 +99,17 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
9399
}
94100

95101
bb2: {
102+
StorageLive(_12);
103+
// DBG: _23 = &_17;
104+
// DBG: _24 = &?;
96105
StorageLive(_10);
97-
// DBG: _21 = &_15;
98-
// DBG: _22 = &?;
99-
_10 = Le(copy _5, copy _4);
100-
switchInt(move _10) -> [0: bb3, otherwise: bb4];
106+
_10 = copy ((*_3).2: usize);
107+
StorageLive(_11);
108+
_11 = copy ((*_3).0: usize);
109+
_12 = Le(move _10, move _11);
110+
StorageDead(_11);
111+
StorageDead(_10);
112+
switchInt(move _12) -> [0: bb3, otherwise: bb4];
101113
}
102114

103115
bb3: {
@@ -106,20 +118,20 @@ fn variant_a::{closure#0}(_1: &mut {closure@$DIR/slice_filter.rs:7:25: 7:39}, _2
106118
}
107119

108120
bb4: {
109-
// DBG: _23 = &_14;
110-
// DBG: _24 = &?;
111-
StorageLive(_11);
112-
_11 = copy ((*_3).1: usize);
113-
StorageLive(_12);
114-
_12 = copy ((*_3).3: usize);
115-
_0 = Le(move _11, move _12);
116-
StorageDead(_12);
117-
StorageDead(_11);
121+
// DBG: _25 = &_16;
122+
// DBG: _26 = &?;
123+
StorageLive(_13);
124+
_13 = copy ((*_3).1: usize);
125+
StorageLive(_14);
126+
_14 = copy ((*_3).3: usize);
127+
_0 = Le(move _13, move _14);
128+
StorageDead(_14);
129+
StorageDead(_13);
118130
goto -> bb5;
119131
}
120132

121133
bb5: {
122-
StorageDead(_10);
134+
StorageDead(_12);
123135
goto -> bb7;
124136
}
125137

0 commit comments

Comments
 (0)