Skip to content

Commit 4b67da2

Browse files
committed
Move state transform after optimizations.
1 parent 2272726 commit 4b67da2

21 files changed

+252
-362
lines changed

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const SELF_ARG: Local = Local::from_u32(1);
174174
const CTX_ARG: Local = Local::from_u32(2);
175175

176176
/// A `yield` point in the coroutine.
177+
#[derive(Debug)]
177178
struct SuspensionPoint<'tcx> {
178179
/// State discriminant used when suspending or resuming at this point.
179180
state: usize,
@@ -651,7 +652,7 @@ fn replace_resume_ty_local<'tcx>(
651652
// We have to replace the `ResumeTy` that is used for type and borrow checking
652653
// with `&mut Context<'_>` in MIR.
653654
#[cfg(debug_assertions)]
654-
{
655+
if local_ty != context_mut_ref {
655656
if let ty::Adt(resume_ty_adt, _) = local_ty.kind() {
656657
let expected_adt = tcx.adt_def(tcx.require_lang_item(LangItem::ResumeTy, body.span));
657658
assert_eq!(*resume_ty_adt, expected_adt);

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
627627
&add_retag::AddRetag,
628628
&erase_deref_temps::EraseDerefTemps,
629629
&elaborate_box_derefs::ElaborateBoxDerefs,
630-
&coroutine::StateTransform,
631630
&Lint(known_panics_lint::KnownPanicsLint),
632631
];
633632
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
@@ -742,6 +741,7 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
742741
&simplify::SimplifyLocals::Final,
743742
&multiple_return_terminators::MultipleReturnTerminators,
744743
&large_enums::EnumSizeOpt { discrepancy: 128 },
744+
&coroutine::StateTransform,
745745
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
746746
&add_call_guards::CriticalCallEdges,
747747
// Cleanup for human readability, off by default.

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
150150
tcx,
151151
&mut body,
152152
&[
153-
&mentioned_items::MentionedItems,
154153
&abort_unwinding_calls::AbortUnwindingCalls,
155154
&add_call_guards::CriticalCallEdges,
156155
],

compiler/rustc_mir_transform/src/shim/async_destructor_ctor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ fn build_adrop_for_coroutine_shim<'tcx>(
216216
body.source.instance = instance;
217217
body.phase = MirPhase::Runtime(RuntimePhase::Initial);
218218
body.var_debug_info.clear();
219+
body.mentioned_items = None;
219220
let pin_adt_ref = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, span));
220221
let args = tcx.mk_args(&[proxy_ref.into()]);
221222
let pin_proxy_ref = Ty::new_adt(tcx, pin_adt_ref, args);

tests/mir-opt/async_drop_live_dead.a-{closure#0}.coroutine_drop_async.0.panic-abort.mir

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,72 +29,68 @@ fn a::{closure#0}(_1: Pin<&mut {async fn body of a<T>()}>, _2: &mut Context<'_>)
2929
bb0: {
3030
_20 = copy (_1.0: &mut {async fn body of a<T>()});
3131
_19 = discriminant((*_20));
32-
switchInt(move _19) -> [0: bb9, 3: bb12, 4: bb13, otherwise: bb14];
32+
switchInt(move _19) -> [0: bb8, 3: bb11, 4: bb12, otherwise: bb13];
3333
}
3434

3535
bb1: {
3636
nop;
3737
nop;
38-
goto -> bb2;
39-
}
40-
41-
bb2: {
4238
_0 = Poll::<()>::Ready(const ());
4339
return;
4440
}
4541

46-
bb3: {
42+
bb2: {
4743
_0 = Poll::<()>::Pending;
4844
discriminant((*_20)) = 4;
4945
return;
5046
}
5147

52-
bb4: {
48+
bb3: {
5349
StorageLive(_16);
5450
_15 = &mut (((*_20) as variant#4).1: impl std::future::Future<Output = ()>);
55-
_16 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _15) -> [return: bb7, unwind unreachable];
51+
_16 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _15) -> [return: bb6, unwind unreachable];
5652
}
5753

58-
bb5: {
54+
bb4: {
5955
unreachable;
6056
}
6157

62-
bb6: {
58+
bb5: {
6359
StorageDead(_16);
6460
_17 = discriminant(_9);
65-
switchInt(move _17) -> [0: bb1, 1: bb3, otherwise: bb5];
61+
switchInt(move _17) -> [0: bb1, 1: bb2, otherwise: bb4];
6662
}
6763

68-
bb7: {
69-
_9 = <impl Future<Output = ()> as Future>::poll(move _16, move _14) -> [return: bb6, unwind unreachable];
64+
bb6: {
65+
_9 = <impl Future<Output = ()> as Future>::poll(move _16, move _14) -> [return: bb5, unwind unreachable];
7066
}
7167

72-
bb8: {
68+
bb7: {
7369
_0 = Poll::<()>::Ready(const ());
7470
return;
7571
}
7672

73+
bb8: {
74+
goto -> bb10;
75+
}
76+
7777
bb9: {
78-
goto -> bb11;
78+
goto -> bb7;
7979
}
8080

8181
bb10: {
82-
goto -> bb8;
82+
drop(((*_20).0: T)) -> [return: bb9, unwind unreachable];
8383
}
8484

8585
bb11: {
86-
drop(((*_20).0: T)) -> [return: bb10, unwind unreachable];
86+
goto -> bb3;
8787
}
8888

8989
bb12: {
90-
goto -> bb4;
90+
goto -> bb3;
9191
}
9292

9393
bb13: {
94-
goto -> bb4;
95-
}
96-
97-
bb14: {
9894
_0 = Poll::<()>::Ready(const ());
9995
return;
10096
}

tests/mir-opt/async_drop_live_dead.a-{closure#0}.coroutine_drop_async.0.panic-unwind.mir

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,95 +29,81 @@ fn a::{closure#0}(_1: Pin<&mut {async fn body of a<T>()}>, _2: &mut Context<'_>)
2929
bb0: {
3030
_20 = copy (_1.0: &mut {async fn body of a<T>()});
3131
_19 = discriminant((*_20));
32-
switchInt(move _19) -> [0: bb12, 2: bb18, 3: bb16, 4: bb17, otherwise: bb19];
32+
switchInt(move _19) -> [0: bb8, 2: bb15, 3: bb13, 4: bb14, otherwise: bb16];
3333
}
3434

3535
bb1: {
3636
nop;
3737
nop;
38-
goto -> bb2;
39-
}
40-
41-
bb2: {
4238
_0 = Poll::<()>::Ready(const ());
4339
return;
4440
}
4541

46-
bb3 (cleanup): {
47-
nop;
48-
nop;
49-
goto -> bb5;
50-
}
51-
52-
bb4 (cleanup): {
53-
goto -> bb15;
54-
}
55-
56-
bb5 (cleanup): {
57-
goto -> bb4;
58-
}
59-
60-
bb6: {
42+
bb2: {
6143
_0 = Poll::<()>::Pending;
6244
discriminant((*_20)) = 4;
6345
return;
6446
}
6547

66-
bb7: {
48+
bb3: {
6749
StorageLive(_16);
6850
_15 = &mut (((*_20) as variant#4).1: impl std::future::Future<Output = ()>);
69-
_16 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _15) -> [return: bb10, unwind: bb15];
51+
_16 = Pin::<&mut impl Future<Output = ()>>::new_unchecked(move _15) -> [return: bb6, unwind: bb12];
7052
}
7153

72-
bb8: {
54+
bb4: {
7355
unreachable;
7456
}
7557

76-
bb9: {
58+
bb5: {
7759
StorageDead(_16);
7860
_17 = discriminant(_9);
79-
switchInt(move _17) -> [0: bb1, 1: bb6, otherwise: bb8];
61+
switchInt(move _17) -> [0: bb1, 1: bb2, otherwise: bb4];
8062
}
8163

82-
bb10: {
83-
_9 = <impl Future<Output = ()> as Future>::poll(move _16, move _14) -> [return: bb9, unwind: bb3];
64+
bb6: {
65+
_9 = <impl Future<Output = ()> as Future>::poll(move _16, move _14) -> [return: bb5, unwind: bb12];
8466
}
8567

86-
bb11: {
68+
bb7: {
8769
_0 = Poll::<()>::Ready(const ());
8870
return;
8971
}
9072

91-
bb12: {
92-
goto -> bb14;
73+
bb8: {
74+
goto -> bb11;
9375
}
9476

95-
bb13: {
96-
goto -> bb11;
77+
bb9 (cleanup): {
78+
goto -> bb12;
9779
}
9880

99-
bb14: {
100-
drop(((*_20).0: T)) -> [return: bb13, unwind: bb4];
81+
bb10: {
82+
goto -> bb7;
10183
}
10284

103-
bb15 (cleanup): {
85+
bb11: {
86+
drop(((*_20).0: T)) -> [return: bb10, unwind: bb9];
87+
}
88+
89+
bb12 (cleanup): {
10490
discriminant((*_20)) = 2;
10591
resume;
10692
}
10793

108-
bb16: {
109-
goto -> bb7;
94+
bb13: {
95+
goto -> bb3;
11096
}
11197

112-
bb17: {
113-
goto -> bb7;
98+
bb14: {
99+
goto -> bb3;
114100
}
115101

116-
bb18: {
117-
assert(const false, "`async fn` resumed after panicking") -> [success: bb18, unwind continue];
102+
bb15: {
103+
assert(const false, "`async fn` resumed after panicking") -> [success: bb15, unwind continue];
118104
}
119105

120-
bb19: {
106+
bb16: {
121107
_0 = Poll::<()>::Ready(const ());
122108
return;
123109
}

0 commit comments

Comments
 (0)