Skip to content

Commit 5e47c8e

Browse files
committed
Simplify transformed body.
1 parent 2856e09 commit 5e47c8e

9 files changed

+200
-112
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,16 +1319,6 @@ fn create_coroutine_resume_function<'tcx>(
13191319
make_coroutine_state_argument_indirect(tcx, body);
13201320
}
13211321
}
1322-
1323-
// Make sure we remove dead blocks to remove
1324-
// unrelated code from the drop part of the function
1325-
simplify::remove_dead_blocks(body);
1326-
1327-
pm::run_passes_no_validate(tcx, body, &[&abort_unwinding_calls::AbortUnwindingCalls], None);
1328-
1329-
if let Some(dumper) = MirDumper::new(tcx, "coroutine_resume", body) {
1330-
dumper.dump_mir(body);
1331-
}
13321322
}
13331323

13341324
/// An operation that can be performed on a coroutine.
@@ -1684,6 +1674,21 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16841674
// Create the Coroutine::resume / Future::poll function
16851675
create_coroutine_resume_function(tcx, transform, body, can_return, can_unwind);
16861676

1677+
if let Some(dumper) = MirDumper::new(tcx, "coroutine_resume", body) {
1678+
dumper.dump_mir(body);
1679+
}
1680+
1681+
pm::run_passes_no_validate(
1682+
tcx,
1683+
body,
1684+
&[
1685+
&crate::abort_unwinding_calls::AbortUnwindingCalls,
1686+
&crate::simplify::SimplifyCfg::PostStateTransform,
1687+
&crate::simplify::SimplifyLocals::PostStateTransform,
1688+
],
1689+
None,
1690+
);
1691+
16871692
// Run derefer to fix Derefs that are not in the first place
16881693
deref_finder(tcx, body);
16891694
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mod ssa;
8080
macro_rules! declare_passes {
8181
(
8282
$(
83-
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* } )?),+ $(,)?;
83+
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* $(,)? } )?),+ $(,)?;
8484
)*
8585
) => {
8686
$(
@@ -179,12 +179,14 @@ declare_passes! {
179179
PreOptimizations,
180180
Final,
181181
MakeShim,
182-
AfterUnreachableEnumBranching
182+
AfterUnreachableEnumBranching,
183+
PostStateTransform,
183184
},
184185
SimplifyLocals {
185186
BeforeConstProp,
186187
AfterGVN,
187-
Final
188+
Final,
189+
PostStateTransform,
188190
};
189191
mod simplify_branches : SimplifyConstCondition {
190192
AfterConstProp,

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub(super) enum SimplifyCfg {
5757
Final,
5858
MakeShim,
5959
AfterUnreachableEnumBranching,
60+
/// Extra run introduced by `StateTransform`.
61+
PostStateTransform,
6062
}
6163

6264
impl SimplifyCfg {
@@ -72,6 +74,7 @@ impl SimplifyCfg {
7274
SimplifyCfg::AfterUnreachableEnumBranching => {
7375
"SimplifyCfg-after-unreachable-enum-branching"
7476
}
77+
SimplifyCfg::PostStateTransform => "SimplifyCfg-post-StateTransform",
7578
}
7679
}
7780
}
@@ -416,6 +419,8 @@ pub(super) enum SimplifyLocals {
416419
BeforeConstProp,
417420
AfterGVN,
418421
Final,
422+
/// Extra run introduced by `StateTransform`.
423+
PostStateTransform,
419424
}
420425

421426
impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
@@ -424,6 +429,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
424429
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
425430
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
426431
SimplifyLocals::Final => "SimplifyLocals-final",
432+
SimplifyLocals::PostStateTransform => "SimplifyLocals-post-StateTransform",
427433
}
428434
}
429435

tests/mir-opt/building/async_await.a-{closure#0}.coroutine_resume.0.mir

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ fn a::{closure#0}(_1: Pin<&mut {async fn body of a()}>, _2: &mut Context<'_>) ->
1919
bb0: {
2020
_5 = copy (_1.0: &mut {async fn body of a()});
2121
_4 = discriminant((*_5));
22-
switchInt(move _4) -> [0: bb1, 1: bb4, otherwise: bb5];
22+
switchInt(move _4) -> [0: bb1, 1: bb9, otherwise: bb10];
2323
}
2424

2525
bb1: {
2626
_3 = const ();
27-
goto -> bb3;
27+
goto -> bb6;
2828
}
2929

3030
bb2: {
@@ -34,14 +34,34 @@ fn a::{closure#0}(_1: Pin<&mut {async fn body of a()}>, _2: &mut Context<'_>) ->
3434
}
3535

3636
bb3: {
37-
goto -> bb2;
37+
return;
3838
}
3939

4040
bb4: {
41-
assert(const false, "`async fn` resumed after completion") -> [success: bb4, unwind unreachable];
41+
goto -> bb8;
42+
}
43+
44+
bb5 (cleanup): {
45+
unreachable;
46+
}
47+
48+
bb6: {
49+
goto -> bb2;
50+
}
51+
52+
bb7 (cleanup): {
53+
resume;
54+
}
55+
56+
bb8: {
57+
goto -> bb3;
58+
}
59+
60+
bb9: {
61+
assert(const false, "`async fn` resumed after completion") -> [success: bb9, unwind continue];
4262
}
4363

44-
bb5: {
64+
bb10: {
4565
unreachable;
4666
}
4767
}

tests/mir-opt/building/async_await.b-{closure#0}.coroutine_resume.0.mir

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
105105
bb0: {
106106
_39 = copy (_1.0: &mut {async fn body of b()});
107107
_38 = discriminant((*_39));
108-
switchInt(move _38) -> [0: bb1, 1: bb27, 3: bb25, 4: bb26, otherwise: bb8];
108+
switchInt(move _38) -> [0: bb1, 1: bb39, 3: bb37, 4: bb38, otherwise: bb40];
109109
}
110110

111111
bb1: {
@@ -296,7 +296,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
296296
bb22: {
297297
nop;
298298
StorageDead(_21);
299-
goto -> bb24;
299+
goto -> bb33;
300300
}
301301

302302
bb23: {
@@ -306,10 +306,67 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
306306
}
307307

308308
bb24: {
309-
goto -> bb23;
309+
StorageDead(_36);
310+
StorageDead(_35);
311+
drop((((*_39) as variant#4).0: {async fn body of a()})) -> [return: bb25, unwind unreachable];
310312
}
311313

312314
bb25: {
315+
nop;
316+
StorageDead(_21);
317+
goto -> bb28;
318+
}
319+
320+
bb26: {
321+
StorageDead(_20);
322+
StorageDead(_19);
323+
drop((((*_39) as variant#3).0: {async fn body of a()})) -> [return: bb27, unwind unreachable];
324+
}
325+
326+
bb27: {
327+
nop;
328+
StorageDead(_4);
329+
StorageDead(_3);
330+
goto -> bb28;
331+
}
332+
333+
bb28: {
334+
goto -> bb34;
335+
}
336+
337+
bb29: {
338+
coroutine_drop;
339+
}
340+
341+
bb30: {
342+
return;
343+
}
344+
345+
bb31: {
346+
goto -> bb36;
347+
}
348+
349+
bb32 (cleanup): {
350+
unreachable;
351+
}
352+
353+
bb33: {
354+
goto -> bb23;
355+
}
356+
357+
bb34: {
358+
goto -> bb29;
359+
}
360+
361+
bb35 (cleanup): {
362+
resume;
363+
}
364+
365+
bb36: {
366+
goto -> bb30;
367+
}
368+
369+
bb37: {
313370
StorageLive(_3);
314371
StorageLive(_4);
315372
StorageLive(_19);
@@ -318,15 +375,19 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
318375
goto -> bb11;
319376
}
320377

321-
bb26: {
378+
bb38: {
322379
StorageLive(_21);
323380
StorageLive(_35);
324381
StorageLive(_36);
325382
_35 = move _2;
326383
goto -> bb21;
327384
}
328385

329-
bb27: {
330-
assert(const false, "`async fn` resumed after completion") -> [success: bb27, unwind unreachable];
386+
bb39: {
387+
assert(const false, "`async fn` resumed after completion") -> [success: bb39, unwind continue];
388+
}
389+
390+
bb40: {
391+
unreachable;
331392
}
332393
}

tests/mir-opt/building/coroutine.main-{closure#0}.StateTransform.after.mir

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
4545
bb0: {
4646
_18 = copy (_1.0: &mut {coroutine@$DIR/coroutine.rs:18:5: 18:18});
4747
_17 = discriminant((*_18));
48-
switchInt(move _17) -> [0: bb1, 1: bb15, 3: bb13, 4: bb14, otherwise: bb16];
48+
switchInt(move _17) -> [0: bb1, 1: bb11, 3: bb9, 4: bb10, otherwise: bb12];
4949
}
5050

5151
bb1: {
@@ -76,11 +76,6 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
7676
}
7777

7878
bb4: {
79-
StorageDead(_4);
80-
drop(_3) -> [return: bb5, unwind unreachable];
81-
}
82-
83-
bb5: {
8479
StorageDead(_3);
8580
StorageLive(_8);
8681
StorageLive(_9);
@@ -91,17 +86,17 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
9186
StorageLive(_12);
9287
StorageLive(_13);
9388
_13 = &(((*_18) as variant#4).0: std::string::String);
94-
_12 = <String as Clone>::clone(move _13) -> [return: bb6, unwind unreachable];
89+
_12 = <String as Clone>::clone(move _13) -> [return: bb5, unwind unreachable];
9590
}
9691

97-
bb6: {
92+
bb5: {
9893
StorageDead(_13);
9994
StorageLive(_14);
10095
StorageLive(_15);
101-
_15 = Location::<'_>::caller() -> [return: bb7, unwind unreachable];
96+
_15 = Location::<'_>::caller() -> [return: bb6, unwind unreachable];
10297
}
10398

104-
bb7: {
99+
bb6: {
105100
_14 = &(*_15);
106101
_9 = (move _10, move _12, move _14);
107102
StorageDead(_14);
@@ -116,54 +111,43 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
116111
return;
117112
}
118113

119-
bb8: {
120-
StorageDead(_9);
121-
drop(_8) -> [return: bb9, unwind unreachable];
122-
}
123-
124-
bb9: {
114+
bb7: {
125115
StorageDead(_15);
126116
StorageDead(_11);
127117
StorageDead(_8);
128118
_16 = const ();
129-
drop((((*_18) as variant#4).0: std::string::String)) -> [return: bb10, unwind unreachable];
119+
drop((((*_18) as variant#4).0: std::string::String)) -> [return: bb8, unwind unreachable];
130120
}
131121

132-
bb10: {
133-
goto -> bb12;
134-
}
135-
136-
bb11: {
122+
bb8: {
137123
_0 = CoroutineState::<(&str, String, &Location<'_>), ()>::Complete(move _16);
138124
discriminant((*_18)) = 1;
139125
return;
140126
}
141127

142-
bb12: {
143-
goto -> bb11;
144-
}
145-
146-
bb13: {
128+
bb9: {
147129
StorageLive(_3);
148130
StorageLive(_4);
149131
_3 = move _2;
150-
goto -> bb4;
132+
StorageDead(_4);
133+
drop(_3) -> [return: bb4, unwind unreachable];
151134
}
152135

153-
bb14: {
136+
bb10: {
154137
StorageLive(_8);
155138
StorageLive(_9);
156139
StorageLive(_11);
157140
StorageLive(_15);
158141
_8 = move _2;
159-
goto -> bb8;
142+
StorageDead(_9);
143+
drop(_8) -> [return: bb7, unwind unreachable];
160144
}
161145

162-
bb15: {
163-
assert(const false, "coroutine resumed after completion") -> [success: bb15, unwind unreachable];
146+
bb11: {
147+
assert(const false, "coroutine resumed after completion") -> [success: bb11, unwind unreachable];
164148
}
165149

166-
bb16: {
150+
bb12: {
167151
unreachable;
168152
}
169153
}

0 commit comments

Comments
 (0)