3
3
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
4
4
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
5
5
use rustc_middle:: mir:: interpret:: {
6
- read_target_uint, AllocId , ConstAllocation , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
6
+ read_target_uint, AllocId , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
7
7
} ;
8
8
9
9
use cranelift_module:: * ;
@@ -200,17 +200,14 @@ pub(crate) fn codegen_const_value<'tcx>(
200
200
CValue :: by_val ( val, layout)
201
201
}
202
202
} ,
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
+ ) ,
212
208
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)
214
211
. offset_i64 ( fx, i64:: try_from ( start) . unwrap ( ) )
215
212
. get_addr ( fx) ;
216
213
let len = fx
@@ -224,9 +221,9 @@ pub(crate) fn codegen_const_value<'tcx>(
224
221
225
222
fn pointer_for_allocation < ' tcx > (
226
223
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
227
- alloc : ConstAllocation < ' tcx > ,
224
+ alloc_id : AllocId ,
228
225
) -> crate :: pointer:: Pointer {
229
- let alloc_id = fx. tcx . create_memory_alloc ( alloc ) ;
226
+ let alloc = fx. tcx . global_alloc ( alloc_id ) . unwrap_memory ( ) ;
230
227
let data_id = data_id_for_alloc_id (
231
228
& mut fx. constants_cx ,
232
229
& mut * fx. module ,
@@ -357,6 +354,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
357
354
unreachable ! ( )
358
355
}
359
356
} ;
357
+ // FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
360
358
let data_id = * cx. anon_allocs . entry ( alloc_id) . or_insert_with ( || {
361
359
module. declare_anonymous_data ( alloc. inner ( ) . mutability . is_mut ( ) , false ) . unwrap ( )
362
360
} ) ;
0 commit comments