Skip to content

Commit 99c1305

Browse files
committed
We're not really using the ConstPropMachine anymore
1 parent a0a7eba commit 99c1305

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+5-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_const_eval::interpret::{
55
self, compile_time_machine, AllocId, ConstAllocation, FnArg, Frame, ImmTy, InterpCx,
66
InterpResult, OpTy, PlaceTy, Pointer,
77
};
8-
use rustc_data_structures::fx::FxHashSet;
98
use rustc_index::bit_set::BitSet;
109
use rustc_index::IndexVec;
1110
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
@@ -49,16 +48,7 @@ pub(crate) macro throw_machine_stop_str($($tt:tt)*) {{
4948
throw_machine_stop!(Zst)
5049
}}
5150

52-
pub(crate) struct ConstPropMachine {
53-
pub written_only_inside_own_block_locals: FxHashSet<Local>,
54-
pub can_const_prop: IndexVec<Local, ConstPropMode>,
55-
}
56-
57-
impl ConstPropMachine {
58-
pub fn new(can_const_prop: IndexVec<Local, ConstPropMode>) -> Self {
59-
Self { written_only_inside_own_block_locals: Default::default(), can_const_prop }
60-
}
61-
}
51+
pub(crate) struct ConstPropMachine;
6252

6353
impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
6454
compile_time_machine!(<'mir, 'tcx>);
@@ -132,23 +122,11 @@ impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
132122
}
133123

134124
fn before_access_local_mut<'a>(
135-
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
136-
frame: usize,
137-
local: Local,
125+
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
126+
_frame: usize,
127+
_local: Local,
138128
) -> InterpResult<'tcx> {
139-
assert_eq!(frame, 0);
140-
match ecx.machine.can_const_prop[local] {
141-
ConstPropMode::NoPropagation => {
142-
throw_machine_stop_str!(
143-
"tried to write to a local that is marked as not propagatable"
144-
)
145-
}
146-
ConstPropMode::OnlyInsideOwnBlock => {
147-
ecx.machine.written_only_inside_own_block_locals.insert(local);
148-
}
149-
ConstPropMode::FullConstProp => {}
150-
}
151-
Ok(())
129+
unreachable!()
152130
}
153131

154132
fn before_access_global(

compiler/rustc_mir_transform/src/const_prop_lint.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt::Debug;
55

66
use rustc_const_eval::interpret::{ImmTy, Projectable};
77
use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar};
8+
use rustc_data_structures::fx::FxHashSet;
89
use rustc_hir::def::DefKind;
910
use rustc_hir::HirId;
1011
use rustc_index::bit_set::BitSet;
@@ -76,6 +77,8 @@ struct ConstPropagator<'mir, 'tcx> {
7677
visited_blocks: BitSet<BasicBlock>,
7778
locals: IndexVec<Local, Value<'tcx>>,
7879
body: &'mir Body<'tcx>,
80+
written_only_inside_own_block_locals: FxHashSet<Local>,
81+
can_const_prop: IndexVec<Local, ConstPropMode>,
7982
}
8083

8184
#[derive(Debug, Clone)]
@@ -181,12 +184,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
181184
let param_env = tcx.param_env_reveal_all_normalized(def_id);
182185

183186
let can_const_prop = CanConstProp::check(tcx, param_env, body);
184-
let ecx = InterpCx::new(
185-
tcx,
186-
tcx.def_span(def_id),
187-
param_env,
188-
ConstPropMachine::new(can_const_prop),
189-
);
187+
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), param_env, ConstPropMachine);
190188

191189
ConstPropagator {
192190
ecx,
@@ -196,6 +194,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
196194
visited_blocks: BitSet::new_empty(body.basic_blocks.len()),
197195
locals: IndexVec::from_elem_n(Value::Uninit, body.local_decls.len()),
198196
body,
197+
can_const_prop,
198+
written_only_inside_own_block_locals: Default::default(),
199199
}
200200
}
201201

@@ -212,14 +212,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
212212
/// but not reading from them anymore.
213213
fn remove_const(&mut self, local: Local) {
214214
self.locals[local] = Value::Uninit;
215-
self.ecx.machine.written_only_inside_own_block_locals.remove(&local);
215+
self.written_only_inside_own_block_locals.remove(&local);
216216
}
217217

218218
fn access_mut(&mut self, place: &Place<'_>) -> Option<&mut Value<'tcx>> {
219-
match self.ecx.machine.can_const_prop[place.local] {
219+
match self.can_const_prop[place.local] {
220220
ConstPropMode::NoPropagation => return None,
221221
ConstPropMode::OnlyInsideOwnBlock => {
222-
self.ecx.machine.written_only_inside_own_block_locals.insert(place.local);
222+
self.written_only_inside_own_block_locals.insert(place.local);
223223
}
224224
ConstPropMode::FullConstProp => {}
225225
}
@@ -776,7 +776,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
776776

777777
let Some(()) = self.check_rvalue(rvalue, location) else { return };
778778

779-
match self.ecx.machine.can_const_prop[place.local] {
779+
match self.can_const_prop[place.local] {
780780
// Do nothing if the place is indirect.
781781
_ if place.is_indirect() => {}
782782
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
@@ -812,7 +812,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
812812

813813
match statement.kind {
814814
StatementKind::SetDiscriminant { ref place, variant_index } => {
815-
match self.ecx.machine.can_const_prop[place.local] {
815+
match self.can_const_prop[place.local] {
816816
// Do nothing if the place is indirect.
817817
_ if place.is_indirect() => {}
818818
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
@@ -879,23 +879,19 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
879879
// which were modified in the current block.
880880
// Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`.
881881
let mut written_only_inside_own_block_locals =
882-
std::mem::take(&mut self.ecx.machine.written_only_inside_own_block_locals);
882+
std::mem::take(&mut self.written_only_inside_own_block_locals);
883883

884884
// This loop can get very hot for some bodies: it check each local in each bb.
885885
// To avoid this quadratic behaviour, we only clear the locals that were modified inside
886886
// the current block.
887887
for local in written_only_inside_own_block_locals.drain() {
888-
debug_assert_eq!(
889-
self.ecx.machine.can_const_prop[local],
890-
ConstPropMode::OnlyInsideOwnBlock
891-
);
888+
debug_assert_eq!(self.can_const_prop[local], ConstPropMode::OnlyInsideOwnBlock);
892889
self.remove_const(local);
893890
}
894-
self.ecx.machine.written_only_inside_own_block_locals =
895-
written_only_inside_own_block_locals;
891+
self.written_only_inside_own_block_locals = written_only_inside_own_block_locals;
896892

897893
if cfg!(debug_assertions) {
898-
for (local, &mode) in self.ecx.machine.can_const_prop.iter_enumerated() {
894+
for (local, &mode) in self.can_const_prop.iter_enumerated() {
899895
match mode {
900896
ConstPropMode::FullConstProp => {}
901897
ConstPropMode::NoPropagation | ConstPropMode::OnlyInsideOwnBlock => {

0 commit comments

Comments
 (0)