Skip to content

Commit 7c1a318

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 3461082 commit 7c1a318

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/constant.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
66
use rustc_middle::mir::interpret::{
7-
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
7+
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
88
};
99
use rustc_middle::ty::ConstKind;
1010
use rustc_span::DUMMY_SP;
@@ -202,7 +202,7 @@ pub(crate) fn codegen_const_value<'tcx>(
202202
&mut fx.constants_cx,
203203
fx.module,
204204
alloc_id,
205-
alloc.mutability,
205+
alloc.inner().mutability,
206206
);
207207
let local_data_id =
208208
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
@@ -257,11 +257,15 @@ pub(crate) fn codegen_const_value<'tcx>(
257257

258258
pub(crate) fn pointer_for_allocation<'tcx>(
259259
fx: &mut FunctionCx<'_, '_, 'tcx>,
260-
alloc: &'tcx Allocation,
260+
alloc: ConstAllocation<'tcx>,
261261
) -> crate::pointer::Pointer {
262262
let alloc_id = fx.tcx.create_memory_alloc(alloc);
263-
let data_id =
264-
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);
263+
let data_id = data_id_for_alloc_id(
264+
&mut fx.constants_cx,
265+
&mut *fx.module,
266+
alloc_id,
267+
alloc.inner().mutability,
268+
);
265269

266270
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
267271
if fx.clif_comments.enabled() {
@@ -361,7 +365,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
361365
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
362366
module
363367
.declare_anonymous_data(
364-
alloc.mutability == rustc_hir::Mutability::Mut,
368+
alloc.inner().mutability == rustc_hir::Mutability::Mut,
365369
false,
366370
)
367371
.unwrap()
@@ -386,6 +390,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
386390
}
387391

388392
let mut data_ctx = DataContext::new();
393+
let alloc = alloc.inner();
389394
data_ctx.set_align(alloc.align.bytes());
390395

391396
if let Some(section_name) = section_name {
@@ -429,7 +434,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
429434
continue;
430435
}
431436
GlobalAlloc::Memory(target_alloc) => {
432-
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.mutability)
437+
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
433438
}
434439
GlobalAlloc::Static(def_id) => {
435440
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)

src/intrinsics/simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
159159
let idx_bytes = match idx_const {
160160
ConstValue::ByRef { alloc, offset } => {
161161
let size = Size::from_bytes(4 * ret_lane_count /* size_of([u32; ret_lane_count]) */);
162-
alloc.get_bytes(fx, alloc_range(offset, size)).unwrap()
162+
alloc.inner().get_bytes(fx, alloc_range(offset, size)).unwrap()
163163
}
164164
_ => unreachable!("{:?}", idx_const),
165165
};

0 commit comments

Comments
 (0)