From b43e3b9f41e1bc4f3d294196103563983e2d8906 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 14:31:14 +0000 Subject: [PATCH 1/7] Add type folder to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 42 +++- compiler/rustc_smir/src/stable_mir/fold.rs | 207 ++++++++++++++++++ .../rustc_smir/src/stable_mir/mir/body.rs | 2 +- compiler/rustc_smir/src/stable_mir/mod.rs | 4 + compiler/rustc_smir/src/stable_mir/ty.rs | 6 + 5 files changed, 255 insertions(+), 6 deletions(-) create mode 100644 compiler/rustc_smir/src/stable_mir/fold.rs diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 52ba4bd4e579d..671928a63c2bf 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -103,8 +103,13 @@ impl<'tcx> Context for Tables<'tcx> { } fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind { - let ty = self.types[ty.0]; - ty.stable(self) + self.types[ty.0].clone().stable(self) + } + + fn mk_ty(&mut self, kind: TyKind) -> stable_mir::ty::Ty { + let n = self.types.len(); + self.types.push(MaybeStable::Stable(kind)); + stable_mir::ty::Ty(n) } fn generics_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics { @@ -128,20 +133,47 @@ impl<'tcx> Context for Tables<'tcx> { } } +#[derive(Clone)] +pub enum MaybeStable { + Stable(S), + Rustc(R), +} + +impl<'tcx, S, R> MaybeStable { + fn stable(self, tables: &mut Tables<'tcx>) -> S + where + R: Stable<'tcx, T = S>, + { + match self { + MaybeStable::Stable(s) => s, + MaybeStable::Rustc(r) => r.stable(tables), + } + } +} + +impl PartialEq for MaybeStable { + fn eq(&self, other: &R) -> bool { + match self { + MaybeStable::Stable(_) => false, + MaybeStable::Rustc(r) => r == other, + } + } +} + pub struct Tables<'tcx> { pub tcx: TyCtxt<'tcx>, pub def_ids: Vec, pub alloc_ids: Vec, - pub types: Vec>, + pub types: Vec>>, } impl<'tcx> Tables<'tcx> { fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty { - if let Some(id) = self.types.iter().position(|&t| t == ty) { + if let Some(id) = self.types.iter().position(|t| *t == ty) { return stable_mir::ty::Ty(id); } let id = self.types.len(); - self.types.push(ty); + self.types.push(MaybeStable::Rustc(ty)); stable_mir::ty::Ty(id) } } diff --git a/compiler/rustc_smir/src/stable_mir/fold.rs b/compiler/rustc_smir/src/stable_mir/fold.rs new file mode 100644 index 0000000000000..560a99cb4973f --- /dev/null +++ b/compiler/rustc_smir/src/stable_mir/fold.rs @@ -0,0 +1,207 @@ +use std::ops::ControlFlow; + +use crate::rustc_internal::Opaque; + +use super::ty::{ + Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, + Promoted, RigidTy, TermKind, Ty, UnevaluatedConst, +}; + +pub trait Folder: Sized { + type Break; + fn visit_ty(&mut self, ty: &Ty) -> ControlFlow { + ty.super_fold(self) + } + fn fold_const(&mut self, c: &Const) -> ControlFlow { + c.super_fold(self) + } +} + +pub trait Foldable: Sized + Clone { + fn fold(&self, folder: &mut V) -> ControlFlow { + self.super_fold(folder) + } + fn super_fold(&self, folder: &mut V) -> ControlFlow; +} + +impl Foldable for Ty { + fn fold(&self, folder: &mut V) -> ControlFlow { + folder.visit_ty(self) + } + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut kind = self.kind(); + match &mut kind { + super::ty::TyKind::RigidTy(ty) => *ty = ty.fold(folder)?, + super::ty::TyKind::Alias(_, alias) => alias.args = alias.args.fold(folder)?, + super::ty::TyKind::Param(_) => {} + super::ty::TyKind::Bound(_, _) => {} + } + ControlFlow::Continue(kind.into()) + } +} + +impl Foldable for Const { + fn fold(&self, folder: &mut V) -> ControlFlow { + folder.fold_const(self) + } + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut this = self.clone(); + match &mut this.literal { + super::ty::ConstantKind::Allocated(alloc) => *alloc = alloc.fold(folder)?, + super::ty::ConstantKind::Unevaluated(uv) => *uv = uv.fold(folder)?, + super::ty::ConstantKind::ParamCt(param) => *param = param.fold(folder)?, + } + ControlFlow::Continue(this) + } +} + +impl Foldable for Opaque { + fn super_fold(&self, _folder: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) + } +} + +impl Foldable for Allocation { + fn super_fold(&self, _folder: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) + } +} + +impl Foldable for UnevaluatedConst { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let UnevaluatedConst { ty, def, args, promoted } = self; + ControlFlow::Continue(UnevaluatedConst { + ty: ty.fold(folder)?, + def: def.fold(folder)?, + args: args.fold(folder)?, + promoted: promoted.fold(folder)?, + }) + } +} + +impl Foldable for ConstDef { + fn super_fold(&self, _folder: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) + } +} + +impl Foldable for Option { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(match self { + Some(val) => Some(val.fold(folder)?), + None => None, + }) + } +} + +impl Foldable for Promoted { + fn super_fold(&self, _folder: &mut V) -> ControlFlow { + ControlFlow::Continue(self.clone()) + } +} + +impl Foldable for GenericArgs { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(GenericArgs(self.0.fold(folder)?)) + } +} + +impl Foldable for GenericArgKind { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut this = self.clone(); + match &mut this { + GenericArgKind::Lifetime(lt) => *lt = lt.fold(folder)?, + GenericArgKind::Type(t) => *t = t.fold(folder)?, + GenericArgKind::Const(c) => *c = c.fold(folder)?, + } + ControlFlow::Continue(this) + } +} + +impl Foldable for RigidTy { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut this = self.clone(); + match &mut this { + RigidTy::Bool + | RigidTy::Char + | RigidTy::Int(_) + | RigidTy::Uint(_) + | RigidTy::Float(_) + | RigidTy::Never + | RigidTy::Foreign(_) + | RigidTy::Str => {} + RigidTy::Array(t, c) => { + *t = t.fold(folder)?; + *c = c.fold(folder)?; + } + RigidTy::Slice(inner) => *inner = inner.fold(folder)?, + RigidTy::RawPtr(ty, _) => *ty = ty.fold(folder)?, + RigidTy::Ref(_, ty, _) => *ty = ty.fold(folder)?, + RigidTy::FnDef(_, args) => *args = args.fold(folder)?, + RigidTy::FnPtr(sig) => *sig = sig.fold(folder)?, + RigidTy::Closure(_, args) => *args = args.fold(folder)?, + RigidTy::Generator(_, args, _) => *args = args.fold(folder)?, + RigidTy::Dynamic(pred, r, _) => { + *pred = pred.fold(folder)?; + *r = r.fold(folder)?; + } + RigidTy::Tuple(fields) => *fields = fields.fold(folder)?, + RigidTy::Adt(_, args) => *args = args.fold(folder)?, + } + ControlFlow::Continue(this) + } +} + +impl Foldable for Vec { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut this = self.clone(); + for arg in &mut this { + *arg = arg.fold(folder)?; + } + ControlFlow::Continue(this) + } +} + +impl Foldable for Binder { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(Self { + value: self.value.fold(folder)?, + bound_vars: self.bound_vars.clone(), + }) + } +} + +impl Foldable for ExistentialPredicate { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + let mut this = self.clone(); + match &mut this { + ExistentialPredicate::Trait(tr) => tr.generic_args = tr.generic_args.fold(folder)?, + ExistentialPredicate::Projection(p) => { + p.term = p.term.fold(folder)?; + p.generic_args = p.generic_args.fold(folder)?; + } + ExistentialPredicate::AutoTrait(_) => {} + } + ControlFlow::Continue(this) + } +} + +impl Foldable for TermKind { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(match self { + TermKind::Type(t) => TermKind::Type(t.fold(folder)?), + TermKind::Const(c) => TermKind::Const(c.fold(folder)?), + }) + } +} + +impl Foldable for FnSig { + fn super_fold(&self, folder: &mut V) -> ControlFlow { + ControlFlow::Continue(Self { + inputs_and_output: self.inputs_and_output.fold(folder)?, + c_variadic: self.c_variadic, + unsafety: self.unsafety, + abi: self.abi.clone(), + }) + } +} diff --git a/compiler/rustc_smir/src/stable_mir/mir/body.rs b/compiler/rustc_smir/src/stable_mir/mir/body.rs index 72f719c2a5e94..5957ae0ada9e7 100644 --- a/compiler/rustc_smir/src/stable_mir/mir/body.rs +++ b/compiler/rustc_smir/src/stable_mir/mir/body.rs @@ -391,7 +391,7 @@ pub enum Mutability { Mut, } -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] pub enum Safety { Unsafe, Normal, diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index f9eafd9de7ad8..90fc569f47bb8 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -20,6 +20,7 @@ use self::ty::{ }; use crate::rustc_smir::Tables; +pub mod fold; pub mod mir; pub mod ty; pub mod visitor; @@ -158,6 +159,9 @@ pub trait Context { /// Obtain the representation of a type. fn ty_kind(&mut self, ty: Ty) -> TyKind; + /// Create a new `Ty` from scratch without information from rustc. + fn mk_ty(&mut self, kind: TyKind) -> Ty; + /// HACK: Until we have fully stable consumers, we need an escape hatch /// to get `DefId`s out of `CrateItem`s. fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>)); diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 1db6b1e3d28eb..5289e233a31dd 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -10,6 +10,12 @@ impl Ty { } } +impl From for Ty { + fn from(value: TyKind) -> Self { + with(|context| context.mk_ty(value)) + } +} + #[derive(Debug, Clone)] pub struct Const { pub literal: ConstantKind, From 7f009e54bd318bdd1e17481442c2331ae0ea9c44 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 14:32:42 +0000 Subject: [PATCH 2/7] Fail to test argument instantiation since we don't have types for most constants --- tests/ui-fulldeps/stable-mir/crate-info.rs | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 182f97373ea44..6bdfed947ce6d 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -110,6 +110,23 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> { other => panic!("{other:?}"), } + let monomorphic = get_item(tcx, &items, (DefKind::Fn, "monomorphic")).unwrap(); + for block in monomorphic.body().blocks { + match &block.terminator { + stable_mir::mir::Terminator::Call { func, .. } => match func { + stable_mir::mir::Operand::Constant(c) => match &c.literal { + stable_mir::ty::ConstantKind::Allocated(alloc) => { + assert!(alloc.bytes.is_empty()) + } + other => panic!("{other:?}"), + }, + other => panic!("{other:?}"), + }, + stable_mir::mir::Terminator::Return => {} + other => panic!("{other:?}"), + } + } + ControlFlow::Continue(()) } @@ -147,6 +164,16 @@ fn generate_input(path: &str) -> std::io::Result<()> { write!( file, r#" + fn generic(t: T) -> [(); U] {{ + _ = t; + [(); U] + }} + + pub fn monomorphic() {{ + generic::<(), 5>(()); + generic::(45); + }} + mod foo {{ pub fn bar(i: i32) -> i64 {{ i as i64 From a370f1baa32bac446542a74c81dd7f99e12dc8fe Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 14:37:30 +0000 Subject: [PATCH 3/7] Also use `Const` in `SMIR` instead of just `ConstantKind` --- compiler/rustc_smir/src/rustc_smir/mod.rs | 30 +++++++++++-------- .../rustc_smir/src/stable_mir/mir/body.rs | 4 +-- tests/ui-fulldeps/stable-mir/crate-info.rs | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 671928a63c2bf..aea9d3ec5804c 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1216,22 +1216,26 @@ impl<'tcx> Stable<'tcx> for ty::TraitDef { } impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> { - type T = stable_mir::ty::ConstantKind; + type T = stable_mir::ty::Const; fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { match *self { - ConstantKind::Ty(c) => c.stable(tables).literal, - ConstantKind::Unevaluated(unev_const, ty) => { - stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst { - ty: tables.intern_ty(ty), - def: tables.const_def(unev_const.def), - args: unev_const.args.stable(tables), - promoted: unev_const.promoted.map(|u| u.as_u32()), - }) - } - ConstantKind::Val(val, ty) => { - stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(ty, val, tables)) - } + ConstantKind::Ty(c) => c.stable(tables), + ConstantKind::Unevaluated(unev_const, ty) => stable_mir::ty::Const { + literal: stable_mir::ty::ConstantKind::Unevaluated( + stable_mir::ty::UnevaluatedConst { + ty: tables.intern_ty(ty), + def: tables.const_def(unev_const.def), + args: unev_const.args.stable(tables), + promoted: unev_const.promoted.map(|u| u.as_u32()), + }, + ), + }, + ConstantKind::Val(val, ty) => stable_mir::ty::Const { + literal: stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation( + ty, val, tables, + )), + }, } } } diff --git a/compiler/rustc_smir/src/stable_mir/mir/body.rs b/compiler/rustc_smir/src/stable_mir/mir/body.rs index 5957ae0ada9e7..449ca4b8145ed 100644 --- a/compiler/rustc_smir/src/stable_mir/mir/body.rs +++ b/compiler/rustc_smir/src/stable_mir/mir/body.rs @@ -1,6 +1,6 @@ use crate::rustc_internal::Opaque; use crate::stable_mir::ty::{ - AdtDef, ClosureDef, Const, ConstantKind, GeneratorDef, GenericArgs, Movability, Region, + AdtDef, ClosureDef, Const, GeneratorDef, GenericArgs, Movability, Region, }; use crate::stable_mir::{self, ty::Ty, Span}; @@ -352,7 +352,7 @@ type UserTypeAnnotationIndex = usize; pub struct Constant { pub span: Span, pub user_ty: Option, - pub literal: ConstantKind, + pub literal: Const, } #[derive(Clone, Debug)] diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 6bdfed947ce6d..31ad42b431dbb 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -114,7 +114,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> { for block in monomorphic.body().blocks { match &block.terminator { stable_mir::mir::Terminator::Call { func, .. } => match func { - stable_mir::mir::Operand::Constant(c) => match &c.literal { + stable_mir::mir::Operand::Constant(c) => match &c.literal.literal { stable_mir::ty::ConstantKind::Allocated(alloc) => { assert!(alloc.bytes.is_empty()) } From 627fa80bdfe0889688e344a7b7dbaf586decd1a6 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 15:07:17 +0000 Subject: [PATCH 4/7] Add types to all constants --- compiler/rustc_smir/src/rustc_smir/mod.rs | 5 +++-- compiler/rustc_smir/src/stable_mir/fold.rs | 4 ++-- compiler/rustc_smir/src/stable_mir/ty.rs | 2 +- compiler/rustc_smir/src/stable_mir/visitor.rs | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index aea9d3ec5804c..6df0138262192 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1136,7 +1136,6 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> { ty::PlaceholderCt(_) => unimplemented!(), ty::Unevaluated(uv) => { stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst { - ty: tables.intern_ty(self.ty()), def: tables.const_def(uv.def), args: uv.args.stable(tables), promoted: None, @@ -1144,6 +1143,7 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> { } ty::ExprCt(_) => unimplemented!(), }, + ty: tables.intern_ty(self.ty()), } } } @@ -1224,17 +1224,18 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> { ConstantKind::Unevaluated(unev_const, ty) => stable_mir::ty::Const { literal: stable_mir::ty::ConstantKind::Unevaluated( stable_mir::ty::UnevaluatedConst { - ty: tables.intern_ty(ty), def: tables.const_def(unev_const.def), args: unev_const.args.stable(tables), promoted: unev_const.promoted.map(|u| u.as_u32()), }, ), + ty: tables.intern_ty(ty), }, ConstantKind::Val(val, ty) => stable_mir::ty::Const { literal: stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation( ty, val, tables, )), + ty: tables.intern_ty(ty), }, } } diff --git a/compiler/rustc_smir/src/stable_mir/fold.rs b/compiler/rustc_smir/src/stable_mir/fold.rs index 560a99cb4973f..89f282d1afbf0 100644 --- a/compiler/rustc_smir/src/stable_mir/fold.rs +++ b/compiler/rustc_smir/src/stable_mir/fold.rs @@ -51,6 +51,7 @@ impl Foldable for Const { super::ty::ConstantKind::Unevaluated(uv) => *uv = uv.fold(folder)?, super::ty::ConstantKind::ParamCt(param) => *param = param.fold(folder)?, } + this.ty = this.ty.fold(folder)?; ControlFlow::Continue(this) } } @@ -69,9 +70,8 @@ impl Foldable for Allocation { impl Foldable for UnevaluatedConst { fn super_fold(&self, folder: &mut V) -> ControlFlow { - let UnevaluatedConst { ty, def, args, promoted } = self; + let UnevaluatedConst { def, args, promoted } = self; ControlFlow::Continue(UnevaluatedConst { - ty: ty.fold(folder)?, def: def.fold(folder)?, args: args.fold(folder)?, promoted: promoted.fold(folder)?, diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 5289e233a31dd..2361f6efe0d1f 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -19,6 +19,7 @@ impl From for Ty { #[derive(Debug, Clone)] pub struct Const { pub literal: ConstantKind, + pub ty: Ty, } type Ident = Opaque; @@ -298,7 +299,6 @@ pub enum ConstantKind { #[derive(Clone, Debug)] pub struct UnevaluatedConst { - pub ty: Ty, pub def: ConstDef, pub args: GenericArgs, pub promoted: Option, diff --git a/compiler/rustc_smir/src/stable_mir/visitor.rs b/compiler/rustc_smir/src/stable_mir/visitor.rs index c928eb1381f79..6f0d96fae3757 100644 --- a/compiler/rustc_smir/src/stable_mir/visitor.rs +++ b/compiler/rustc_smir/src/stable_mir/visitor.rs @@ -47,7 +47,8 @@ impl Visitable for Const { super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor), super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor), super::ty::ConstantKind::ParamCt(param) => param.visit(visitor), - } + }?; + self.ty.visit(visitor) } } @@ -65,8 +66,7 @@ impl Visitable for Allocation { impl Visitable for UnevaluatedConst { fn super_visit(&self, visitor: &mut V) -> ControlFlow { - let UnevaluatedConst { ty, def, args, promoted } = self; - ty.visit(visitor)?; + let UnevaluatedConst { def, args, promoted } = self; def.visit(visitor)?; args.visit(visitor)?; promoted.visit(visitor) From 98d26d9c4d8f0e94ad347723606449a74596dfba Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 15:17:27 +0000 Subject: [PATCH 5/7] Deopaquify `ParamConst` --- compiler/rustc_smir/src/rustc_smir/mod.rs | 10 +++++++++- compiler/rustc_smir/src/stable_mir/fold.rs | 2 +- compiler/rustc_smir/src/stable_mir/ty.rs | 8 +++++++- compiler/rustc_smir/src/stable_mir/visitor.rs | 17 +++++++++-------- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 6df0138262192..c50cc7cd7cdb5 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -1129,7 +1129,7 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> { tables, )) } - ty::ParamCt(param) => stable_mir::ty::ConstantKind::ParamCt(opaque(¶m)), + ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)), ty::ErrorCt(_) => unreachable!(), ty::InferCt(_) => unreachable!(), ty::BoundCt(_, _) => unimplemented!(), @@ -1148,6 +1148,14 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> { } } +impl<'tcx> Stable<'tcx> for ty::ParamConst { + type T = stable_mir::ty::ParamConst; + fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { + use stable_mir::ty::ParamConst; + ParamConst { index: self.index, name: self.name.to_string() } + } +} + impl<'tcx> Stable<'tcx> for ty::ParamTy { type T = stable_mir::ty::ParamTy; fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { diff --git a/compiler/rustc_smir/src/stable_mir/fold.rs b/compiler/rustc_smir/src/stable_mir/fold.rs index 89f282d1afbf0..3567a5c5a3db6 100644 --- a/compiler/rustc_smir/src/stable_mir/fold.rs +++ b/compiler/rustc_smir/src/stable_mir/fold.rs @@ -49,7 +49,7 @@ impl Foldable for Const { match &mut this.literal { super::ty::ConstantKind::Allocated(alloc) => *alloc = alloc.fold(folder)?, super::ty::ConstantKind::Unevaluated(uv) => *uv = uv.fold(folder)?, - super::ty::ConstantKind::ParamCt(param) => *param = param.fold(folder)?, + super::ty::ConstantKind::Param(_) => {} } this.ty = this.ty.fold(folder)?; ControlFlow::Continue(this) diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 2361f6efe0d1f..7c58b640ec77f 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -294,7 +294,13 @@ pub struct Allocation { pub enum ConstantKind { Allocated(Allocation), Unevaluated(UnevaluatedConst), - ParamCt(Opaque), + Param(ParamConst), +} + +#[derive(Clone, Debug)] +pub struct ParamConst { + pub index: u32, + pub name: String, } #[derive(Clone, Debug)] diff --git a/compiler/rustc_smir/src/stable_mir/visitor.rs b/compiler/rustc_smir/src/stable_mir/visitor.rs index 6f0d96fae3757..c86063d2ed6eb 100644 --- a/compiler/rustc_smir/src/stable_mir/visitor.rs +++ b/compiler/rustc_smir/src/stable_mir/visitor.rs @@ -30,11 +30,12 @@ impl Visitable for Ty { } fn super_visit(&self, visitor: &mut V) -> ControlFlow { match self.kind() { - super::ty::TyKind::RigidTy(ty) => ty.visit(visitor), - super::ty::TyKind::Alias(_, alias) => alias.args.visit(visitor), - super::ty::TyKind::Param(_) => todo!(), - super::ty::TyKind::Bound(_, _) => todo!(), + super::ty::TyKind::RigidTy(ty) => ty.visit(visitor)?, + super::ty::TyKind::Alias(_, alias) => alias.args.visit(visitor)?, + super::ty::TyKind::Param(_) => {} + super::ty::TyKind::Bound(_, _) => {} } + ControlFlow::Continue(()) } } @@ -44,10 +45,10 @@ impl Visitable for Const { } fn super_visit(&self, visitor: &mut V) -> ControlFlow { match &self.literal { - super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor), - super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor), - super::ty::ConstantKind::ParamCt(param) => param.visit(visitor), - }?; + super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?, + super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?, + super::ty::ConstantKind::Param(_) => {} + } self.ty.visit(visitor) } } From 202fbed1a693ee2600bd3ec605f026b3ac3193a7 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 15:18:42 +0000 Subject: [PATCH 6/7] Allow fetching the SMIR body of FnDefs --- compiler/rustc_smir/src/rustc_smir/mod.rs | 4 ++-- compiler/rustc_smir/src/stable_mir/mod.rs | 4 ++-- compiler/rustc_smir/src/stable_mir/ty.rs | 12 +++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index c50cc7cd7cdb5..a764073648139 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -78,8 +78,8 @@ impl<'tcx> Context for Tables<'tcx> { impl_trait.stable(self) } - fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body { - let def_id = self[item.0]; + fn mir_body(&mut self, item: stable_mir::DefId) -> stable_mir::mir::Body { + let def_id = self[item]; let mir = self.tcx.optimized_mir(def_id); stable_mir::mir::Body { blocks: mir diff --git a/compiler/rustc_smir/src/stable_mir/mod.rs b/compiler/rustc_smir/src/stable_mir/mod.rs index 90fc569f47bb8..9cab7230b971b 100644 --- a/compiler/rustc_smir/src/stable_mir/mod.rs +++ b/compiler/rustc_smir/src/stable_mir/mod.rs @@ -87,7 +87,7 @@ pub struct CrateItem(pub(crate) DefId); impl CrateItem { pub fn body(&self) -> mir::Body { - with(|cx| cx.mir_body(self)) + with(|cx| cx.mir_body(self.0)) } } @@ -138,7 +138,7 @@ pub trait Context { fn entry_fn(&mut self) -> Option; /// Retrieve all items of the local crate that have a MIR associated with them. fn all_local_items(&mut self) -> CrateItems; - fn mir_body(&mut self, item: &CrateItem) -> mir::Body; + fn mir_body(&mut self, item: DefId) -> mir::Body; fn all_trait_decls(&mut self) -> TraitDecls; fn trait_decl(&mut self, trait_def: &TraitDef) -> TraitDecl; fn all_trait_impls(&mut self) -> ImplTraitDecls; diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 7c58b640ec77f..ad7a67275eeeb 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -1,4 +1,8 @@ -use super::{mir::Mutability, mir::Safety, with, AllocId, DefId}; +use super::{ + mir::Safety, + mir::{Body, Mutability}, + with, AllocId, DefId, +}; use crate::rustc_internal::Opaque; #[derive(Copy, Clone, Debug)] @@ -95,6 +99,12 @@ pub struct ForeignDef(pub(crate) DefId); #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct FnDef(pub(crate) DefId); +impl FnDef { + pub fn body(&self) -> Body { + with(|ctx| ctx.mir_body(self.0)) + } +} + #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct ClosureDef(pub(crate) DefId); From 0f4ff52e004156cfeefbb4ebc590ffda595024c9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 4 Sep 2023 15:18:53 +0000 Subject: [PATCH 7/7] Implement and test monomorphization --- compiler/rustc_smir/src/stable_mir/fold.rs | 27 +++++++++++++-- compiler/rustc_smir/src/stable_mir/ty.rs | 38 ++++++++++++++++++++++ tests/ui-fulldeps/stable-mir/crate-info.rs | 31 ++++++++++++++++-- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_smir/src/stable_mir/fold.rs b/compiler/rustc_smir/src/stable_mir/fold.rs index 3567a5c5a3db6..831cfb40a1542 100644 --- a/compiler/rustc_smir/src/stable_mir/fold.rs +++ b/compiler/rustc_smir/src/stable_mir/fold.rs @@ -3,8 +3,8 @@ use std::ops::ControlFlow; use crate::rustc_internal::Opaque; use super::ty::{ - Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs, - Promoted, RigidTy, TermKind, Ty, UnevaluatedConst, + Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind, + GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst, }; pub trait Folder: Sized { @@ -205,3 +205,26 @@ impl Foldable for FnSig { }) } } + +pub enum Never {} + +/// In order to instantiate a `Foldable`'s generic parameters with specific arguments, +/// `GenericArgs` can be used as a `Folder` that replaces all mentions of generic params +/// with the entries in its list. +impl Folder for GenericArgs { + type Break = Never; + + fn visit_ty(&mut self, ty: &Ty) -> ControlFlow { + ControlFlow::Continue(match ty.kind() { + TyKind::Param(p) => self[p], + _ => *ty, + }) + } + + fn fold_const(&mut self, c: &Const) -> ControlFlow { + ControlFlow::Continue(match &c.literal { + ConstantKind::Param(p) => self[p.clone()].clone(), + _ => c.clone(), + }) + } +} diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index ad7a67275eeeb..7e344dc516be9 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -138,6 +138,22 @@ pub struct ImplDef(pub(crate) DefId); #[derive(Clone, Debug)] pub struct GenericArgs(pub Vec); +impl std::ops::Index for GenericArgs { + type Output = Ty; + + fn index(&self, index: ParamTy) -> &Self::Output { + self.0[index.index as usize].expect_ty() + } +} + +impl std::ops::Index for GenericArgs { + type Output = Const; + + fn index(&self, index: ParamConst) -> &Self::Output { + self.0[index.index as usize].expect_const() + } +} + #[derive(Clone, Debug)] pub enum GenericArgKind { Lifetime(Region), @@ -145,6 +161,28 @@ pub enum GenericArgKind { Const(Const), } +impl GenericArgKind { + /// Panic if this generic argument is not a type, otherwise + /// return the type. + #[track_caller] + pub fn expect_ty(&self) -> &Ty { + match self { + GenericArgKind::Type(ty) => ty, + _ => panic!("{self:?}"), + } + } + + /// Panic if this generic argument is not a const, otherwise + /// return the const. + #[track_caller] + pub fn expect_const(&self) -> &Const { + match self { + GenericArgKind::Const(c) => c, + _ => panic!("{self:?}"), + } + } +} + #[derive(Clone, Debug)] pub enum TermKind { Type(Ty), diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 31ad42b431dbb..d55eae86f074d 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -8,6 +8,7 @@ #![feature(rustc_private)] #![feature(assert_matches)] +#![feature(control_flow_enum)] extern crate rustc_hir; extern crate rustc_middle; @@ -15,7 +16,10 @@ extern crate rustc_smir; use rustc_hir::def::DefKind; use rustc_middle::ty::TyCtxt; -use rustc_smir::{rustc_internal, stable_mir}; +use rustc_smir::{ + rustc_internal, + stable_mir::{self, fold::Foldable}, +}; use std::assert_matches::assert_matches; use std::io::Write; use std::ops::ControlFlow; @@ -116,7 +120,30 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> { stable_mir::mir::Terminator::Call { func, .. } => match func { stable_mir::mir::Operand::Constant(c) => match &c.literal.literal { stable_mir::ty::ConstantKind::Allocated(alloc) => { - assert!(alloc.bytes.is_empty()) + assert!(alloc.bytes.is_empty()); + match c.literal.ty.kind() { + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::FnDef( + def, + mut args, + )) => { + let func = def.body(); + match func.locals[1] + .fold(&mut args) + .continue_value() + .unwrap() + .kind() + { + stable_mir::ty::TyKind::RigidTy( + stable_mir::ty::RigidTy::Uint(_), + ) => {} + stable_mir::ty::TyKind::RigidTy( + stable_mir::ty::RigidTy::Tuple(_), + ) => {} + other => panic!("{other:?}"), + } + } + other => panic!("{other:?}"), + } } other => panic!("{other:?}"), },