Skip to content

Commit 9610a3f

Browse files
authored
TyKind in GlobalAllocs (#84)
`GlobalAlloc`s need to be decoded to their types, this can be done easier by preprocessing the python (maybe in the future in the repo). But for the python to preprocess it will go easier with the `Ty` available in the JSON. - Changed `AllocInfo` to be a struct that contains the `GlobalAlloc` information, originally it was a enum that was a duplicate of `stable_mir::mir::alloc::GlobalAlloc`. The struct helps with the JSON as the field names come through. - Changed the functions that get the `TyKind` from the allocs and provs to work with `Ty` - `collect_alloc` also takes a `Ty` as a parameter instead of a `TyKind` now - `AllocMap` is now collects the `Ty` also
1 parent 8dcacda commit 9610a3f

26 files changed

+238
-224
lines changed

src/printer.rs

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use serde::{Serialize, Serializer};
3434
use stable_mir::{
3535
abi::LayoutShape,
3636
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+
},
3841
ty::{AdtDef, Allocation, ConstDef, ForeignItemKind, IndexedVal, RigidTy, TyKind},
3942
visitor::{Visitable, Visitor},
4043
CrateDef, CrateItem, ItemKind,
@@ -474,18 +477,8 @@ impl Serialize for ItemSource {
474477
}
475478
}
476479

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-
}
487480
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)>;
489482
type TyMap =
490483
HashMap<stable_mir::ty::Ty, (stable_mir::ty::TyKind, Option<stable_mir::abi::LayoutShape>)>;
491484
type SpanMap = HashMap<usize, (String, usize, usize, usize, usize)>;
@@ -644,15 +637,18 @@ fn update_link_map<'tcx>(
644637
}
645638
}
646639

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> {
648641
use stable_mir::ty::RigidTy;
642+
let pointer_kind = pointer_ty.kind();
649643
// 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();
653646
}
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(),
656652
RigidTy::FnPtr(_) | RigidTy::Adt(..) => None, // TODO: Check for Adt if the GenericArgs are related to prov
657653
unimplemented => {
658654
todo!("Unimplemented RigidTy allocation: {:?}", unimplemented);
@@ -662,47 +658,49 @@ fn get_prov_type(maybe_kind: Option<stable_mir::ty::TyKind>) -> Option<stable_mi
662658

663659
fn collect_alloc(
664660
val_collector: &mut InternedValueCollector,
665-
kind: Option<stable_mir::ty::TyKind>,
661+
ty: stable_mir::ty::Ty,
666662
val: stable_mir::mir::alloc::AllocId,
667663
) {
668-
use stable_mir::mir::alloc::GlobalAlloc;
669664
let entry = val_collector.visited_allocs.entry(val);
670665
if matches!(entry, std::collections::hash_map::Entry::Occupied(_)) {
671666
return;
672667
}
668+
let kind = ty.kind();
673669
let global_alloc = GlobalAlloc::from(val);
674670
match global_alloc {
675671
GlobalAlloc::Memory(ref alloc) => {
676-
let pointed_kind = get_prov_type(kind);
672+
let pointed_ty = get_prov_ty(ty);
677673
#[cfg(feature = "debug_log")]
678674
println!(
679675
"DEBUG: called collect_alloc: {:?}:{:?}:{:?}",
680-
val, pointed_kind, global_alloc
676+
val,
677+
pointed_ty.map(|ty| ty.kind()),
678+
global_alloc
681679
);
682-
entry.or_insert(AllocInfo::Memory(alloc.clone())); // TODO: include pointed_kind.clone().unwrap() ?
680+
entry.or_insert((ty, global_alloc.clone()));
683681
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);
685683
});
686684
}
687-
GlobalAlloc::Static(def) => {
685+
GlobalAlloc::Static(_) => {
688686
assert!(
689-
kind.clone().unwrap().builtin_deref(true).is_some(),
687+
kind.clone().builtin_deref(true).is_some(),
690688
"Allocated pointer is not a built-in pointer type: {:?}",
691689
kind
692690
);
693-
entry.or_insert(AllocInfo::Static(def));
691+
entry.or_insert((ty, global_alloc.clone()));
694692
}
695-
GlobalAlloc::VTable(ty, traitref) => {
693+
GlobalAlloc::VTable(_, _) => {
696694
assert!(
697-
kind.clone().unwrap().builtin_deref(true).is_some(),
695+
kind.clone().builtin_deref(true).is_some(),
698696
"Allocated pointer is not a built-in pointer type: {:?}",
699697
kind
700698
);
701-
entry.or_insert(AllocInfo::VTable(ty, traitref));
699+
entry.or_insert((ty, global_alloc.clone()));
702700
}
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()));
706704
}
707705
};
708706
}
@@ -785,9 +783,11 @@ impl MirVisitor for InternedValueCollector<'_, '_> {
785783
alloc,
786784
constant.ty().kind()
787785
);
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));
791791
}
792792
ConstantKind::Ty(ty_const) => {
793793
if let TyConstKind::Value(..) = ty_const.kind() {
@@ -1183,7 +1183,7 @@ type SourceData = (String, usize, usize, usize, usize);
11831183
pub struct SmirJson<'t> {
11841184
pub name: String,
11851185
pub crate_id: u64,
1186-
pub allocs: Vec<(AllocId, AllocInfo)>,
1186+
pub allocs: Vec<AllocInfo>,
11871187
pub functions: Vec<(LinkMapKey<'t>, FnSymType)>,
11881188
pub uneval_consts: Vec<(ConstDef, String)>,
11891189
pub items: Vec<Item>,
@@ -1200,6 +1200,13 @@ pub struct SmirJsonDebugInfo<'t> {
12001200
foreign_modules: Vec<(String, Vec<ForeignModule>)>,
12011201
}
12021202

1203+
#[derive(Serialize)]
1204+
pub struct AllocInfo {
1205+
alloc_id: AllocId,
1206+
ty: stable_mir::ty::Ty,
1207+
global_alloc: GlobalAlloc,
1208+
}
1209+
12031210
// Serialization Entrypoint
12041211
// ========================
12051212

@@ -1213,7 +1220,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
12131220

12141221
// FIXME: We dump extra static items here --- this should be handled better
12151222
for (_, alloc) in visited_allocs.iter() {
1216-
if let AllocInfo::Static(def) = alloc {
1223+
if let (_, GlobalAlloc::Static(def)) = alloc {
12171224
let mono_item =
12181225
stable_mir::mir::mono::MonoItem::Fn(stable_mir::mir::mono::Instance::from(*def));
12191226
let item_name = &mono_item_name(tcx, &mono_item);
@@ -1246,7 +1253,14 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
12461253
.into_iter()
12471254
.map(|(k, (_, name))| (k, name))
12481255
.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<_>>();
12501264
let crate_id = tcx.stable_crate_id(LOCAL_CRATE).as_u64();
12511265

12521266
let mut types = visited_tys
@@ -1258,7 +1272,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson {
12581272
let mut spans = span_map.into_iter().collect::<Vec<_>>();
12591273

12601274
// 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()));
12621276
functions.sort_by(|a, b| a.0 .0.to_index().cmp(&b.0 .0.to_index()));
12631277
items.sort();
12641278
types.sort_by(|a, b| a.0.to_index().cmp(&b.0.to_index()));

tests/integration/normalise-filter.jq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.functions = ( [ .functions[] | if .[1].NormalSym then .[1].NormalSym = .[1].NormalSym[:-17] else . end ] )
33
| .items = ( [ .items[] | if .symbol_name then .symbol_name = .symbol_name[:-17] else . end ] )
44
# delete unstable alloc, function, and type IDs
5-
| .allocs = ( [ .allocs[] ] | map(del(.[0])) )
5+
| .allocs = ( .allocs | map(del(.alloc_id)) | map(del(.ty)) )
66
| .functions = ( [ .functions[] ] | map(del(.[0])) )
77
| .types = ( [ .types[] ] | map(del(.[0])) )
88
# remove "Never" type

0 commit comments

Comments
 (0)