@@ -6,30 +6,28 @@ use cranelift_module::*;
6
6
use rustc_data_structures:: fx:: FxHashSet ;
7
7
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
8
8
use rustc_middle:: mir:: interpret:: { read_target_uint, AllocId , GlobalAlloc , Scalar } ;
9
- use rustc_middle:: ty:: ScalarInt ;
9
+ use rustc_middle:: ty:: { Binder , ExistentialTraitRef , ScalarInt } ;
10
10
11
11
use crate :: prelude:: * ;
12
12
13
13
pub ( crate ) struct ConstantCx {
14
14
todo : Vec < TodoItem > ,
15
- done : FxHashSet < DataId > ,
16
15
anon_allocs : FxHashMap < AllocId , DataId > ,
17
16
}
18
17
19
- #[ derive( Copy , Clone , Debug ) ]
18
+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
20
19
enum TodoItem {
21
20
Alloc ( AllocId ) ,
22
21
Static ( DefId ) ,
23
22
}
24
23
25
24
impl ConstantCx {
26
25
pub ( crate ) fn new ( ) -> Self {
27
- ConstantCx { todo : vec ! [ ] , done : FxHashSet :: default ( ) , anon_allocs : FxHashMap :: default ( ) }
26
+ ConstantCx { todo : vec ! [ ] , anon_allocs : FxHashMap :: default ( ) }
28
27
}
29
28
30
29
pub ( crate ) fn finalize ( mut self , tcx : TyCtxt < ' _ > , module : & mut dyn Module ) {
31
30
define_all_allocs ( tcx, module, & mut self ) ;
32
- self . done . clear ( ) ;
33
31
}
34
32
}
35
33
@@ -153,14 +151,12 @@ pub(crate) fn codegen_const_value<'tcx>(
153
151
fx. bcx . ins ( ) . func_addr ( fx. pointer_type , local_func_id)
154
152
}
155
153
GlobalAlloc :: VTable ( ty, trait_ref) => {
156
- let alloc_id = fx. tcx . vtable_allocation ( ( ty, trait_ref) ) ;
157
- let alloc = fx. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
158
- // FIXME: factor this common code with the `Memory` arm into a function?
159
- let data_id = data_id_for_alloc_id (
154
+ let data_id = data_id_for_vtable (
155
+ fx. tcx ,
160
156
& mut fx. constants_cx ,
161
157
fx. module ,
162
- alloc_id ,
163
- alloc . inner ( ) . mutability ,
158
+ ty ,
159
+ trait_ref ,
164
160
) ;
165
161
let local_data_id =
166
162
fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
@@ -208,12 +204,8 @@ fn pointer_for_allocation<'tcx>(
208
204
alloc_id : AllocId ,
209
205
) -> crate :: pointer:: Pointer {
210
206
let alloc = fx. tcx . global_alloc ( alloc_id) . unwrap_memory ( ) ;
211
- let data_id = data_id_for_alloc_id (
212
- & mut fx. constants_cx ,
213
- & mut * fx. module ,
214
- alloc_id,
215
- alloc. inner ( ) . mutability ,
216
- ) ;
207
+ let data_id =
208
+ data_id_for_alloc_id ( & mut fx. constants_cx , fx. module , alloc_id, alloc. inner ( ) . mutability ) ;
217
209
218
210
let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
219
211
if fx. clif_comments . enabled ( ) {
@@ -235,6 +227,17 @@ pub(crate) fn data_id_for_alloc_id(
235
227
. or_insert_with ( || module. declare_anonymous_data ( mutability. is_mut ( ) , false ) . unwrap ( ) )
236
228
}
237
229
230
+ pub ( crate ) fn data_id_for_vtable < ' tcx > (
231
+ tcx : TyCtxt < ' tcx > ,
232
+ cx : & mut ConstantCx ,
233
+ module : & mut dyn Module ,
234
+ ty : Ty < ' tcx > ,
235
+ trait_ref : Option < Binder < ' tcx , ExistentialTraitRef < ' tcx > > > ,
236
+ ) -> DataId {
237
+ let alloc_id = tcx. vtable_allocation ( ( ty, trait_ref) ) ;
238
+ data_id_for_alloc_id ( cx, module, alloc_id, Mutability :: Not )
239
+ }
240
+
238
241
fn data_id_for_static (
239
242
tcx : TyCtxt < ' _ > ,
240
243
module : & mut dyn Module ,
@@ -327,7 +330,12 @@ fn data_id_for_static(
327
330
}
328
331
329
332
fn define_all_allocs ( tcx : TyCtxt < ' _ > , module : & mut dyn Module , cx : & mut ConstantCx ) {
333
+ let mut done = FxHashSet :: default ( ) ;
330
334
while let Some ( todo_item) = cx. todo . pop ( ) {
335
+ if !done. insert ( todo_item) {
336
+ continue ;
337
+ }
338
+
331
339
let ( data_id, alloc, section_name) = match todo_item {
332
340
TodoItem :: Alloc ( alloc_id) => {
333
341
let alloc = match tcx. global_alloc ( alloc_id) {
@@ -358,10 +366,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
358
366
}
359
367
} ;
360
368
361
- if cx. done . contains ( & data_id) {
362
- continue ;
363
- }
364
-
365
369
let mut data = DataDescription :: new ( ) ;
366
370
let alloc = alloc. inner ( ) ;
367
371
data. set_align ( alloc. align . bytes ( ) ) ;
@@ -418,8 +422,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
418
422
data_id_for_alloc_id ( cx, module, alloc_id, target_alloc. inner ( ) . mutability )
419
423
}
420
424
GlobalAlloc :: VTable ( ty, trait_ref) => {
421
- let alloc_id = tcx. vtable_allocation ( ( ty, trait_ref) ) ;
422
- data_id_for_alloc_id ( cx, module, alloc_id, Mutability :: Not )
425
+ data_id_for_vtable ( tcx, cx, module, ty, trait_ref)
423
426
}
424
427
GlobalAlloc :: Static ( def_id) => {
425
428
if tcx. codegen_fn_attrs ( def_id) . flags . contains ( CodegenFnAttrFlags :: THREAD_LOCAL )
@@ -446,7 +449,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
446
449
}
447
450
448
451
module. define_data ( data_id, & data) . unwrap ( ) ;
449
- cx. done . insert ( data_id) ;
450
452
}
451
453
452
454
assert ! ( cx. todo. is_empty( ) , "{:?}" , cx. todo) ;
0 commit comments