From 8fbab183d7202880f87f6cc722d63cc4bbd1d140 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 9 Jun 2024 23:47:41 -0700 Subject: Delete `ConstDebugInfo` pass --- .../rustc_mir_transform/src/const_debuginfo.rs | 103 ---------------- compiler/rustc_mir_transform/src/lib.rs | 2 - .../const_debuginfo.main.ConstDebugInfo.diff | 115 ------------------ .../const_debuginfo.main.SingleUseConsts.diff | 129 +++++++++++++++++++++ tests/mir-opt/const_debuginfo.rs | 6 +- .../issue_106141.outer.Inline.panic-abort.diff | 2 +- .../issue_106141.outer.Inline.panic-unwind.diff | 2 +- 7 files changed, 135 insertions(+), 224 deletions(-) delete mode 100644 compiler/rustc_mir_transform/src/const_debuginfo.rs delete mode 100644 tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff create mode 100644 tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff diff --git a/compiler/rustc_mir_transform/src/const_debuginfo.rs b/compiler/rustc_mir_transform/src/const_debuginfo.rs deleted file mode 100644 index c758325fb98..00000000000 --- a/compiler/rustc_mir_transform/src/const_debuginfo.rs +++ /dev/null @@ -1,103 +0,0 @@ -//! Finds locals which are assigned once to a const and unused except for debuginfo and converts -//! their debuginfo to use the const directly, allowing the local to be removed. - -use rustc_middle::{ - mir::{ - visit::{PlaceContext, Visitor}, - Body, ConstOperand, Local, Location, Operand, Rvalue, StatementKind, VarDebugInfoContents, - }, - ty::TyCtxt, -}; - -use crate::MirPass; -use rustc_index::{bit_set::BitSet, IndexVec}; - -pub struct ConstDebugInfo; - -impl<'tcx> MirPass<'tcx> for ConstDebugInfo { - fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - // Disabled in favour of `SingleUseConsts` - sess.mir_opt_level() > 2 - } - - fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - trace!("running ConstDebugInfo on {:?}", body.source); - - for (local, constant) in find_optimization_opportunities(body) { - for debuginfo in &mut body.var_debug_info { - if let VarDebugInfoContents::Place(p) = debuginfo.value { - if p.local == local && p.projection.is_empty() { - trace!( - "changing debug info for {:?} from place {:?} to constant {:?}", - debuginfo.name, - p, - constant - ); - debuginfo.value = VarDebugInfoContents::Const(constant); - } - } - } - } - } -} - -struct LocalUseVisitor { - local_mutating_uses: IndexVec, - local_assignment_locations: IndexVec>, -} - -fn find_optimization_opportunities<'tcx>(body: &Body<'tcx>) -> Vec<(Local, ConstOperand<'tcx>)> { - let mut visitor = LocalUseVisitor { - local_mutating_uses: IndexVec::from_elem(0, &body.local_decls), - local_assignment_locations: IndexVec::from_elem(None, &body.local_decls), - }; - - visitor.visit_body(body); - - let mut locals_to_debuginfo = BitSet::new_empty(body.local_decls.len()); - for debuginfo in &body.var_debug_info { - if let VarDebugInfoContents::Place(p) = debuginfo.value - && let Some(l) = p.as_local() - { - locals_to_debuginfo.insert(l); - } - } - - let mut eligible_locals = Vec::new(); - for (local, mutating_uses) in visitor.local_mutating_uses.drain_enumerated(..) { - if mutating_uses != 1 || !locals_to_debuginfo.contains(local) { - continue; - } - - if let Some(location) = visitor.local_assignment_locations[local] { - let bb = &body[location.block]; - - // The value is assigned as the result of a call, not a constant - if bb.statements.len() == location.statement_index { - continue; - } - - if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(box c)))) = - &bb.statements[location.statement_index].kind - { - if let Some(local) = p.as_local() { - eligible_locals.push((local, *c)); - } - } - } - } - - eligible_locals -} - -impl Visitor<'_> for LocalUseVisitor { - fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) { - if context.is_mutating_use() { - self.local_mutating_uses[local] = self.local_mutating_uses[local].saturating_add(1); - - if context.is_place_assignment() { - self.local_assignment_locations[local] = Some(location); - } - } - } -} diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 23e2c128771..551760f4703 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -55,7 +55,6 @@ mod remove_place_mention; // This pass is public to allow external drivers to perform MIR cleanup mod add_subtyping_projections; pub mod cleanup_post_borrowck; -mod const_debuginfo; mod copy_prop; mod coroutine; mod cost_checker; @@ -595,7 +594,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &simplify::SimplifyLocals::AfterGVN, &dataflow_const_prop::DataflowConstProp, &single_use_consts::SingleUseConsts, - &const_debuginfo::ConstDebugInfo, &o1(simplify_branches::SimplifyConstCondition::AfterConstProp), &jump_threading::JumpThreading, &early_otherwise_branch::EarlyOtherwiseBranch, diff --git a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff b/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff deleted file mode 100644 index c1529dbee13..00000000000 --- a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff +++ /dev/null @@ -1,115 +0,0 @@ -- // MIR for `main` before ConstDebugInfo -+ // MIR for `main` after ConstDebugInfo - - fn main() -> () { - let mut _0: (); - let _1: u8; - let mut _5: u8; - let mut _6: u8; - let mut _7: u8; - let mut _8: u8; - let mut _14: u32; - let mut _15: u32; - scope 1 { -- debug x => _1; -+ debug x => const 1_u8; - let _2: u8; - scope 2 { -- debug y => _2; -+ debug y => const 2_u8; - let _3: u8; - scope 3 { -- debug z => _3; -+ debug z => const 3_u8; - let _4: u8; - scope 4 { -- debug sum => _4; -+ debug sum => const 6_u8; - let _9: &str; - scope 5 { -- debug s => _9; -+ debug s => const "hello, world!"; - let _10: (bool, bool, u32); - scope 6 { - debug f => _10; - let _11: std::option::Option; - scope 7 { -- debug o => _11; -+ debug o => const Option::::Some(99_u16); - let _12: Point; - scope 8 { -- debug p => _12; -+ debug p => const Point {{ x: 32_u32, y: 32_u32 }}; - let _13: u32; - scope 9 { -- debug a => _13; -+ debug a => const 64_u32; - } - } - } - } - } - } - } - } - } - - bb0: { - nop; - _1 = const 1_u8; - nop; - _2 = const 2_u8; - nop; - _3 = const 3_u8; - StorageLive(_4); - StorageLive(_5); - StorageLive(_6); - _6 = const 1_u8; - StorageLive(_7); - _7 = const 2_u8; - _5 = const 3_u8; - StorageDead(_7); - StorageDead(_6); - StorageLive(_8); - _8 = const 3_u8; - _4 = const 6_u8; - StorageDead(_8); - StorageDead(_5); - StorageLive(_9); - _9 = const "hello, world!"; - StorageLive(_10); - _10 = (const true, const false, const 123_u32); - StorageLive(_11); - _11 = const Option::::Some(99_u16); - StorageLive(_12); - _12 = const Point {{ x: 32_u32, y: 32_u32 }}; - StorageLive(_13); - nop; - _14 = const 32_u32; - StorageLive(_15); - _15 = const 32_u32; - _13 = const 64_u32; - StorageDead(_15); - nop; - _0 = const (); - StorageDead(_13); - StorageDead(_12); - StorageDead(_11); - StorageDead(_10); - StorageDead(_9); - StorageDead(_4); - nop; - nop; - nop; - return; - } - } - - ALLOC0 (size: 8, align: 4) { - 20 00 00 00 20 00 00 00 │ ... ... - } - - ALLOC1 (size: 4, align: 2) { - 01 00 63 00 │ ..c. - } - diff --git a/tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff b/tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff new file mode 100644 index 00000000000..ac33f51984c --- /dev/null +++ b/tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff @@ -0,0 +1,129 @@ +- // MIR for `main` before SingleUseConsts ++ // MIR for `main` after SingleUseConsts + + fn main() -> () { + let mut _0: (); + let _1: u8; + let mut _5: u8; + let mut _6: u8; + let mut _7: u8; + let mut _8: u8; + let mut _14: u32; + let mut _15: u32; + scope 1 { +- debug x => _1; ++ debug x => const 1_u8; + let _2: u8; + scope 2 { +- debug y => _2; ++ debug y => const 2_u8; + let _3: u8; + scope 3 { +- debug z => _3; ++ debug z => const 3_u8; + let _4: u8; + scope 4 { +- debug sum => _4; ++ debug sum => const 6_u8; + let _9: &str; + scope 5 { +- debug s => _9; ++ debug s => const "hello, world!"; + let _10: (bool, bool, u32); + scope 6 { + debug f => _10; + let _11: std::option::Option; + scope 7 { +- debug o => _11; ++ debug o => const Option::::Some(99_u16); + let _12: Point; + scope 8 { +- debug p => _12; ++ debug p => const Point {{ x: 32_u32, y: 32_u32 }}; + let _13: u32; + scope 9 { +- debug a => _13; ++ debug a => const 64_u32; + } + } + } + } + } + } + } + } + } + + bb0: { + nop; +- _1 = const 1_u8; + nop; +- _2 = const 2_u8; + nop; +- _3 = const 3_u8; ++ nop; ++ nop; ++ nop; + StorageLive(_4); + StorageLive(_5); + StorageLive(_6); +- _6 = const 1_u8; ++ nop; + StorageLive(_7); +- _7 = const 2_u8; +- _5 = const 3_u8; ++ nop; ++ nop; + StorageDead(_7); + StorageDead(_6); + StorageLive(_8); +- _8 = const 3_u8; +- _4 = const 6_u8; ++ nop; ++ nop; + StorageDead(_8); + StorageDead(_5); + StorageLive(_9); +- _9 = const "hello, world!"; ++ nop; + StorageLive(_10); + _10 = (const true, const false, const 123_u32); + StorageLive(_11); +- _11 = const Option::::Some(99_u16); ++ nop; + StorageLive(_12); +- _12 = const Point {{ x: 32_u32, y: 32_u32 }}; ++ nop; + StorageLive(_13); + nop; +- _14 = const 32_u32; ++ nop; + StorageLive(_15); +- _15 = const 32_u32; +- _13 = const 64_u32; ++ nop; ++ nop; + StorageDead(_15); + nop; + _0 = const (); + StorageDead(_13); + StorageDead(_12); + StorageDead(_11); + StorageDead(_10); + StorageDead(_9); + StorageDead(_4); + nop; + nop; + nop; + return; + } + } + + ALLOC0 (size: 8, align: 4) { + 20 00 00 00 20 00 00 00 │ ... ... + } + + ALLOC1 (size: 4, align: 2) { + 01 00 63 00 │ ..c. + } + diff --git a/tests/mir-opt/const_debuginfo.rs b/tests/mir-opt/const_debuginfo.rs index b880d7e07a6..907d7fef067 100644 --- a/tests/mir-opt/const_debuginfo.rs +++ b/tests/mir-opt/const_debuginfo.rs @@ -1,12 +1,14 @@ -//@ test-mir-pass: ConstDebugInfo +//@ test-mir-pass: SingleUseConsts //@ compile-flags: -C overflow-checks=no -Zmir-enable-passes=+GVN +#![allow(unused)] + struct Point { x: u32, y: u32, } -// EMIT_MIR const_debuginfo.main.ConstDebugInfo.diff +// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff fn main() { // CHECK-LABEL: fn main( // CHECK: debug x => const 1_u8; diff --git a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff index 24e5587f963..2ab79cc2a50 100644 --- a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-abort.diff @@ -8,7 +8,7 @@ + let mut _2: bool; + let mut _3: bool; + scope 2 { -+ debug buffer => const inner::promoted[0]; ++ debug buffer => _1; + scope 3 { + debug index => _0; + } diff --git a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff index 4eb8a5c9847..4d96a862885 100644 --- a/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/issue_106141.outer.Inline.panic-unwind.diff @@ -8,7 +8,7 @@ + let mut _2: bool; + let mut _3: bool; + scope 2 { -+ debug buffer => const inner::promoted[0]; ++ debug buffer => _1; + scope 3 { + debug index => _0; + } -- cgit 1.4.1-3-g733a5