-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 #122225 - DianQK:nits-120268, r=cjgillot
Rename `UninhabitedEnumBranching` to `UnreachableEnumBranching` Per [#120268](#120268 (comment)), I rename `UninhabitedEnumBranching` to `UnreachableEnumBranching` . I solved some nits to add some comments. I adjusted the workaround restrictions. This should be useful for `a <= b` and `if let Some/Ok(v)`. For enum with few variants, `early-tailduplication` should not cause compile time overhead. r? RalfJung
- Loading branch information
Showing
33 changed files
with
256 additions
and
166 deletions.
There are no files selected for viewing
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
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 was deleted.
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,43 @@ | ||
//@ compile-flags: -O | ||
|
||
#![crate_type = "lib"] | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Int(u32); | ||
|
||
const A: Int = Int(201); | ||
const B: Int = Int(270); | ||
const C: Int = Int(153); | ||
|
||
// The code is from https://github.com/rust-lang/rust/issues/119520. | ||
// This code will basically turn into `matches!(x.partial_cmp(&A), Some(Greater | Equal))`. | ||
// The otherwise branch must be `Less`. | ||
// CHECK-LABEL: @implicit_match( | ||
// CHECK-SAME: [[TMP0:%.*]]) | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: [[TMP1:%.*]] = add i32 [[TMP0]], -201 | ||
// CHECK-NEXT: icmp ult i32 [[TMP1]], 70 | ||
// CHECK-NEXT: icmp eq i32 [[TMP0]], 153 | ||
// CHECK-NEXT: [[SPEC_SELECT:%.*]] = or i1 | ||
// CHECK-NEXT: ret i1 [[SPEC_SELECT]] | ||
#[no_mangle] | ||
pub fn implicit_match(x: Int) -> bool { | ||
(x >= A && x <= B) | ||
|| x == C | ||
} | ||
|
||
// The code is from https://github.com/rust-lang/rust/issues/110097. | ||
// We expect it to generate the same optimized code as a full match. | ||
// CHECK-LABEL: @if_let( | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: insertvalue | ||
// CHECK-NEXT: insertvalue | ||
// CHECK-NEXT: ret | ||
#[no_mangle] | ||
pub fn if_let(val: Result<i32, ()>) -> Result<i32, ()> { | ||
if let Ok(x) = val { | ||
Ok(x) | ||
} else { | ||
Err(()) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...plify_dead_blocks.assert_nonzero_nonmax.SimplifyCfg-after-unreachable-enum-branching.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,33 @@ | ||
- // MIR for `assert_nonzero_nonmax` before SimplifyCfg-after-unreachable-enum-branching | ||
+ // MIR for `assert_nonzero_nonmax` after SimplifyCfg-after-unreachable-enum-branching | ||
|
||
fn assert_nonzero_nonmax(_1: u8) -> u8 { | ||
let mut _0: u8; | ||
|
||
bb0: { | ||
- switchInt(_1) -> [0: bb3, 1: bb2, 255: bb3, otherwise: bb4]; | ||
+ switchInt(_1) -> [0: bb2, 1: bb1, 255: bb2, otherwise: bb3]; | ||
} | ||
|
||
bb1: { | ||
- _0 = const 1_u8; | ||
- return; | ||
- } | ||
- | ||
- bb2: { | ||
_0 = const 2_u8; | ||
return; | ||
} | ||
|
||
- bb3: { | ||
+ bb2: { | ||
unreachable; | ||
} | ||
|
||
- bb4: { | ||
+ bb3: { | ||
_0 = _1; | ||
return; | ||
} | ||
} | ||
|
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,52 @@ | ||
//@ unit-test: SimplifyCfg-after-unreachable-enum-branching | ||
#![feature(custom_mir, core_intrinsics)] | ||
#![crate_type = "lib"] | ||
|
||
use std::intrinsics::mir::*; | ||
|
||
// Check that we correctly cleaned up the dead BB. | ||
// EMIT_MIR simplify_dead_blocks.assert_nonzero_nonmax.SimplifyCfg-after-unreachable-enum-branching.diff | ||
#[custom_mir(dialect = "runtime", phase = "post-cleanup")] | ||
pub unsafe fn assert_nonzero_nonmax(x: u8) -> u8 { | ||
// CHECK-LABEL: fn assert_nonzero_nonmax( | ||
// CHECK: bb0: { | ||
// CHECK-NEXT: switchInt({{.*}}) -> [0: [[unreachable:bb.*]], 1: [[retblock2:bb.*]], 255: [[unreachable:bb.*]], otherwise: [[retblock:bb.*]]]; | ||
// CHECK-NEXT: } | ||
// CHECK-NOT: _0 = const 1_u8; | ||
// CHECK: [[retblock2]]: { | ||
// CHECK-NEXT: _0 = const 2_u8; | ||
// CHECK-NEXT: return; | ||
// CHECK-NEXT: } | ||
// CHECK: [[unreachable]]: { | ||
// CHECK-NEXT: unreachable; | ||
// CHECK-NEXT: } | ||
// CHECK: [[retblock]]: { | ||
// CHECK-NEXT: _0 = _1; | ||
// CHECK-NEXT: return; | ||
// CHECK-NEXT: } | ||
mir!( | ||
{ | ||
match x { | ||
0 => unreachable, | ||
1 => retblock2, | ||
u8::MAX => unreachable, | ||
_ => retblock, | ||
} | ||
} | ||
deadRetblock1 = { | ||
RET = 1; | ||
Return() | ||
} | ||
retblock2 = { | ||
RET = 2; | ||
Return() | ||
} | ||
unreachable = { | ||
Unreachable() | ||
} | ||
retblock = { | ||
RET = x; | ||
Return() | ||
} | ||
) | ||
} |
25 changes: 0 additions & 25 deletions
25
...nreachable_blocks.assert_nonzero_nonmax.SimplifyCfg-after-uninhabited-enum-branching.diff
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...fallthrough.UninhabitedEnumBranching.diff → ...fallthrough.UnreachableEnumBranching.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
4 changes: 2 additions & 2 deletions
4
...fallthrough.UninhabitedEnumBranching.diff → ...fallthrough.UnreachableEnumBranching.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
Oops, something went wrong.