Skip to content

Commit 92d1850

Browse files
committed
Introduce ConstAllocation.
Currently some `Allocation`s are interned, some are not, and it's very hard to tell at a use point which is which. This commit introduces `ConstAllocation` for the known-interned ones, which makes the division much clearer. `ConstAllocation::inner()` is used to get the underlying `Allocation`. In some places it's natural to use an `Allocation`, in some it's natural to use a `ConstAllocation`, and in some places there's no clear choice. I've tried to make things look as nice as possible, while generally favouring `ConstAllocation`, which is the type that embodies more information. This does require quite a few calls to `inner()`. The commit also tweaks how `PartialOrd` works for `Interned`. The previous code was too clever by half, building on `T: Ord` to make the code shorter. That caused problems with deriving `PartialOrd` and `Ord` for `ConstAllocation`, so I changed it to build on `T: PartialOrd`, which is slightly more verbose but much more standard and avoided the problems.
1 parent 8238b91 commit 92d1850

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/common.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_codegen_ssa::traits::{
1313
use rustc_middle::mir::Mutability;
1414
use rustc_middle::ty::ScalarInt;
1515
use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
16-
use rustc_middle::mir::interpret::{Allocation, GlobalAlloc, Scalar};
16+
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1717
use rustc_span::Symbol;
1818
use rustc_target::abi::{self, HasDataLayout, Pointer, Size};
1919

@@ -230,6 +230,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
230230
match self.tcx.global_alloc(alloc_id) {
231231
GlobalAlloc::Memory(alloc) => {
232232
let init = const_alloc_to_gcc(self, alloc);
233+
let alloc = alloc.inner();
233234
let value =
234235
match alloc.mutability {
235236
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
@@ -262,21 +263,21 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
262263
}
263264
}
264265

265-
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value {
266+
fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
266267
const_alloc_to_gcc(self, alloc)
267268
}
268269

269-
fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: &Allocation, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
270-
assert_eq!(alloc.align, layout.align.abi);
270+
fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
271+
assert_eq!(alloc.inner().align, layout.align.abi);
271272
let ty = self.type_ptr_to(layout.gcc_type(self, true));
272273
let value =
273274
if layout.size == Size::ZERO {
274-
let value = self.const_usize(alloc.align.bytes());
275+
let value = self.const_usize(alloc.inner().align.bytes());
275276
self.context.new_cast(None, value, ty)
276277
}
277278
else {
278279
let init = const_alloc_to_gcc(self, alloc);
279-
let base_addr = self.static_addr_of(init, alloc.align, None);
280+
let base_addr = self.static_addr_of(init, alloc.inner().align, None);
280281

281282
let array = self.const_bitcast(base_addr, self.type_i8p());
282283
let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);

src/consts.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}
77
use rustc_middle::mir::mono::MonoItem;
88
use rustc_middle::ty::{self, Instance, Ty};
99
use rustc_middle::ty::layout::LayoutOf;
10-
use rustc_middle::mir::interpret::{self, Allocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
10+
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
1111
use rustc_span::Span;
1212
use rustc_span::def_id::DefId;
1313
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
@@ -284,7 +284,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
284284
}
285285
}
286286

287-
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: &Allocation) -> RValue<'gcc> {
287+
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAllocation<'tcx>) -> RValue<'gcc> {
288+
let alloc = alloc.inner();
288289
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
289290
let dl = cx.data_layout();
290291
let pointer_size = dl.pointer_size.bytes() as usize;
@@ -338,7 +339,7 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: &Alloca
338339
cx.const_struct(&llvals, true)
339340
}
340341

341-
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, &'tcx Allocation), ErrorHandled> {
342+
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, ConstAllocation<'tcx>), ErrorHandled> {
342343
let alloc = cx.tcx.eval_static_initializer(def_id)?;
343344
Ok((const_alloc_to_gcc(cx, alloc), alloc))
344345
}

0 commit comments

Comments
 (0)