Skip to content

Commit

Permalink
more smir
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jun 4, 2024
1 parent f972570 commit 42e1789
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
15 changes: 7 additions & 8 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let bytes = value.as_bytes();
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);

mir::Const::Ty(ty, 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) -> MirConst {
let mut tables = self.0.borrow_mut();
mir::Const::Ty(tables.tcx.types.bool, 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<MirConst, Error> {
Expand All @@ -428,11 +430,8 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
})?;
Ok(mir::Const::Ty(
ty,
ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty),
)
.stable(&mut *tables))
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,
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
42 changes: 42 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,48 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
}
}

pub fn mir_const_from_ty_const<'tcx>(
tables: &mut Tables<'tcx>,
ty_const: ty::Const<'tcx>,
ty: Ty<'tcx>,
) -> stable_mir::ty::MirConst {
let kind = match ty_const.kind() {
ty::Value(ty, val) => {
let val = match val {
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
ty::ValTree::Branch(branch) => {
ty::ValTree::Branch(tables.tcx.lift(branch).unwrap())
}
};
let ty = tables.tcx.lift(ty).unwrap();
let const_val = tables.tcx.valtree_to_const_val((ty, val));
if matches!(const_val, mir::ConstValue::ZeroSized) {
stable_mir::ty::ConstantKind::ZeroSized
} else {
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
ty, const_val, tables,
))
}
}
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
ty::ErrorCt(_) => unreachable!(),
ty::InferCt(_) => unreachable!(),
ty::BoundCt(_, _) => unimplemented!(),
ty::PlaceholderCt(_) => unimplemented!(),
ty::Unevaluated(uv) => {
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
def: tables.const_def(uv.def),
args: uv.args.stable(tables),
promoted: None,
})
}
ty::ExprCt(_) => unimplemented!(),
};
let stable_ty = tables.intern_ty(ty);
let id = tables.intern_mir_const(mir::Const::Ty(ty, ty_const));
stable_mir::ty::MirConst::new(kind, stable_ty, id)
}

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

Expand Down
5 changes: 5 additions & 0 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ impl TyConst {
Self { kind, id }
}

/// Retrieve the constant kind.
pub fn kind(&self) -> &TyConstKind {
&self.kind
}

/// Creates an interned usize constant.
fn try_from_target_usize(val: u64) -> Result<Self, Error> {
with(|cx| cx.try_new_ty_const_uint(val.into(), UintTy::Usize))
Expand Down
6 changes: 3 additions & 3 deletions tests/ui-fulldeps/stable-mir/check_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_smir::rustc_internal;
use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::mono::Instance;
use stable_mir::mir::{Body, Constant, Operand, Rvalue, StatementKind, TerminatorKind};
use stable_mir::ty::{Const, ConstantKind};
use stable_mir::ty::{ConstantKind, MirConst};
use stable_mir::{CrateDef, CrateItems, ItemKind};
use std::convert::TryFrom;
use std::io::Write;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn check_msg(body: &Body, expected: &str) {
};
assert_eq!(alloc.provenance.ptrs.len(), 1);

let alloc_prov_id = alloc.provenance.ptrs[0].1 .0;
let alloc_prov_id = alloc.provenance.ptrs[0].1.0;
let GlobalAlloc::Memory(val) = GlobalAlloc::from(alloc_prov_id) else {
unreachable!()
};
Expand All @@ -95,7 +95,7 @@ fn change_panic_msg(mut body: Body, new_msg: &str) -> Body {
for bb in &mut body.blocks {
match &mut bb.terminator.kind {
TerminatorKind::Call { args, .. } => {
let new_const = Const::from_str(new_msg);
let new_const = MirConst::from_str(new_msg);
args[0] = Operand::Constant(Constant {
literal: new_const,
span: bb.terminator.span,
Expand Down

0 comments on commit 42e1789

Please sign in to comment.