Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable MonoItem #116465

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'tcx> Tables<'tcx> {
stable_mir::ty::Prov(self.create_alloc_id(aid))
}

fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
pub(super) fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
// FIXME: this becomes inefficient when we have too many ids
for (i, &d) in self.def_ids.iter().enumerate() {
if d == did {
Expand Down
59 changes: 59 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,3 +1610,62 @@ impl<'tcx> Stable<'tcx> for DefKind {
opaque(self)
}
}

impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> {
type T = stable_mir::ty::Instance;

fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
let ty::Instance { def, args } = self;
stable_mir::ty::Instance { def: def.stable(tables), args: args.stable(tables) }
}
}

impl<'tcx> Stable<'tcx> for ty::InstanceDef<'tcx> {
type T = stable_mir::ty::InstanceDef;

fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::InstanceDef::*;

match *self {
ty::InstanceDef::Item(did) => Item(tables.create_def_id(did)),
ty::InstanceDef::Intrinsic(did) => Intrinsic(tables.create_def_id(did)),
ty::InstanceDef::VTableShim(did) => VTableShim(tables.create_def_id(did)),
ty::InstanceDef::ReifyShim(did) => ReifyShim(tables.create_def_id(did)),
ty::InstanceDef::FnPtrShim(did, ty) => {
FnPtrShim(tables.create_def_id(did), tables.intern_ty(ty))
}
ty::InstanceDef::Virtual(did, vtable_idx) => {
Virtual(tables.create_def_id(did), vtable_idx)
}
ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
ClosureOnceShim { call_once: tables.create_def_id(call_once), track_caller }
}
ty::InstanceDef::ThreadLocalShim(did) => ThreadLocalShim(tables.create_def_id(did)),
ty::InstanceDef::DropGlue(did, drop_glue) => {
DropGlue(tables.create_def_id(did), drop_glue.map(|ty| tables.intern_ty(ty)))
}
ty::InstanceDef::CloneShim(did, ty) => {
CloneShim(tables.create_def_id(did), tables.intern_ty(ty))
}
ty::InstanceDef::FnPtrAddrShim(did, ty) => {
FnPtrAddrShim(tables.create_def_id(did), tables.intern_ty(ty))
}
}
}
}

impl<'tcx> Stable<'tcx> for mir::mono::MonoItem<'tcx> {
type T = stable_mir::mir::mono::MonoItem;

fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::mir::mono::MonoItem::*;

match *self {
mir::mono::MonoItem::Fn(instance) => Fn(instance.stable(tables)),
mir::mono::MonoItem::Static(did) => Static(tables.create_def_id(did)),
mir::mono::MonoItem::GlobalAsm(iid) => {
GlobalAsm(opaque(tables.tcx.hir().item(iid).expect_global_asm()))
}
}
}
}
1 change: 1 addition & 0 deletions compiler/stable_mir/src/mir.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod body;
pub mod mono;

pub use body::*;
8 changes: 8 additions & 0 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::{ty::Instance, DefId, Opaque};

#[derive(Clone, Debug)]
pub enum MonoItem {
Fn(Instance),
Static(DefId),
GlobalAsm(Opaque),
}
21 changes: 21 additions & 0 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,24 @@ pub enum ImplPolarity {
Negative,
Reservation,
}

#[derive(Clone, Debug)]
pub struct Instance {
pub def: InstanceDef,
pub args: GenericArgs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe merge this into the InstanceDef variants? For shims this duplicates information with the Ty and having it get out of sync could potentially crash the compiler and if it doesn't crash would likely misbehave.

}

#[derive(Clone, Debug)]
pub enum InstanceDef {
Item(DefId),
Intrinsic(DefId),
VTableShim(DefId),
ReifyShim(DefId),
FnPtrShim(DefId, Ty),
Virtual(DefId, usize),
ClosureOnceShim { call_once: DefId, track_caller: bool },
ThreadLocalShim(DefId),
DropGlue(DefId, Option<Ty>),
CloneShim(DefId, Ty),
FnPtrAddrShim(DefId, Ty),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should expose all the kinds of shims rustc has. This list changes occasionally. Instead I think you should have Item, Intrinsic, Virtual and a generic Shim variant which only exposes just enough to get the Stable MIR for the specific instantiation of said shim.

Loading