From 4f9a2bd87ffc5fd7c836478749180dad18324afa Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 1 Dec 2022 00:24:07 -0500 Subject: [PATCH 1/4] Tweak assert_unsafe_precondition helpers so they inline in MIR --- library/core/src/intrinsics.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index a521905a9e735..05c155ebb3ce8 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -2297,12 +2297,22 @@ pub(crate) use assert_unsafe_precondition; /// Checks whether `ptr` is properly aligned with respect to /// `align_of::()`. +#[inline] pub(crate) fn is_aligned_and_not_null(ptr: *const T) -> bool { - !ptr.is_null() && ptr.is_aligned() + // A reasonable implementation of this would be + // !ptr.is_null() && ptr.is_aligned() + // However that implementation is based on many layers of abstraction, and results in a lot + // more MIR being generated, which matters for this function especially because when debug + // assertions are enabled it is called in very many places. This simpler implementation seems + // to be worth 0-5% on debug builds. + let addr = ptr.addr(); + let mask = const { crate::mem::align_of::() - 1 }; + (addr != 0) && (addr & mask == 0) } /// Checks whether an allocation of `len` instances of `T` exceeds /// the maximum allowed allocation size. +#[inline] pub(crate) fn is_valid_allocation_size(len: usize) -> bool { let max_len = const { let size = crate::mem::size_of::(); @@ -2313,6 +2323,7 @@ pub(crate) fn is_valid_allocation_size(len: usize) -> bool { /// Checks whether the regions of memory starting at `src` and `dst` of size /// `count * size_of::()` do *not* overlap. +#[inline] pub(crate) fn is_nonoverlapping(src: *const T, dst: *const T, count: usize) -> bool { let src_usize = src.addr(); let dst_usize = dst.addr(); From 07f16319eaa0722032df5f68c89e1f756701b14b Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 23 Dec 2022 16:46:33 -0500 Subject: [PATCH 2/4] Try to delete dead calls instead of inlining them --- compiler/rustc_mir_transform/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index aba5a8580f194..a3b62391f9461 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -555,6 +555,10 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &unreachable_prop::UnreachablePropagation, &uninhabited_enum_branching::UninhabitedEnumBranching, &o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")), + &const_prop::ConstProp, + &const_debuginfo::ConstDebugInfo, + &o1(simplify_branches::SimplifyConstCondition::new("before-inline")), + &o1(simplify::SimplifyCfg::new("before-inline")), &inline::Inline, &remove_storage_markers::RemoveStorageMarkers, &remove_zsts::RemoveZsts, From f085e7a6c01e991929c82cd4d9427db16718d3ac Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 26 Dec 2022 14:10:40 -0500 Subject: [PATCH 3/4] Bless --- .../rustc_mir_transform/src/const_prop.rs | 21 ++++++++++++++--- .../src/const_prop_lint.rs | 2 +- compiler/rustc_mir_transform/src/lib.rs | 6 ++--- .../const_debuginfo.main.ConstDebugInfo.diff | 12 ++++------ .../bad_op_div_by_zero.main.ConstProp.diff | 5 ++-- .../bad_op_mod_by_zero.main.ConstProp.diff | 11 ++------- .../boolean_identities.test.ConstProp.diff | 9 +++----- ...l_flow_simplification.hello.ConstProp.diff | 16 ------------- src/test/mir-opt/dead_code_does_not_inline.rs | 10 ++++++++ ...mplifyConstCondition-after-const-prop.diff | 23 ------------------- .../simplify_match.main.ConstProp.diff | 19 +-------------- 11 files changed, 44 insertions(+), 90 deletions(-) create mode 100644 src/test/mir-opt/dead_code_does_not_inline.rs diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index e384cfe165990..d0bd7e7ab789f 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -57,7 +57,14 @@ macro_rules! throw_machine_stop_str { }}; } -pub struct ConstProp; +pub struct ConstProp { + only_bools: bool, +} +impl ConstProp { + pub fn new(only_bools: bool) -> Self { + ConstProp { only_bools } + } +} impl<'tcx> MirPass<'tcx> for ConstProp { fn is_enabled(&self, sess: &rustc_session::Session) -> bool { @@ -149,7 +156,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp { // constants, instead of just checking for const-folding succeeding. // That would require a uniform one-def no-mutation analysis // and RPO (or recursing when needing the value of a local). - let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx); + let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx, self.only_bools); optimization_finder.visit_body(body); trace!("ConstProp done for {:?}", def_id); @@ -373,12 +380,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { body: &Body<'tcx>, dummy_body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>, + only_bools: bool, ) -> ConstPropagator<'mir, 'tcx> { let def_id = body.source.def_id(); let substs = &InternalSubsts::identity_for_item(tcx, def_id); let param_env = tcx.param_env_reveal_all_normalized(def_id); - let can_const_prop = CanConstProp::check(tcx, param_env, body); + let can_const_prop = CanConstProp::check(tcx, param_env, body, only_bools); let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len()); for (l, mode) in can_const_prop.iter_enumerated() { if *mode == ConstPropMode::OnlyInsideOwnBlock { @@ -858,6 +866,7 @@ impl CanConstProp { tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, body: &Body<'tcx>, + only_bools: bool, ) -> IndexVec { let mut cpv = CanConstProp { can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls), @@ -869,6 +878,12 @@ impl CanConstProp { }; for (local, val) in cpv.can_const_prop.iter_enumerated_mut() { let ty = body.local_decls[local].ty; + if only_bools { + if ty != tcx.types.bool { + *val = ConstPropMode::NoPropagation; + continue; + } + } match tcx.layout_of(param_env.and(ty)) { Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {} // Either the layout fails to compute, then we can't use this local anyway diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 0ab67228f3f40..2d970d62aad84 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -182,7 +182,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let substs = &InternalSubsts::identity_for_item(tcx, def_id); let param_env = tcx.param_env_reveal_all_normalized(def_id); - let can_const_prop = CanConstProp::check(tcx, param_env, body); + let can_const_prop = CanConstProp::check(tcx, param_env, body, false); let mut only_propagate_inside_block_locals = BitSet::new_empty(can_const_prop.len()); for (l, mode) in can_const_prop.iter_enumerated() { if *mode == ConstPropMode::OnlyInsideOwnBlock { diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index a3b62391f9461..0f672a65d2b18 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -410,7 +410,7 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) - pm::run_passes( tcx, &mut body, - &[&const_prop::ConstProp], + &[&const_prop::ConstProp::new(false)], Some(MirPhase::Runtime(RuntimePhase::Optimized)), ); } @@ -555,7 +555,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &unreachable_prop::UnreachablePropagation, &uninhabited_enum_branching::UninhabitedEnumBranching, &o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")), - &const_prop::ConstProp, + &const_prop::ConstProp::new(true), &const_debuginfo::ConstDebugInfo, &o1(simplify_branches::SimplifyConstCondition::new("before-inline")), &o1(simplify::SimplifyCfg::new("before-inline")), @@ -572,7 +572,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &separate_const_switch::SeparateConstSwitch, // // FIXME(#70073): This pass is responsible for both optimization as well as some lints. - &const_prop::ConstProp, + &const_prop::ConstProp::new(false), &dataflow_const_prop::DataflowConstProp, // // Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0. diff --git a/src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff b/src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff index e959e1b2f2c30..54da4c41be734 100644 --- a/src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff +++ b/src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff @@ -11,24 +11,20 @@ let mut _14: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:13: +13:16 let mut _15: u32; // in scope 0 at $DIR/const_debuginfo.rs:+13:19: +13:22 scope 1 { -- debug x => _1; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10 -+ debug x => const 1_u8; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10 + debug x => const 1_u8; // in scope 1 at $DIR/const_debuginfo.rs:+1:9: +1:10 let _2: u8; // in scope 1 at $DIR/const_debuginfo.rs:+2:9: +2:10 scope 2 { -- debug y => _2; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10 -+ debug y => const 2_u8; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10 + debug y => const 2_u8; // in scope 2 at $DIR/const_debuginfo.rs:+2:9: +2:10 let _3: u8; // in scope 2 at $DIR/const_debuginfo.rs:+3:9: +3:10 scope 3 { -- debug z => _3; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10 -+ debug z => const 3_u8; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10 + debug z => const 3_u8; // in scope 3 at $DIR/const_debuginfo.rs:+3:9: +3:10 let _4: u8; // in scope 3 at $DIR/const_debuginfo.rs:+4:9: +4:12 scope 4 { - debug sum => _4; // in scope 4 at $DIR/const_debuginfo.rs:+4:9: +4:12 + debug sum => const 6_u8; // in scope 4 at $DIR/const_debuginfo.rs:+4:9: +4:12 let _9: &str; // in scope 4 at $DIR/const_debuginfo.rs:+6:9: +6:10 scope 5 { -- debug s => _9; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10 -+ debug s => const "hello, world!"; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10 + debug s => const "hello, world!"; // in scope 5 at $DIR/const_debuginfo.rs:+6:9: +6:10 let _10: (bool, bool, u32); // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10 let _16: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10 let _17: bool; // in scope 5 at $DIR/const_debuginfo.rs:+8:9: +8:10 diff --git a/src/test/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff b/src/test/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff index bea32a67ef4a0..959e72675af69 100644 --- a/src/test/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/bad_op_div_by_zero.main.ConstProp.diff @@ -31,11 +31,10 @@ bb1: { - _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 -- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 ++ _5 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 + _6 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 - _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 - assert(!move _7, "attempt to compute `{} / {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 -+ _5 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 -+ _6 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 + _7 = const false; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 + assert(!const false, "attempt to compute `{} / {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_div_by_zero.rs:+2:14: +2:19 } diff --git a/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff b/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff index 22151304259bc..4684e802cd5f4 100644 --- a/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff @@ -32,16 +32,9 @@ bb1: { - _5 = Eq(_3, const -1_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -- _6 = Eq(const 1_i32, const i32::MIN); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -- _7 = BitAnd(move _5, move _6); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -- assert(!move _7, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, _3) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + _5 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -+ _6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -+ _7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 -+ assert(!const false, "attempt to compute the remainder of `{} % {}`, which would overflow", const 1_i32, const 0_i32) -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 - } - - bb2: { + _6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + _7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 - _2 = Rem(const 1_i32, move _3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + _2 = Rem(const 1_i32, const 0_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 StorageDead(_3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:18: +2:19 diff --git a/src/test/mir-opt/const_prop/boolean_identities.test.ConstProp.diff b/src/test/mir-opt/const_prop/boolean_identities.test.ConstProp.diff index 0de800917534a..6a7da1771a746 100644 --- a/src/test/mir-opt/const_prop/boolean_identities.test.ConstProp.diff +++ b/src/test/mir-opt/const_prop/boolean_identities.test.ConstProp.diff @@ -14,17 +14,14 @@ StorageLive(_3); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15 StorageLive(_4); // scope 0 at $DIR/boolean_identities.rs:+1:6: +1:7 _4 = _2; // scope 0 at $DIR/boolean_identities.rs:+1:6: +1:7 -- _3 = BitOr(move _4, const true); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15 -+ _3 = const true; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15 + _3 = const true; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:15 StorageDead(_4); // scope 0 at $DIR/boolean_identities.rs:+1:14: +1:15 StorageLive(_5); // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29 StorageLive(_6); // scope 0 at $DIR/boolean_identities.rs:+1:19: +1:20 _6 = _1; // scope 0 at $DIR/boolean_identities.rs:+1:19: +1:20 -- _5 = BitAnd(move _6, const false); // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29 -+ _5 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29 + _5 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:18: +1:29 StorageDead(_6); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29 -- _0 = BitAnd(move _3, move _5); // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29 -+ _0 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29 + _0 = const false; // scope 0 at $DIR/boolean_identities.rs:+1:5: +1:29 StorageDead(_5); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29 StorageDead(_3); // scope 0 at $DIR/boolean_identities.rs:+1:28: +1:29 return; // scope 0 at $DIR/boolean_identities.rs:+2:2: +2:2 diff --git a/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff b/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff index 147670f8a915c..7d6bacec0fa79 100644 --- a/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff +++ b/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff @@ -9,22 +9,6 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 _1 = const _; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 -- switchInt(move _1) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 -+ switchInt(const false) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 - } - - bb1: { - StorageLive(_2); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL - _2 = begin_panic::<&str>(const "explicit panic"); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL - // mir::Constant - // + span: $SRC_DIR/std/src/panic.rs:LL:COL - // + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value() } - // mir::Constant - // + span: $SRC_DIR/std/src/panic.rs:LL:COL - // + literal: Const { ty: &str, val: Value(Slice(..)) } - } - - bb2: { nop; // scope 0 at $DIR/control_flow_simplification.rs:+3:6: +3:6 StorageDead(_1); // scope 0 at $DIR/control_flow_simplification.rs:+3:5: +3:6 return; // scope 0 at $DIR/control_flow_simplification.rs:+4:2: +4:2 diff --git a/src/test/mir-opt/dead_code_does_not_inline.rs b/src/test/mir-opt/dead_code_does_not_inline.rs new file mode 100644 index 0000000000000..7873fca085098 --- /dev/null +++ b/src/test/mir-opt/dead_code_does_not_inline.rs @@ -0,0 +1,10 @@ +//EMIT_MIR dead_code_does_not_inline.main.Inline.after.mir +fn main() { + if false { + callee(); + } +} + +fn callee() { + println!("Wooooo I'm invisible"); +} diff --git a/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff b/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff index aea0114744352..78f1539e9923d 100644 --- a/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff +++ b/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff @@ -9,30 +9,7 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 _1 = const false; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 -- switchInt(const false) -> [0: bb3, otherwise: bb1]; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 -+ goto -> bb3; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 - } - - bb1: { - StorageLive(_2); // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15 - _2 = noop() -> bb2; // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15 - // mir::Constant - // + span: $DIR/simplify_if.rs:7:9: 7:13 - // + literal: Const { ty: fn() {noop}, val: Value() } - } - - bb2: { - StorageDead(_2); // scope 0 at $DIR/simplify_if.rs:+2:15: +2:16 - nop; // scope 0 at $DIR/simplify_if.rs:+1:14: +3:6 - goto -> bb4; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6 - } - - bb3: { nop; // scope 0 at $DIR/simplify_if.rs:+3:6: +3:6 - goto -> bb4; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6 - } - - bb4: { StorageDead(_1); // scope 0 at $DIR/simplify_if.rs:+3:5: +3:6 return; // scope 0 at $DIR/simplify_if.rs:+4:2: +4:2 } diff --git a/src/test/mir-opt/simplify_match.main.ConstProp.diff b/src/test/mir-opt/simplify_match.main.ConstProp.diff index f00ac5716a751..e3d4cc1b43e34 100644 --- a/src/test/mir-opt/simplify_match.main.ConstProp.diff +++ b/src/test/mir-opt/simplify_match.main.ConstProp.diff @@ -13,26 +13,9 @@ StorageLive(_1); // scope 0 at $DIR/simplify_match.rs:+1:11: +1:31 StorageLive(_2); // scope 0 at $DIR/simplify_match.rs:+1:17: +1:18 _2 = const false; // scope 0 at $DIR/simplify_match.rs:+1:21: +1:26 -- _1 = _2; // scope 1 at $DIR/simplify_match.rs:+1:28: +1:29 -+ _1 = const false; // scope 1 at $DIR/simplify_match.rs:+1:28: +1:29 + _1 = const false; // scope 1 at $DIR/simplify_match.rs:+1:28: +1:29 StorageDead(_2); // scope 0 at $DIR/simplify_match.rs:+1:30: +1:31 -- switchInt(_1) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/simplify_match.rs:+1:5: +1:31 -+ switchInt(const false) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/simplify_match.rs:+1:5: +1:31 - } - - bb1: { nop; // scope 0 at $DIR/simplify_match.rs:+3:18: +3:20 - goto -> bb3; // scope 0 at $DIR/simplify_match.rs:+3:18: +3:20 - } - - bb2: { - _0 = noop() -> bb3; // scope 0 at $DIR/simplify_match.rs:+2:17: +2:23 - // mir::Constant - // + span: $DIR/simplify_match.rs:7:17: 7:21 - // + literal: Const { ty: fn() {noop}, val: Value() } - } - - bb3: { StorageDead(_1); // scope 0 at $DIR/simplify_match.rs:+5:1: +5:2 return; // scope 0 at $DIR/simplify_match.rs:+5:2: +5:2 } From a4b5bfeff3f499380bce0f768e8155e325c2f45d Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 26 Dec 2022 14:19:25 -0500 Subject: [PATCH 4/4] Fix, bless --- compiler/rustc_mir_transform/src/lib.rs | 1 - .../bad_op_mod_by_zero.main.ConstProp.diff | 4 ++ ...l_flow_simplification.hello.ConstProp.diff | 15 ++++++++ ...code_does_not_inline.main.Inline.after.mir | 37 +++++++++++++++++++ src/test/mir-opt/dead_code_does_not_inline.rs | 6 +-- ...mplifyConstCondition-after-const-prop.diff | 8 ++++ .../simplify_match.main.ConstProp.diff | 8 ++++ 7 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/test/mir-opt/dead_code_does_not_inline.main.Inline.after.mir diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 0f672a65d2b18..03320b5d59a8a 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -558,7 +558,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &const_prop::ConstProp::new(true), &const_debuginfo::ConstDebugInfo, &o1(simplify_branches::SimplifyConstCondition::new("before-inline")), - &o1(simplify::SimplifyCfg::new("before-inline")), &inline::Inline, &remove_storage_markers::RemoveStorageMarkers, &remove_zsts::RemoveZsts, diff --git a/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff b/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff index 4684e802cd5f4..2b70604012772 100644 --- a/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/bad_op_mod_by_zero.main.ConstProp.diff @@ -35,6 +35,10 @@ + _5 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 _6 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 _7 = const false; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + goto -> bb2; // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + } + + bb2: { - _2 = Rem(const 1_i32, move _3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 + _2 = Rem(const 1_i32, const 0_i32); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:14: +2:19 StorageDead(_3); // scope 1 at $DIR/bad_op_mod_by_zero.rs:+2:18: +2:19 diff --git a/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff b/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff index 7d6bacec0fa79..ed9811ef975b9 100644 --- a/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff +++ b/src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff @@ -9,6 +9,21 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 _1 = const _; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 + goto -> bb2; // scope 0 at $DIR/control_flow_simplification.rs:+1:8: +1:21 + } + + bb1: { + StorageLive(_2); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL + _2 = begin_panic::<&str>(const "explicit panic"); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/std/src/panic.rs:LL:COL + // + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value() } + // mir::Constant + // + span: $SRC_DIR/std/src/panic.rs:LL:COL + // + literal: Const { ty: &str, val: Value(Slice(..)) } + } + + bb2: { nop; // scope 0 at $DIR/control_flow_simplification.rs:+3:6: +3:6 StorageDead(_1); // scope 0 at $DIR/control_flow_simplification.rs:+3:5: +3:6 return; // scope 0 at $DIR/control_flow_simplification.rs:+4:2: +4:2 diff --git a/src/test/mir-opt/dead_code_does_not_inline.main.Inline.after.mir b/src/test/mir-opt/dead_code_does_not_inline.main.Inline.after.mir new file mode 100644 index 0000000000000..28dcefb0c5654 --- /dev/null +++ b/src/test/mir-opt/dead_code_does_not_inline.main.Inline.after.mir @@ -0,0 +1,37 @@ +// MIR for `main` after Inline + +fn main() -> () { + let mut _0: (); // return place in scope 0 at $DIR/dead_code_does_not_inline.rs:+0:15: +0:15 + let mut _1: bool; // in scope 0 at $DIR/dead_code_does_not_inline.rs:+1:8: +1:13 + let _2: (); // in scope 0 at $DIR/dead_code_does_not_inline.rs:+2:9: +2:17 + + bb0: { + StorageLive(_1); // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:8: +1:13 + _1 = const false; // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:8: +1:13 + goto -> bb3; // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:8: +1:13 + } + + bb1: { + StorageLive(_2); // scope 0 at $DIR/dead_code_does_not_inline.rs:+2:9: +2:17 + _2 = callee() -> bb2; // scope 0 at $DIR/dead_code_does_not_inline.rs:+2:9: +2:17 + // mir::Constant + // + span: $DIR/dead_code_does_not_inline.rs:4:9: 4:15 + // + literal: Const { ty: fn() {callee}, val: Value() } + } + + bb2: { + StorageDead(_2); // scope 0 at $DIR/dead_code_does_not_inline.rs:+2:17: +2:18 + _0 = const (); // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:14: +3:6 + goto -> bb4; // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:5: +3:6 + } + + bb3: { + _0 = const (); // scope 0 at $DIR/dead_code_does_not_inline.rs:+3:6: +3:6 + goto -> bb4; // scope 0 at $DIR/dead_code_does_not_inline.rs:+1:5: +3:6 + } + + bb4: { + StorageDead(_1); // scope 0 at $DIR/dead_code_does_not_inline.rs:+3:5: +3:6 + return; // scope 0 at $DIR/dead_code_does_not_inline.rs:+4:2: +4:2 + } +} diff --git a/src/test/mir-opt/dead_code_does_not_inline.rs b/src/test/mir-opt/dead_code_does_not_inline.rs index 7873fca085098..1cd44fec7ca8e 100644 --- a/src/test/mir-opt/dead_code_does_not_inline.rs +++ b/src/test/mir-opt/dead_code_does_not_inline.rs @@ -1,10 +1,10 @@ -//EMIT_MIR dead_code_does_not_inline.main.Inline.after.mir -fn main() { +// EMIT_MIR dead_code_does_not_inline.main.Inline.after.mir +pub fn main() { if false { callee(); } } -fn callee() { +pub fn callee() { println!("Wooooo I'm invisible"); } diff --git a/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff b/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff index 78f1539e9923d..6f051b184a217 100644 --- a/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff +++ b/src/test/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff @@ -9,7 +9,15 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 _1 = const false; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 + goto -> bb1; // scope 0 at $DIR/simplify_if.rs:+1:8: +1:13 + } + + bb1: { nop; // scope 0 at $DIR/simplify_if.rs:+3:6: +3:6 + goto -> bb2; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6 + } + + bb2: { StorageDead(_1); // scope 0 at $DIR/simplify_if.rs:+3:5: +3:6 return; // scope 0 at $DIR/simplify_if.rs:+4:2: +4:2 } diff --git a/src/test/mir-opt/simplify_match.main.ConstProp.diff b/src/test/mir-opt/simplify_match.main.ConstProp.diff index e3d4cc1b43e34..2f9ce858d3eba 100644 --- a/src/test/mir-opt/simplify_match.main.ConstProp.diff +++ b/src/test/mir-opt/simplify_match.main.ConstProp.diff @@ -15,7 +15,15 @@ _2 = const false; // scope 0 at $DIR/simplify_match.rs:+1:21: +1:26 _1 = const false; // scope 1 at $DIR/simplify_match.rs:+1:28: +1:29 StorageDead(_2); // scope 0 at $DIR/simplify_match.rs:+1:30: +1:31 + goto -> bb1; // scope 0 at $DIR/simplify_match.rs:+1:5: +1:31 + } + + bb1: { nop; // scope 0 at $DIR/simplify_match.rs:+3:18: +3:20 + goto -> bb2; // scope 0 at $DIR/simplify_match.rs:+3:18: +3:20 + } + + bb2: { StorageDead(_1); // scope 0 at $DIR/simplify_match.rs:+5:1: +5:2 return; // scope 0 at $DIR/simplify_match.rs:+5:2: +5:2 }