Skip to content

Commit

Permalink
mir-opt: Do not handle the cleanup BB in the EarlyOtherwiseBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Dec 18, 2024
1 parent 93aea1d commit d08738c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 86 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ fn evaluate_candidate<'tcx>(
parent: BasicBlock,
) -> Option<OptimizationData<'tcx>> {
let bbs = &body.basic_blocks;
// NB: If this BB is a cleanup, we may need to figure out what else needs to be handled.
if bbs[parent].is_cleanup {
return None;
}
let TerminatorKind::SwitchInt { targets, discr: parent_discr } = &bbs[parent].terminator().kind
else {
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
let mut _7: bool;
let mut _8: bool;
let mut _9: isize;
+ let mut _10: bool;
scope 1 {
debug _trailers => _5;
}
Expand Down Expand Up @@ -45,41 +44,35 @@

bb4: {
_0 = const ();
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb5: {
_0 = const ();
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb6: {
StorageLive(_6);
_6 = copy ((((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>) as Err).0: u8);
_0 = const ();
StorageDead(_6);
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb7: {
StorageLive(_5);
_5 = move ((((((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>) as Ok).0: std::option::Option<std::vec::Vec<u8>>) as Some).0: std::vec::Vec<u8>);
_0 = const ();
- drop(_5) -> [return: bb8, unwind: bb20];
+ drop(_5) -> [return: bb8, unwind: bb16];
drop(_5) -> [return: bb8, unwind: bb20];
}

bb8: {
StorageDead(_5);
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb9 (cleanup): {
+ StorageDead(_10);
resume;
}

Expand All @@ -88,8 +81,7 @@
}

bb11: {
- switchInt(copy _7) -> [0: bb12, otherwise: bb16];
+ switchInt(copy _7) -> [0: bb12, otherwise: bb14];
switchInt(copy _7) -> [0: bb12, otherwise: bb16];
}

bb12: {
Expand All @@ -98,45 +90,37 @@
}

bb13: {
- switchInt(copy _8) -> [0: bb14, otherwise: bb15];
- }
-
- bb14: {
switchInt(copy _8) -> [0: bb14, otherwise: bb15];
}
bb14: {
_8 = const false;
goto -> bb12;
}

- bb15: {
- goto -> bb14;
- }
-
- bb16: {
+ bb14: {
bb15: {
goto -> bb14;
}

bb16: {
_9 = discriminant(((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>));
switchInt(move _9) -> [0: bb13, otherwise: bb12];
}

- bb17: {
+ bb15: {
bb17: {
switchInt(copy _4) -> [0: bb11, otherwise: bb10];
}

- bb18 (cleanup): {
- switchInt(copy _3) -> [0: bb19, otherwise: bb9];
+ bb16 (cleanup): {
+ StorageLive(_10);
+ _10 = Ne(copy _4, copy _3);
+ switchInt(move _10) -> [0: bb17, otherwise: bb9];
bb18 (cleanup): {
switchInt(copy _3) -> [0: bb19, otherwise: bb9];
}

- bb19 (cleanup): {
+ bb17 (cleanup): {
+ StorageDead(_10);
bb19 (cleanup): {
goto -> bb9;
- }
-
- bb20 (cleanup): {
- switchInt(copy _4) -> [0: bb18, otherwise: bb9];
}
bb20 (cleanup): {
switchInt(copy _4) -> [0: bb18, otherwise: bb9];
}
}

11 changes: 3 additions & 8 deletions tests/mir-opt/early_otherwise_branch_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use std::task::Poll;

// We find a matching pattern in the unwind path,
// and we need to create a cleanup BB for this case to meet the unwind invariants rule.
// NB: This transform is not happening currently.

// EMIT_MIR early_otherwise_branch_unwind.unwind.EarlyOtherwiseBranch.diff
fn unwind<T>(val: Option<Option<Option<T>>>) {
// CHECK-LABEL: fn unwind(
// CHECK: drop({{.*}}) -> [return: bb{{.*}}, unwind: [[PARENT_UNWIND_BB:bb.*]]];
// CHECK: [[PARENT_UNWIND_BB]] (cleanup): {
// CHECK-NEXT: StorageLive
// CHECK-NEXT: [[CMP_LOCAL:_.*]] = Ne
// CHECK-NEXT: switchInt(move [[CMP_LOCAL]]) -> [0: [[NEW_UNWIND_BB:bb.*]], otherwise
// CHECK: [[NEW_UNWIND_BB]] (cleanup): {
// CHECK-NEXT: switchInt
match val {
Some(Some(Some(_v))) => {}
Some(Some(None)) => {}
Expand All @@ -30,10 +28,7 @@ pub fn poll(val: Poll<Result<Option<Vec<u8>>, u8>>) {
// CHECK-LABEL: fn poll(
// CHECK: drop({{.*}}) -> [return: bb{{.*}}, unwind: [[PARENT_UNWIND_BB:bb.*]]];
// CHECK: [[PARENT_UNWIND_BB]] (cleanup): {
// CHECK-NEXT: StorageLive
// CHECK-NEXT: [[CMP_LOCAL:_.*]] = Ne
// CHECK-NEXT: switchInt(move [[CMP_LOCAL]]) -> [0: [[NEW_UNWIND_BB:bb.*]], otherwise
// CHECK: [[NEW_UNWIND_BB]] (cleanup): {
// CHECK-NEXT: switchInt
match val {
Poll::Ready(Ok(Some(_trailers))) => {}
Poll::Ready(Err(_err)) => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
let mut _6: bool;
let mut _7: bool;
let mut _8: isize;
+ let mut _9: bool;
scope 1 {
debug _v => _5;
}
Expand Down Expand Up @@ -41,38 +40,32 @@

bb4: {
_0 = const ();
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb5: {
_0 = const ();
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb6: {
_0 = const ();
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb7: {
StorageLive(_5);
_5 = move ((((((_1 as Some).0: std::option::Option<std::option::Option<T>>) as Some).0: std::option::Option<T>) as Some).0: T);
_0 = const ();
- drop(_5) -> [return: bb8, unwind: bb20];
+ drop(_5) -> [return: bb8, unwind: bb16];
drop(_5) -> [return: bb8, unwind: bb20];
}

bb8: {
StorageDead(_5);
- goto -> bb17;
+ goto -> bb15;
goto -> bb17;
}

bb9 (cleanup): {
+ StorageDead(_9);
resume;
}

Expand All @@ -81,8 +74,7 @@
}

bb11: {
- switchInt(copy _6) -> [0: bb12, otherwise: bb16];
+ switchInt(copy _6) -> [0: bb12, otherwise: bb14];
switchInt(copy _6) -> [0: bb12, otherwise: bb16];
}

bb12: {
Expand All @@ -91,45 +83,37 @@
}

bb13: {
- switchInt(copy _7) -> [0: bb14, otherwise: bb15];
- }
-
- bb14: {
switchInt(copy _7) -> [0: bb14, otherwise: bb15];
}
bb14: {
_7 = const false;
goto -> bb12;
}

- bb15: {
- goto -> bb14;
- }
-
- bb16: {
+ bb14: {
bb15: {
goto -> bb14;
}

bb16: {
_8 = discriminant(((_1 as Some).0: std::option::Option<std::option::Option<T>>));
switchInt(move _8) -> [1: bb13, otherwise: bb12];
}

- bb17: {
+ bb15: {
bb17: {
switchInt(copy _4) -> [1: bb11, otherwise: bb10];
}

- bb18 (cleanup): {
- switchInt(copy _3) -> [1: bb19, otherwise: bb9];
+ bb16 (cleanup): {
+ StorageLive(_9);
+ _9 = Ne(copy _4, copy _3);
+ switchInt(move _9) -> [0: bb17, otherwise: bb9];
bb18 (cleanup): {
switchInt(copy _3) -> [1: bb19, otherwise: bb9];
}

- bb19 (cleanup): {
+ bb17 (cleanup): {
+ StorageDead(_9);
bb19 (cleanup): {
goto -> bb9;
- }
-
- bb20 (cleanup): {
- switchInt(copy _4) -> [1: bb18, otherwise: bb9];
}
bb20 (cleanup): {
switchInt(copy _4) -> [1: bb18, otherwise: bb9];
}
}

0 comments on commit d08738c

Please sign in to comment.