Skip to content

Commit 6e17782

Browse files
committed
Simplify transformed body.
1 parent 5efb81d commit 6e17782

31 files changed

Lines changed: 2332 additions & 2876 deletions

File tree

compiler/rustc_mir_transform/src/coroutine/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,6 @@ fn create_coroutine_resume_function<'tcx>(
868868
}
869869
}
870870

871-
// Make sure we remove dead blocks to remove
872-
// unrelated code from the drop part of the function
873-
simplify::remove_dead_blocks(body);
874-
875-
pm::run_passes_no_validate(tcx, body, &[&abort_unwinding_calls::AbortUnwindingCalls], None);
876-
877871
// Run derefer to fix Derefs that are not in the first place
878872
deref_finder(tcx, body, false);
879873

@@ -884,6 +878,17 @@ fn create_coroutine_resume_function<'tcx>(
884878
if let Some(dumper) = MirDumper::new(tcx, "coroutine_resume", body) {
885879
dumper.dump_mir(body);
886880
}
881+
882+
pm::run_passes_no_validate(
883+
tcx,
884+
body,
885+
&[
886+
&crate::abort_unwinding_calls::AbortUnwindingCalls,
887+
&crate::simplify::SimplifyCfg::PostStateTransform,
888+
&crate::simplify::SimplifyLocals::PostStateTransform,
889+
],
890+
None,
891+
);
887892
}
888893

889894
/// An operation that can be performed on a coroutine.

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub use shim::build_drop_shim;
7979
macro_rules! declare_passes {
8080
(
8181
$(
82-
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* } )?),+ $(,)?;
82+
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* $(,)? } )?),+ $(,)?;
8383
)*
8484
) => {
8585
$(
@@ -186,12 +186,14 @@ declare_passes! {
186186
PreOptimizations,
187187
Final,
188188
MakeShim,
189-
AfterUnreachableEnumBranching
189+
AfterUnreachableEnumBranching,
190+
PostStateTransform,
190191
},
191192
SimplifyLocals {
192193
BeforeConstProp,
193194
AfterGVN,
194-
Final
195+
Final,
196+
PostStateTransform,
195197
};
196198
mod simplify_branches : SimplifyConstCondition {
197199
AfterInstSimplify,

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/coroutine/async_await.a-{closure#0}.StateTransform.diff

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,25 @@
3030
+ _6 = std::future::ResumeTy(move _7);
3131
+ _5 = copy (_1.0: &mut {async fn body of a()});
3232
+ _4 = discriminant((*_5));
33-
+ switchInt(move _4) -> [0: bb5, 1: bb3, otherwise: bb4];
33+
+ switchInt(move _4) -> [0: bb3, 1: bb1, otherwise: bb2];
3434
}
3535

3636
bb1: {
37-
+ _0 = Poll::<()>::Ready(move _3);
38-
+ discriminant((*_5)) = 1;
39-
return;
37+
- return;
38+
+ assert(const false, "`async fn` resumed after completion") -> [success: bb1, unwind continue];
4039
}
4140

4241
- bb2 (cleanup): {
4342
- resume;
4443
+ bb2: {
45-
+ goto -> bb1;
46-
+ }
47-
+
48-
+ bb3: {
49-
+ assert(const false, "`async fn` resumed after completion") -> [success: bb3, unwind continue];
50-
+ }
51-
+
52-
+ bb4: {
5344
+ unreachable;
5445
+ }
5546
+
56-
+ bb5: {
47+
+ bb3: {
5748
+ _3 = const ();
58-
+ goto -> bb2;
49+
+ _0 = Poll::<()>::Ready(move _3);
50+
+ discriminant((*_5)) = 1;
51+
+ return;
5952
}
6053
}
6154

0 commit comments

Comments
 (0)