Skip to content

Commit

Permalink
Split smir Const into TyConst and MirConst
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jun 4, 2024
1 parent 27529d5 commit 7e08f80
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 134 deletions.
44 changes: 19 additions & 25 deletions compiler/rustc_smir/src/rustc_internal/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
// Prefer importing stable_mir over internal rustc constructs to make this file more readable.
use crate::rustc_smir::Tables;
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy, TyCtxt};
use rustc_middle::ty::{self as rustc_ty, Const as InternalConst, Ty as InternalTy, TyCtxt};
use rustc_span::Symbol;
use stable_mir::abi::Layout;
use stable_mir::mir::alloc::AllocId;
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
use stable_mir::mir::{BinOp, Mutability, Place, ProjectionElem, Safety, UnOp};
use stable_mir::ty::{
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
GenericArgKind, GenericArgs, IndexedVal, IntTy, Movability, Pattern, Region, RigidTy, Span,
TermKind, TraitRef, Ty, UintTy, VariantDef, VariantIdx,
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, DynKind,
ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
GenericArgKind, GenericArgs, IndexedVal, IntTy, MirConst, Movability, Pattern, Region, RigidTy,
Span, TermKind, TraitRef, Ty, TyConst, UintTy, VariantDef, VariantIdx,
};
use stable_mir::{CrateItem, CrateNum, DefId};

Expand Down Expand Up @@ -55,7 +55,7 @@ impl RustcInternal for GenericArgKind {
let arg: rustc_ty::GenericArg<'tcx> = match self {
GenericArgKind::Lifetime(reg) => reg.internal(tables, tcx).into(),
GenericArgKind::Type(ty) => ty.internal(tables, tcx).into(),
GenericArgKind::Const(cnst) => ty_const(cnst, tables, tcx).into(),
GenericArgKind::Const(cnst) => cnst.internal(tables, tcx).into(),
};
tcx.lift(arg).unwrap()
}
Expand All @@ -76,13 +76,20 @@ impl RustcInternal for Ty {
}
}

impl RustcInternal for TyConst {
type T<'tcx> = InternalConst<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
tcx.lift(tables.ty_consts[self.id]).unwrap()
}
}

impl RustcInternal for Pattern {
type T<'tcx> = rustc_ty::Pattern<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
tcx.mk_pat(match self {
Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range {
start: start.as_ref().map(|c| ty_const(c, tables, tcx)),
end: end.as_ref().map(|c| ty_const(c, tables, tcx)),
start: start.as_ref().map(|c| c.internal(tables, tcx)),
end: end.as_ref().map(|c| c.internal(tables, tcx)),
include_end: *include_end,
},
})
Expand All @@ -101,7 +108,7 @@ impl RustcInternal for RigidTy {
RigidTy::Float(float_ty) => rustc_ty::TyKind::Float(float_ty.internal(tables, tcx)),
RigidTy::Never => rustc_ty::TyKind::Never,
RigidTy::Array(ty, cnst) => {
rustc_ty::TyKind::Array(ty.internal(tables, tcx), ty_const(cnst, tables, tcx))
rustc_ty::TyKind::Array(ty.internal(tables, tcx), cnst.internal(tables, tcx))
}
RigidTy::Pat(ty, pat) => {
rustc_ty::TyKind::Pat(ty.internal(tables, tcx), pat.internal(tables, tcx))
Expand Down Expand Up @@ -239,23 +246,10 @@ impl RustcInternal for VariantDef {
}
}

fn ty_const<'tcx>(
constant: &Const,
tables: &mut Tables<'_>,
tcx: TyCtxt<'tcx>,
) -> rustc_ty::Const<'tcx> {
match constant.internal(tables, tcx) {
rustc_middle::mir::Const::Ty(c) => c,
cnst => {
panic!("Trying to convert constant `{constant:?}` to type constant, but found {cnst:?}")
}
}
}

impl RustcInternal for Const {
impl RustcInternal for MirConst {
type T<'tcx> = rustc_middle::mir::Const<'tcx>;
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
let constant = tables.constants[self.id];
let constant = tables.mir_consts[self.id];
match constant {
rustc_middle::mir::Const::Ty(ty) => rustc_middle::mir::Const::Ty(tcx.lift(ty).unwrap()),
rustc_middle::mir::Const::Unevaluated(uneval, ty) => {
Expand Down Expand Up @@ -392,7 +386,7 @@ impl RustcInternal for TermKind {
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
match self {
TermKind::Type(ty) => ty.internal(tables, tcx).into(),
TermKind::Const(const_) => ty_const(const_, tables, tcx).into(),
TermKind::Const(cnst) => cnst.internal(tables, tcx).into(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ where
spans: IndexMap::default(),
types: IndexMap::default(),
instances: IndexMap::default(),
constants: IndexMap::default(),
ty_consts: IndexMap::default(),
mir_consts: IndexMap::default(),
layouts: IndexMap::default(),
}));
stable_mir::compiler_interface::run(&tables, || init(&tables, f))
Expand Down
59 changes: 46 additions & 13 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#![allow(rustc::usage_of_qualified_ty)]

use rustc_abi::HasDataLayout;
use rustc_middle::ty;
use rustc_middle::ty::layout::{
FnAbiOf, FnAbiOfHelpers, HasParamEnv, HasTyCtxt, LayoutOf, LayoutOfHelpers,
};
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
use rustc_middle::ty::{
GenericPredicates, Instance, List, ParamEnv, ScalarInt, TyCtxt, TypeVisitableExt, ValTree,
};
use rustc_middle::{mir, ty};
use rustc_span::def_id::LOCAL_CRATE;
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
use stable_mir::compiler_interface::Context;
Expand All @@ -22,9 +22,9 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef};
use stable_mir::mir::{BinOp, Body, Place, UnOp};
use stable_mir::target::{MachineInfo, MachineSize};
use stable_mir::ty::{
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind,
UintTy, VariantDef,
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, Ty,
TyConst, TyKind, UintTy, VariantDef,
};
use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, ItemKind, Symbol};
use std::cell::RefCell;
Expand Down Expand Up @@ -360,7 +360,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
def.internal(&mut *tables, tcx).fields.iter().map(|f| f.stable(&mut *tables)).collect()
}

fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> {
fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let mir_const = cnst.internal(&mut *tables, tcx);
mir_const
.try_eval_target_usize(tables.tcx, ParamEnv::empty())
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let mir_const = cnst.internal(&mut *tables, tcx);
Expand All @@ -369,7 +377,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}

fn try_new_const_zst(&self, ty: Ty) -> Result<Const, Error> {
fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let ty_internal = ty.internal(&mut *tables, tcx);
Expand All @@ -390,25 +398,45 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
)));
}

Ok(ty::Const::zero_sized(tables.tcx, ty_internal).stable(&mut *tables))
Ok(mir::Const::Ty(ty::Const::zero_sized(tables.tcx, ty_internal)).stable(&mut *tables))
}

fn new_const_str(&self, value: &str) -> Const {
fn new_const_str(&self, value: &str) -> MirConst {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let ty = ty::Ty::new_static_str(tcx);
let bytes = value.as_bytes();
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);

ty::Const::new_value(tcx, val_tree, ty).stable(&mut *tables)
let ct = ty::Const::new_value(tcx, val_tree, ty);
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
}

fn new_const_bool(&self, value: bool) -> Const {
fn new_const_bool(&self, value: bool) -> MirConst {
let mut tables = self.0.borrow_mut();
ty::Const::from_bool(tables.tcx, value).stable(&mut *tables)
let ct = ty::Const::from_bool(tables.tcx, value);
let ty = tables.tcx.types.bool;
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
}

fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<Const, Error> {
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;

// We don't use Const::from_bits since it doesn't have any error checking.
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
})?;
let ct = ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty);
Ok(super::convert::mir_const_from_ty_const(&mut *tables, ct, ty))
}
fn try_new_ty_const_uint(
&self,
value: u128,
uint_ty: UintTy,
) -> Result<stable_mir::ty::TyConst, Error> {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
Expand Down Expand Up @@ -453,7 +481,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
.stable(&mut *tables)
}

fn const_pretty(&self, cnst: &stable_mir::ty::Const) -> String {
fn mir_const_pretty(&self, cnst: &stable_mir::ty::MirConst) -> String {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
cnst.internal(&mut *tables, tcx).to_string()
Expand All @@ -474,6 +502,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.types[ty].kind().stable(&mut *tables)
}

fn ty_const_pretty(&self, ct: stable_mir::ty::TyConstId) -> String {
let tables = self.0.borrow_mut();
tables.ty_consts[ct].to_string()
}

fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
Expand Down
20 changes: 11 additions & 9 deletions compiler/rustc_smir/src/rustc_smir/convert/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_middle::mir::interpret::alloc_range;
use rustc_middle::mir::mono::MonoItem;
use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::{ConstOperand, Statement, UserTypeProjection, VarDebugInfoFragment};
use stable_mir::ty::{Allocation, Const, ConstantKind};
use stable_mir::ty::{Allocation, ConstantKind, MirConst};
use stable_mir::{opaque, Error};

use crate::rustc_smir::{alloc, Stable, Tables};
Expand Down Expand Up @@ -724,11 +724,16 @@ impl<'tcx> Stable<'tcx> for mir::interpret::GlobalAlloc<'tcx> {
}

impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
type T = stable_mir::ty::Const;
type T = stable_mir::ty::MirConst;

fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let id = tables.intern_mir_const(tables.tcx.lift(*self).unwrap());
match *self {
mir::Const::Ty(c) => c.stable(tables),
mir::Const::Ty(c) => MirConst::new(
stable_mir::ty::ConstantKind::Ty(c.stable(tables)),
c.ty().stable(tables),
id,
),
mir::Const::Unevaluated(unev_const, ty) => {
let kind =
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
Expand All @@ -737,21 +742,18 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
promoted: unev_const.promoted.map(|u| u.as_u32()),
});
let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
Const::new(kind, ty, id)
MirConst::new(kind, ty, id)
}
mir::Const::Val(mir::ConstValue::ZeroSized, ty) => {
let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
Const::new(ConstantKind::ZeroSized, ty, id)
MirConst::new(ConstantKind::ZeroSized, ty, id)
}
mir::Const::Val(val, ty) => {
let ty = tables.tcx.lift(ty).unwrap();
let val = tables.tcx.lift(val).unwrap();
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
let ty = ty.stable(tables);
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
Const::new(kind, ty, id)
MirConst::new(kind, ty, id)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod error;
mod mir;
mod ty;

pub use ty::mir_const_from_ty_const;

impl<'tcx> Stable<'tcx> for rustc_hir::Safety {
type T = stable_mir::mir::Safety;
fn stable(&self, _: &mut Tables<'_>) -> Self::T {
Expand Down
Loading

0 comments on commit 7e08f80

Please sign in to comment.