Skip to content

Commit

Permalink
Auto merge of rust-lang#117783 - tmiasko:inline-ret, r=cjgillot
Browse files Browse the repository at this point in the history
 Fix insertion of statements to be executed along return edge in inlining

Inlining creates additional statements to be executed along the return
edge: an assignment to the destination, storage end for temporaries.

Previously those statements where inserted directly into a call target,
but this is incorrect when the target has other predecessors.

Avoid the issue by creating a new dedicated block for those statements.
When the block happens to be redundant it will be removed by CFG
simplification that follows inlining.

Fixes rust-lang#117355
  • Loading branch information
bors committed Nov 20, 2023
2 parents 4f3da90 + 1acecda commit db9e655
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 674 deletions.
297 changes: 163 additions & 134 deletions compiler/rustc_mir_transform/src/inline.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -69,66 +69,66 @@
+ }
+
+ bb2: {
+ _4 = <() as F>::call() -> [return: bb1, unwind continue];
+ }
+
+ bb3: {
+ StorageDead(_7);
+ StorageDead(_6);
+ StorageDead(_5);
+ _3 = <() as F>::call() -> [return: bb3, unwind continue];
+ _3 = <() as F>::call() -> [return: bb2, unwind continue];
+ }
+
+ bb3: {
+ _4 = <() as F>::call() -> [return: bb1, unwind continue];
+ bb4: {
+ _7 = <() as E>::call() -> [return: bb3, unwind continue];
+ }
+
+ bb4: {
+ bb5: {
+ StorageDead(_10);
+ StorageDead(_9);
+ StorageDead(_8);
+ _6 = <() as E>::call() -> [return: bb5, unwind continue];
+ _6 = <() as E>::call() -> [return: bb4, unwind continue];
+ }
+
+ bb5: {
+ _7 = <() as E>::call() -> [return: bb2, unwind continue];
+ bb6: {
+ _10 = <() as D>::call() -> [return: bb5, unwind continue];
+ }
+
+ bb6: {
+ bb7: {
+ StorageDead(_13);
+ StorageDead(_12);
+ StorageDead(_11);
+ _9 = <() as D>::call() -> [return: bb7, unwind continue];
+ _9 = <() as D>::call() -> [return: bb6, unwind continue];
+ }
+
+ bb7: {
+ _10 = <() as D>::call() -> [return: bb4, unwind continue];
+ bb8: {
+ _13 = <() as C>::call() -> [return: bb7, unwind continue];
+ }
+
+ bb8: {
+ bb9: {
+ StorageDead(_16);
+ StorageDead(_15);
+ StorageDead(_14);
+ _12 = <() as C>::call() -> [return: bb9, unwind continue];
+ _12 = <() as C>::call() -> [return: bb8, unwind continue];
+ }
+
+ bb9: {
+ _13 = <() as C>::call() -> [return: bb6, unwind continue];
+ bb10: {
+ _16 = <() as B>::call() -> [return: bb9, unwind continue];
+ }
+
+ bb10: {
+ bb11: {
+ StorageDead(_19);
+ StorageDead(_18);
+ StorageDead(_17);
+ _15 = <() as B>::call() -> [return: bb11, unwind continue];
+ }
+
+ bb11: {
+ _16 = <() as B>::call() -> [return: bb8, unwind continue];
+ _15 = <() as B>::call() -> [return: bb10, unwind continue];
+ }
+
+ bb12: {
+ _18 = <() as A>::call() -> [return: bb13, unwind continue];
+ }
+
+ bb13: {
+ _19 = <() as A>::call() -> [return: bb10, unwind continue];
+ _19 = <() as A>::call() -> [return: bb11, unwind continue];
}
}

42 changes: 42 additions & 0 deletions tests/mir-opt/inline/indirect_destination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Test for inlining with an indirect destination place.
//
// unit-test: Inline
// edition: 2021
// needs-unwind
#![crate_type = "lib"]
#![feature(custom_mir, core_intrinsics)]
use core::intrinsics::mir::*;

#[custom_mir(dialect = "runtime", phase = "initial")]
// CHECK-LABEL: fn f(
// CHECK: bb1: {
// CHECK-NEXT: StorageLive([[A:.*]]);
// CHECK-NEXT: [[A]] = &mut (*_1);
// CHECK-NEXT: StorageLive([[B:.*]]);
// CHECK-NEXT: [[B]] = const 42_u8;
// CHECK-NEXT: (*[[A]]) = move [[B]];
// CHECK-NEXT: StorageDead([[B]]);
// CHECK-NEXT: StorageDead([[A]]);
// CHECK-NEXT: goto -> bb1;
// CHECK-NEXT: }
pub fn f(a: *mut u8) {
mir! {
{
Goto(bb1)
}
bb1 = {
Call(*a = g(), bb1, UnwindUnreachable())
}
}
}

#[custom_mir(dialect = "runtime", phase = "initial")]
#[inline(always)]
fn g() -> u8 {
mir! {
{
RET = 42;
Return()
}
}
}
74 changes: 38 additions & 36 deletions tests/mir-opt/inline/inline_coroutine.main.Inline.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,10 @@
StorageLive(_3);
StorageLive(_4);
- _4 = g() -> [return: bb1, unwind continue];
- }
-
- bb1: {
+ _4 = {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8 (#0)};
_3 = &mut _4;
- _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}>::new(move _3) -> [return: bb2, unwind: bb5];
- }
-
- bb2: {
+ _3 = &mut _4;
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}> { pointer: move _3 };
StorageDead(_3);
- _1 = <{coroutine@$DIR/inline_coroutine.rs:19:5: 19:8} as Coroutine<bool>>::resume(move _2, const false) -> [return: bb3, unwind: bb5];
+ StorageDead(_3);
+ StorageLive(_5);
+ _5 = const false;
+ StorageLive(_6);
Expand All @@ -55,40 +47,50 @@
+ switchInt(move _7) -> [0: bb5, 1: bb9, 3: bb10, otherwise: bb11];
}

bb1: {
- _3 = &mut _4;
- _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:19:5: 19:8}>::new(move _3) -> [return: bb2, unwind: bb5];
+ StorageDead(_4);
+ _0 = const ();
+ StorageDead(_1);
+ return;
}

- bb2: {
- StorageDead(_3);
- _1 = <{coroutine@$DIR/inline_coroutine.rs:19:5: 19:8} as Coroutine<bool>>::resume(move _2, const false) -> [return: bb3, unwind: bb5];
+ bb2 (cleanup): {
+ drop(_4) -> [return: bb3, unwind terminate(cleanup)];
}

- bb3: {
+ bb1: {
+ StorageDead(_7);
+ StorageDead(_6);
+ StorageDead(_5);
StorageDead(_2);
- StorageDead(_2);
- drop(_4) -> [return: bb4, unwind: bb6];
+ drop(_4) -> [return: bb2, unwind: bb4];
+ bb3 (cleanup): {
+ resume;
}

- bb4: {
+ bb2: {
StorageDead(_4);
_0 = const ();
StorageDead(_1);
return;
bb4: {
- StorageDead(_4);
- _0 = const ();
- StorageDead(_1);
- return;
+ StorageDead(_7);
+ StorageDead(_6);
+ StorageDead(_5);
+ StorageDead(_2);
+ drop(_4) -> [return: bb1, unwind: bb3];
}

- bb5 (cleanup): {
- drop(_4) -> [return: bb6, unwind terminate(cleanup)];
+ bb3 (cleanup): {
+ drop(_4) -> [return: bb4, unwind terminate(cleanup)];
}

- bb6 (cleanup): {
+ bb4 (cleanup): {
resume;
+ }
+
+ bb5: {
+ StorageLive(_8);
+ switchInt(_5) -> [0: bb6, otherwise: bb7];
+ }
+
}

- bb6 (cleanup): {
- resume;
+ bb6: {
+ _8 = const 13_i32;
+ goto -> bb8;
Expand All @@ -102,19 +104,19 @@
+ bb8: {
+ _1 = CoroutineState::<i32, bool>::Yielded(move _8);
+ discriminant((*_6)) = 3;
+ goto -> bb1;
+ goto -> bb4;
+ }
+
+ bb9: {
+ assert(const false, "coroutine resumed after completion") -> [success: bb9, unwind: bb3];
+ assert(const false, "coroutine resumed after completion") -> [success: bb9, unwind: bb2];
+ }
+
+ bb10: {
+ StorageLive(_8);
+ StorageDead(_8);
+ _1 = CoroutineState::<i32, bool>::Complete(_5);
+ discriminant((*_6)) = 1;
+ goto -> bb1;
+ goto -> bb4;
+ }
+
+ bb11: {
Expand Down
Loading

0 comments on commit db9e655

Please sign in to comment.