From f465ab97e4a010f367a4bd07d09e7d6549f139f9 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Wed, 2 Aug 2023 00:08:29 +0300 Subject: [PATCH] Add constants to smir --- compiler/rustc_smir/src/rustc_internal/mod.rs | 8 ++ compiler/rustc_smir/src/rustc_smir/mod.rs | 74 +++++++++++++++++++ .../rustc_smir/src/stable_mir/mir/body.rs | 2 +- compiler/rustc_smir/src/stable_mir/ty.rs | 43 ++++++++++- 4 files changed, 125 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 6a26a4a22ed42..d24f83810659b 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -63,6 +63,10 @@ pub fn trait_def(did: DefId) -> stable_mir::ty::TraitDef { with_tables(|t| t.trait_def(did)) } +pub fn const_def(did: DefId) -> stable_mir::ty::ConstDef { + with_tables(|t| t.const_def(did)) +} + impl<'tcx> Tables<'tcx> { pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId { self.def_ids[item.0] @@ -108,6 +112,10 @@ impl<'tcx> Tables<'tcx> { stable_mir::ty::TraitDef(self.create_def_id(did)) } + pub fn const_def(&mut self, did: DefId) -> stable_mir::ty::ConstDef { + stable_mir::ty::ConstDef(self.create_def_id(did)) + } + 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() { diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index c4bdec0ee28b1..8a425bff399e2 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -13,6 +13,7 @@ use crate::stable_mir::ty::{FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy} use crate::stable_mir::{self, Context}; use rustc_hir as hir; use rustc_middle::mir::coverage::CodeRegion; +use rustc_middle::mir::interpret::ConstValue; use rustc_middle::mir::{self}; use rustc_middle::ty::{self, Ty, TyCtxt, Variance}; use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; @@ -1063,3 +1064,76 @@ impl<'tcx> Stable<'tcx> for rustc_middle::ty::BoundTy { BoundTy { var: self.var.as_usize(), kind: self.kind.stable(tables) } } } + +impl<'tcx> Stable<'tcx> for rustc_middle::mir::Constant<'tcx> { + type T = stable_mir::ty::Constant; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + stable_mir::ty::Constant { + span: opaque(&self.span), + user_ty: self.user_ty.map(|f| f.index()), + literal: self.literal.stable(tables), + } + } +} + +impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> { + type T = stable_mir::ty::ConstantKind; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + use mir::ConstantKind::*; + match self { + Ty(cnst) => stable_mir::ty::ConstantKind::Ty(opaque(cnst)), + Unevaluated(uneval_cnst, ty) => stable_mir::ty::ConstantKind::Unevaluated( + uneval_cnst.stable(tables), + tables.intern_ty(*ty), + ), + Val(_, _) => todo!(), + } + } +} + +impl<'tcx> Stable<'tcx> for rustc_middle::mir::UnevaluatedConst<'tcx> { + type T = stable_mir::ty::UnevulatedConst; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + stable_mir::ty::UnevulatedConst { + def: tables.const_def(self.def), + args: self.args.stable(tables), + promoted: self.promoted.map(|p| p.as_usize()), + } + } +} + +impl<'tcx> Stable<'tcx> for ConstValue<'tcx> { + type T = stable_mir::ty::ConstValue; + + fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T { + use ConstValue::*; + match self { + Scalar(scalar) => stable_mir::ty::ConstValue::Scalar(scalar.stable(tables)), + ZeroSized => stable_mir::ty::ConstValue::ZeroSized, + Slice { data, start, end } => { + stable_mir::ty::ConstValue::Slice { data: opaque(data), start: *start, end: *end } + } + ByRef { alloc, offset } => stable_mir::ty::ConstValue::ByRef { + alloc: opaque(alloc), + offset: offset.bytes_usize(), + }, + } + } +} + +impl<'tcx> Stable<'tcx> for mir::interpret::Scalar { + type T = stable_mir::ty::Scalar; + + fn stable(&self, _: &mut Tables<'tcx>) -> Self::T { + use mir::interpret::Scalar::*; + match self { + Int(scalar_int) => stable_mir::ty::Scalar::Int(opaque(scalar_int)), + Ptr(pointer, u) => { + stable_mir::ty::Scalar::Ptr(pointer.into_parts().1.bytes_usize(), *u) + } + } + } +} diff --git a/compiler/rustc_smir/src/stable_mir/mir/body.rs b/compiler/rustc_smir/src/stable_mir/mir/body.rs index c16bd6cbd70e2..a5b8f9b5f4662 100644 --- a/compiler/rustc_smir/src/stable_mir/mir/body.rs +++ b/compiler/rustc_smir/src/stable_mir/mir/body.rs @@ -381,7 +381,7 @@ type FieldIdx = usize; /// The source-order index of a variant in a type. pub type VariantIdx = usize; -type UserTypeAnnotationIndex = usize; +pub type UserTypeAnnotationIndex = usize; #[derive(Clone, Debug)] pub struct SwitchTarget { diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 025225b8d19f1..c768b9df9abf2 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -1,4 +1,7 @@ -use super::{mir::Mutability, with, DefId}; +use super::{ + mir::{Mutability, UserTypeAnnotationIndex}, + with, DefId, +}; use crate::rustc_internal::Opaque; #[derive(Copy, Clone, Debug)] @@ -12,7 +15,42 @@ impl Ty { pub(crate) type Const = Opaque; pub(crate) type Region = Opaque; +type ScalarInt = Opaque; +type ConstAllocation = Opaque; type Span = Opaque; +type Pointer = usize; +type Promoted = usize; +type Size = usize; + +pub struct Constant { + pub span: Span, + pub user_ty: Option, + pub literal: ConstantKind, +} + +pub enum ConstantKind { + Ty(Const), + Unevaluated(UnevulatedConst, Ty), + Val(ConstValue, Ty), +} + +pub struct UnevulatedConst { + pub def: ConstDef, + pub args: GenericArgs, + pub promoted: Option, +} + +pub enum ConstValue { + Scalar(Scalar), + ZeroSized, + Slice { data: ConstAllocation, start: usize, end: usize }, + ByRef { alloc: ConstAllocation, offset: Size }, +} + +pub enum Scalar { + Int(ScalarInt), + Ptr(Pointer, u8), +} #[derive(Clone, Debug)] pub enum TyKind { @@ -104,6 +142,9 @@ pub struct AliasDef(pub(crate) DefId); #[derive(Clone, PartialEq, Eq, Debug)] pub struct TraitDef(pub(crate) DefId); +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ConstDef(pub(crate) DefId); + #[derive(Clone, Debug)] pub struct GenericArgs(pub Vec);