forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#121397 - DianQK:early_otherwise_branch_sound,…
… r=<try> Re-enable the early otherwise branch optimization Fixes rust-lang#95162. Fixes rust-lang#119014. Fixes rust-lang#117970. An invalid enum discriminant can come from anywhere. We have to check to see if all successors contain the discriminant statement. It should not be UB that we pass in an invalid enum discriminant when calling a function, this is more like LLVM's poison value. UB only when used. Although miri would consider the following code to be UB. (It's fine for miri.) https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=18602870aaeb07cbdf7dfcd2c28961a2 I extended the scenario with scalars and the same target values. r? compiler
- Loading branch information
Showing
20 changed files
with
1,154 additions
and
311 deletions.
There are no files selected for viewing
451 changes: 243 additions & 208 deletions
451
compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@ compile-flags: -O | ||
|
||
#![crate_type = "lib"] | ||
|
||
pub enum Enum { | ||
A(u32), | ||
B(u32), | ||
C(u32), | ||
} | ||
|
||
#[no_mangle] | ||
pub fn foo(lhs: &Enum, rhs: &Enum) -> bool { | ||
// CHECK-LABEL: define{{.*}}i1 @foo( | ||
// CHECK-NOT: switch | ||
// CHECK-NOT: br | ||
// CHECK: [[SELECT:%.*]] = select | ||
// CHECK-NEXT: ret i1 [[SELECT]] | ||
// CHECK-NEXT: } | ||
match (lhs, rhs) { | ||
(Enum::A(lhs), Enum::A(rhs)) => lhs == rhs, | ||
(Enum::B(lhs), Enum::B(rhs)) => lhs == rhs, | ||
(Enum::C(lhs), Enum::C(rhs)) => lhs == rhs, | ||
_ => false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/mir-opt/early_otherwise_branch.opt10.EarlyOtherwiseBranch.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
- // MIR for `opt10` before EarlyOtherwiseBranch | ||
+ // MIR for `opt10` after EarlyOtherwiseBranch | ||
|
||
fn opt10(_1: E8, _2: E16) -> u32 { | ||
debug x => _1; | ||
debug y => _2; | ||
let mut _0: u32; | ||
let mut _3: (E8, E16); | ||
let mut _4: E8; | ||
let mut _5: E16; | ||
let mut _6: u16; | ||
let mut _7: u16; | ||
let mut _8: u16; | ||
let mut _9: u8; | ||
+ let mut _10: u16; | ||
|
||
bb0: { | ||
StorageLive(_3); | ||
StorageLive(_4); | ||
_4 = move _1; | ||
StorageLive(_5); | ||
_5 = move _2; | ||
_3 = (move _4, move _5); | ||
StorageDead(_5); | ||
StorageDead(_4); | ||
_9 = discriminant((_3.0: E8)); | ||
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9]; | ||
+ StorageLive(_10); | ||
+ _10 = discriminant((_3.1: E16)); | ||
+ switchInt(move _10) -> [0: bb7, otherwise: bb1]; | ||
} | ||
|
||
bb1: { | ||
+ StorageDead(_10); | ||
_0 = const 0_u32; | ||
- goto -> bb8; | ||
+ goto -> bb5; | ||
} | ||
|
||
bb2: { | ||
- _6 = discriminant((_3.1: E16)); | ||
- switchInt(move _6) -> [0: bb5, otherwise: bb1]; | ||
+ _0 = const 1_u32; | ||
+ goto -> bb5; | ||
} | ||
|
||
bb3: { | ||
- _7 = discriminant((_3.1: E16)); | ||
- switchInt(move _7) -> [0: bb6, otherwise: bb1]; | ||
+ _0 = const 2_u32; | ||
+ goto -> bb5; | ||
} | ||
|
||
bb4: { | ||
- _8 = discriminant((_3.1: E16)); | ||
- switchInt(move _8) -> [0: bb7, otherwise: bb1]; | ||
+ _0 = const 3_u32; | ||
+ goto -> bb5; | ||
} | ||
|
||
bb5: { | ||
- _0 = const 1_u32; | ||
- goto -> bb8; | ||
+ StorageDead(_3); | ||
+ return; | ||
} | ||
|
||
bb6: { | ||
- _0 = const 2_u32; | ||
- goto -> bb8; | ||
+ unreachable; | ||
} | ||
|
||
bb7: { | ||
- _0 = const 3_u32; | ||
- goto -> bb8; | ||
- } | ||
- | ||
- bb8: { | ||
- StorageDead(_3); | ||
- return; | ||
- } | ||
- | ||
- bb9: { | ||
- unreachable; | ||
+ StorageDead(_10); | ||
+ switchInt(_9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb6]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.