Skip to content

Commit 7ad6b33

Browse files
committed
Remove ConstPropMachine and re-use the DummyMachine instead
1 parent 44d699f commit 7ad6b33

File tree

2 files changed

+4
-141
lines changed

2 files changed

+4
-141
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

+1-138
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
//! Propagates constants for early reporting of statically known
22
//! assertion failures
33
4-
use rustc_const_eval::interpret::{
5-
self, compile_time_machine, AllocId, ConstAllocation, FnArg, Frame, ImmTy, InterpCx,
6-
InterpResult, OpTy, PlaceTy, Pointer,
7-
};
84
use rustc_index::bit_set::BitSet;
95
use rustc_index::IndexVec;
106
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
117
use rustc_middle::mir::*;
12-
use rustc_middle::query::TyCtxtAt;
13-
use rustc_middle::ty::layout::TyAndLayout;
14-
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
15-
use rustc_span::def_id::DefId;
8+
use rustc_middle::ty::{ParamEnv, TyCtxt};
169
use rustc_target::abi::Size;
17-
use rustc_target::spec::abi::Abi as CallAbi;
1810

1911
/// The maximum number of bytes that we'll allocate space for a local or the return value.
2012
/// Needed for #66397, because otherwise we eval into large places and that can cause OOM or just
@@ -48,135 +40,6 @@ pub(crate) macro throw_machine_stop_str($($tt:tt)*) {{
4840
throw_machine_stop!(Zst)
4941
}}
5042

51-
pub(crate) struct ConstPropMachine;
52-
53-
impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
54-
compile_time_machine!(<'mir, 'tcx>);
55-
56-
const PANIC_ON_ALLOC_FAIL: bool = true; // all allocations are small (see `MAX_ALLOC_LIMIT`)
57-
58-
const POST_MONO_CHECKS: bool = false; // this MIR is still generic!
59-
60-
type MemoryKind = !;
61-
62-
#[inline(always)]
63-
fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
64-
false // no reason to enforce alignment
65-
}
66-
67-
#[inline(always)]
68-
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>, _layout: TyAndLayout<'tcx>) -> bool {
69-
false // for now, we don't enforce validity
70-
}
71-
72-
fn load_mir(
73-
_ecx: &InterpCx<'mir, 'tcx, Self>,
74-
_instance: ty::InstanceDef<'tcx>,
75-
) -> InterpResult<'tcx, &'tcx Body<'tcx>> {
76-
throw_machine_stop_str!("calling functions isn't supported in ConstProp")
77-
}
78-
79-
fn panic_nounwind(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: &str) -> InterpResult<'tcx> {
80-
throw_machine_stop_str!("panicking isn't supported in ConstProp")
81-
}
82-
83-
fn find_mir_or_eval_fn(
84-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
85-
_instance: ty::Instance<'tcx>,
86-
_abi: CallAbi,
87-
_args: &[FnArg<'tcx>],
88-
_destination: &PlaceTy<'tcx>,
89-
_target: Option<BasicBlock>,
90-
_unwind: UnwindAction,
91-
) -> InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> {
92-
Ok(None)
93-
}
94-
95-
fn call_intrinsic(
96-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
97-
_instance: ty::Instance<'tcx>,
98-
_args: &[OpTy<'tcx>],
99-
_destination: &PlaceTy<'tcx>,
100-
_target: Option<BasicBlock>,
101-
_unwind: UnwindAction,
102-
) -> InterpResult<'tcx> {
103-
throw_machine_stop_str!("calling intrinsics isn't supported in ConstProp")
104-
}
105-
106-
fn assert_panic(
107-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
108-
_msg: &rustc_middle::mir::AssertMessage<'tcx>,
109-
_unwind: rustc_middle::mir::UnwindAction,
110-
) -> InterpResult<'tcx> {
111-
bug!("panics terminators are not evaluated in ConstProp")
112-
}
113-
114-
fn binary_ptr_op(
115-
_ecx: &InterpCx<'mir, 'tcx, Self>,
116-
_bin_op: BinOp,
117-
_left: &ImmTy<'tcx>,
118-
_right: &ImmTy<'tcx>,
119-
) -> InterpResult<'tcx, (ImmTy<'tcx>, bool)> {
120-
// We can't do this because aliasing of memory can differ between const eval and llvm
121-
throw_machine_stop_str!("pointer arithmetic or comparisons aren't supported in ConstProp")
122-
}
123-
124-
fn before_access_local_mut<'a>(
125-
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
126-
_frame: usize,
127-
_local: Local,
128-
) -> InterpResult<'tcx> {
129-
unreachable!()
130-
}
131-
132-
fn before_access_global(
133-
_tcx: TyCtxtAt<'tcx>,
134-
_machine: &Self,
135-
_alloc_id: AllocId,
136-
alloc: ConstAllocation<'tcx>,
137-
_static_def_id: Option<DefId>,
138-
is_write: bool,
139-
) -> InterpResult<'tcx> {
140-
if is_write {
141-
throw_machine_stop_str!("can't write to global");
142-
}
143-
// If the static allocation is mutable, then we can't const prop it as its content
144-
// might be different at runtime.
145-
if alloc.inner().mutability.is_mut() {
146-
throw_machine_stop_str!("can't access mutable globals in ConstProp");
147-
}
148-
149-
Ok(())
150-
}
151-
152-
#[inline(always)]
153-
fn expose_ptr(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx> {
154-
throw_machine_stop_str!("exposing pointers isn't supported in ConstProp")
155-
}
156-
157-
#[inline(always)]
158-
fn init_frame_extra(
159-
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
160-
frame: Frame<'mir, 'tcx>,
161-
) -> InterpResult<'tcx, Frame<'mir, 'tcx>> {
162-
Ok(frame)
163-
}
164-
165-
#[inline(always)]
166-
fn stack<'a>(
167-
_ecx: &'a InterpCx<'mir, 'tcx, Self>,
168-
) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>] {
169-
&[]
170-
}
171-
172-
#[inline(always)]
173-
fn stack_mut<'a>(
174-
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
175-
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>> {
176-
unreachable!()
177-
}
178-
}
179-
18043
/// The mode that `ConstProp` is allowed to run in for a given `Local`.
18144
#[derive(Clone, Copy, Debug, PartialEq)]
18245
pub enum ConstPropMode {

compiler/rustc_mir_transform/src/const_prop_lint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_span::Span;
1818
use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
1919

2020
use crate::const_prop::CanConstProp;
21-
use crate::const_prop::ConstPropMachine;
2221
use crate::const_prop::ConstPropMode;
22+
use crate::dataflow_const_prop::DummyMachine;
2323
use crate::errors::{AssertLint, AssertLintKind};
2424
use crate::MirLint;
2525

@@ -70,7 +70,7 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
7070

7171
/// Finds optimization opportunities on the MIR.
7272
struct ConstPropagator<'mir, 'tcx> {
73-
ecx: InterpCx<'mir, 'tcx, ConstPropMachine>,
73+
ecx: InterpCx<'mir, 'tcx, DummyMachine>,
7474
tcx: TyCtxt<'tcx>,
7575
param_env: ParamEnv<'tcx>,
7676
worklist: Vec<BasicBlock>,
@@ -184,7 +184,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
184184
let param_env = tcx.param_env_reveal_all_normalized(def_id);
185185

186186
let can_const_prop = CanConstProp::check(tcx, param_env, body);
187-
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), param_env, ConstPropMachine);
187+
let ecx = InterpCx::new(tcx, tcx.def_span(def_id), param_env, DummyMachine);
188188

189189
ConstPropagator {
190190
ecx,

0 commit comments

Comments
 (0)