Skip to content

Commit 387aa6b

Browse files
committed
Also consider call and yield as MIR SSA.
1 parent 26089ba commit 387aa6b

8 files changed

+79
-98
lines changed

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ impl SsaLocals {
134134
body: &'a Body<'tcx>,
135135
) -> impl Iterator<Item = (Local, &'a Rvalue<'tcx>, Location)> + 'a {
136136
self.assignment_order.iter().filter_map(|&local| {
137-
if let Set1::One(LocationExtended::Plain(loc)) = self.assignments[local] {
138-
// `loc` must point to a direct assignment to `local`.
139-
let Either::Left(stmt) = body.stmt_at(loc) else { bug!() };
137+
if let Set1::One(LocationExtended::Plain(loc)) = self.assignments[local]
138+
// `loc` must point to:
139+
// - a direct assignment to `local`;
140+
// - a call or a yield terminator.
141+
&& let Either::Left(stmt) = body.stmt_at(loc)
142+
{
140143
let Some((target, rvalue)) = stmt.kind.as_assign() else { bug!() };
141144
assert_eq!(target.as_local(), Some(local));
142145
Some((local, rvalue, loc))
@@ -230,34 +233,28 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {
230233
}
231234

232235
fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, loc: Location) {
233-
if place.projection.first() == Some(&PlaceElem::Deref) {
234-
// Do not do anything for storage statements and debuginfo.
236+
if let PlaceContext::MutatingUse(MutatingUseContext::Store | MutatingUseContext::Call | MutatingUseContext::Yield) = ctxt
237+
&& let Some(local) = place.as_local()
238+
{
239+
self.assignments[local].insert(LocationExtended::Plain(loc));
240+
if let Set1::One(_) = self.assignments[local] {
241+
// Only record if SSA-like, to avoid growing the vector needlessly.
242+
self.assignment_order.push(local);
243+
}
244+
} else if place.projection.first() == Some(&PlaceElem::Deref) {
245+
// Do not do anything for debuginfo.
235246
if ctxt.is_use() {
236247
// Only change the context if it is a real use, not a "use" in debuginfo.
237248
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
238249

239250
self.visit_projection(place.as_ref(), new_ctxt, loc);
240251
self.dominators.check_dominates(&mut self.assignments[place.local], loc);
241252
}
242-
return;
243253
} else {
244254
self.visit_projection(place.as_ref(), ctxt, loc);
245255
self.visit_local(place.local, ctxt, loc);
246256
}
247257
}
248-
249-
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, loc: Location) {
250-
if let Some(local) = place.as_local() {
251-
self.assignments[local].insert(LocationExtended::Plain(loc));
252-
if let Set1::One(_) = self.assignments[local] {
253-
// Only record if SSA-like, to avoid growing the vector needlessly.
254-
self.assignment_order.push(local);
255-
}
256-
} else {
257-
self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), loc);
258-
}
259-
self.visit_rvalue(rvalue, loc);
260-
}
261258
}
262259

263260
#[instrument(level = "trace", skip(ssa, body))]

tests/mir-opt/inline/inline_diverging.h.Inline.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
+ scope 1 (inlined call_twice::<!, fn() -> ! {sleep}>) {
99
+ debug f => _2;
1010
+ let mut _3: &fn() -> ! {sleep};
11-
+ let mut _4: !;
11+
+ let _4: !;
1212
+ let mut _5: &fn() -> ! {sleep};
13-
+ let mut _6: !;
1413
+ scope 2 {
1514
+ debug a => _4;
15+
+ let _6: !;
1616
+ scope 3 {
1717
+ debug b => _6;
1818
+ }

tests/mir-opt/inline/inline_diverging.h.Inline.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
+ let mut _3: &fn() -> ! {sleep};
1111
+ let _4: !;
1212
+ let mut _5: &fn() -> ! {sleep};
13-
+ let mut _6: !;
1413
+ let mut _7: !;
1514
+ scope 2 {
1615
+ debug a => _4;
16+
+ let _6: !;
1717
+ scope 3 {
1818
+ debug b => _6;
1919
+ }

tests/mir-opt/pre-codegen/loops.filter_mapped.PreCodegen.after.mir

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,69 @@ fn filter_mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> Option<U>) -> ()
66
let mut _0: ();
77
let mut _3: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>;
88
let mut _4: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>;
9-
let mut _5: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>;
10-
let mut _8: std::option::Option<U>;
11-
let mut _9: isize;
12-
let _11: ();
13-
let mut _12: &mut std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>;
9+
let mut _7: std::option::Option<U>;
10+
let mut _8: isize;
11+
let _10: ();
12+
let mut _11: &mut std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>;
1413
scope 1 {
15-
debug iter => _5;
16-
let _10: U;
14+
debug iter => _4;
15+
let _9: U;
1716
scope 2 {
18-
debug x => _10;
17+
debug x => _9;
1918
}
2019
scope 4 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as Iterator>::next) {
21-
debug self => _12;
22-
let mut _6: &mut impl Iterator<Item = T>;
23-
let mut _7: &mut impl Fn(T) -> Option<U>;
20+
debug self => _11;
21+
let mut _5: &mut impl Iterator<Item = T>;
22+
let mut _6: &mut impl Fn(T) -> Option<U>;
2423
}
2524
}
2625
scope 3 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as IntoIterator>::into_iter) {
2726
debug self => _3;
2827
}
2928

3029
bb0: {
31-
StorageLive(_4);
32-
StorageLive(_3);
3330
_3 = <impl Iterator<Item = T> as Iterator>::filter_map::<U, impl Fn(T) -> Option<U>>(move _1, move _2) -> [return: bb1, unwind continue];
3431
}
3532

3633
bb1: {
34+
StorageLive(_4);
3735
_4 = move _3;
38-
StorageDead(_3);
39-
StorageLive(_5);
40-
_5 = move _4;
4136
goto -> bb2;
4237
}
4338

4439
bb2: {
45-
StorageLive(_8);
46-
StorageLive(_6);
47-
_6 = &mut (_5.0: impl Iterator<Item = T>);
4840
StorageLive(_7);
49-
_7 = &mut (_5.1: impl Fn(T) -> Option<U>);
50-
_8 = <impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>(move _6, move _7) -> [return: bb3, unwind: bb9];
41+
StorageLive(_5);
42+
_5 = &mut (_4.0: impl Iterator<Item = T>);
43+
StorageLive(_6);
44+
_6 = &mut (_4.1: impl Fn(T) -> Option<U>);
45+
_7 = <impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>(move _5, move _6) -> [return: bb3, unwind: bb9];
5146
}
5247

5348
bb3: {
54-
StorageDead(_7);
5549
StorageDead(_6);
56-
_9 = discriminant(_8);
57-
switchInt(move _9) -> [0: bb4, 1: bb6, otherwise: bb8];
50+
StorageDead(_5);
51+
_8 = discriminant(_7);
52+
switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8];
5853
}
5954

6055
bb4: {
61-
StorageDead(_8);
62-
drop(_5) -> [return: bb5, unwind continue];
56+
StorageDead(_7);
57+
drop(_4) -> [return: bb5, unwind continue];
6358
}
6459

6560
bb5: {
66-
StorageDead(_5);
6761
StorageDead(_4);
6862
return;
6963
}
7064

7165
bb6: {
72-
_10 = move ((_8 as Some).0: U);
73-
_11 = opaque::<U>(move _10) -> [return: bb7, unwind: bb9];
66+
_9 = move ((_7 as Some).0: U);
67+
_10 = opaque::<U>(move _9) -> [return: bb7, unwind: bb9];
7468
}
7569

7670
bb7: {
77-
StorageDead(_8);
71+
StorageDead(_7);
7872
goto -> bb2;
7973
}
8074

@@ -83,7 +77,7 @@ fn filter_mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> Option<U>) -> ()
8377
}
8478

8579
bb9 (cleanup): {
86-
drop(_5) -> [return: bb10, unwind terminate(cleanup)];
80+
drop(_4) -> [return: bb10, unwind terminate(cleanup)];
8781
}
8882

8983
bb10 (cleanup): {

tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,61 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
66
let mut _0: ();
77
let mut _3: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
88
let mut _4: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
9-
let mut _5: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
10-
let mut _6: &mut std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
11-
let mut _7: std::option::Option<U>;
12-
let mut _8: isize;
13-
let _10: ();
9+
let mut _5: &mut std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>;
10+
let mut _6: std::option::Option<U>;
11+
let mut _7: isize;
12+
let _9: ();
1413
scope 1 {
15-
debug iter => _5;
16-
let _9: U;
14+
debug iter => _4;
15+
let _8: U;
1716
scope 2 {
18-
debug x => _9;
17+
debug x => _8;
1918
}
2019
}
2120
scope 3 (inlined <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as IntoIterator>::into_iter) {
2221
debug self => _3;
2322
}
2423

2524
bb0: {
26-
StorageLive(_4);
27-
StorageLive(_3);
2825
_3 = <impl Iterator<Item = T> as Iterator>::map::<U, impl Fn(T) -> U>(move _1, move _2) -> [return: bb1, unwind continue];
2926
}
3027

3128
bb1: {
29+
StorageLive(_4);
3230
_4 = move _3;
33-
StorageDead(_3);
34-
StorageLive(_5);
35-
_5 = move _4;
3631
goto -> bb2;
3732
}
3833

3934
bb2: {
40-
StorageLive(_7);
4135
StorageLive(_6);
42-
_6 = &mut _5;
43-
_7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(move _6) -> [return: bb3, unwind: bb9];
36+
StorageLive(_5);
37+
_5 = &mut _4;
38+
_6 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(move _5) -> [return: bb3, unwind: bb9];
4439
}
4540

4641
bb3: {
47-
StorageDead(_6);
48-
_8 = discriminant(_7);
49-
switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8];
42+
StorageDead(_5);
43+
_7 = discriminant(_6);
44+
switchInt(move _7) -> [0: bb4, 1: bb6, otherwise: bb8];
5045
}
5146

5247
bb4: {
53-
StorageDead(_7);
54-
drop(_5) -> [return: bb5, unwind continue];
48+
StorageDead(_6);
49+
drop(_4) -> [return: bb5, unwind continue];
5550
}
5651

5752
bb5: {
58-
StorageDead(_5);
5953
StorageDead(_4);
6054
return;
6155
}
6256

6357
bb6: {
64-
_9 = move ((_7 as Some).0: U);
65-
_10 = opaque::<U>(move _9) -> [return: bb7, unwind: bb9];
58+
_8 = move ((_6 as Some).0: U);
59+
_9 = opaque::<U>(move _8) -> [return: bb7, unwind: bb9];
6660
}
6761

6862
bb7: {
69-
StorageDead(_7);
63+
StorageDead(_6);
7064
goto -> bb2;
7165
}
7266

@@ -75,7 +69,7 @@ fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () {
7569
}
7670

7771
bb9 (cleanup): {
78-
drop(_5) -> [return: bb10, unwind terminate(cleanup)];
72+
drop(_4) -> [return: bb10, unwind terminate(cleanup)];
7973
}
8074

8175
bb10 (cleanup): {

tests/mir-opt/pre-codegen/slice_index.slice_index_range.PreCodegen.after.panic-abort.mir

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ fn slice_index_range(_1: &[u32], _2: std::ops::Range<usize>) -> &[u32] {
77
scope 1 (inlined #[track_caller] core::slice::index::<impl Index<std::ops::Range<usize>> for [u32]>::index) {
88
debug self => _1;
99
debug index => _2;
10-
let _3: &[u32];
1110
}
1211

1312
bb0: {
14-
StorageLive(_3);
15-
_3 = <std::ops::Range<usize> as SliceIndex<[u32]>>::index(move _2, move _1) -> [return: bb1, unwind unreachable];
13+
_0 = <std::ops::Range<usize> as SliceIndex<[u32]>>::index(move _2, move _1) -> [return: bb1, unwind unreachable];
1614
}
1715

1816
bb1: {
19-
_0 = _3;
20-
StorageDead(_3);
2117
return;
2218
}
2319
}

tests/mir-opt/pre-codegen/slice_index.slice_index_range.PreCodegen.after.panic-unwind.mir

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ fn slice_index_range(_1: &[u32], _2: std::ops::Range<usize>) -> &[u32] {
77
scope 1 (inlined #[track_caller] core::slice::index::<impl Index<std::ops::Range<usize>> for [u32]>::index) {
88
debug self => _1;
99
debug index => _2;
10-
let _3: &[u32];
1110
}
1211

1312
bb0: {
14-
StorageLive(_3);
15-
_3 = <std::ops::Range<usize> as SliceIndex<[u32]>>::index(move _2, move _1) -> [return: bb1, unwind continue];
13+
_0 = <std::ops::Range<usize> as SliceIndex<[u32]>>::index(move _2, move _1) -> [return: bb1, unwind continue];
1614
}
1715

1816
bb1: {
19-
_0 = _3;
20-
StorageDead(_3);
2117
return;
2218
}
2319
}

tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
StorageDead(_7);
9393
- StorageDead(_6);
9494
- StorageLive(_10);
95-
StorageLive(_11);
96-
StorageLive(_12);
95+
- StorageLive(_11);
96+
- StorageLive(_12);
9797
StorageLive(_13);
9898
_26 = const _;
9999
_13 = &(*_26);
@@ -105,21 +105,25 @@
105105
bb5: {
106106
StorageDead(_15);
107107
StorageDead(_13);
108-
_11 = &(*_12);
109-
_16 = Len((*_11));
108+
- _11 = &(*_12);
109+
- _16 = Len((*_11));
110+
+ _16 = Len((*_12));
110111
_17 = const 3_usize;
111112
_18 = Ge(move _16, move _17);
112113
switchInt(move _18) -> [0: bb7, otherwise: bb6];
113114
}
114115

115116
bb6: {
116117
StorageLive(_19);
117-
_19 = &(*_11)[1 of 3];
118+
- _19 = &(*_11)[1 of 3];
119+
+ _19 = &(*_12)[1 of 3];
118120
StorageLive(_20);
119-
_20 = &(*_11)[2:-1];
121+
- _20 = &(*_11)[2:-1];
122+
+ _20 = &(*_12)[2:-1];
120123
StorageLive(_21);
121-
_21 = &(*_11)[-1 of 3];
124+
- _21 = &(*_11)[-1 of 3];
122125
- _10 = const ();
126+
+ _21 = &(*_12)[-1 of 3];
123127
StorageDead(_21);
124128
StorageDead(_20);
125129
StorageDead(_19);
@@ -132,8 +136,8 @@
132136
}
133137

134138
bb8: {
135-
StorageDead(_12);
136-
StorageDead(_11);
139+
- StorageDead(_12);
140+
- StorageDead(_11);
137141
- StorageDead(_10);
138142
StorageLive(_22);
139143
StorageLive(_23);

0 commit comments

Comments
 (0)