Skip to content

Commit 738ebcf

Browse files
author
Vytautas Astrauskas
committed
Remove now unnecessary resolve_maybe_global_alloc.
1 parent 9e46807 commit 738ebcf

File tree

2 files changed

+17
-43
lines changed

2 files changed

+17
-43
lines changed

src/librustc_mir/interpret/machine.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::{Borrow, Cow};
66
use std::hash::Hash;
77

88
use rustc_middle::mir;
9-
use rustc_middle::ty::{self, query::TyCtxtAt, Ty};
9+
use rustc_middle::ty::{self, Ty};
1010
use rustc_span::def_id::DefId;
1111

1212
use super::{
@@ -230,10 +230,11 @@ pub trait Machine<'mir, 'tcx>: Sized {
230230
id
231231
}
232232

233-
/// Called when converting a `ty::Const` to an operand.
233+
/// Called when converting a `ty::Const` to an operand (in
234+
/// `eval_const_to_op`).
234235
///
235-
/// Miri uses this callback for creating unique allocation ids for thread
236-
/// locals. In Rust, one way for creating a thread local is by marking a
236+
/// Miri uses this callback for creating per thread allocations for thread
237+
/// locals. In Rust, one way of creating a thread local is by marking a
237238
/// static with `#[thread_local]`. On supported platforms this gets
238239
/// translated to a LLVM thread local for which LLVM automatically ensures
239240
/// that each thread gets its own copy. Since LLVM automatically handles
@@ -243,11 +244,12 @@ pub trait Machine<'mir, 'tcx>: Sized {
243244
/// plan is to change MIR to make accesses to thread locals explicit
244245
/// (https://github.com/rust-lang/rust/issues/70685). While the issue 70685
245246
/// is not fixed, our current workaround in Miri is to use this function to
246-
/// reserve fresh allocation ids for each thread. Please note that here we
247-
/// only **reserve** the allocation ids; the actual allocation for the
248-
/// thread local statics is done in `Memory::get_global_alloc`, which uses
249-
/// `resolve_maybe_global_alloc` to retrieve information about the
250-
/// allocation id we generated.
247+
/// make per-thread copies of thread locals. Please note that we cannot make
248+
/// these copies in `canonical_alloc_id` because that is too late: for
249+
/// example, if one created a pointer in thread `t1` to a thread local and
250+
/// sent it to another thread `t2`, resolving the access in
251+
/// `canonical_alloc_id` would result in pointer pointing to `t2`'s thread
252+
/// local and not `t1` as it should.
251253
#[inline]
252254
fn eval_maybe_thread_local_static_const(
253255
_ecx: &InterpCx<'mir, 'tcx, Self>,
@@ -256,22 +258,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
256258
Ok(val)
257259
}
258260

259-
/// Called to obtain the `GlobalAlloc` associated with the allocation id.
260-
///
261-
/// Miri uses this callback to resolve the information about the original
262-
/// thread local static for which `canonical_alloc_id` reserved a fresh
263-
/// allocation id. Since `canonical_alloc_id` does not create the actual
264-
/// allocation and the reserved allocation id has no reference to its
265-
/// parent, we need to ask Miri to retrieve information for us.
266-
#[inline(always)]
267-
fn resolve_maybe_global_alloc(
268-
tcx: TyCtxtAt<'tcx>,
269-
_memory_extra: &Self::MemoryExtra,
270-
id: AllocId,
271-
) -> Option<mir::interpret::GlobalAlloc<'tcx>> {
272-
tcx.alloc_map.lock().get(id)
273-
}
274-
275261
/// Called to initialize the "extra" state of an allocation and make the pointers
276262
/// it contains (in relocations) tagged. The way we construct allocations is
277263
/// to always first construct it without extra and then add the extra.

src/librustc_mir/interpret/memory.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
429429
id: AllocId,
430430
is_write: bool,
431431
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
432-
// The call to `resolve_maybe_global_alloc` is needed to enable Miri to
433-
// support thread local statics. In
434-
// `M::eval_maybe_thread_local_static_const`, for a thread local static,
435-
// Miri reserves a fresh allocation id, but the actual allocation is
436-
// left to the code that handles statics which calls this function
437-
// (`get_global_alloc`). Since the allocation id is fresh, it has no
438-
// information about the original static. The call to
439-
// `resolve_maybe_global_alloc` allows Miri to retrieve this information
440-
// for us.
441-
let (alloc, def_id) = match M::resolve_maybe_global_alloc(tcx, memory_extra, id) {
432+
let alloc = tcx.alloc_map.lock().get(id);
433+
let (alloc, def_id) = match alloc {
442434
Some(GlobalAlloc::Memory(mem)) => {
443435
// Memory of a constant or promoted or anonymous memory referenced by a static.
444436
(mem, None)
@@ -598,14 +590,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
598590
}
599591

600592
// # Statics
601-
// The call to `resolve_maybe_global_alloc` is needed here because Miri
602-
// via the callback to `eval_maybe_thread_local_static_const` in
603-
// `eval_const_to_op` reserves fresh allocation ids for thread local
604-
// statics. However, the actual allocation is done not in
605-
// `resolve_maybe_global_alloc`, but in `get_raw` and `get_raw_mut`.
606-
// Since this function may get called before `get_raw`, we need to allow
607-
// Miri to retrieve the information about the static for us.
608-
match M::resolve_maybe_global_alloc(self.tcx, &self.extra, id) {
593+
// Can't do this in the match argument, we may get cycle errors since the lock would
594+
// be held throughout the match.
595+
let alloc = self.tcx.alloc_map.lock().get(id);
596+
match alloc {
609597
Some(GlobalAlloc::Static(did)) => {
610598
// Use size and align of the type.
611599
let ty = self.tcx.type_of(did);

0 commit comments

Comments
 (0)