Skip to content

Commit da3dd71

Browse files
committed
Go back to just passing MemoryExtra to the machine-level allocation hooks
This is needed to avoid doing unnecessary global alloc_map lookups
1 parent 7348d2a commit da3dd71

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/librustc_mir/const_eval.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::interpret::{self,
2323
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar,
2424
RawConst, ConstValue,
2525
InterpResult, InterpErrorInfo, InterpError, GlobalId, InterpretCx, StackPopCleanup,
26-
Allocation, AllocId, MemoryKind, Memory,
26+
Allocation, AllocId, MemoryKind,
2727
snapshot, RefTracking, intern_const_alloc_recursive,
2828
};
2929

@@ -406,7 +406,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
406406

407407
#[inline(always)]
408408
fn tag_allocation<'b>(
409-
_memory: &Memory<'mir, 'tcx, Self>,
409+
_memory_extra: &(),
410410
_id: AllocId,
411411
alloc: Cow<'b, Allocation>,
412412
_kind: Option<MemoryKind<!>>,
@@ -417,7 +417,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
417417

418418
#[inline(always)]
419419
fn tag_static_base_pointer(
420-
_memory: &Memory<'mir, 'tcx, Self>,
420+
_memory_extra: &(),
421421
_id: AllocId,
422422
) -> Self::PointerTag {
423423
()

src/librustc_mir/interpret/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
174174
/// For static allocations, the tag returned must be the same as the one returned by
175175
/// `tag_static_base_pointer`.
176176
fn tag_allocation<'b>(
177-
memory: &Memory<'mir, 'tcx, Self>,
177+
memory_extra: &Self::MemoryExtra,
178178
id: AllocId,
179179
alloc: Cow<'b, Allocation>,
180180
kind: Option<MemoryKind<Self::MemoryKinds>>,
@@ -186,7 +186,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
186186
/// Be aware that requesting the `Allocation` for that `id` will lead to cycles
187187
/// for cyclic statics!
188188
fn tag_static_base_pointer(
189-
memory: &Memory<'mir, 'tcx, Self>,
189+
memory_extra: &Self::MemoryExtra,
190190
id: AllocId,
191191
) -> Self::PointerTag;
192192

src/librustc_mir/interpret/memory.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
117117

118118
#[inline]
119119
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
120-
ptr.with_tag(M::tag_static_base_pointer(&self, ptr.alloc_id))
120+
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
121121
}
122122

123123
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> Pointer<M::PointerTag> {
@@ -150,7 +150,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
150150
kind: MemoryKind<M::MemoryKinds>,
151151
) -> Pointer<M::PointerTag> {
152152
let id = self.tcx.alloc_map.lock().reserve();
153-
let (alloc, tag) = M::tag_allocation(&self, id, Cow::Owned(alloc), Some(kind));
153+
let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind));
154154
self.alloc_map.insert(id, (kind, alloc.into_owned()));
155155
Pointer::from(id).with_tag(tag)
156156
}
@@ -365,9 +365,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
365365
/// contains a reference to memory that was created during its evaluation (i.e., not to
366366
/// another static), those inner references only exist in "resolved" form.
367367
fn get_static_alloc(
368-
id: AllocId,
368+
memory_extra: &M::MemoryExtra,
369369
tcx: TyCtxtAt<'tcx>,
370-
memory: &Memory<'mir, 'tcx, M>,
370+
id: AllocId,
371371
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
372372
let alloc = tcx.alloc_map.lock().get(id);
373373
let alloc = match alloc {
@@ -391,7 +391,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
391391
};
392392
// use the raw query here to break validation cycles. Later uses of the static
393393
// will call the full query anyway
394-
let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))
394+
let raw_const = tcx.tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))
395395
.map_err(|err| {
396396
// no need to report anything, the const_eval call takes care of that
397397
// for statics
@@ -411,7 +411,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
411411
// We got tcx memory. Let the machine figure out whether and how to
412412
// turn that into memory with the right pointer tag.
413413
Ok(M::tag_allocation(
414-
memory,
414+
memory_extra,
415415
id, // always use the ID we got as input, not the "hidden" one.
416416
alloc,
417417
M::STATIC_KIND.map(MemoryKind::Machine),
@@ -427,7 +427,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
427427
// `get_static_alloc` that we can actually use directly without inserting anything anywhere.
428428
// So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
429429
let a = self.alloc_map.get_or(id, || {
430-
let alloc = Self::get_static_alloc(id, self.tcx, &self).map_err(Err)?;
430+
let alloc = Self::get_static_alloc(&self.extra, self.tcx, id).map_err(Err)?;
431431
match alloc {
432432
Cow::Borrowed(alloc) => {
433433
// We got a ref, cheaply return that as an "error" so that the
@@ -456,11 +456,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
456456
id: AllocId,
457457
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
458458
let tcx = self.tcx;
459-
let alloc = Self::get_static_alloc(id, tcx, &self);
459+
let memory_extra = &self.extra;
460460
let a = self.alloc_map.get_mut_or(id, || {
461461
// Need to make a copy, even if `get_static_alloc` is able
462462
// to give us a cheap reference.
463-
let alloc = alloc?;
463+
let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
464464
if alloc.mutability == Mutability::Immutable {
465465
return err!(ModifiedConstantMemory);
466466
}

0 commit comments

Comments
 (0)