Skip to content

Commit 2a32a2b

Browse files
committed
Special case deref projections in SsaVisitor.
1 parent 43ee4d1 commit 2a32a2b

File tree

6 files changed

+222
-14
lines changed

6 files changed

+222
-14
lines changed

compiler/rustc_mir_transform/src/ssa.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ struct SsaVisitor {
179179
assignment_order: Vec<Local>,
180180
}
181181

182+
impl SsaVisitor {
183+
fn check_assignment_dominates(&mut self, local: Local, loc: Location) {
184+
let set = &mut self.assignments[local];
185+
let assign_dominates = match *set {
186+
Set1::Empty | Set1::Many => false,
187+
Set1::One(LocationExtended::Arg) => true,
188+
Set1::One(LocationExtended::Plain(assign)) => {
189+
assign.successor_within_block().dominates(loc, &self.dominators)
190+
}
191+
};
192+
// We are visiting a use that is not dominated by an assignment.
193+
// Either there is a cycle involved, or we are reading for uninitialized local.
194+
// Bail out.
195+
if !assign_dominates {
196+
*set = Set1::Many;
197+
}
198+
}
199+
}
200+
182201
impl<'tcx> Visitor<'tcx> for SsaVisitor {
183202
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
184203
match ctxt {
@@ -192,24 +211,26 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor {
192211
// Immutable borrows are taken into account in `SsaLocals::new` by
193212
// removing non-freeze locals.
194213
PlaceContext::NonMutatingUse(_) => {
195-
let set = &mut self.assignments[local];
196-
let assign_dominates = match *set {
197-
Set1::Empty | Set1::Many => false,
198-
Set1::One(LocationExtended::Arg) => true,
199-
Set1::One(LocationExtended::Plain(assign)) => {
200-
assign.successor_within_block().dominates(loc, &self.dominators)
201-
}
202-
};
203-
// We are visiting a use that is not dominated by an assignment.
204-
// Either there is a cycle involved, or we are reading for uninitialized local.
205-
// Bail out.
206-
if !assign_dominates {
207-
*set = Set1::Many;
208-
}
214+
self.check_assignment_dominates(local, loc);
209215
}
210216
PlaceContext::NonUse(_) => {}
211217
}
212218
}
219+
220+
fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, loc: Location) {
221+
if place.projection.first() == Some(&PlaceElem::Deref) {
222+
// Do not do anything for storage statements and debuginfo.
223+
if ctxt.is_use() {
224+
// A use through a `deref` only reads from the local, and cannot write to it.
225+
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection);
226+
227+
self.visit_projection(place.as_ref(), new_ctxt, loc);
228+
self.check_assignment_dominates(place.local, loc);
229+
}
230+
return;
231+
}
232+
self.super_place(place, ctxt, loc);
233+
}
213234
}
214235

215236
#[instrument(level = "trace", skip(ssa, body))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
- // MIR for `demiraw` before CopyProp
2+
+ // MIR for `demiraw` after CopyProp
3+
4+
fn demiraw(_1: u8) -> () {
5+
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:12: +0:17
6+
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:23: +0:23
7+
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
let mut _4: &mut u8; // in scope 0 at $DIR/reborrow.rs:+2:22: +2:29
9+
scope 1 {
10+
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
11+
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
12+
scope 2 {
13+
debug b => _3; // in scope 2 at $DIR/reborrow.rs:+2:9: +2:10
14+
let _5: *mut u8; // in scope 2 at $DIR/reborrow.rs:+3:9: +3:10
15+
scope 4 {
16+
- debug c => _5; // in scope 4 at $DIR/reborrow.rs:+3:9: +3:10
17+
+ debug c => _2; // in scope 4 at $DIR/reborrow.rs:+3:9: +3:10
18+
}
19+
}
20+
scope 3 {
21+
}
22+
}
23+
24+
bb0: {
25+
- StorageLive(_2); // scope 0 at $DIR/reborrow.rs:+1:9: +1:10
26+
_2 = &raw mut _1; // scope 0 at $DIR/reborrow.rs:+1:13: +1:23
27+
StorageLive(_3); // scope 1 at $DIR/reborrow.rs:+2:9: +2:10
28+
StorageLive(_4); // scope 1 at $DIR/reborrow.rs:+2:22: +2:29
29+
_4 = &mut (*_2); // scope 3 at $DIR/reborrow.rs:+2:22: +2:29
30+
_3 = &mut (*_4); // scope 1 at $DIR/reborrow.rs:+2:22: +2:29
31+
StorageDead(_4); // scope 1 at $DIR/reborrow.rs:+2:31: +2:32
32+
- StorageLive(_5); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
33+
- _5 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
34+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:23: +4:2
35+
- StorageDead(_5); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
36+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
37+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
38+
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
39+
}
40+
}
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- // MIR for `miraw` before CopyProp
2+
+ // MIR for `miraw` after CopyProp
3+
4+
fn miraw(_1: u8) -> () {
5+
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
6+
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
7+
let _2: *mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
scope 1 {
9+
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
10+
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
11+
scope 2 {
12+
debug b => _3; // in scope 2 at $DIR/reborrow.rs:+2:9: +2:10
13+
let _4: *mut u8; // in scope 2 at $DIR/reborrow.rs:+3:9: +3:10
14+
scope 4 {
15+
- debug c => _4; // in scope 4 at $DIR/reborrow.rs:+3:9: +3:10
16+
+ debug c => _2; // in scope 4 at $DIR/reborrow.rs:+3:9: +3:10
17+
}
18+
}
19+
scope 3 {
20+
}
21+
}
22+
23+
bb0: {
24+
- StorageLive(_2); // scope 0 at $DIR/reborrow.rs:+1:9: +1:10
25+
_2 = &raw mut _1; // scope 0 at $DIR/reborrow.rs:+1:13: +1:23
26+
StorageLive(_3); // scope 1 at $DIR/reborrow.rs:+2:9: +2:10
27+
_3 = &raw mut (*_2); // scope 3 at $DIR/reborrow.rs:+2:22: +2:33
28+
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
29+
- _4 = _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
30+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
31+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
32+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
33+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
34+
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
35+
}
36+
}
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- // MIR for `remut` before CopyProp
2+
+ // MIR for `remut` after CopyProp
3+
4+
fn remut(_1: u8) -> () {
5+
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
6+
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
7+
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
scope 1 {
9+
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
10+
let _3: &mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
11+
scope 2 {
12+
debug b => _3; // in scope 2 at $DIR/reborrow.rs:+2:9: +2:10
13+
let _4: &mut u8; // in scope 2 at $DIR/reborrow.rs:+3:9: +3:10
14+
scope 3 {
15+
- debug c => _4; // in scope 3 at $DIR/reborrow.rs:+3:9: +3:10
16+
+ debug c => _2; // in scope 3 at $DIR/reborrow.rs:+3:9: +3:10
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
- StorageLive(_2); // scope 0 at $DIR/reborrow.rs:+1:9: +1:10
23+
_2 = &mut _1; // scope 0 at $DIR/reborrow.rs:+1:13: +1:19
24+
StorageLive(_3); // scope 1 at $DIR/reborrow.rs:+2:9: +2:10
25+
_3 = &mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:20
26+
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
27+
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
28+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
29+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
30+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
31+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
32+
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
33+
}
34+
}
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- // MIR for `reraw` before CopyProp
2+
+ // MIR for `reraw` after CopyProp
3+
4+
fn reraw(_1: u8) -> () {
5+
debug x => _1; // in scope 0 at $DIR/reborrow.rs:+0:10: +0:15
6+
let mut _0: (); // return place in scope 0 at $DIR/reborrow.rs:+0:21: +0:21
7+
let _2: &mut u8; // in scope 0 at $DIR/reborrow.rs:+1:9: +1:10
8+
scope 1 {
9+
debug a => _2; // in scope 1 at $DIR/reborrow.rs:+1:9: +1:10
10+
let _3: *mut u8; // in scope 1 at $DIR/reborrow.rs:+2:9: +2:10
11+
scope 2 {
12+
debug b => _3; // in scope 2 at $DIR/reborrow.rs:+2:9: +2:10
13+
let _4: &mut u8; // in scope 2 at $DIR/reborrow.rs:+3:9: +3:10
14+
scope 3 {
15+
- debug c => _4; // in scope 3 at $DIR/reborrow.rs:+3:9: +3:10
16+
+ debug c => _2; // in scope 3 at $DIR/reborrow.rs:+3:9: +3:10
17+
}
18+
}
19+
}
20+
21+
bb0: {
22+
- StorageLive(_2); // scope 0 at $DIR/reborrow.rs:+1:9: +1:10
23+
_2 = &mut _1; // scope 0 at $DIR/reborrow.rs:+1:13: +1:19
24+
StorageLive(_3); // scope 1 at $DIR/reborrow.rs:+2:9: +2:10
25+
_3 = &raw mut (*_2); // scope 1 at $DIR/reborrow.rs:+2:13: +2:24
26+
- StorageLive(_4); // scope 2 at $DIR/reborrow.rs:+3:9: +3:10
27+
- _4 = move _2; // scope 2 at $DIR/reborrow.rs:+3:13: +3:14
28+
_0 = const (); // scope 0 at $DIR/reborrow.rs:+0:21: +4:2
29+
- StorageDead(_4); // scope 2 at $DIR/reborrow.rs:+4:1: +4:2
30+
StorageDead(_3); // scope 1 at $DIR/reborrow.rs:+4:1: +4:2
31+
- StorageDead(_2); // scope 0 at $DIR/reborrow.rs:+4:1: +4:2
32+
return; // scope 0 at $DIR/reborrow.rs:+4:2: +4:2
33+
}
34+
}
35+

tests/mir-opt/copy-prop/reborrow.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Check that CopyProp considers reborrows as not mutating the pointer.
2+
// unit-test: CopyProp
3+
4+
#![feature(raw_ref_op)]
5+
6+
// EMIT_MIR reborrow.remut.CopyProp.diff
7+
fn remut(mut x: u8) {
8+
let a = &mut x;
9+
let b = &mut *a; //< this cannot mutate a.
10+
let c = a; //< so `c` and `a` can be merged.
11+
}
12+
13+
// EMIT_MIR reborrow.reraw.CopyProp.diff
14+
fn reraw(mut x: u8) {
15+
let a = &mut x;
16+
let b = &raw mut *a; //< this cannot mutate a.
17+
let c = a; //< so `c` and `a` can be merged.
18+
}
19+
20+
// EMIT_MIR reborrow.miraw.CopyProp.diff
21+
fn miraw(mut x: u8) {
22+
let a = &raw mut x;
23+
let b = unsafe { &raw mut *a }; //< this cannot mutate a.
24+
let c = a; //< so `c` and `a` can be merged.
25+
}
26+
27+
// EMIT_MIR reborrow.demiraw.CopyProp.diff
28+
fn demiraw(mut x: u8) {
29+
let a = &raw mut x;
30+
let b = unsafe { &mut *a }; //< this cannot mutate a.
31+
let c = a; //< so `c` and `a` can be merged.
32+
}
33+
34+
fn main() {
35+
remut(0);
36+
reraw(0);
37+
miraw(0);
38+
demiraw(0);
39+
}

0 commit comments

Comments
 (0)