Skip to content

Commit 6227455

Browse files
authored
Rollup merge of #117745 - ouz-a:emit_smir, r=celinval
Emit smir This adds ability to `-Zunpretty=smir` and get smir output of a Rust file, this is obliviously pretty basic compared to `mir` output but I think we could iteratively improve it, and even at this state this is useful for us. r? ``@celinval``
2 parents aa2289d + 92657f1 commit 6227455

File tree

12 files changed

+340
-9
lines changed

12 files changed

+340
-9
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3815,6 +3815,7 @@ dependencies = [
38153815
"rustc_query_system",
38163816
"rustc_resolve",
38173817
"rustc_session",
3818+
"rustc_smir",
38183819
"rustc_span",
38193820
"rustc_symbol_mangling",
38203821
"rustc_target",

compiler/rustc_driver_impl/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rustc_privacy = { path = "../rustc_privacy" }
4343
rustc_query_system = { path = "../rustc_query_system" }
4444
rustc_resolve = { path = "../rustc_resolve" }
4545
rustc_session = { path = "../rustc_session" }
46+
rustc_smir ={ path = "../rustc_smir" }
4647
rustc_span = { path = "../rustc_span" }
4748
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
4849
rustc_target = { path = "../rustc_target" }

compiler/rustc_driver_impl/src/pretty.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
1111
use rustc_session::Session;
12+
use rustc_smir::rustc_internal::pretty::write_smir_pretty;
1213
use rustc_span::symbol::Ident;
1314
use rustc_span::FileName;
1415

@@ -325,6 +326,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
325326
write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
326327
String::from_utf8(out).unwrap()
327328
}
329+
StableMir => {
330+
let mut out = Vec::new();
331+
write_smir_pretty(ex.tcx(), &mut out).unwrap();
332+
String::from_utf8(out).unwrap()
333+
}
328334
ThirTree => {
329335
let tcx = ex.tcx();
330336
let mut out = String::new();

compiler/rustc_session/src/config.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2926,12 +2926,13 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) ->
29262926
"thir-tree" => ThirTree,
29272927
"thir-flat" => ThirFlat,
29282928
"mir" => Mir,
2929+
"stable-mir" => StableMir,
29292930
"mir-cfg" => MirCFG,
29302931
name => handler.early_error(format!(
29312932
"argument to `unpretty` must be one of `normal`, `identified`, \
29322933
`expanded`, `expanded,identified`, `expanded,hygiene`, \
29332934
`ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \
2934-
`hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir` or \
2935+
`hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir`, `stable-mir`, or \
29352936
`mir-cfg`; got {name}"
29362937
)),
29372938
};
@@ -3106,6 +3107,8 @@ pub enum PpMode {
31063107
Mir,
31073108
/// `-Zunpretty=mir-cfg`
31083109
MirCFG,
3110+
/// `-Zunpretty=stable-mir`
3111+
StableMir,
31093112
}
31103113

31113114
impl PpMode {
@@ -3122,21 +3125,22 @@ impl PpMode {
31223125
| ThirTree
31233126
| ThirFlat
31243127
| Mir
3125-
| MirCFG => true,
3128+
| MirCFG
3129+
| StableMir => true,
31263130
}
31273131
}
31283132
pub fn needs_hir(&self) -> bool {
31293133
use PpMode::*;
31303134
match *self {
31313135
Source(_) | AstTree | AstTreeExpanded => false,
31323136

3133-
Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG => true,
3137+
Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | StableMir => true,
31343138
}
31353139
}
31363140

31373141
pub fn needs_analysis(&self) -> bool {
31383142
use PpMode::*;
3139-
matches!(*self, Hir(PpHirMode::Typed) | Mir | MirCFG | ThirTree | ThirFlat)
3143+
matches!(*self, Hir(PpHirMode::Typed) | Mir | StableMir | MirCFG | ThirTree | ThirFlat)
31403144
}
31413145
}
31423146

compiler/rustc_smir/src/rustc_internal/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::hash::Hash;
2121
use std::ops::Index;
2222

2323
mod internal;
24+
pub mod pretty;
2425

2526
pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
2627
with_tables(|tables| item.stable(tables))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::io;
2+
3+
use super::run;
4+
use rustc_middle::ty::TyCtxt;
5+
6+
pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> {
7+
writeln!(
8+
w,
9+
"// WARNING: This is highly experimental output it's intended for stable-mir developers only."
10+
)?;
11+
writeln!(
12+
w,
13+
"// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir."
14+
)?;
15+
let _ = run(tcx, || {
16+
let items = stable_mir::all_local_items();
17+
let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::<Vec<_>>();
18+
});
19+
Ok(())
20+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::rustc_internal::{IndexMap, RustcInternal};
10+
use crate::rustc_internal::{internal, IndexMap, RustcInternal};
1111
use crate::rustc_smir::stable_mir::ty::{BoundRegion, Region};
1212
use rustc_hir as hir;
1313
use rustc_hir::def::DefKind;
@@ -105,6 +105,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
105105
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
106106
}
107107

108+
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
109+
internal(cnst).to_string()
110+
}
111+
108112
fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span {
109113
let mut tables = self.0.borrow_mut();
110114
tables.tcx.def_span(tables[def_id]).stable(&mut *tables)
@@ -404,6 +408,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
404408
.map(|decl| stable_mir::mir::LocalDecl {
405409
ty: decl.ty.stable(tables),
406410
span: decl.source_info.span.stable(tables),
411+
mutability: decl.mutability.stable(tables),
407412
})
408413
.collect(),
409414
self.arg_count,

compiler/stable_mir/src/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
2020
use crate::mir::mono::InstanceDef;
2121
use crate::mir::Body;
22-
use std::cell::Cell;
2322
use std::fmt;
2423
use std::fmt::Debug;
24+
use std::{cell::Cell, io};
2525

2626
use self::ty::{
2727
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
@@ -36,10 +36,12 @@ pub mod mir;
3636
pub mod ty;
3737
pub mod visitor;
3838

39+
use crate::mir::pretty::function_name;
40+
use crate::mir::Mutability;
3941
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind};
4042
pub use error::*;
4143
use mir::mono::Instance;
42-
use ty::{FnDef, GenericArgs};
44+
use ty::{Const, FnDef, GenericArgs};
4345

4446
/// Use String for now but we should replace it.
4547
pub type Symbol = String;
@@ -137,6 +139,11 @@ impl CrateItem {
137139
pub fn ty(&self) -> Ty {
138140
with(|cx| cx.def_ty(self.0))
139141
}
142+
143+
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
144+
writeln!(w, "{}", function_name(*self))?;
145+
self.body().dump(w)
146+
}
140147
}
141148

142149
/// Return the function where execution starts if the current
@@ -223,6 +230,9 @@ pub trait Context {
223230
/// Returns the type of given crate item.
224231
fn def_ty(&self, item: DefId) -> Ty;
225232

233+
/// Returns literal value of a const as a string.
234+
fn const_literal(&self, cnst: &Const) -> String;
235+
226236
/// `Span` of an item
227237
fn span_of_an_item(&self, def_id: DefId) -> Span;
228238

compiler/stable_mir/src/mir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod body;
22
pub mod mono;
3+
pub mod pretty;
34
pub mod visit;
45

56
pub use body::*;

compiler/stable_mir/src/mir/body.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use crate::mir::pretty::{function_body, pretty_statement};
12
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, Ty};
23
use crate::Opaque;
34
use crate::Span;
4-
5+
use std::io;
56
/// The SMIR representation of a single function.
67
#[derive(Clone, Debug)]
78
pub struct Body {
@@ -56,6 +57,28 @@ impl Body {
5657
pub fn locals(&self) -> &[LocalDecl] {
5758
&self.locals
5859
}
60+
61+
pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
62+
writeln!(w, "{}", function_body(self))?;
63+
self.blocks
64+
.iter()
65+
.enumerate()
66+
.map(|(index, block)| -> io::Result<()> {
67+
writeln!(w, " bb{}: {{", index)?;
68+
let _ = block
69+
.statements
70+
.iter()
71+
.map(|statement| -> io::Result<()> {
72+
writeln!(w, "{}", pretty_statement(&statement.kind))?;
73+
Ok(())
74+
})
75+
.collect::<Vec<_>>();
76+
writeln!(w, " }}").unwrap();
77+
Ok(())
78+
})
79+
.collect::<Result<Vec<_>, _>>()?;
80+
Ok(())
81+
}
5982
}
6083

6184
type LocalDecls = Vec<LocalDecl>;
@@ -64,6 +87,7 @@ type LocalDecls = Vec<LocalDecl>;
6487
pub struct LocalDecl {
6588
pub ty: Ty,
6689
pub span: Span,
90+
pub mutability: Mutability,
6791
}
6892

6993
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)