We're not really using the ConstPropMachine anymore

This commit is contained in:
Oli Scherer 2024-01-05 17:26:59 +00:00
parent 89e6a67310
commit 6ecb2aa580
2 changed files with 20 additions and 46 deletions

View File

@ -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<Local>,
pub can_const_prop: IndexVec<Local, ConstPropMode>,
}
impl ConstPropMachine {
pub fn new(can_const_prop: IndexVec<Local, ConstPropMode>) -> 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(

View File

@ -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<BasicBlock>,
locals: IndexVec<Local, Value<'tcx>>,
body: &'mir Body<'tcx>,
written_only_inside_own_block_locals: FxHashSet<Local>,
can_const_prop: IndexVec<Local, ConstPropMode>,
}
#[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 => {