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

Add constants to smir #114342

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
8 changes: 8 additions & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down
74 changes: 74 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
}
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/stable_mir/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 42 additions & 1 deletion compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<UserTypeAnnotationIndex>,
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<Promoted>,
}

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 {
Expand Down Expand Up @@ -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<GenericArgKind>);

Expand Down
Loading