about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-09-12 08:42:36 +0200
committerRalf Jung <post@ralfj.de>2023-09-14 07:27:31 +0200
commit90d894e122d2223126ffcce79910659c2a211ea7 (patch)
tree602cb3645a6cf42da4bb0ca9616c34d4c1c0626d
parenta5b81faef0c0c70a7a254c5146e70629a2d5c923 (diff)
downloadrust-90d894e122d2223126ffcce79910659c2a211ea7.tar.gz
rust-90d894e122d2223126ffcce79910659c2a211ea7.zip
make it more clear which functions create fresh AllocId
-rw-r--r--src/constant.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/constant.rs b/src/constant.rs
index 12e492da6e9..8c67760a0b9 100644
--- a/src/constant.rs
+++ b/src/constant.rs
@@ -3,7 +3,7 @@
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
 use rustc_middle::mir::interpret::{
-    read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
+    read_target_uint, AllocId, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
 };
 
 use cranelift_module::*;
@@ -200,17 +200,14 @@ pub(crate) fn codegen_const_value<'tcx>(
                 CValue::by_val(val, layout)
             }
         },
-        ConstValue::Indirect { alloc_id, offset } => {
-            let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
-            // FIXME: avoid creating multiple allocations for the same AllocId?
-            CValue::by_ref(
-                pointer_for_allocation(fx, alloc)
-                    .offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
-                layout,
-            )
-        }
+        ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
+            pointer_for_allocation(fx, alloc_id)
+                .offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
+            layout,
+        ),
         ConstValue::Slice { data, start, end } => {
-            let ptr = pointer_for_allocation(fx, data)
+            let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
+            let ptr = pointer_for_allocation(fx, alloc_id)
                 .offset_i64(fx, i64::try_from(start).unwrap())
                 .get_addr(fx);
             let len = fx
@@ -224,9 +221,9 @@ pub(crate) fn codegen_const_value<'tcx>(
 
 fn pointer_for_allocation<'tcx>(
     fx: &mut FunctionCx<'_, '_, 'tcx>,
-    alloc: ConstAllocation<'tcx>,
+    alloc_id: AllocId,
 ) -> crate::pointer::Pointer {
-    let alloc_id = fx.tcx.create_memory_alloc(alloc);
+    let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
     let data_id = data_id_for_alloc_id(
         &mut fx.constants_cx,
         &mut *fx.module,
@@ -357,6 +354,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
                         unreachable!()
                     }
                 };
+                // FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
                 let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
                     module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
                 });