Skip to content

Commit c6bf11c

Browse files
authored
Rollup merge of #47205 - eddyb:alloc-id, r=oli-obk
miri: use AllocId instead of u64. This makes @alexreg's miri allocation -> LLVM global translation more straight-forward. r? @oli-obk
2 parents 48a0f3a + 0907494 commit c6bf11c

File tree

8 files changed

+71
-72
lines changed

8 files changed

+71
-72
lines changed

src/librustc/mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'tcx> MemoryPointer {
145145
}
146146

147147

148-
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
148+
#[derive(Copy, Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
149149
pub struct AllocId(pub u64);
150150

151151
impl fmt::Display for AllocId {

src/librustc/ty/context.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -895,31 +895,29 @@ pub struct InterpretInterner<'tcx> {
895895
allocs: FxHashSet<&'tcx interpret::Allocation>,
896896

897897
/// Allows obtaining function instance handles via a unique identifier
898-
functions: FxHashMap<u64, Instance<'tcx>>,
898+
functions: FxHashMap<interpret::AllocId, Instance<'tcx>>,
899899

900900
/// Inverse map of `interpret_functions`.
901901
/// Used so we don't allocate a new pointer every time we need one
902-
function_cache: FxHashMap<Instance<'tcx>, u64>,
902+
function_cache: FxHashMap<Instance<'tcx>, interpret::AllocId>,
903903

904904
/// Allows obtaining const allocs via a unique identifier
905-
alloc_by_id: FxHashMap<u64, &'tcx interpret::Allocation>,
905+
alloc_by_id: FxHashMap<interpret::AllocId, &'tcx interpret::Allocation>,
906906

907907
/// The AllocId to assign to the next new regular allocation.
908908
/// Always incremented, never gets smaller.
909-
next_id: u64,
909+
next_id: interpret::AllocId,
910910

911911
/// Allows checking whether a constant already has an allocation
912-
///
913-
/// The pointers are to the beginning of an `alloc_by_id` allocation
914-
alloc_cache: FxHashMap<interpret::GlobalId<'tcx>, interpret::Pointer>,
912+
alloc_cache: FxHashMap<interpret::GlobalId<'tcx>, interpret::AllocId>,
915913

916914
/// A cache for basic byte allocations keyed by their contents. This is used to deduplicate
917915
/// allocations for string and bytestring literals.
918-
literal_alloc_cache: FxHashMap<Vec<u8>, u64>,
916+
literal_alloc_cache: FxHashMap<Vec<u8>, interpret::AllocId>,
919917
}
920918

921919
impl<'tcx> InterpretInterner<'tcx> {
922-
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> u64 {
920+
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> interpret::AllocId {
923921
if let Some(&alloc_id) = self.function_cache.get(&instance) {
924922
return alloc_id;
925923
}
@@ -932,29 +930,29 @@ impl<'tcx> InterpretInterner<'tcx> {
932930

933931
pub fn get_fn(
934932
&self,
935-
id: u64,
933+
id: interpret::AllocId,
936934
) -> Option<Instance<'tcx>> {
937935
self.functions.get(&id).cloned()
938936
}
939937

940938
pub fn get_alloc(
941939
&self,
942-
id: u64,
940+
id: interpret::AllocId,
943941
) -> Option<&'tcx interpret::Allocation> {
944942
self.alloc_by_id.get(&id).cloned()
945943
}
946944

947945
pub fn get_cached(
948946
&self,
949947
global_id: interpret::GlobalId<'tcx>,
950-
) -> Option<interpret::Pointer> {
948+
) -> Option<interpret::AllocId> {
951949
self.alloc_cache.get(&global_id).cloned()
952950
}
953951

954952
pub fn cache(
955953
&mut self,
956954
global_id: interpret::GlobalId<'tcx>,
957-
ptr: interpret::Pointer,
955+
ptr: interpret::AllocId,
958956
) {
959957
if let Some(old) = self.alloc_cache.insert(global_id, ptr) {
960958
bug!("tried to cache {:?}, but was already existing as {:#?}", global_id, old);
@@ -963,7 +961,7 @@ impl<'tcx> InterpretInterner<'tcx> {
963961

964962
pub fn intern_at_reserved(
965963
&mut self,
966-
id: u64,
964+
id: interpret::AllocId,
967965
alloc: &'tcx interpret::Allocation,
968966
) {
969967
if let Some(old) = self.alloc_by_id.insert(id, alloc) {
@@ -975,9 +973,9 @@ impl<'tcx> InterpretInterner<'tcx> {
975973
/// yet have an allocation backing it.
976974
pub fn reserve(
977975
&mut self,
978-
) -> u64 {
976+
) -> interpret::AllocId {
979977
let next = self.next_id;
980-
self.next_id = self.next_id
978+
self.next_id.0 = self.next_id.0
981979
.checked_add(1)
982980
.expect("You overflowed a u64 by incrementing by 1... \
983981
You've just earned yourself a free drink if we ever meet. \
@@ -1069,7 +1067,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10691067
}
10701068

10711069
/// Allocates a byte or string literal for `mir::interpret`
1072-
pub fn allocate_cached(self, bytes: &[u8]) -> u64 {
1070+
pub fn allocate_cached(self, bytes: &[u8]) -> interpret::AllocId {
10731071
// check whether we already allocated this literal or a constant with the same memory
10741072
if let Some(&alloc_id) = self.interpret_interner.borrow().literal_alloc_cache.get(bytes) {
10751073
return alloc_id;

src/librustc_mir/interpret/const_eval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::indexed_vec::Idx;
1212
use syntax::ast::Mutability;
1313
use syntax::codemap::Span;
1414

15-
use rustc::mir::interpret::{EvalResult, EvalError, EvalErrorKind, GlobalId, Value, Pointer, PrimVal};
15+
use rustc::mir::interpret::{EvalResult, EvalError, EvalErrorKind, GlobalId, Value, MemoryPointer, Pointer, PrimVal};
1616
use super::{Place, EvalContext, StackPopCleanup, ValTy};
1717

1818
use rustc_const_math::ConstInt;
@@ -67,7 +67,7 @@ pub fn eval_body<'a, 'tcx>(
6767
layout.align,
6868
None,
6969
)?;
70-
tcx.interpret_interner.borrow_mut().cache(cid, ptr.into());
70+
tcx.interpret_interner.borrow_mut().cache(cid, ptr.alloc_id);
7171
let cleanup = StackPopCleanup::MarkStatic(Mutability::Immutable);
7272
let name = ty::tls::with(|tcx| tcx.item_path_str(instance.def_id()));
7373
trace!("const_eval: pushing stack frame for global: {}", name);
@@ -81,8 +81,8 @@ pub fn eval_body<'a, 'tcx>(
8181

8282
while ecx.step()? {}
8383
}
84-
let value = tcx.interpret_interner.borrow().get_cached(cid).expect("global not cached");
85-
Ok((value, instance_ty))
84+
let alloc = tcx.interpret_interner.borrow().get_cached(cid).expect("global not cached");
85+
Ok((MemoryPointer::new(alloc, 0).into(), instance_ty))
8686
}
8787

8888
pub fn eval_body_as_integer<'a, 'tcx>(

src/librustc_mir/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,8 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
950950
}
951951

952952
pub fn read_global_as_value(&self, gid: GlobalId, layout: TyLayout) -> Value {
953-
Value::ByRef(self.tcx.interpret_interner.borrow().get_cached(gid).expect("global not cached"),
954-
layout.align)
953+
let alloc = self.tcx.interpret_interner.borrow().get_cached(gid).expect("global not cached");
954+
Value::ByRef(MemoryPointer::new(alloc, 0).into(), layout.align)
955955
}
956956

957957
pub fn force_allocation(&mut self, place: Place) -> EvalResult<'tcx, Place> {

src/librustc_mir/interpret/machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! This separation exists to ensure that no fancy miri features like
33
//! interpreting common C functions leak into CTFE.
44
5-
use rustc::mir::interpret::{EvalResult, PrimVal, MemoryPointer, AccessKind};
5+
use rustc::mir::interpret::{AllocId, EvalResult, PrimVal, MemoryPointer, AccessKind};
66
use super::{EvalContext, Place, ValTy, Memory};
77

88
use rustc::mir;
@@ -89,12 +89,12 @@ pub trait Machine<'tcx>: Sized {
8989

9090
fn add_lock<'a>(
9191
_mem: &mut Memory<'a, 'tcx, Self>,
92-
_id: u64,
92+
_id: AllocId,
9393
) {}
9494

9595
fn free_lock<'a>(
9696
_mem: &mut Memory<'a, 'tcx, Self>,
97-
_id: u64,
97+
_id: AllocId,
9898
_len: u64,
9999
) -> EvalResult<'tcx> {
100100
Ok(())

0 commit comments

Comments
 (0)