Skip to content

Commit 96ea142

Browse files
committed
make alloc_extra machine hook a bit nicer
1 parent c8743db commit 96ea142

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
457457
}
458458

459459
#[inline(always)]
460-
fn tag_allocation<'b>(
460+
fn init_allocation_extra<'b>(
461461
_memory_extra: &(),
462462
_id: AllocId,
463463
alloc: Cow<'b, Allocation>,
464464
_kind: Option<MemoryKind<!>>,
465-
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
465+
) -> Cow<'b, Allocation<Self::PointerTag>> {
466466
// We do not use a tag so we can just cheaply forward the allocation
467-
(alloc, ())
467+
alloc
468468
}
469469

470470
#[inline(always)]

src/librustc_mir/interpret/eval_context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
250250
self.memory.force_bits(scalar, size)
251251
}
252252

253+
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
254+
/// the *canonical* machine pointer to the allocation. This represents a *direct*
255+
/// access to that memory, as opposed to access through a pointer that was created
256+
/// by the program. Must never be used for derived (program-created) pointers!
253257
#[inline(always)]
254258
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
255259
self.memory.tag_static_base_pointer(ptr)

src/librustc_mir/interpret/machine.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,12 @@ pub trait Machine<'mir, 'tcx>: Sized {
240240
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
241241
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
242242
/// owned allocation to the map even when the map is shared.)
243-
///
244-
/// For static allocations, the tag returned must be the same as the one returned by
245-
/// `tag_static_base_pointer`.
246-
fn tag_allocation<'b>(
243+
fn init_allocation_extra<'b>(
247244
memory_extra: &Self::MemoryExtra,
248245
id: AllocId,
249246
alloc: Cow<'b, Allocation>,
250247
kind: Option<MemoryKind<Self::MemoryKinds>>,
251-
) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
248+
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
252249

253250
/// Return the "base" tag for the given static allocation: the one that is used for direct
254251
/// accesses to this static/const/fn allocation.

src/librustc_mir/interpret/memory.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
143143
}
144144
}
145145

146+
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
147+
/// the *canonical* machine pointer to the allocation. This represents a *direct*
148+
/// access to that memory, as opposed to access through a pointer that was created
149+
/// by the program. Must never be used for derived (program-created) pointers!
146150
#[inline]
147151
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
148152
ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
@@ -191,9 +195,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
191195
kind: MemoryKind<M::MemoryKinds>,
192196
) -> Pointer<M::PointerTag> {
193197
let id = self.tcx.alloc_map.lock().reserve();
194-
let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind));
198+
let alloc = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind));
195199
self.alloc_map.insert(id, (kind, alloc.into_owned()));
196-
Pointer::from(id).with_tag(tag)
200+
self.tag_static_base_pointer(Pointer::from(id))
197201
}
198202

199203
pub fn reallocate(
@@ -473,14 +477,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
473477
}
474478
}
475479
};
476-
// We got tcx memory. Let the machine figure out whether and how to
477-
// turn that into memory with the right pointer tag.
478-
Ok(M::tag_allocation(
480+
// We got tcx memory. Let the machine initialize its "extra" stuff.
481+
Ok(M::init_allocation_extra(
479482
memory_extra,
480483
id, // always use the ID we got as input, not the "hidden" one.
481484
alloc,
482485
M::STATIC_KIND.map(MemoryKind::Machine),
483-
).0)
486+
))
484487
}
485488

486489
/// Gives raw access to the `Allocation`, without bounds or alignment checks.

src/librustc_mir/transform/const_prop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine {
191191
}
192192

193193
#[inline(always)]
194-
fn tag_allocation<'b>(
194+
fn init_allocation_extra<'b>(
195195
_memory_extra: &(),
196196
_id: AllocId,
197197
alloc: Cow<'b, Allocation>,
198198
_kind: Option<MemoryKind<!>>,
199-
) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
199+
) -> Cow<'b, Allocation<Self::PointerTag>> {
200200
// We do not use a tag so we can just cheaply forward the allocation
201-
(alloc, ())
201+
alloc
202202
}
203203

204204
#[inline(always)]

0 commit comments

Comments
 (0)