Skip to content

Commit 6ecb2aa

Browse files
committed
We're not really using the ConstPropMachine anymore
1 parent 89e6a67 commit 6ecb2aa

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 5 additions & 27 deletions
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

Lines changed: 15 additions & 19 deletions
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
}
@@ -775,7 +775,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
775775

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

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

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

883883
// This loop can get very hot for some bodies: it check each local in each bb.
884884
// To avoid this quadratic behaviour, we only clear the locals that were modified inside
885885
// the current block.
886886
// The order in which we remove consts does not matter.
887887
#[allow(rustc::potential_query_instability)]
888888
for local in written_only_inside_own_block_locals.drain() {
889-
debug_assert_eq!(
890-
self.ecx.machine.can_const_prop[local],
891-
ConstPropMode::OnlyInsideOwnBlock
892-
);
889+
debug_assert_eq!(self.can_const_prop[local], ConstPropMode::OnlyInsideOwnBlock);
893890
self.remove_const(local);
894891
}
895-
self.ecx.machine.written_only_inside_own_block_locals =
896-
written_only_inside_own_block_locals;
892+
self.written_only_inside_own_block_locals = written_only_inside_own_block_locals;
897893

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

0 commit comments

Comments
 (0)