Skip to content

Commit 90181a4

Browse files
committed
Simplify transformed body.
1 parent 42359ce commit 90181a4

8 files changed

+192
-92
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,16 +1305,6 @@ fn create_coroutine_resume_function<'tcx>(
13051305
// unlike for all other coroutine kinds.
13061306
CoroutineKind::Desugared(CoroutineDesugaring::Gen, _) => {}
13071307
}
1308-
1309-
// Make sure we remove dead blocks to remove
1310-
// unrelated code from the drop part of the function
1311-
simplify::remove_dead_blocks(body);
1312-
1313-
pm::run_passes_no_validate(tcx, body, &[&abort_unwinding_calls::AbortUnwindingCalls], None);
1314-
1315-
if let Some(dumper) = MirDumper::new(tcx, "coroutine_resume", body) {
1316-
dumper.dump_mir(body);
1317-
}
13181308
}
13191309

13201310
/// An operation that can be performed on a coroutine.
@@ -1670,6 +1660,21 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
16701660
// Create the Coroutine::resume / Future::poll function
16711661
create_coroutine_resume_function(tcx, transform, body, can_return, can_unwind);
16721662

1663+
if let Some(dumper) = MirDumper::new(tcx, "coroutine_resume", body) {
1664+
dumper.dump_mir(body);
1665+
}
1666+
1667+
pm::run_passes_no_validate(
1668+
tcx,
1669+
body,
1670+
&[
1671+
&crate::abort_unwinding_calls::AbortUnwindingCalls,
1672+
&crate::simplify::SimplifyCfg::PostStateTransform,
1673+
&crate::simplify::SimplifyLocals::PostStateTransform,
1674+
],
1675+
None,
1676+
);
1677+
16731678
// Run derefer to fix Derefs that are not in the first place
16741679
deref_finder(tcx, body);
16751680
}

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
$(
@@ -180,12 +180,14 @@ declare_passes! {
180180
PreOptimizations,
181181
Final,
182182
MakeShim,
183-
AfterUnreachableEnumBranching
183+
AfterUnreachableEnumBranching,
184+
PostStateTransform,
184185
},
185186
SimplifyLocals {
186187
BeforeConstProp,
187188
AfterGVN,
188-
Final
189+
Final,
190+
PostStateTransform,
189191
};
190192
mod simplify_branches : SimplifyConstCondition {
191193
AfterConstProp,

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub(super) enum SimplifyCfg {
5555
Final,
5656
MakeShim,
5757
AfterUnreachableEnumBranching,
58+
/// Extra run introduced by `StateTransform`.
59+
PostStateTransform,
5860
}
5961

6062
impl SimplifyCfg {
@@ -70,6 +72,7 @@ impl SimplifyCfg {
7072
SimplifyCfg::AfterUnreachableEnumBranching => {
7173
"SimplifyCfg-after-unreachable-enum-branching"
7274
}
75+
SimplifyCfg::PostStateTransform => "SimplifyCfg-post-StateTransform",
7376
}
7477
}
7578
}
@@ -390,6 +393,8 @@ pub(super) enum SimplifyLocals {
390393
BeforeConstProp,
391394
AfterGVN,
392395
Final,
396+
/// Extra run introduced by `StateTransform`.
397+
PostStateTransform,
393398
}
394399

395400
impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
@@ -398,6 +403,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
398403
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
399404
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
400405
SimplifyLocals::Final => "SimplifyLocals-final",
406+
SimplifyLocals::PostStateTransform => "SimplifyLocals-post-StateTransform",
401407
}
402408
}
403409

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
@@ -17,12 +17,12 @@ fn a::{closure#0}(_1: Pin<&mut {async fn body of a()}>, _2: &mut Context<'_>) ->
1717

1818
bb0: {
1919
_4 = discriminant((*(_1.0: &mut {async fn body of a()})));
20-
switchInt(move _4) -> [0: bb1, 1: bb4, otherwise: bb5];
20+
switchInt(move _4) -> [0: bb1, 1: bb9, otherwise: bb10];
2121
}
2222

2323
bb1: {
2424
_3 = const ();
25-
goto -> bb3;
25+
goto -> bb6;
2626
}
2727

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

3434
bb3: {
35-
goto -> bb2;
35+
return;
3636
}
3737

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

42-
bb5: {
62+
bb10: {
4363
unreachable;
4464
}
4565
}

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
@@ -103,7 +103,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
103103

104104
bb0: {
105105
_38 = discriminant((*(_1.0: &mut {async fn body of b()})));
106-
switchInt(move _38) -> [0: bb1, 1: bb27, 3: bb25, 4: bb26, otherwise: bb8];
106+
switchInt(move _38) -> [0: bb1, 1: bb39, 3: bb37, 4: bb38, otherwise: bb40];
107107
}
108108

109109
bb1: {
@@ -294,7 +294,7 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
294294
bb22: {
295295
nop;
296296
StorageDead(_21);
297-
goto -> bb24;
297+
goto -> bb33;
298298
}
299299

300300
bb23: {
@@ -304,10 +304,67 @@ fn b::{closure#0}(_1: Pin<&mut {async fn body of b()}>, _2: &mut Context<'_>) ->
304304
}
305305

306306
bb24: {
307-
goto -> bb23;
307+
StorageDead(_36);
308+
StorageDead(_35);
309+
drop((((*(_1.0: &mut {async fn body of b()})) as variant#4).0: {async fn body of a()})) -> [return: bb25, unwind unreachable];
308310
}
309311

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

319-
bb26: {
376+
bb38: {
320377
StorageLive(_21);
321378
StorageLive(_35);
322379
StorageLive(_36);
323380
_35 = move _2;
324381
goto -> bb21;
325382
}
326383

327-
bb27: {
328-
assert(const false, "`async fn` resumed after completion") -> [success: bb27, unwind unreachable];
384+
bb39: {
385+
assert(const false, "`async fn` resumed after completion") -> [success: bb39, unwind continue];
386+
}
387+
388+
bb40: {
389+
unreachable;
329390
}
330391
}

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
@@ -52,7 +52,7 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
5252
bb0: {
5353
_18 = deref_copy (_1.0: &mut {coroutine@$DIR/coroutine.rs:18:5: 18:18});
5454
_17 = discriminant((*_18));
55-
switchInt(move _17) -> [0: bb1, 1: bb15, 3: bb13, 4: bb14, otherwise: bb16];
55+
switchInt(move _17) -> [0: bb1, 1: bb11, 3: bb9, 4: bb10, otherwise: bb12];
5656
}
5757

5858
bb1: {
@@ -86,11 +86,6 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
8686
}
8787

8888
bb4: {
89-
StorageDead(_4);
90-
drop(_3) -> [return: bb5, unwind unreachable];
91-
}
92-
93-
bb5: {
9489
StorageDead(_3);
9590
StorageLive(_8);
9691
StorageLive(_9);
@@ -102,17 +97,17 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
10297
StorageLive(_13);
10398
_22 = deref_copy (_1.0: &mut {coroutine@$DIR/coroutine.rs:18:5: 18:18});
10499
_13 = &(((*_22) as variant#4).0: std::string::String);
105-
_12 = <String as Clone>::clone(move _13) -> [return: bb6, unwind unreachable];
100+
_12 = <String as Clone>::clone(move _13) -> [return: bb5, unwind unreachable];
106101
}
107102

108-
bb6: {
103+
bb5: {
109104
StorageDead(_13);
110105
StorageLive(_14);
111106
StorageLive(_15);
112-
_15 = Location::<'_>::caller() -> [return: bb7, unwind unreachable];
107+
_15 = Location::<'_>::caller() -> [return: bb6, unwind unreachable];
113108
}
114109

115-
bb7: {
110+
bb6: {
116111
_14 = &(*_15);
117112
_9 = (move _10, move _12, move _14);
118113
StorageDead(_14);
@@ -128,56 +123,45 @@ fn main::{closure#0}(_1: Pin<&mut {coroutine@$DIR/coroutine.rs:18:5: 18:18}>, _2
128123
return;
129124
}
130125

131-
bb8: {
132-
StorageDead(_9);
133-
drop(_8) -> [return: bb9, unwind unreachable];
134-
}
135-
136-
bb9: {
126+
bb7: {
137127
StorageDead(_15);
138128
StorageDead(_11);
139129
StorageDead(_8);
140130
_16 = const ();
141131
_24 = deref_copy (_1.0: &mut {coroutine@$DIR/coroutine.rs:18:5: 18:18});
142-
drop((((*_24) as variant#4).0: std::string::String)) -> [return: bb10, unwind unreachable];
132+
drop((((*_24) as variant#4).0: std::string::String)) -> [return: bb8, unwind unreachable];
143133
}
144134

145-
bb10: {
146-
goto -> bb12;
147-
}
148-
149-
bb11: {
135+
bb8: {
150136
_0 = CoroutineState::<(&str, String, &Location<'_>), ()>::Complete(move _16);
151137
_25 = deref_copy (_1.0: &mut {coroutine@$DIR/coroutine.rs:18:5: 18:18});
152138
discriminant((*_25)) = 1;
153139
return;
154140
}
155141

156-
bb12: {
157-
goto -> bb11;
158-
}
159-
160-
bb13: {
142+
bb9: {
161143
StorageLive(_3);
162144
StorageLive(_4);
163145
_3 = move _2;
164-
goto -> bb4;
146+
StorageDead(_4);
147+
drop(_3) -> [return: bb4, unwind unreachable];
165148
}
166149

167-
bb14: {
150+
bb10: {
168151
StorageLive(_8);
169152
StorageLive(_9);
170153
StorageLive(_11);
171154
StorageLive(_15);
172155
_8 = move _2;
173-
goto -> bb8;
156+
StorageDead(_9);
157+
drop(_8) -> [return: bb7, unwind unreachable];
174158
}
175159

176-
bb15: {
177-
assert(const false, "coroutine resumed after completion") -> [success: bb15, unwind unreachable];
160+
bb11: {
161+
assert(const false, "coroutine resumed after completion") -> [success: bb11, unwind unreachable];
178162
}
179163

180-
bb16: {
164+
bb12: {
181165
unreachable;
182166
}
183167
}

0 commit comments

Comments
 (0)