Skip to content

Commit 8c406dc

Browse files
committed
Allow miri allocation interning to work im generic Machines
This is necessary so that the `ComstPropMachine` can intern allocations.
1 parent f1b882b commit 8c406dc

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
458458
}
459459
}
460460

461-
#[derive(Clone, Copy, Eq, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
461+
#[derive(Clone, Copy, Eq, PartialEq, RustcEncodable, RustcDecodable, HashStable, Hash)]
462462
pub enum ScalarMaybeUndef<Tag = (), Id = AllocId> {
463463
Scalar(Scalar<Tag, Id>),
464464
Undef,

src/librustc_mir/interpret/intern.rs

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@
33
//! After a const evaluation has computed a value, before we destroy the const evaluator's session
44
//! memory, we need to extract all memory allocations to the global memory pool so they stay around.
55
6-
use rustc::ty::{Ty, self};
7-
use rustc::mir::interpret::{InterpResult, ErrorHandled};
8-
use rustc::hir;
96
use super::validity::RefTracking;
10-
use rustc_data_structures::fx::FxHashSet;
7+
use rustc::hir;
8+
use rustc::mir::interpret::{ErrorHandled, InterpResult};
9+
use rustc::ty::{self, Ty};
10+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1111

1212
use syntax::ast::Mutability;
1313

1414
use super::{
15-
ValueVisitor, MemoryKind, AllocId, MPlaceTy, Scalar,
15+
AllocId, Allocation, InterpCx, Machine, MemoryKind, MPlaceTy, Scalar, ValueVisitor,
1616
};
17-
use crate::const_eval::{CompileTimeInterpreter, CompileTimeEvalContext};
1817

19-
struct InternVisitor<'rt, 'mir, 'tcx> {
18+
struct InternVisitor<'rt, 'mir, 'tcx, M>
19+
where
20+
M: Machine<
21+
'mir,
22+
'tcx,
23+
MemoryKinds = !,
24+
PointerTag = (),
25+
ExtraFnVal = !,
26+
FrameExtra = (),
27+
MemoryExtra = (),
28+
AllocExtra = (),
29+
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
30+
>,
31+
{
2032
/// The ectx from which we intern.
21-
ecx: &'rt mut CompileTimeEvalContext<'mir, 'tcx>,
33+
ecx: &'rt mut InterpCx<'mir, 'tcx, M>,
2234
/// Previously encountered safe references.
2335
ref_tracking: &'rt mut RefTracking<(MPlaceTy<'tcx>, Mutability, InternMode)>,
2436
/// A list of all encountered allocations. After type-based interning, we traverse this list to
@@ -58,18 +70,28 @@ struct IsStaticOrFn;
5870
/// `immutable` things might become mutable if `ty` is not frozen.
5971
/// `ty` can be `None` if there is no potential interior mutability
6072
/// to account for (e.g. for vtables).
61-
fn intern_shallow<'rt, 'mir, 'tcx>(
62-
ecx: &'rt mut CompileTimeEvalContext<'mir, 'tcx>,
73+
fn intern_shallow<'rt, 'mir, 'tcx, M>(
74+
ecx: &'rt mut InterpCx<'mir, 'tcx, M>,
6375
leftover_allocations: &'rt mut FxHashSet<AllocId>,
6476
mode: InternMode,
6577
alloc_id: AllocId,
6678
mutability: Mutability,
6779
ty: Option<Ty<'tcx>>,
68-
) -> InterpResult<'tcx, Option<IsStaticOrFn>> {
69-
trace!(
70-
"InternVisitor::intern {:?} with {:?}",
71-
alloc_id, mutability,
72-
);
80+
) -> InterpResult<'tcx, Option<IsStaticOrFn>>
81+
where
82+
M: Machine<
83+
'mir,
84+
'tcx,
85+
MemoryKinds = !,
86+
PointerTag = (),
87+
ExtraFnVal = !,
88+
FrameExtra = (),
89+
MemoryExtra = (),
90+
AllocExtra = (),
91+
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
92+
>,
93+
{
94+
trace!("InternVisitor::intern {:?} with {:?}", alloc_id, mutability,);
7395
// remove allocation
7496
let tcx = ecx.tcx;
7597
let (kind, mut alloc) = match ecx.memory.alloc_map.remove(&alloc_id) {
@@ -130,7 +152,20 @@ fn intern_shallow<'rt, 'mir, 'tcx>(
130152
Ok(None)
131153
}
132154

133-
impl<'rt, 'mir, 'tcx> InternVisitor<'rt, 'mir, 'tcx> {
155+
impl<'rt, 'mir, 'tcx, M> InternVisitor<'rt, 'mir, 'tcx, M>
156+
where
157+
M: Machine<
158+
'mir,
159+
'tcx,
160+
MemoryKinds = !,
161+
PointerTag = (),
162+
ExtraFnVal = !,
163+
FrameExtra = (),
164+
MemoryExtra = (),
165+
AllocExtra = (),
166+
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
167+
>,
168+
{
134169
fn intern_shallow(
135170
&mut self,
136171
alloc_id: AllocId,
@@ -148,15 +183,27 @@ impl<'rt, 'mir, 'tcx> InternVisitor<'rt, 'mir, 'tcx> {
148183
}
149184
}
150185

151-
impl<'rt, 'mir, 'tcx>
152-
ValueVisitor<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>
186+
impl<'rt, 'mir, 'tcx, M>
187+
ValueVisitor<'mir, 'tcx, M>
153188
for
154-
InternVisitor<'rt, 'mir, 'tcx>
189+
InternVisitor<'rt, 'mir, 'tcx, M>
190+
where
191+
M: Machine<
192+
'mir,
193+
'tcx,
194+
MemoryKinds = !,
195+
PointerTag = (),
196+
ExtraFnVal = !,
197+
FrameExtra = (),
198+
MemoryExtra = (),
199+
AllocExtra = (),
200+
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
201+
>,
155202
{
156203
type V = MPlaceTy<'tcx>;
157204

158205
#[inline(always)]
159-
fn ecx(&self) -> &CompileTimeEvalContext<'mir, 'tcx> {
206+
fn ecx(&self) -> &InterpCx<'mir, 'tcx, M> {
160207
&self.ecx
161208
}
162209

@@ -265,12 +312,25 @@ for
265312
}
266313
}
267314

268-
pub fn intern_const_alloc_recursive(
269-
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
315+
pub fn intern_const_alloc_recursive<M>(
316+
ecx: &mut InterpCx<'mir, 'tcx, M>,
270317
// The `mutability` of the place, ignoring the type.
271318
place_mut: Option<hir::Mutability>,
272319
ret: MPlaceTy<'tcx>,
273-
) -> InterpResult<'tcx> {
320+
) -> InterpResult<'tcx>
321+
where
322+
M: Machine<
323+
'mir,
324+
'tcx,
325+
MemoryKinds = !,
326+
PointerTag = (),
327+
ExtraFnVal = !,
328+
FrameExtra = (),
329+
MemoryExtra = (),
330+
AllocExtra = (),
331+
MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation)>,
332+
>,
333+
{
274334
let tcx = ecx.tcx;
275335
let (base_mutability, base_intern_mode) = match place_mut {
276336
Some(hir::Mutability::Immutable) => (Mutability::Immutable, InternMode::Static),

src/librustc_mir/interpret/operand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_macros::HashStable;
2727
/// operations and fat pointers. This idea was taken from rustc's codegen.
2828
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
2929
/// defined on `Immediate`, and do not have to work with a `Place`.
30-
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable)]
30+
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
3131
pub enum Immediate<Tag=(), Id=AllocId> {
3232
Scalar(ScalarMaybeUndef<Tag, Id>),
3333
ScalarPair(ScalarMaybeUndef<Tag, Id>, ScalarMaybeUndef<Tag, Id>),
@@ -104,7 +104,7 @@ impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
104104
/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
105105
/// or still in memory. The latter is an optimization, to delay reading that chunk of
106106
/// memory and to avoid having to store arbitrary-sized data here.
107-
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable)]
107+
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
108108
pub enum Operand<Tag=(), Id=AllocId> {
109109
Immediate(Immediate<Tag, Id>),
110110
Indirect(MemPlace<Tag, Id>),
@@ -134,7 +134,7 @@ impl<Tag> Operand<Tag> {
134134
}
135135
}
136136

137-
#[derive(Copy, Clone, Debug, PartialEq)]
137+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
138138
pub struct OpTy<'tcx, Tag=()> {
139139
op: Operand<Tag>, // Keep this private, it helps enforce invariants
140140
pub layout: TyLayout<'tcx>,

0 commit comments

Comments
 (0)