From 6ecb2aa58029f922acda8011737db1647643a975 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 5 Jan 2024 17:26:59 +0000 Subject: [PATCH] We're not really using the `ConstPropMachine` anymore --- .../rustc_mir_transform/src/const_prop.rs | 32 +++-------------- .../src/const_prop_lint.rs | 34 ++++++++----------- 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index a517997c1eb..69894b6f07b 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -5,7 +5,6 @@ self, compile_time_machine, AllocId, ConstAllocation, FnArg, Frame, ImmTy, InterpCx, InterpResult, OpTy, PlaceTy, Pointer, }; -use rustc_data_structures::fx::FxHashSet; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; @@ -49,16 +48,7 @@ fn add_args( throw_machine_stop!(Zst) }} -pub(crate) struct ConstPropMachine { - pub written_only_inside_own_block_locals: FxHashSet, - pub can_const_prop: IndexVec, -} - -impl ConstPropMachine { - pub fn new(can_const_prop: IndexVec) -> Self { - Self { written_only_inside_own_block_locals: Default::default(), can_const_prop } - } -} +pub(crate) struct ConstPropMachine; impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for ConstPropMachine { compile_time_machine!(<'mir, 'tcx>); @@ -132,23 +122,11 @@ fn binary_ptr_op( } fn before_access_local_mut<'a>( - ecx: &'a mut InterpCx<'mir, 'tcx, Self>, - frame: usize, - local: Local, + _ecx: &'a mut InterpCx<'mir, 'tcx, Self>, + _frame: usize, + _local: Local, ) -> InterpResult<'tcx> { - assert_eq!(frame, 0); - match ecx.machine.can_const_prop[local] { - ConstPropMode::NoPropagation => { - throw_machine_stop_str!( - "tried to write to a local that is marked as not propagatable" - ) - } - ConstPropMode::OnlyInsideOwnBlock => { - ecx.machine.written_only_inside_own_block_locals.insert(local); - } - ConstPropMode::FullConstProp => {} - } - Ok(()) + unreachable!() } fn before_access_global( diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index c1f2447784d..f2612987c6a 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -5,6 +5,7 @@ use rustc_const_eval::interpret::{ImmTy, Projectable}; use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar}; +use rustc_data_structures::fx::FxHashSet; use rustc_hir::def::DefKind; use rustc_hir::HirId; use rustc_index::bit_set::BitSet; @@ -76,6 +77,8 @@ struct ConstPropagator<'mir, 'tcx> { visited_blocks: BitSet, locals: IndexVec>, body: &'mir Body<'tcx>, + written_only_inside_own_block_locals: FxHashSet, + can_const_prop: IndexVec, } #[derive(Debug, Clone)] @@ -181,12 +184,7 @@ fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> let param_env = tcx.param_env_reveal_all_normalized(def_id); let can_const_prop = CanConstProp::check(tcx, param_env, body); - let ecx = InterpCx::new( - tcx, - tcx.def_span(def_id), - param_env, - ConstPropMachine::new(can_const_prop), - ); + let ecx = InterpCx::new(tcx, tcx.def_span(def_id), param_env, ConstPropMachine); ConstPropagator { ecx, @@ -196,6 +194,8 @@ fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> visited_blocks: BitSet::new_empty(body.basic_blocks.len()), locals: IndexVec::from_elem_n(Value::Uninit, body.local_decls.len()), body, + can_const_prop, + written_only_inside_own_block_locals: Default::default(), } } @@ -212,14 +212,14 @@ fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> { /// but not reading from them anymore. fn remove_const(&mut self, local: Local) { self.locals[local] = Value::Uninit; - self.ecx.machine.written_only_inside_own_block_locals.remove(&local); + self.written_only_inside_own_block_locals.remove(&local); } fn access_mut(&mut self, place: &Place<'_>) -> Option<&mut Value<'tcx>> { - match self.ecx.machine.can_const_prop[place.local] { + match self.can_const_prop[place.local] { ConstPropMode::NoPropagation => return None, ConstPropMode::OnlyInsideOwnBlock => { - self.ecx.machine.written_only_inside_own_block_locals.insert(place.local); + self.written_only_inside_own_block_locals.insert(place.local); } ConstPropMode::FullConstProp => {} } @@ -775,7 +775,7 @@ fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: let Some(()) = self.check_rvalue(rvalue, location) else { return }; - match self.ecx.machine.can_const_prop[place.local] { + match self.can_const_prop[place.local] { // Do nothing if the place is indirect. _ if place.is_indirect() => {} ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local), @@ -811,7 +811,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { match statement.kind { StatementKind::SetDiscriminant { ref place, variant_index } => { - match self.ecx.machine.can_const_prop[place.local] { + match self.can_const_prop[place.local] { // Do nothing if the place is indirect. _ if place.is_indirect() => {} ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local), @@ -878,7 +878,7 @@ fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'t // which were modified in the current block. // Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`. let mut written_only_inside_own_block_locals = - std::mem::take(&mut self.ecx.machine.written_only_inside_own_block_locals); + std::mem::take(&mut self.written_only_inside_own_block_locals); // This loop can get very hot for some bodies: it check each local in each bb. // To avoid this quadratic behaviour, we only clear the locals that were modified inside @@ -886,17 +886,13 @@ fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'t // The order in which we remove consts does not matter. #[allow(rustc::potential_query_instability)] for local in written_only_inside_own_block_locals.drain() { - debug_assert_eq!( - self.ecx.machine.can_const_prop[local], - ConstPropMode::OnlyInsideOwnBlock - ); + debug_assert_eq!(self.can_const_prop[local], ConstPropMode::OnlyInsideOwnBlock); self.remove_const(local); } - self.ecx.machine.written_only_inside_own_block_locals = - written_only_inside_own_block_locals; + self.written_only_inside_own_block_locals = written_only_inside_own_block_locals; if cfg!(debug_assertions) { - for (local, &mode) in self.ecx.machine.can_const_prop.iter_enumerated() { + for (local, &mode) in self.can_const_prop.iter_enumerated() { match mode { ConstPropMode::FullConstProp => {} ConstPropMode::NoPropagation | ConstPropMode::OnlyInsideOwnBlock => {