Skip to content

Commit a0a7eba

Browse files
committed
Const prop doesn't need a stack anymore
1 parent 309e520 commit a0a7eba

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,18 @@ pub(crate) macro throw_machine_stop_str($($tt:tt)*) {{
4949
throw_machine_stop!(Zst)
5050
}}
5151

52-
pub(crate) struct ConstPropMachine<'mir, 'tcx> {
53-
/// The virtual call stack.
54-
stack: Vec<Frame<'mir, 'tcx>>,
52+
pub(crate) struct ConstPropMachine {
5553
pub written_only_inside_own_block_locals: FxHashSet<Local>,
5654
pub can_const_prop: IndexVec<Local, ConstPropMode>,
5755
}
5856

59-
impl ConstPropMachine<'_, '_> {
57+
impl ConstPropMachine {
6058
pub fn new(can_const_prop: IndexVec<Local, ConstPropMode>) -> Self {
61-
Self {
62-
stack: Vec::new(),
63-
written_only_inside_own_block_locals: Default::default(),
64-
can_const_prop,
65-
}
59+
Self { written_only_inside_own_block_locals: Default::default(), can_const_prop }
6660
}
6761
}
6862

69-
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> {
63+
impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
7064
compile_time_machine!(<'mir, 'tcx>);
7165

7266
const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small (see `MAX_ALLOC_LIMIT`)
@@ -192,16 +186,16 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
192186

193187
#[inline(always)]
194188
fn stack<'a>(
195-
ecx: &'a InterpCx<'mir, 'tcx, Self>,
189+
_ecx: &'a InterpCx<'mir, 'tcx, Self>,
196190
) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>] {
197-
&ecx.machine.stack
191+
&[]
198192
}
199193

200194
#[inline(always)]
201195
fn stack_mut<'a>(
202-
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
196+
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
203197
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>> {
204-
&mut ecx.machine.stack
198+
unreachable!()
205199
}
206200
}
207201

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
44
use std::fmt::Debug;
55

6-
use rustc_const_eval::interpret::{ImmTy, MPlaceTy, Projectable};
7-
use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar, StackPopCleanup};
6+
use rustc_const_eval::interpret::{ImmTy, Projectable};
7+
use rustc_const_eval::interpret::{InterpCx, InterpResult, OpTy, Scalar};
88
use rustc_hir::def::DefKind;
99
use rustc_hir::HirId;
1010
use rustc_index::bit_set::BitSet;
1111
use rustc_index::{Idx, IndexVec};
1212
use rustc_middle::mir::visit::Visitor;
1313
use rustc_middle::mir::*;
1414
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
15-
use rustc_middle::ty::GenericArgs;
16-
use rustc_middle::ty::{
17-
self, ConstInt, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, TypeVisitableExt,
18-
};
15+
use rustc_middle::ty::{self, ConstInt, ParamEnv, ScalarInt, Ty, TyCtxt, TypeVisitableExt};
1916
use rustc_span::Span;
2017
use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
2118

@@ -72,12 +69,13 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
7269

7370
/// Finds optimization opportunities on the MIR.
7471
struct ConstPropagator<'mir, 'tcx> {
75-
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
72+
ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
7673
tcx: TyCtxt<'tcx>,
7774
param_env: ParamEnv<'tcx>,
7875
worklist: Vec<BasicBlock>,
7976
visited_blocks: BitSet<BasicBlock>,
8077
locals: IndexVec<Local, Value<'tcx>>,
78+
body: &'mir Body<'tcx>,
8179
}
8280

8381
#[derive(Debug, Clone)]
@@ -180,43 +178,29 @@ impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
180178
impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
181179
fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
182180
let def_id = body.source.def_id();
183-
let args = &GenericArgs::identity_for_item(tcx, def_id);
184181
let param_env = tcx.param_env_reveal_all_normalized(def_id);
185182

186183
let can_const_prop = CanConstProp::check(tcx, param_env, body);
187-
let mut ecx = InterpCx::new(
184+
let ecx = InterpCx::new(
188185
tcx,
189186
tcx.def_span(def_id),
190187
param_env,
191188
ConstPropMachine::new(can_const_prop),
192189
);
193190

194-
let ret = MPlaceTy::fake_alloc_zst(ecx.layout_of(tcx.types.unit).unwrap()).into();
195-
196-
ecx.push_stack_frame(
197-
Instance::new(def_id, args),
198-
body,
199-
&ret,
200-
StackPopCleanup::Root { cleanup: false },
201-
)
202-
.expect("failed to push initial stack frame");
203-
204191
ConstPropagator {
205192
ecx,
206193
tcx,
207194
param_env,
208195
worklist: vec![START_BLOCK],
209196
visited_blocks: BitSet::new_empty(body.basic_blocks.len()),
210197
locals: IndexVec::from_elem_n(Value::Uninit, body.local_decls.len()),
198+
body,
211199
}
212200
}
213201

214-
fn body(&self) -> &'mir Body<'tcx> {
215-
self.ecx.frame().body
216-
}
217-
218202
fn local_decls(&self) -> &'mir LocalDecls<'tcx> {
219-
&self.body().local_decls
203+
&self.body.local_decls
220204
}
221205

222206
fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> {
@@ -243,7 +227,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
243227
}
244228

245229
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
246-
source_info.scope.lint_root(&self.body().source_scopes)
230+
source_info.scope.lint_root(&self.body.source_scopes)
247231
}
248232

249233
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
@@ -332,7 +316,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
332316
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
333317
// appropriate to use.
334318
assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
335-
let source_info = self.body().source_info(location);
319+
let source_info = self.body.source_info(location);
336320
self.report_assert_as_lint(
337321
source_info,
338322
AssertLint::ArithmeticOverflow(
@@ -370,7 +354,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
370354
let r_bits = r.to_scalar().to_bits(right_size).ok();
371355
if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
372356
debug!("check_binary_op: reporting assert for {:?}", location);
373-
let source_info = self.body().source_info(location);
357+
let source_info = self.body.source_info(location);
374358
let panic = AssertKind::Overflow(
375359
op,
376360
match l {
@@ -398,7 +382,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
398382
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
399383
Ok(overflow)
400384
})? {
401-
let source_info = self.body().source_info(location);
385+
let source_info = self.body.source_info(location);
402386
self.report_assert_as_lint(
403387
source_info,
404388
AssertLint::ArithmeticOverflow(
@@ -545,7 +529,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
545529
// Need proper const propagator for these.
546530
_ => return None,
547531
};
548-
let source_info = self.body().source_info(location);
532+
let source_info = self.body.source_info(location);
549533
self.report_assert_as_lint(
550534
source_info,
551535
AssertLint::UnconditionalPanic(source_info.span, msg),
@@ -564,7 +548,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
564548
.layout_of(self.local_decls()[local].ty)
565549
.map_or(true, |layout| layout.is_zst()),
566550
"failed to remove values for `{local:?}`, value={val:?}: {:#?}",
567-
self.body(),
551+
self.body,
568552
)
569553
}
570554
}
@@ -580,7 +564,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
580564
return None;
581565
}
582566
use rustc_middle::mir::Rvalue::*;
583-
let layout = self.use_ecx(|this| this.ecx.eval_place(*dest))?.layout;
567+
let layout = self.ecx.layout_of(dest.ty(self.body, self.tcx).ty).ok()?;
584568
trace!(?layout);
585569

586570
let val: Value<'_> = match *rvalue {

0 commit comments

Comments
 (0)