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 :: { CodegenCx , prelude:: * } ;
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,14 @@ enum TodoItem {
28
28
}
29
29
30
30
impl ConstantCx {
31
+ pub ( crate ) fn new ( ) -> Self {
32
+ ConstantCx {
33
+ todo : vec ! [ ] ,
34
+ done : FxHashSet :: default ( ) ,
35
+ anon_allocs : FxHashMap :: default ( ) ,
36
+ }
37
+ }
38
+
31
39
pub ( crate ) fn finalize ( mut self , tcx : TyCtxt < ' _ > , module : & mut dyn Module ) {
32
40
//println!("todo {:?}", self.todo);
33
41
define_all_allocs ( tcx, module, & mut self ) ;
@@ -74,8 +82,10 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
74
82
all_constants_ok
75
83
}
76
84
77
- pub ( crate ) fn codegen_static ( constants_cx : & mut ConstantCx , def_id : DefId ) {
85
+ pub ( crate ) fn codegen_static ( cx : & mut CodegenCx < ' _ , ' _ > , def_id : DefId ) {
86
+ let mut constants_cx = ConstantCx :: new ( ) ;
78
87
constants_cx. todo . push ( TodoItem :: Static ( def_id) ) ;
88
+ constants_cx. finalize ( cx. tcx , & mut * cx. module ) ;
79
89
}
80
90
81
91
pub ( crate ) fn codegen_tls_ref < ' tcx > (
@@ -182,9 +192,13 @@ pub(crate) fn codegen_const_value<'tcx>(
182
192
let alloc_kind = fx. tcx . get_global_alloc ( ptr. alloc_id ) ;
183
193
let base_addr = match alloc_kind {
184
194
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 ) ;
195
+ fx. constants_cx . todo . push ( TodoItem :: Alloc ( ptr. alloc_id ) ) ;
196
+ let data_id = data_id_for_alloc_id (
197
+ & mut fx. constants_cx ,
198
+ fx. cx . module ,
199
+ ptr. alloc_id ,
200
+ alloc. mutability ,
201
+ ) ;
188
202
let local_data_id =
189
203
fx. cx . module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
190
204
if fx. clif_comments . enabled ( ) {
@@ -243,8 +257,9 @@ fn pointer_for_allocation<'tcx>(
243
257
alloc : & ' tcx Allocation ,
244
258
) -> crate :: pointer:: Pointer {
245
259
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 ) ;
260
+ fx. constants_cx . todo . push ( TodoItem :: Alloc ( alloc_id) ) ;
261
+ let data_id =
262
+ data_id_for_alloc_id ( & mut fx. constants_cx , & mut * fx. cx . module , alloc_id, alloc. mutability ) ;
248
263
249
264
let local_data_id = fx. cx . module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
250
265
if fx. clif_comments . enabled ( ) {
@@ -255,18 +270,16 @@ fn pointer_for_allocation<'tcx>(
255
270
}
256
271
257
272
fn data_id_for_alloc_id (
273
+ cx : & mut ConstantCx ,
258
274
module : & mut dyn Module ,
259
275
alloc_id : AllocId ,
260
276
mutability : rustc_hir:: Mutability ,
261
277
) -> 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
- )
278
+ * cx. anon_allocs . entry ( alloc_id) . or_insert_with ( || {
279
+ module
280
+ . declare_anonymous_data ( mutability == rustc_hir:: Mutability :: Mut , false )
269
281
. unwrap ( )
282
+ } )
270
283
}
271
284
272
285
fn data_id_for_static (
@@ -344,7 +357,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
344
357
GlobalAlloc :: Memory ( alloc) => alloc,
345
358
GlobalAlloc :: Function ( _) | GlobalAlloc :: Static ( _) => unreachable ! ( ) ,
346
359
} ;
347
- let data_id = data_id_for_alloc_id ( module, alloc_id, alloc. mutability ) ;
360
+ let data_id = data_id_for_alloc_id ( cx , module, alloc_id, alloc. mutability ) ;
348
361
( data_id, alloc, None )
349
362
}
350
363
TodoItem :: Static ( def_id) => {
@@ -397,7 +410,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
397
410
}
398
411
GlobalAlloc :: Memory ( target_alloc) => {
399
412
cx. todo . push ( TodoItem :: Alloc ( reloc) ) ;
400
- data_id_for_alloc_id ( module, reloc, target_alloc. mutability )
413
+ data_id_for_alloc_id ( cx , module, reloc, target_alloc. mutability )
401
414
}
402
415
GlobalAlloc :: Static ( def_id) => {
403
416
if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL )
@@ -419,8 +432,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
419
432
data_ctx. write_data_addr ( offset. bytes ( ) as u32 , global_value, addend as i64 ) ;
420
433
}
421
434
422
- // FIXME don't duplicate definitions in lazy jit mode
423
- let _ = module. define_data ( data_id, & data_ctx) ;
435
+ module. define_data ( data_id, & data_ctx) . unwrap ( ) ;
424
436
cx. done . insert ( data_id) ;
425
437
}
426
438
0 commit comments