Skip to content

Commit 90d894e

Browse files
committed
make it more clear which functions create fresh AllocId
1 parent a5b81fa commit 90d894e

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/constant.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc_middle::mir::interpret::{
6-
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
6+
read_target_uint, AllocId, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
77
};
88

99
use cranelift_module::*;
@@ -200,17 +200,14 @@ pub(crate) fn codegen_const_value<'tcx>(
200200
CValue::by_val(val, layout)
201201
}
202202
},
203-
ConstValue::Indirect { alloc_id, offset } => {
204-
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
205-
// FIXME: avoid creating multiple allocations for the same AllocId?
206-
CValue::by_ref(
207-
pointer_for_allocation(fx, alloc)
208-
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
209-
layout,
210-
)
211-
}
203+
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
204+
pointer_for_allocation(fx, alloc_id)
205+
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
206+
layout,
207+
),
212208
ConstValue::Slice { data, start, end } => {
213-
let ptr = pointer_for_allocation(fx, data)
209+
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
210+
let ptr = pointer_for_allocation(fx, alloc_id)
214211
.offset_i64(fx, i64::try_from(start).unwrap())
215212
.get_addr(fx);
216213
let len = fx
@@ -224,9 +221,9 @@ pub(crate) fn codegen_const_value<'tcx>(
224221

225222
fn pointer_for_allocation<'tcx>(
226223
fx: &mut FunctionCx<'_, '_, 'tcx>,
227-
alloc: ConstAllocation<'tcx>,
224+
alloc_id: AllocId,
228225
) -> crate::pointer::Pointer {
229-
let alloc_id = fx.tcx.create_memory_alloc(alloc);
226+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
230227
let data_id = data_id_for_alloc_id(
231228
&mut fx.constants_cx,
232229
&mut *fx.module,
@@ -357,6 +354,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
357354
unreachable!()
358355
}
359356
};
357+
// FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
360358
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
361359
module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
362360
});

0 commit comments

Comments
 (0)