2
2
3
3
use rustc_span:: DUMMY_SP ;
4
4
5
- use rustc_data_structures:: fx:: FxHashSet ;
5
+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
6
6
use rustc_errors:: ErrorReported ;
7
7
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
8
8
use rustc_middle:: mir:: interpret:: {
@@ -13,12 +13,12 @@ use rustc_middle::ty::ConstKind;
13
13
use cranelift_codegen:: ir:: GlobalValueData ;
14
14
use cranelift_module:: * ;
15
15
16
- use crate :: prelude:: * ;
16
+ use crate :: { prelude:: * , CodegenCx } ;
17
17
18
- #[ derive( Default ) ]
19
18
pub ( crate ) struct ConstantCx {
20
19
todo : Vec < TodoItem > ,
21
20
done : FxHashSet < DataId > ,
21
+ anon_allocs : FxHashMap < AllocId , DataId > ,
22
22
}
23
23
24
24
#[ derive( Copy , Clone , Debug ) ]
@@ -28,6 +28,10 @@ enum TodoItem {
28
28
}
29
29
30
30
impl ConstantCx {
31
+ pub ( crate ) fn new ( ) -> Self {
32
+ ConstantCx { todo : vec ! [ ] , done : FxHashSet :: default ( ) , anon_allocs : FxHashMap :: default ( ) }
33
+ }
34
+
31
35
pub ( crate ) fn finalize ( mut self , tcx : TyCtxt < ' _ > , module : & mut dyn Module ) {
32
36
//println!("todo {:?}", self.todo);
33
37
define_all_allocs ( tcx, module, & mut self ) ;
@@ -74,8 +78,10 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
74
78
all_constants_ok
75
79
}
76
80
77
- pub ( crate ) fn codegen_static ( constants_cx : & mut ConstantCx , def_id : DefId ) {
81
+ pub ( crate ) fn codegen_static ( cx : & mut CodegenCx < ' _ , ' _ > , def_id : DefId ) {
82
+ let mut constants_cx = ConstantCx :: new ( ) ;
78
83
constants_cx. todo . push ( TodoItem :: Static ( def_id) ) ;
84
+ constants_cx. finalize ( cx. tcx , & mut * cx. module ) ;
79
85
}
80
86
81
87
pub ( crate ) fn codegen_tls_ref < ' tcx > (
@@ -182,9 +188,13 @@ pub(crate) fn codegen_const_value<'tcx>(
182
188
let alloc_kind = fx. tcx . get_global_alloc ( ptr. alloc_id ) ;
183
189
let base_addr = match alloc_kind {
184
190
Some ( GlobalAlloc :: Memory ( alloc) ) => {
185
- fx. cx . constants_cx . todo . push ( TodoItem :: Alloc ( ptr. alloc_id ) ) ;
186
- let data_id =
187
- data_id_for_alloc_id ( fx. cx . module , ptr. alloc_id , alloc. mutability ) ;
191
+ fx. constants_cx . todo . push ( TodoItem :: Alloc ( ptr. alloc_id ) ) ;
192
+ let data_id = data_id_for_alloc_id (
193
+ & mut fx. constants_cx ,
194
+ fx. cx . module ,
195
+ ptr. alloc_id ,
196
+ alloc. mutability ,
197
+ ) ;
188
198
let local_data_id =
189
199
fx. cx . module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
190
200
if fx. clif_comments . enabled ( ) {
@@ -243,8 +253,9 @@ fn pointer_for_allocation<'tcx>(
243
253
alloc : & ' tcx Allocation ,
244
254
) -> crate :: pointer:: Pointer {
245
255
let alloc_id = fx. tcx . create_memory_alloc ( alloc) ;
246
- fx. cx . constants_cx . todo . push ( TodoItem :: Alloc ( alloc_id) ) ;
247
- let data_id = data_id_for_alloc_id ( fx. cx . module , alloc_id, alloc. mutability ) ;
256
+ fx. constants_cx . todo . push ( TodoItem :: Alloc ( alloc_id) ) ;
257
+ let data_id =
258
+ data_id_for_alloc_id ( & mut fx. constants_cx , & mut * fx. cx . module , alloc_id, alloc. mutability ) ;
248
259
249
260
let local_data_id = fx. cx . module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
250
261
if fx. clif_comments . enabled ( ) {
@@ -255,18 +266,14 @@ fn pointer_for_allocation<'tcx>(
255
266
}
256
267
257
268
fn data_id_for_alloc_id (
269
+ cx : & mut ConstantCx ,
258
270
module : & mut dyn Module ,
259
271
alloc_id : AllocId ,
260
272
mutability : rustc_hir:: Mutability ,
261
273
) -> DataId {
262
- module
263
- . declare_data (
264
- & format ! ( ".L__alloc_{:x}" , alloc_id. 0 ) ,
265
- Linkage :: Local ,
266
- mutability == rustc_hir:: Mutability :: Mut ,
267
- false ,
268
- )
269
- . unwrap ( )
274
+ * cx. anon_allocs . entry ( alloc_id) . or_insert_with ( || {
275
+ module. declare_anonymous_data ( mutability == rustc_hir:: Mutability :: Mut , false ) . unwrap ( )
276
+ } )
270
277
}
271
278
272
279
fn data_id_for_static (
@@ -344,7 +351,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
344
351
GlobalAlloc :: Memory ( alloc) => alloc,
345
352
GlobalAlloc :: Function ( _) | GlobalAlloc :: Static ( _) => unreachable ! ( ) ,
346
353
} ;
347
- let data_id = data_id_for_alloc_id ( module, alloc_id, alloc. mutability ) ;
354
+ let data_id = data_id_for_alloc_id ( cx , module, alloc_id, alloc. mutability ) ;
348
355
( data_id, alloc, None )
349
356
}
350
357
TodoItem :: Static ( def_id) => {
@@ -397,7 +404,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
397
404
}
398
405
GlobalAlloc :: Memory ( target_alloc) => {
399
406
cx. todo . push ( TodoItem :: Alloc ( reloc) ) ;
400
- data_id_for_alloc_id ( module, reloc, target_alloc. mutability )
407
+ data_id_for_alloc_id ( cx , module, reloc, target_alloc. mutability )
401
408
}
402
409
GlobalAlloc :: Static ( def_id) => {
403
410
if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL )
@@ -419,8 +426,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
419
426
data_ctx. write_data_addr ( offset. bytes ( ) as u32 , global_value, addend as i64 ) ;
420
427
}
421
428
422
- // FIXME don't duplicate definitions in lazy jit mode
423
- let _ = module. define_data ( data_id, & data_ctx) ;
429
+ module. define_data ( data_id, & data_ctx) . unwrap ( ) ;
424
430
cx. done . insert ( data_id) ;
425
431
}
426
432
0 commit comments