@@ -6,30 +6,28 @@ use cranelift_module::*;
66use rustc_data_structures:: fx:: FxHashSet ;
77use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
88use rustc_middle:: mir:: interpret:: { read_target_uint, AllocId , GlobalAlloc , Scalar } ;
9- use rustc_middle:: ty:: ScalarInt ;
9+ use rustc_middle:: ty:: { Binder , ExistentialTraitRef , ScalarInt } ;
1010
1111use crate :: prelude:: * ;
1212
1313pub ( crate ) struct ConstantCx {
1414 todo : Vec < TodoItem > ,
15- done : FxHashSet < DataId > ,
1615 anon_allocs : FxHashMap < AllocId , DataId > ,
1716}
1817
19- #[ derive( Copy , Clone , Debug ) ]
18+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
2019enum TodoItem {
2120 Alloc ( AllocId ) ,
2221 Static ( DefId ) ,
2322}
2423
2524impl ConstantCx {
2625 pub ( crate ) fn new ( ) -> Self {
27- ConstantCx { todo : vec ! [ ] , done : FxHashSet :: default ( ) , anon_allocs : FxHashMap :: default ( ) }
26+ ConstantCx { todo : vec ! [ ] , anon_allocs : FxHashMap :: default ( ) }
2827 }
2928
3029 pub ( crate ) fn finalize ( mut self , tcx : TyCtxt < ' _ > , module : & mut dyn Module ) {
3130 define_all_allocs ( tcx, module, & mut self ) ;
32- self . done . clear ( ) ;
3331 }
3432}
3533
@@ -153,14 +151,12 @@ pub(crate) fn codegen_const_value<'tcx>(
153151 fx. bcx . ins ( ) . func_addr ( fx. pointer_type , local_func_id)
154152 }
155153 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 ,
160156 & mut fx. constants_cx ,
161157 fx. module ,
162- alloc_id ,
163- alloc . inner ( ) . mutability ,
158+ ty ,
159+ trait_ref ,
164160 ) ;
165161 let local_data_id =
166162 fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
@@ -208,12 +204,8 @@ fn pointer_for_allocation<'tcx>(
208204 alloc_id : AllocId ,
209205) -> crate :: pointer:: Pointer {
210206 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 ) ;
217209
218210 let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
219211 if fx. clif_comments . enabled ( ) {
@@ -235,6 +227,17 @@ pub(crate) fn data_id_for_alloc_id(
235227 . or_insert_with ( || module. declare_anonymous_data ( mutability. is_mut ( ) , false ) . unwrap ( ) )
236228}
237229
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+
238241fn data_id_for_static (
239242 tcx : TyCtxt < ' _ > ,
240243 module : & mut dyn Module ,
@@ -327,7 +330,12 @@ fn data_id_for_static(
327330}
328331
329332fn define_all_allocs ( tcx : TyCtxt < ' _ > , module : & mut dyn Module , cx : & mut ConstantCx ) {
333+ let mut done = FxHashSet :: default ( ) ;
330334 while let Some ( todo_item) = cx. todo . pop ( ) {
335+ if !done. insert ( todo_item) {
336+ continue ;
337+ }
338+
331339 let ( data_id, alloc, section_name) = match todo_item {
332340 TodoItem :: Alloc ( alloc_id) => {
333341 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
358366 }
359367 } ;
360368
361- if cx. done . contains ( & data_id) {
362- continue ;
363- }
364-
365369 let mut data = DataDescription :: new ( ) ;
366370 let alloc = alloc. inner ( ) ;
367371 data. set_align ( alloc. align . bytes ( ) ) ;
@@ -418,8 +422,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
418422 data_id_for_alloc_id ( cx, module, alloc_id, target_alloc. inner ( ) . mutability )
419423 }
420424 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)
423426 }
424427 GlobalAlloc :: Static ( def_id) => {
425428 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
446449 }
447450
448451 module. define_data ( data_id, & data) . unwrap ( ) ;
449- cx. done . insert ( data_id) ;
450452 }
451453
452454 assert ! ( cx. todo. is_empty( ) , "{:?}" , cx. todo) ;
0 commit comments