@@ -34,7 +34,10 @@ use serde::{Serialize, Serializer};
34
34
use stable_mir:: {
35
35
abi:: LayoutShape ,
36
36
mir:: mono:: { Instance , InstanceKind , MonoItem } ,
37
- mir:: { alloc:: AllocId , visit:: MirVisitor , Body , LocalDecl , Rvalue , Terminator , TerminatorKind } ,
37
+ mir:: {
38
+ alloc:: AllocId , alloc:: GlobalAlloc , visit:: MirVisitor , Body , LocalDecl , Rvalue , Terminator ,
39
+ TerminatorKind ,
40
+ } ,
38
41
ty:: { AdtDef , Allocation , ConstDef , ForeignItemKind , IndexedVal , RigidTy , TyKind } ,
39
42
visitor:: { Visitable , Visitor } ,
40
43
CrateDef , CrateItem , ItemKind ,
@@ -474,18 +477,8 @@ impl Serialize for ItemSource {
474
477
}
475
478
}
476
479
477
- #[ derive( Serialize ) ]
478
- pub enum AllocInfo {
479
- Function ( stable_mir:: mir:: mono:: Instance ) ,
480
- VTable (
481
- stable_mir:: ty:: Ty ,
482
- Option < stable_mir:: ty:: Binder < stable_mir:: ty:: ExistentialTraitRef > > ,
483
- ) ,
484
- Static ( stable_mir:: mir:: mono:: StaticDef ) ,
485
- Memory ( stable_mir:: ty:: Allocation ) , // TODO include stable_mir::ty::TyKind?
486
- }
487
480
type LinkMap < ' tcx > = HashMap < LinkMapKey < ' tcx > , ( ItemSource , FnSymType ) > ;
488
- type AllocMap = HashMap < stable_mir:: mir:: alloc:: AllocId , AllocInfo > ;
481
+ type AllocMap = HashMap < stable_mir:: mir:: alloc:: AllocId , ( stable_mir :: ty :: Ty , GlobalAlloc ) > ;
489
482
type TyMap =
490
483
HashMap < stable_mir:: ty:: Ty , ( stable_mir:: ty:: TyKind , Option < stable_mir:: abi:: LayoutShape > ) > ;
491
484
type SpanMap = HashMap < usize , ( String , usize , usize , usize , usize ) > ;
@@ -644,15 +637,18 @@ fn update_link_map<'tcx>(
644
637
}
645
638
}
646
639
647
- fn get_prov_type ( maybe_kind : Option < stable_mir:: ty:: TyKind > ) -> Option < stable_mir:: ty:: TyKind > {
640
+ fn get_prov_ty ( pointer_ty : stable_mir:: ty:: Ty ) -> Option < stable_mir:: ty:: Ty > {
648
641
use stable_mir:: ty:: RigidTy ;
642
+ let pointer_kind = pointer_ty. kind ( ) ;
649
643
// check for pointers
650
- let kind = maybe_kind?;
651
- if let Some ( ty) = kind. builtin_deref ( true ) {
652
- return ty. ty . kind ( ) . into ( ) ;
644
+ if let Some ( ty) = pointer_kind. builtin_deref ( true ) {
645
+ return ty. ty . into ( ) ;
653
646
}
654
- match kind. rigid ( ) . expect ( "Non-rigid-ty allocation found!" ) {
655
- RigidTy :: Array ( ty, _) | RigidTy :: Slice ( ty) | RigidTy :: Ref ( _, ty, _) => ty. kind ( ) . into ( ) ,
647
+ match pointer_kind
648
+ . rigid ( )
649
+ . expect ( "Non-rigid-ty allocation found!" )
650
+ {
651
+ RigidTy :: Array ( ty, _) | RigidTy :: Slice ( ty) | RigidTy :: Ref ( _, ty, _) => ( * ty) . into ( ) ,
656
652
RigidTy :: FnPtr ( _) | RigidTy :: Adt ( ..) => None , // TODO: Check for Adt if the GenericArgs are related to prov
657
653
unimplemented => {
658
654
todo ! ( "Unimplemented RigidTy allocation: {:?}" , unimplemented) ;
@@ -662,47 +658,49 @@ fn get_prov_type(maybe_kind: Option<stable_mir::ty::TyKind>) -> Option<stable_mi
662
658
663
659
fn collect_alloc (
664
660
val_collector : & mut InternedValueCollector ,
665
- kind : Option < stable_mir:: ty:: TyKind > ,
661
+ ty : stable_mir:: ty:: Ty ,
666
662
val : stable_mir:: mir:: alloc:: AllocId ,
667
663
) {
668
- use stable_mir:: mir:: alloc:: GlobalAlloc ;
669
664
let entry = val_collector. visited_allocs . entry ( val) ;
670
665
if matches ! ( entry, std:: collections:: hash_map:: Entry :: Occupied ( _) ) {
671
666
return ;
672
667
}
668
+ let kind = ty. kind ( ) ;
673
669
let global_alloc = GlobalAlloc :: from ( val) ;
674
670
match global_alloc {
675
671
GlobalAlloc :: Memory ( ref alloc) => {
676
- let pointed_kind = get_prov_type ( kind ) ;
672
+ let pointed_ty = get_prov_ty ( ty ) ;
677
673
#[ cfg( feature = "debug_log" ) ]
678
674
println ! (
679
675
"DEBUG: called collect_alloc: {:?}:{:?}:{:?}" ,
680
- val, pointed_kind, global_alloc
676
+ val,
677
+ pointed_ty. map( |ty| ty. kind( ) ) ,
678
+ global_alloc
681
679
) ;
682
- entry. or_insert ( AllocInfo :: Memory ( alloc . clone ( ) ) ) ; // TODO: include pointed_kind.clone().unwrap() ?
680
+ entry. or_insert ( ( ty , global_alloc . clone ( ) ) ) ;
683
681
alloc. provenance . ptrs . iter ( ) . for_each ( |( _, prov) | {
684
- collect_alloc ( val_collector, pointed_kind . clone ( ) , prov. 0 ) ;
682
+ collect_alloc ( val_collector, pointed_ty . unwrap ( ) , prov. 0 ) ;
685
683
} ) ;
686
684
}
687
- GlobalAlloc :: Static ( def ) => {
685
+ GlobalAlloc :: Static ( _ ) => {
688
686
assert ! (
689
- kind. clone( ) . unwrap ( ) . builtin_deref( true ) . is_some( ) ,
687
+ kind. clone( ) . builtin_deref( true ) . is_some( ) ,
690
688
"Allocated pointer is not a built-in pointer type: {:?}" ,
691
689
kind
692
690
) ;
693
- entry. or_insert ( AllocInfo :: Static ( def ) ) ;
691
+ entry. or_insert ( ( ty , global_alloc . clone ( ) ) ) ;
694
692
}
695
- GlobalAlloc :: VTable ( ty , traitref ) => {
693
+ GlobalAlloc :: VTable ( _ , _ ) => {
696
694
assert ! (
697
- kind. clone( ) . unwrap ( ) . builtin_deref( true ) . is_some( ) ,
695
+ kind. clone( ) . builtin_deref( true ) . is_some( ) ,
698
696
"Allocated pointer is not a built-in pointer type: {:?}" ,
699
697
kind
700
698
) ;
701
- entry. or_insert ( AllocInfo :: VTable ( ty, traitref ) ) ;
699
+ entry. or_insert ( ( ty, global_alloc . clone ( ) ) ) ;
702
700
}
703
- GlobalAlloc :: Function ( inst ) => {
704
- assert ! ( kind. unwrap ( ) . is_fn_ptr( ) ) ;
705
- entry. or_insert ( AllocInfo :: Function ( inst ) ) ;
701
+ GlobalAlloc :: Function ( _ ) => {
702
+ assert ! ( kind. is_fn_ptr( ) ) ;
703
+ entry. or_insert ( ( ty , global_alloc . clone ( ) ) ) ;
706
704
}
707
705
} ;
708
706
}
@@ -785,9 +783,11 @@ impl MirVisitor for InternedValueCollector<'_, '_> {
785
783
alloc,
786
784
constant. ty( ) . kind( )
787
785
) ;
788
- alloc. provenance . ptrs . iter ( ) . for_each ( |( _offset, prov) | {
789
- collect_alloc ( self , Some ( constant. ty ( ) . kind ( ) ) , prov. 0 )
790
- } ) ;
786
+ alloc
787
+ . provenance
788
+ . ptrs
789
+ . iter ( )
790
+ . for_each ( |( _offset, prov) | collect_alloc ( self , constant. ty ( ) , prov. 0 ) ) ;
791
791
}
792
792
ConstantKind :: Ty ( ty_const) => {
793
793
if let TyConstKind :: Value ( ..) = ty_const. kind ( ) {
@@ -1183,7 +1183,7 @@ type SourceData = (String, usize, usize, usize, usize);
1183
1183
pub struct SmirJson < ' t > {
1184
1184
pub name : String ,
1185
1185
pub crate_id : u64 ,
1186
- pub allocs : Vec < ( AllocId , AllocInfo ) > ,
1186
+ pub allocs : Vec < AllocInfo > ,
1187
1187
pub functions : Vec < ( LinkMapKey < ' t > , FnSymType ) > ,
1188
1188
pub uneval_consts : Vec < ( ConstDef , String ) > ,
1189
1189
pub items : Vec < Item > ,
@@ -1200,6 +1200,13 @@ pub struct SmirJsonDebugInfo<'t> {
1200
1200
foreign_modules : Vec < ( String , Vec < ForeignModule > ) > ,
1201
1201
}
1202
1202
1203
+ #[ derive( Serialize ) ]
1204
+ pub struct AllocInfo {
1205
+ alloc_id : AllocId ,
1206
+ ty : stable_mir:: ty:: Ty ,
1207
+ global_alloc : GlobalAlloc ,
1208
+ }
1209
+
1203
1210
// Serialization Entrypoint
1204
1211
// ========================
1205
1212
@@ -1213,7 +1220,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
1213
1220
1214
1221
// FIXME: We dump extra static items here --- this should be handled better
1215
1222
for ( _, alloc) in visited_allocs. iter ( ) {
1216
- if let AllocInfo :: Static ( def) = alloc {
1223
+ if let ( _ , GlobalAlloc :: Static ( def) ) = alloc {
1217
1224
let mono_item =
1218
1225
stable_mir:: mir:: mono:: MonoItem :: Fn ( stable_mir:: mir:: mono:: Instance :: from ( * def) ) ;
1219
1226
let item_name = & mono_item_name ( tcx, & mono_item) ;
@@ -1246,7 +1253,14 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
1246
1253
. into_iter ( )
1247
1254
. map ( |( k, ( _, name) ) | ( k, name) )
1248
1255
. collect :: < Vec < _ > > ( ) ;
1249
- let mut allocs = visited_allocs. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
1256
+ let mut allocs = visited_allocs
1257
+ . into_iter ( )
1258
+ . map ( |( alloc_id, ( ty, global_alloc) ) | AllocInfo {
1259
+ alloc_id,
1260
+ ty,
1261
+ global_alloc,
1262
+ } )
1263
+ . collect :: < Vec < _ > > ( ) ;
1250
1264
let crate_id = tcx. stable_crate_id ( LOCAL_CRATE ) . as_u64 ( ) ;
1251
1265
1252
1266
let mut types = visited_tys
@@ -1258,7 +1272,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
1258
1272
let mut spans = span_map. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
1259
1273
1260
1274
// sort output vectors to stabilise output (a bit)
1261
- allocs. sort_by ( |a, b| a. 0 . to_index ( ) . cmp ( & b. 0 . to_index ( ) ) ) ;
1275
+ allocs. sort_by ( |a, b| a. alloc_id . to_index ( ) . cmp ( & b. alloc_id . to_index ( ) ) ) ;
1262
1276
functions. sort_by ( |a, b| a. 0 . 0 . to_index ( ) . cmp ( & b. 0 . 0 . to_index ( ) ) ) ;
1263
1277
items. sort ( ) ;
1264
1278
types. sort_by ( |a, b| a. 0 . to_index ( ) . cmp ( & b. 0 . to_index ( ) ) ) ;
0 commit comments