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.
This commit is contained in:
Nicholas Nethercote 2022-03-02 07:15:04 +11:00
parent 346108202d
commit 7c1a318c7b
2 changed files with 13 additions and 8 deletions

View File

@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::interpret::{ use rustc_middle::mir::interpret::{
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar, read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
}; };
use rustc_middle::ty::ConstKind; use rustc_middle::ty::ConstKind;
use rustc_span::DUMMY_SP; use rustc_span::DUMMY_SP;
@ -202,7 +202,7 @@ pub(crate) fn codegen_const_value<'tcx>(
&mut fx.constants_cx, &mut fx.constants_cx,
fx.module, fx.module,
alloc_id, alloc_id,
alloc.mutability, alloc.inner().mutability,
); );
let local_data_id = let local_data_id =
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
@ -257,11 +257,15 @@ pub(crate) fn codegen_const_value<'tcx>(
pub(crate) fn pointer_for_allocation<'tcx>( pub(crate) fn pointer_for_allocation<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>, fx: &mut FunctionCx<'_, '_, 'tcx>,
alloc: &'tcx Allocation, alloc: ConstAllocation<'tcx>,
) -> crate::pointer::Pointer { ) -> crate::pointer::Pointer {
let alloc_id = fx.tcx.create_memory_alloc(alloc); let alloc_id = fx.tcx.create_memory_alloc(alloc);
let data_id = let data_id = data_id_for_alloc_id(
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability); &mut fx.constants_cx,
&mut *fx.module,
alloc_id,
alloc.inner().mutability,
);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func); let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() { if fx.clif_comments.enabled() {
@ -361,7 +365,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| { let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module module
.declare_anonymous_data( .declare_anonymous_data(
alloc.mutability == rustc_hir::Mutability::Mut, alloc.inner().mutability == rustc_hir::Mutability::Mut,
false, false,
) )
.unwrap() .unwrap()
@ -386,6 +390,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
} }
let mut data_ctx = DataContext::new(); let mut data_ctx = DataContext::new();
let alloc = alloc.inner();
data_ctx.set_align(alloc.align.bytes()); data_ctx.set_align(alloc.align.bytes());
if let Some(section_name) = section_name { if let Some(section_name) = section_name {
@ -429,7 +434,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
continue; continue;
} }
GlobalAlloc::Memory(target_alloc) => { GlobalAlloc::Memory(target_alloc) => {
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.mutability) data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
} }
GlobalAlloc::Static(def_id) => { GlobalAlloc::Static(def_id) => {
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)

View File

@ -159,7 +159,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
let idx_bytes = match idx_const { let idx_bytes = match idx_const {
ConstValue::ByRef { alloc, offset } => { ConstValue::ByRef { alloc, offset } => {
let size = Size::from_bytes(4 * ret_lane_count /* size_of([u32; ret_lane_count]) */); let size = Size::from_bytes(4 * ret_lane_count /* size_of([u32; ret_lane_count]) */);
alloc.get_bytes(fx, alloc_range(offset, size)).unwrap() alloc.inner().get_bytes(fx, alloc_range(offset, size)).unwrap()
} }
_ => unreachable!("{:?}", idx_const), _ => unreachable!("{:?}", idx_const),
}; };