Skip to content

Commit 5c6e234

Browse files
committed
Encode types in SMIR
1 parent 7f74ae5 commit 5c6e234

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

Diff for: compiler/rustc_smir/src/rustc_internal/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
4747
}
4848

4949
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
50-
crate::stable_mir::run(Tables { tcx, def_ids: vec![] }, f);
50+
crate::stable_mir::run(Tables { tcx, def_ids: vec![], types: vec![] }, f);
5151
}

Diff for: compiler/rustc_smir/src/rustc_smir/mod.rs

+54-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::stable_mir::{self, Context};
11-
use rustc_middle::ty::TyCtxt;
10+
use crate::stable_mir::{self, ty::TyKind, Context};
11+
use rustc_middle::ty::{self, Ty, TyCtxt};
1212
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1313
use tracing::debug;
1414

@@ -34,7 +34,7 @@ impl<'tcx> Context for Tables<'tcx> {
3434
fn entry_fn(&mut self) -> Option<stable_mir::CrateItem> {
3535
Some(self.crate_item(self.tcx.entry_fn(())?.0))
3636
}
37-
fn mir_body(&self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
37+
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
3838
let def_id = self.item_def_id(item);
3939
let mir = self.tcx.optimized_mir(def_id);
4040
stable_mir::mir::Body {
@@ -46,17 +46,68 @@ impl<'tcx> Context for Tables<'tcx> {
4646
statements: block.statements.iter().map(rustc_statement_to_statement).collect(),
4747
})
4848
.collect(),
49+
locals: mir.local_decls.iter().map(|decl| self.intern_ty(decl.ty)).collect(),
4950
}
5051
}
5152

5253
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>)) {
5354
f(self)
5455
}
56+
57+
fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind {
58+
self.rustc_ty_to_ty(self.types[ty.0])
59+
}
5560
}
5661

5762
pub struct Tables<'tcx> {
5863
pub tcx: TyCtxt<'tcx>,
5964
pub def_ids: Vec<DefId>,
65+
pub types: Vec<Ty<'tcx>>,
66+
}
67+
68+
impl<'tcx> Tables<'tcx> {
69+
fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind {
70+
match ty.kind() {
71+
ty::Bool => TyKind::Bool,
72+
ty::Char => todo!(),
73+
ty::Int(_) => todo!(),
74+
ty::Uint(_) => todo!(),
75+
ty::Float(_) => todo!(),
76+
ty::Adt(_, _) => todo!(),
77+
ty::Foreign(_) => todo!(),
78+
ty::Str => todo!(),
79+
ty::Array(_, _) => todo!(),
80+
ty::Slice(_) => todo!(),
81+
ty::RawPtr(_) => todo!(),
82+
ty::Ref(_, _, _) => todo!(),
83+
ty::FnDef(_, _) => todo!(),
84+
ty::FnPtr(_) => todo!(),
85+
ty::Placeholder(..) => todo!(),
86+
ty::Dynamic(_, _, _) => todo!(),
87+
ty::Closure(_, _) => todo!(),
88+
ty::Generator(_, _, _) => todo!(),
89+
ty::GeneratorWitness(_) => todo!(),
90+
ty::GeneratorWitnessMIR(_, _) => todo!(),
91+
ty::Never => todo!(),
92+
ty::Tuple(fields) => {
93+
TyKind::Tuple(fields.iter().map(|ty| self.intern_ty(ty)).collect())
94+
}
95+
ty::Alias(_, _) => todo!(),
96+
ty::Param(_) => todo!(),
97+
ty::Bound(_, _) => todo!(),
98+
ty::Infer(_) => todo!(),
99+
ty::Error(_) => todo!(),
100+
}
101+
}
102+
103+
fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
104+
if let Some(id) = self.types.iter().position(|&t| t == ty) {
105+
return stable_mir::ty::Ty(id);
106+
}
107+
let id = self.types.len();
108+
self.types.push(ty);
109+
stable_mir::ty::Ty(id)
110+
}
60111
}
61112

62113
/// Build a stable mir crate from a given crate number.

Diff for: compiler/rustc_smir/src/stable_mir/mir/body.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use crate::stable_mir::ty::Ty;
2+
13
#[derive(Clone, Debug)]
24
pub struct Body {
35
pub blocks: Vec<BasicBlock>,
6+
pub locals: Vec<Ty>,
47
}
58

69
#[derive(Clone, Debug)]

Diff for: compiler/rustc_smir/src/stable_mir/ty.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use super::with;
2+
3+
#[derive(Copy, Clone, Debug)]
4+
pub struct Ty(pub usize);
5+
6+
impl Ty {
7+
pub fn kind(&self) -> TyKind {
8+
with(|context| context.ty_kind(*self))
9+
}
10+
}
11+
12+
pub enum TyKind {
13+
Bool,
14+
Tuple(Vec<Ty>),
15+
}

Diff for: tests/ui-fulldeps/stable-mir/crate-info.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
4040

4141
let bar = get_item(tcx, &items, (DefKind::Fn, "bar")).unwrap();
4242
let body = bar.body();
43+
assert_eq!(body.locals.len(), 2);
4344
assert_eq!(body.blocks.len(), 1);
4445
let block = &body.blocks[0];
4546
assert_eq!(block.statements.len(), 1);
@@ -54,6 +55,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
5455

5556
let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap();
5657
let body = foo_bar.body();
58+
assert_eq!(body.locals.len(), 7);
5759
assert_eq!(body.blocks.len(), 4);
5860
let block = &body.blocks[0];
5961
match &block.terminator {

0 commit comments

Comments
 (0)