Skip to content

Commit

Permalink
Auto merge of rust-lang#124156 - DianQK:disable-match_branches, r=<try>
Browse files Browse the repository at this point in the history
Disable MatchBranchSimplification

Due to the miscompilation mentioned in rust-lang#124150, We need to disable MatchBranchSimplification temporarily.

To fully resolve this issue, my plan is:
1. Disable MatchBranchSimplification (this PR).
2. Remove all potentially unclear transforms in rust-lang#124122.
3. Gradually add back the removed transforms (possibly multiple PRs).

r? `@Nilstrieb` or `@oli-obk`
  • Loading branch information
bors committed Apr 19, 2024
2 parents c4f112a + 2d3178b commit e7a5d6f
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 71 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub struct MatchBranchSimplification;

impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 1
// unsound: https://github.com/rust-lang/rust/issues/124150
sess.mir_opt_level() >= 1 && sess.opts.unstable_opts.unsound_mir_opts
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
let _6: *mut [bool; 0];
scope 6 {
scope 10 (inlined NonNull::<[bool; 0]>::new_unchecked) {
let mut _8: bool;
let _9: ();
let mut _10: *mut ();
let mut _11: *const [bool; 0];
let _8: ();
let mut _9: *mut ();
let mut _10: *const [bool; 0];
scope 11 (inlined core::ub_checks::check_language_ub) {
let mut _11: bool;
scope 12 (inlined core::ub_checks::check_language_ub::runtime) {
}
}
Expand All @@ -44,18 +44,18 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_9);
StorageLive(_8);
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
StorageLive(_7);
_7 = const 1_usize;
_6 = const {0x1 as *mut [bool; 0]};
StorageDead(_7);
StorageLive(_10);
StorageLive(_11);
StorageLive(_8);
_8 = UbChecks();
switchInt(move _8) -> [0: bb5, otherwise: bb3];
_11 = UbChecks();
switchInt(move _11) -> [0: bb6, otherwise: bb5];
}

bb1: {
Expand All @@ -68,34 +68,39 @@
}

bb3: {
StorageLive(_10);
_10 = const {0x1 as *mut ()};
_9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb4, unwind unreachable];
StorageDead(_9);
goto -> bb4;
}

bb4: {
StorageDead(_10);
goto -> bb5;
}

bb5: {
StorageDead(_8);
_11 = const {0x1 as *const [bool; 0]};
_10 = const {0x1 as *const [bool; 0]};
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
StorageDead(_11);
StorageDead(_10);
StorageDead(_6);
_4 = const Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
StorageDead(_5);
_3 = const Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};
StorageDead(_4);
_2 = const Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global);
StorageDead(_9);
StorageDead(_8);
StorageDead(_3);
_1 = const A {{ foo: Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC2, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global) }};
StorageDead(_2);
_0 = const ();
drop(_1) -> [return: bb1, unwind: bb2];
}

bb5: {
StorageDead(_11);
StorageLive(_9);
_9 = const {0x1 as *mut ()};
_8 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable];
}

bb6: {
StorageDead(_11);
goto -> bb4;
}
}

ALLOC2 (size: 16, align: 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
let _6: *mut [bool; 0];
scope 6 {
scope 10 (inlined NonNull::<[bool; 0]>::new_unchecked) {
let mut _8: bool;
let _9: ();
let mut _10: *mut ();
let mut _11: *const [bool; 0];
let _8: ();
let mut _9: *mut ();
let mut _10: *const [bool; 0];
scope 11 (inlined core::ub_checks::check_language_ub) {
let mut _11: bool;
scope 12 (inlined core::ub_checks::check_language_ub::runtime) {
}
}
Expand All @@ -44,7 +44,7 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
StorageLive(_9);
StorageLive(_8);
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
Expand All @@ -54,10 +54,10 @@
+ _7 = const 1_usize;
+ _6 = const {0x1 as *mut [bool; 0]};
StorageDead(_7);
StorageLive(_10);
StorageLive(_11);
StorageLive(_8);
_8 = UbChecks();
switchInt(move _8) -> [0: bb5, otherwise: bb3];
_11 = UbChecks();
switchInt(move _11) -> [0: bb6, otherwise: bb5];
}

bb1: {
Expand All @@ -70,25 +70,16 @@
}

bb3: {
StorageLive(_10);
- _10 = _6 as *mut () (PtrToPtr);
- _9 = NonNull::<T>::new_unchecked::precondition_check(move _10) -> [return: bb4, unwind unreachable];
+ _10 = const {0x1 as *mut ()};
+ _9 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb4, unwind unreachable];
StorageDead(_9);
goto -> bb4;
}

bb4: {
StorageDead(_10);
goto -> bb5;
}

bb5: {
StorageDead(_8);
- _11 = _6 as *const [bool; 0] (PointerCoercion(MutToConstPointer));
- _5 = NonNull::<[bool; 0]> { pointer: _11 };
+ _11 = const {0x1 as *const [bool; 0]};
- _10 = _6 as *const [bool; 0] (PointerCoercion(MutToConstPointer));
- _5 = NonNull::<[bool; 0]> { pointer: _10 };
+ _10 = const {0x1 as *const [bool; 0]};
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
StorageDead(_11);
StorageDead(_10);
StorageDead(_6);
- _4 = Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
+ _4 = const Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
Expand All @@ -98,14 +89,28 @@
StorageDead(_4);
- _2 = Box::<[bool]>(_3, const std::alloc::Global);
+ _2 = const Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC1, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global);
StorageDead(_9);
StorageDead(_8);
StorageDead(_3);
- _1 = A { foo: move _2 };
+ _1 = const A {{ foo: Box::<[bool]>(Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC2, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }}, std::alloc::Global) }};
StorageDead(_2);
_0 = const ();
drop(_1) -> [return: bb1, unwind: bb2];
}

bb5: {
StorageDead(_11);
StorageLive(_9);
- _9 = _6 as *mut () (PtrToPtr);
- _8 = NonNull::<T>::new_unchecked::precondition_check(move _9) -> [return: bb3, unwind unreachable];
+ _9 = const {0x1 as *mut ()};
+ _8 = NonNull::<T>::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable];
}

bb6: {
StorageDead(_11);
goto -> bb4;
}
+ }
+
+ ALLOC2 (size: 16, align: 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
+ scope 3 (inlined unreachable_unchecked) {
+ let mut _4: bool;
+ let _5: ();
+ let mut _7: bool;
+ scope 4 (inlined core::ub_checks::check_language_ub) {
+ let mut _6: bool;
+ scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
+ }
+ }
Expand All @@ -30,26 +32,42 @@
}

bb1: {
- StorageDead(_2);
- return;
+ unreachable;
}

- bb2 (cleanup): {
- resume;
+ }
+
+ bb2: {
+ StorageLive(_7);
+ StorageLive(_4);
+ _4 = UbChecks();
+ assume(_4);
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
+ StorageLive(_6);
+ _6 = UbChecks();
+ switchInt(move _6) -> [0: bb5, otherwise: bb4];
+ }
+
+ bb3: {
+ _0 = move ((_2 as Some).0: T);
+ StorageDead(_5);
+ StorageDead(_3);
+ StorageDead(_2);
+ return;
StorageDead(_2);
return;
}

- bb2 (cleanup): {
- resume;
+ bb4: {
+ _4 = const true;
+ goto -> bb6;
+ }
+
+ bb5: {
+ _4 = const false;
+ goto -> bb6;
+ }
+
+ bb6: {
+ StorageDead(_6);
+ _7 = _4;
+ assume(move _7);
+ _5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
scope 2 {
}
scope 3 (inlined unreachable_unchecked) {
let mut _3: bool;
let _4: ();
scope 4 (inlined core::ub_checks::check_language_ub) {
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
}
Expand All @@ -18,16 +20,22 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
bb0: {
StorageLive(_2);
_2 = discriminant(_1);
switchInt(move _2) -> [0: bb2, 1: bb1, otherwise: bb2];
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb3];
}

bb1: {
_3 = const false;
assume(_3);
_4 = unreachable_unchecked::precondition_check() -> [return: bb3, unwind unreachable];
}

bb2: {
_0 = ((_1 as Some).0: T);
StorageDead(_2);
return;
}

bb2: {
bb3: {
unreachable;
}
}
1 change: 1 addition & 0 deletions tests/mir-opt/instsimplify/ub_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ unit-test: InstSimplify
//@ should-fail Broken due to https://github.com/rust-lang/rust/issues/124150.
//@ compile-flags: -Cdebug-assertions=no -Zinline-mir

// EMIT_MIR ub_check.unwrap_unchecked.InstSimplify.diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
scope 3 (inlined unreachable_unchecked) {
let mut _4: bool;
let _5: ();
let mut _7: bool;
scope 4 (inlined core::ub_checks::check_language_ub) {
let mut _6: bool;
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
}
}
Expand All @@ -33,11 +35,12 @@
}

bb2: {
StorageLive(_7);
StorageLive(_4);
- _4 = UbChecks();
+ _4 = const false;
assume(_4);
_5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
StorageLive(_6);
- _6 = UbChecks();
+ _6 = const false;
switchInt(move _6) -> [0: bb5, otherwise: bb4];
}

bb3: {
Expand All @@ -47,5 +50,22 @@
StorageDead(_2);
return;
}

bb4: {
_4 = const true;
goto -> bb6;
}

bb5: {
_4 = const false;
goto -> bb6;
}

bb6: {
StorageDead(_6);
_7 = _4;
assume(move _7);
_5 = unreachable_unchecked::precondition_check() -> [return: bb1, unwind unreachable];
}
}

3 changes: 2 additions & 1 deletion tests/mir-opt/issues/issue_75439.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//@ unit-test: MatchBranchSimplification
// skip-filecheck
// EMIT_MIR issue_75439.foo.MatchBranchSimplification.diff
//@ ignore-endian-big

use std::mem::transmute;

// EMIT_MIR issue_75439.foo.MatchBranchSimplification.diff
pub fn foo(bytes: [u8; 16]) -> Option<[u8; 4]> {
// big endian `u32`s
let dwords: [u32; 4] = unsafe { transmute(bytes) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ fn ub_if_b(_1: Thing) -> Thing {
let mut _0: Thing;
let mut _2: isize;
scope 1 (inlined unreachable_unchecked) {
let mut _3: bool;
let _4: ();
scope 2 (inlined core::ub_checks::check_language_ub) {
scope 3 (inlined core::ub_checks::check_language_ub::runtime) {
}
Expand All @@ -13,7 +15,7 @@ fn ub_if_b(_1: Thing) -> Thing {

bb0: {
_2 = discriminant(_1);
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb2];
switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb3];
}

bb1: {
Expand All @@ -22,6 +24,12 @@ fn ub_if_b(_1: Thing) -> Thing {
}

bb2: {
_3 = const false;
assume(_3);
_4 = unreachable_unchecked::precondition_check() -> [return: bb3, unwind unreachable];
}

bb3: {
unreachable;
}
}
Loading

0 comments on commit e7a5d6f

Please sign in to comment.