From 2d3178b3e22be7400cae63cb143eddb752e325fa Mon Sep 17 00:00:00 2001 From: DianQK Date: Fri, 19 Apr 2024 18:22:36 +0800 Subject: [PATCH] Disable MatchBranchSimplification --- .../rustc_mir_transform/src/match_branches.rs | 3 +- ....DataflowConstProp.64bit.panic-unwind.diff | 45 +++++++++------- ...xed_slice.main.GVN.64bit.panic-unwind.diff | 53 ++++++++++--------- ....unwrap_unchecked.Inline.panic-unwind.diff | 40 ++++++++++---- ...nchecked.PreCodegen.after.panic-unwind.mir | 12 ++++- tests/mir-opt/instsimplify/ub_check.rs | 1 + ...b_check.unwrap_unchecked.InstSimplify.diff | 28 ++++++++-- tests/mir-opt/issues/issue_75439.rs | 3 +- ...witch_targets.ub_if_b.PreCodegen.after.mir | 10 +++- ...foo.SimplifyLocals-final.panic-unwind.diff | 29 +++++++--- tests/mir-opt/switch_to_self.rs | 1 + 11 files changed, 154 insertions(+), 71 deletions(-) diff --git a/compiler/rustc_mir_transform/src/match_branches.rs b/compiler/rustc_mir_transform/src/match_branches.rs index 4d9a198eeb293..113ecc282f888 100644 --- a/compiler/rustc_mir_transform/src/match_branches.rs +++ b/compiler/rustc_mir_transform/src/match_branches.rs @@ -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>) { diff --git a/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff b/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff index 2e75a63e29052..19e1c0a811f28 100644 --- a/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff +++ b/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff @@ -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) { } } @@ -44,7 +44,7 @@ StorageLive(_1); StorageLive(_2); StorageLive(_3); - StorageLive(_9); + StorageLive(_8); StorageLive(_4); StorageLive(_5); StorageLive(_6); @@ -52,10 +52,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: { @@ -68,34 +68,39 @@ } bb3: { - StorageLive(_10); - _10 = const {0x1 as *mut ()}; - _9 = NonNull::::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::::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable]; + } + + bb6: { + StorageDead(_11); + goto -> bb4; + } } ALLOC2 (size: 16, align: 8) { diff --git a/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-unwind.diff b/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-unwind.diff index 920e66452e3b3..66b369c515254 100644 --- a/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-unwind.diff +++ b/tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-unwind.diff @@ -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) { } } @@ -44,7 +44,7 @@ StorageLive(_1); StorageLive(_2); StorageLive(_3); - StorageLive(_9); + StorageLive(_8); StorageLive(_4); StorageLive(_5); StorageLive(_6); @@ -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: { @@ -70,25 +70,16 @@ } bb3: { - StorageLive(_10); -- _10 = _6 as *mut () (PtrToPtr); -- _9 = NonNull::::new_unchecked::precondition_check(move _10) -> [return: bb4, unwind unreachable]; -+ _10 = const {0x1 as *mut ()}; -+ _9 = NonNull::::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]> }}; @@ -98,7 +89,7 @@ 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) }}; @@ -106,6 +97,20 @@ _0 = const (); drop(_1) -> [return: bb1, unwind: bb2]; } + + bb5: { + StorageDead(_11); + StorageLive(_9); +- _9 = _6 as *mut () (PtrToPtr); +- _8 = NonNull::::new_unchecked::precondition_check(move _9) -> [return: bb3, unwind unreachable]; ++ _9 = const {0x1 as *mut ()}; ++ _8 = NonNull::::new_unchecked::precondition_check(const {0x1 as *mut ()}) -> [return: bb3, unwind unreachable]; + } + + bb6: { + StorageDead(_11); + goto -> bb4; + } + } + + ALLOC2 (size: 16, align: 8) { diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff index 5271e538dafe4..deb3a0330f714 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff @@ -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) { + } + } @@ -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]; } } diff --git a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir index dcab8a679a875..bc67c0af3bf74 100644 --- a/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir @@ -8,6 +8,8 @@ fn unwrap_unchecked(_1: Option) -> 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) { } @@ -18,16 +20,22 @@ fn unwrap_unchecked(_1: Option) -> 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; } } diff --git a/tests/mir-opt/instsimplify/ub_check.rs b/tests/mir-opt/instsimplify/ub_check.rs index fc568abcd601c..00a2d7bac99e6 100644 --- a/tests/mir-opt/instsimplify/ub_check.rs +++ b/tests/mir-opt/instsimplify/ub_check.rs @@ -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 diff --git a/tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify.diff b/tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify.diff index 4d8d658984235..a57ee1fce717f 100644 --- a/tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify.diff +++ b/tests/mir-opt/instsimplify/ub_check.unwrap_unchecked.InstSimplify.diff @@ -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) { } } @@ -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: { @@ -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]; + } } diff --git a/tests/mir-opt/issues/issue_75439.rs b/tests/mir-opt/issues/issue_75439.rs index 8c710a33aa8e3..4498a9cf6b685 100644 --- a/tests/mir-opt/issues/issue_75439.rs +++ b/tests/mir-opt/issues/issue_75439.rs @@ -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) }; diff --git a/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir index 518fedffc1698..a6d0271080a6d 100644 --- a/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir @@ -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) { } @@ -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: { @@ -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; } } diff --git a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff index 686581591fc4e..9bdc21b7c32a2 100644 --- a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff +++ b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff @@ -8,6 +8,8 @@ let mut _3: std::option::Option; let mut _4: isize; let mut _5: isize; + let mut _7: bool; +- let mut _8: u8; scope 1 { debug a => _6; let _6: u8; @@ -23,31 +25,44 @@ StorageDead(_3); StorageDead(_2); _5 = discriminant((_1.0: std::option::Option)); - switchInt(move _5) -> [1: bb1, 0: bb3, otherwise: bb5]; + switchInt(move _5) -> [1: bb1, 0: bb6, otherwise: bb8]; } bb1: { _4 = discriminant((_1.1: std::option::Option)); - switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5]; + switchInt(move _4) -> [0: bb2, 1: bb6, otherwise: bb8]; } bb2: { - StorageLive(_6); _6 = (((_1.0: std::option::Option) as Some).0: u8); - StorageDead(_6); - goto -> bb3; + StorageLive(_7); + _7 = Gt(_6, const 42_u8); + switchInt(move _7) -> [0: bb4, otherwise: bb3]; } bb3: { - drop(_1) -> [return: bb4, unwind continue]; + goto -> bb5; } bb4: { + goto -> bb5; + } + + bb5: { + StorageDead(_7); + goto -> bb6; + } + + bb6: { + drop(_1) -> [return: bb7, unwind continue]; + } + + bb7: { StorageDead(_1); return; } - bb5: { + bb8: { unreachable; } } diff --git a/tests/mir-opt/switch_to_self.rs b/tests/mir-opt/switch_to_self.rs index fc270fd33cf84..9a89c412c5528 100644 --- a/tests/mir-opt/switch_to_self.rs +++ b/tests/mir-opt/switch_to_self.rs @@ -1,3 +1,4 @@ +//@ unit-test: MatchBranchSimplification // skip-filecheck // Test that MatchBranchSimplification doesn't ICE on a SwitchInt where // one of the targets is the block that the SwitchInt terminates.