Skip to content

Commit 38ae8f3

Browse files
committed
Simplify the tcx.alloc_map API
1 parent 29630ce commit 38ae8f3

File tree

18 files changed

+71
-83
lines changed

18 files changed

+71
-83
lines changed

src/librustc_codegen_llvm/common.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
244244
}
245245
}
246246
Scalar::Ptr(ptr) => {
247-
let alloc_kind = self.tcx.alloc_map.lock().get(ptr.alloc_id);
248-
let base_addr = match alloc_kind {
247+
let base_addr = match self.tcx.get_global_alloc(ptr.alloc_id) {
249248
Some(GlobalAlloc::Memory(alloc)) => {
250249
let init = const_alloc_to_llvm(self, alloc);
251250
let value = match alloc.mutability {

src/librustc_codegen_ssa/mir/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
9292
_ => bug!("from_const: invalid ScalarPair layout: {:#?}", layout),
9393
};
9494
let a = Scalar::from(Pointer::new(
95-
bx.tcx().alloc_map.lock().create_memory_alloc(data),
95+
bx.tcx().create_memory_alloc(data),
9696
Size::from_bytes(start),
9797
));
9898
let a_llval = bx.scalar_to_backend(

src/librustc_middle/ich/impls_ty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
136136
ty::tls::with_opt(|tcx| {
137137
trace!("hashing {:?}", *self);
138138
let tcx = tcx.expect("can't hash AllocIds during hir lowering");
139-
let alloc_kind = tcx.alloc_map.lock().get(*self);
140-
alloc_kind.hash_stable(hcx, hasher);
139+
tcx.get_global_alloc(*self).hash_stable(hcx, hasher);
141140
});
142141
}
143142
}

src/librustc_middle/mir/interpret/mod.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
197197
tcx: TyCtxt<'tcx>,
198198
alloc_id: AllocId,
199199
) -> Result<(), E::Error> {
200-
let alloc: GlobalAlloc<'tcx> =
201-
tcx.alloc_map.lock().get(alloc_id).expect("no value for given alloc ID");
202-
match alloc {
200+
match tcx.get_global_alloc(alloc_id).expect("no value for given alloc ID") {
203201
GlobalAlloc::Memory(alloc) => {
204202
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
205203
AllocDiscriminant::Alloc.encode(encoder)?;
@@ -294,7 +292,7 @@ impl<'s> AllocDecodingSession<'s> {
294292
AllocDiscriminant::Alloc => {
295293
// If this is an allocation, we need to reserve an
296294
// `AllocId` so we can decode cyclic graphs.
297-
let alloc_id = decoder.tcx().alloc_map.lock().reserve();
295+
let alloc_id = decoder.tcx().reserve_alloc_id();
298296
*entry =
299297
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
300298
Some(alloc_id)
@@ -338,23 +336,23 @@ impl<'s> AllocDecodingSession<'s> {
338336
// We already have a reserved `AllocId`.
339337
let alloc_id = alloc_id.unwrap();
340338
trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
341-
decoder.tcx().alloc_map.lock().set_alloc_id_same_memory(alloc_id, alloc);
339+
decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc);
342340
Ok(alloc_id)
343341
}
344342
AllocDiscriminant::Fn => {
345343
assert!(alloc_id.is_none());
346344
trace!("creating fn alloc ID");
347345
let instance = ty::Instance::decode(decoder)?;
348346
trace!("decoded fn alloc instance: {:?}", instance);
349-
let alloc_id = decoder.tcx().alloc_map.lock().create_fn_alloc(instance);
347+
let alloc_id = decoder.tcx().create_fn_alloc(instance);
350348
Ok(alloc_id)
351349
}
352350
AllocDiscriminant::Static => {
353351
assert!(alloc_id.is_none());
354352
trace!("creating extern static alloc ID");
355353
let did = DefId::decode(decoder)?;
356354
trace!("decoded static def-ID: {:?}", did);
357-
let alloc_id = decoder.tcx().alloc_map.lock().create_static_alloc(did);
355+
let alloc_id = decoder.tcx().create_static_alloc(did);
358356
Ok(alloc_id)
359357
}
360358
}
@@ -400,12 +398,6 @@ impl<'tcx> AllocMap<'tcx> {
400398
pub fn new() -> Self {
401399
AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(0) }
402400
}
403-
404-
/// Obtains a new allocation ID that can be referenced but does not
405-
/// yet have an allocation backing it.
406-
///
407-
/// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
408-
/// an `AllocId` from a query.
409401
pub fn reserve(&mut self) -> AllocId {
410402
let next = self.next_id;
411403
self.next_id.0 = self.next_id.0.checked_add(1).expect(
@@ -415,34 +407,46 @@ impl<'tcx> AllocMap<'tcx> {
415407
);
416408
next
417409
}
410+
}
411+
412+
impl<'tcx> TyCtxt<'tcx> {
413+
/// Obtains a new allocation ID that can be referenced but does not
414+
/// yet have an allocation backing it.
415+
///
416+
/// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
417+
/// an `AllocId` from a query.
418+
pub fn reserve_alloc_id(&self) -> AllocId {
419+
self.alloc_map.lock().reserve()
420+
}
418421

419422
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
420423
/// Should only be used for function pointers and statics, we don't want
421424
/// to dedup IDs for "real" memory!
422-
fn reserve_and_set_dedup(&mut self, alloc: GlobalAlloc<'tcx>) -> AllocId {
425+
fn reserve_and_set_dedup(&self, alloc: GlobalAlloc<'tcx>) -> AllocId {
426+
let mut alloc_map = self.alloc_map.lock();
423427
match alloc {
424428
GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {}
425429
GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
426430
}
427-
if let Some(&alloc_id) = self.dedup.get(&alloc) {
431+
if let Some(&alloc_id) = alloc_map.dedup.get(&alloc) {
428432
return alloc_id;
429433
}
430-
let id = self.reserve();
434+
let id = alloc_map.reserve();
431435
debug!("creating alloc {:?} with id {}", alloc, id);
432-
self.alloc_map.insert(id, alloc.clone());
433-
self.dedup.insert(alloc, id);
436+
alloc_map.alloc_map.insert(id, alloc.clone());
437+
alloc_map.dedup.insert(alloc, id);
434438
id
435439
}
436440

437441
/// Generates an `AllocId` for a static or return a cached one in case this function has been
438442
/// called on the same static before.
439-
pub fn create_static_alloc(&mut self, static_id: DefId) -> AllocId {
443+
pub fn create_static_alloc(&self, static_id: DefId) -> AllocId {
440444
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
441445
}
442446

443447
/// Generates an `AllocId` for a function. Depending on the function type,
444448
/// this might get deduplicated or assigned a new ID each time.
445-
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> AllocId {
449+
pub fn create_fn_alloc(&self, instance: Instance<'tcx>) -> AllocId {
446450
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
447451
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
448452
// duplicated across crates.
@@ -456,8 +460,9 @@ impl<'tcx> AllocMap<'tcx> {
456460
});
457461
if is_generic {
458462
// Get a fresh ID.
459-
let id = self.reserve();
460-
self.alloc_map.insert(id, GlobalAlloc::Function(instance));
463+
let mut alloc_map = self.alloc_map.lock();
464+
let id = alloc_map.reserve();
465+
alloc_map.alloc_map.insert(id, GlobalAlloc::Function(instance));
461466
id
462467
} else {
463468
// Deduplicate.
@@ -470,8 +475,8 @@ impl<'tcx> AllocMap<'tcx> {
470475
/// Statics with identical content will still point to the same `Allocation`, i.e.,
471476
/// their data will be deduplicated through `Allocation` interning -- but they
472477
/// are different places in memory and as such need different IDs.
473-
pub fn create_memory_alloc(&mut self, mem: &'tcx Allocation) -> AllocId {
474-
let id = self.reserve();
478+
pub fn create_memory_alloc(&self, mem: &'tcx Allocation) -> AllocId {
479+
let id = self.reserve_alloc_id();
475480
self.set_alloc_id_memory(id, mem);
476481
id
477482
}
@@ -482,38 +487,38 @@ impl<'tcx> AllocMap<'tcx> {
482487
/// This function exists to allow const eval to detect the difference between evaluation-
483488
/// local dangling pointers and allocations in constants/statics.
484489
#[inline]
485-
pub fn get(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
486-
self.alloc_map.get(&id).cloned()
490+
pub fn get_global_alloc(&self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
491+
self.alloc_map.lock().alloc_map.get(&id).cloned()
487492
}
488493

489494
/// Panics if the `AllocId` does not refer to an `Allocation`
490495
pub fn unwrap_memory(&self, id: AllocId) -> &'tcx Allocation {
491-
match self.get(id) {
496+
match self.get_global_alloc(id) {
492497
Some(GlobalAlloc::Memory(mem)) => mem,
493498
_ => bug!("expected allocation ID {} to point to memory", id),
494499
}
495500
}
496501

497502
/// Panics if the `AllocId` does not refer to a function
498503
pub fn unwrap_fn(&self, id: AllocId) -> Instance<'tcx> {
499-
match self.get(id) {
504+
match self.get_global_alloc(id) {
500505
Some(GlobalAlloc::Function(instance)) => instance,
501506
_ => bug!("expected allocation ID {} to point to a function", id),
502507
}
503508
}
504509

505510
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
506511
/// call this function twice, even with the same `Allocation` will ICE the compiler.
507-
pub fn set_alloc_id_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
508-
if let Some(old) = self.alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
512+
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {
513+
if let Some(old) = self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
509514
bug!("tried to set allocation ID {}, but it was already existing as {:#?}", id, old);
510515
}
511516
}
512517

513518
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
514519
/// twice for the same `(AllocId, Allocation)` pair.
515-
fn set_alloc_id_same_memory(&mut self, id: AllocId, mem: &'tcx Allocation) {
516-
self.alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
520+
fn set_alloc_id_same_memory(&self, id: AllocId, mem: &'tcx Allocation) {
521+
self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
517522
}
518523
}
519524

src/librustc_middle/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ pub struct Constant<'tcx> {
24102410
impl Constant<'tcx> {
24112411
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
24122412
match self.literal.val.try_to_scalar() {
2413-
Some(Scalar::Ptr(ptr)) => match tcx.alloc_map.lock().get(ptr.alloc_id) {
2413+
Some(Scalar::Ptr(ptr)) => match tcx.get_global_alloc(ptr.alloc_id) {
24142414
Some(GlobalAlloc::Static(def_id)) => Some(def_id),
24152415
Some(_) => None,
24162416
None => {

src/librustc_middle/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ pub struct GlobalCtxt<'tcx> {
975975
allocation_interner: ShardedHashMap<&'tcx Allocation, ()>,
976976

977977
/// Stores memory for globals (statics/consts).
978-
pub alloc_map: Lock<interpret::AllocMap<'tcx>>,
978+
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
979979

980980
layout_interner: ShardedHashMap<&'tcx Layout, ()>,
981981

@@ -1013,7 +1013,7 @@ impl<'tcx> TyCtxt<'tcx> {
10131013
// Create an allocation that just contains these bytes.
10141014
let alloc = interpret::Allocation::from_byte_aligned_bytes(bytes);
10151015
let alloc = self.intern_const_alloc(alloc);
1016-
self.alloc_map.lock().create_memory_alloc(alloc)
1016+
self.create_memory_alloc(alloc)
10171017
}
10181018

10191019
pub fn intern_stability(self, stab: attr::Stability) -> &'tcx attr::Stability {

src/librustc_middle/ty/print/pretty.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,6 @@ pub trait PrettyPrinter<'tcx>:
956956
) => {
957957
let byte_str = self
958958
.tcx()
959-
.alloc_map
960-
.lock()
961959
.unwrap_memory(ptr.alloc_id)
962960
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
963961
.unwrap();
@@ -1021,10 +1019,7 @@ pub trait PrettyPrinter<'tcx>:
10211019
)?;
10221020
}
10231021
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1024-
let instance = {
1025-
let alloc_map = self.tcx().alloc_map.lock();
1026-
alloc_map.unwrap_fn(ptr.alloc_id)
1027-
};
1022+
let instance = self.tcx().unwrap_fn(ptr.alloc_id);
10281023
self = self.typed_value(
10291024
|this| this.print_value_path(instance.def_id(), instance.substs),
10301025
|this| this.print_type(ty),

src/librustc_middle/ty/relate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
549549
if a_val == b_val {
550550
Ok(ConstValue::Scalar(a_val))
551551
} else if let ty::FnPtr(_) = a.ty.kind {
552-
let alloc_map = tcx.alloc_map.lock();
553-
let a_instance = alloc_map.unwrap_fn(a_val.assert_ptr().alloc_id);
554-
let b_instance = alloc_map.unwrap_fn(b_val.assert_ptr().alloc_id);
552+
let a_instance = tcx.unwrap_fn(a_val.assert_ptr().alloc_id);
553+
let b_instance = tcx.unwrap_fn(b_val.assert_ptr().alloc_id);
555554
if a_instance == b_instance {
556555
Ok(ConstValue::Scalar(a_val))
557556
} else {

src/librustc_mir/const_eval/eval_queries.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub(super) fn op_to_const<'tcx>(
130130

131131
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
132132
Scalar::Ptr(ptr) => {
133-
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
133+
let alloc = ecx.tcx.unwrap_memory(ptr.alloc_id);
134134
ConstValue::ByRef { alloc, offset: ptr.offset }
135135
}
136136
Scalar::Raw { data, .. } => {
@@ -154,9 +154,7 @@ pub(super) fn op_to_const<'tcx>(
154154
},
155155
Immediate::ScalarPair(a, b) => {
156156
let (data, start) = match a.not_undef().unwrap() {
157-
Scalar::Ptr(ptr) => {
158-
(ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id), ptr.offset.bytes())
159-
}
157+
Scalar::Ptr(ptr) => (ecx.tcx.unwrap_memory(ptr.alloc_id), ptr.offset.bytes()),
160158
Scalar::Raw { .. } => (
161159
ecx.tcx
162160
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
@@ -202,10 +200,7 @@ fn validate_and_turn_into_const<'tcx>(
202200
// whether they become immediates.
203201
if is_static || cid.promoted.is_some() {
204202
let ptr = mplace.ptr.assert_ptr();
205-
Ok(ConstValue::ByRef {
206-
alloc: ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id),
207-
offset: ptr.offset,
208-
})
203+
Ok(ConstValue::ByRef { alloc: ecx.tcx.unwrap_memory(ptr.alloc_id), offset: ptr.offset })
209204
} else {
210205
Ok(op_to_const(&ecx, mplace.into()))
211206
}

src/librustc_mir/interpret/intern.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
9191
// If the pointer is dangling (neither in local nor global memory), we leave it
9292
// to validation to error. The `delay_span_bug` ensures that we don't forget such
9393
// a check in validation.
94-
if tcx.alloc_map.lock().get(alloc_id).is_none() {
94+
if tcx.get_global_alloc(alloc_id).is_none() {
9595
tcx.sess.delay_span_bug(ecx.tcx.span, "tried to intern dangling pointer");
9696
}
9797
// treat dangling pointers like other statics
@@ -134,7 +134,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx>>(
134134
// link the alloc id to the actual allocation
135135
let alloc = tcx.intern_const_alloc(alloc);
136136
leftover_allocations.extend(alloc.relocations().iter().map(|&(_, ((), reloc))| reloc));
137-
tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
137+
tcx.set_alloc_id_memory(alloc_id, alloc);
138138
Ok(None)
139139
}
140140

@@ -389,7 +389,7 @@ where
389389
}
390390
}
391391
let alloc = tcx.intern_const_alloc(alloc);
392-
tcx.alloc_map.lock().set_alloc_id_memory(alloc_id, alloc);
392+
tcx.set_alloc_id_memory(alloc_id, alloc);
393393
for &(_, ((), reloc)) in alloc.relocations().iter() {
394394
if leftover_allocations.insert(reloc) {
395395
todo.push(reloc);
@@ -398,7 +398,7 @@ where
398398
} else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) {
399399
// dangling pointer
400400
throw_ub_format!("encountered dangling pointer in final constant")
401-
} else if ecx.tcx.alloc_map.lock().get(alloc_id).is_none() {
401+
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() {
402402
// We have hit an `AllocId` that is neither in local or global memory and isn't marked
403403
// as dangling by local memory.
404404
span_bug!(ecx.tcx.span, "encountered unknown alloc id {:?}", alloc_id);

0 commit comments

Comments
 (0)