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

Emit smir #117745

Merged
merged 7 commits into from
Nov 18, 2023
Merged

Emit smir #117745

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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3815,6 +3815,7 @@ dependencies = [
"rustc_query_system",
"rustc_resolve",
"rustc_session",
"rustc_smir",
"rustc_span",
"rustc_symbol_mangling",
"rustc_target",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_driver_impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rustc_privacy = { path = "../rustc_privacy" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_resolve = { path = "../rustc_resolve" }
rustc_session = { path = "../rustc_session" }
rustc_smir ={ path = "../rustc_smir" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" }
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_driver_impl/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
use rustc_session::Session;
use rustc_smir::rustc_internal::pretty::write_smir_pretty;
use rustc_span::symbol::Ident;
use rustc_span::FileName;

Expand Down Expand Up @@ -325,6 +326,11 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
String::from_utf8(out).unwrap()
}
StableMir => {
let mut out = Vec::new();
write_smir_pretty(ex.tcx(), &mut out).unwrap();
String::from_utf8(out).unwrap()
}
ThirTree => {
let tcx = ex.tcx();
let mut out = String::new();
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2926,12 +2926,13 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) ->
"thir-tree" => ThirTree,
"thir-flat" => ThirFlat,
"mir" => Mir,
"stable-mir" => StableMir,
"mir-cfg" => MirCFG,
name => handler.early_error(format!(
"argument to `unpretty` must be one of `normal`, `identified`, \
`expanded`, `expanded,identified`, `expanded,hygiene`, \
`ast-tree`, `ast-tree,expanded`, `hir`, `hir,identified`, \
`hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir` or \
`hir,typed`, `hir-tree`, `thir-tree`, `thir-flat`, `mir`, `stable-mir`, or \
`mir-cfg`; got {name}"
)),
};
Expand Down Expand Up @@ -3106,6 +3107,8 @@ pub enum PpMode {
Mir,
/// `-Zunpretty=mir-cfg`
MirCFG,
/// `-Zunpretty=stable-mir`
StableMir,
}

impl PpMode {
Expand All @@ -3122,21 +3125,22 @@ impl PpMode {
| ThirTree
| ThirFlat
| Mir
| MirCFG => true,
| MirCFG
| StableMir => true,
}
}
pub fn needs_hir(&self) -> bool {
use PpMode::*;
match *self {
Source(_) | AstTree | AstTreeExpanded => false,

Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG => true,
Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | StableMir => true,
}
}

pub fn needs_analysis(&self) -> bool {
use PpMode::*;
matches!(*self, Hir(PpHirMode::Typed) | Mir | MirCFG | ThirTree | ThirFlat)
matches!(*self, Hir(PpHirMode::Typed) | Mir | StableMir | MirCFG | ThirTree | ThirFlat)
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::hash::Hash;
use std::ops::Index;

mod internal;
pub mod pretty;

pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
with_tables(|tables| item.stable(tables))
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_smir/src/rustc_internal/pretty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::io;

use super::run;
use rustc_middle::ty::TyCtxt;

pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> {
writeln!(
w,
"// WARNING: This is highly experimental output it's intended for stable-mir developers only."
)?;
writeln!(
w,
"// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir."
)?;
let _ = run(tcx, || {
let items = stable_mir::all_local_items();
let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::<Vec<_>>();
});
Ok(())
}
7 changes: 6 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.

use crate::rustc_internal::{IndexMap, RustcInternal};
use crate::rustc_internal::{internal, IndexMap, RustcInternal};
use crate::rustc_smir::stable_mir::ty::{BoundRegion, Region};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
Expand Down Expand Up @@ -105,6 +105,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
}

fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
internal(cnst).to_string()
}

fn span_of_an_item(&self, def_id: stable_mir::DefId) -> Span {
let mut tables = self.0.borrow_mut();
tables.tcx.def_span(tables[def_id]).stable(&mut *tables)
Expand Down Expand Up @@ -404,6 +408,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
.map(|decl| stable_mir::mir::LocalDecl {
ty: decl.ty.stable(tables),
span: decl.source_info.span.stable(tables),
mutability: decl.mutability.stable(tables),
})
.collect(),
self.arg_count,
Expand Down
14 changes: 12 additions & 2 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

use crate::mir::mono::InstanceDef;
use crate::mir::Body;
use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;
use std::{cell::Cell, io};

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, IndexedVal, LineInfo, Span, TraitDecl,
Expand All @@ -36,10 +36,12 @@ pub mod mir;
pub mod ty;
pub mod visitor;

use crate::mir::pretty::function_name;
use crate::mir::Mutability;
use crate::ty::{AdtDef, AdtKind, ClosureDef, ClosureKind};
pub use error::*;
use mir::mono::Instance;
use ty::{FnDef, GenericArgs};
use ty::{Const, FnDef, GenericArgs};

/// Use String for now but we should replace it.
pub type Symbol = String;
Expand Down Expand Up @@ -137,6 +139,11 @@ impl CrateItem {
pub fn ty(&self) -> Ty {
with(|cx| cx.def_ty(self.0))
}

pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
writeln!(w, "{}", function_name(*self))?;
self.body().dump(w)
}
}

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

/// Returns literal value of a const as a string.
fn const_literal(&self, cnst: &Const) -> String;

/// `Span` of an item
fn span_of_an_item(&self, def_id: DefId) -> Span;

Expand Down
1 change: 1 addition & 0 deletions compiler/stable_mir/src/mir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod body;
pub mod mono;
pub mod pretty;
pub mod visit;

pub use body::*;
Expand Down
26 changes: 25 additions & 1 deletion compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::mir::pretty::{function_body, pretty_statement};
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, Ty};
use crate::Opaque;
use crate::Span;

use std::io;
/// The SMIR representation of a single function.
#[derive(Clone, Debug)]
pub struct Body {
Expand Down Expand Up @@ -56,6 +57,28 @@ impl Body {
pub fn locals(&self) -> &[LocalDecl] {
&self.locals
}

pub fn dump<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
writeln!(w, "{}", function_body(self))?;
self.blocks
.iter()
.enumerate()
.map(|(index, block)| -> io::Result<()> {
writeln!(w, " bb{}: {{", index)?;
let _ = block
.statements
.iter()
.map(|statement| -> io::Result<()> {
writeln!(w, "{}", pretty_statement(&statement.kind))?;
Ok(())
})
.collect::<Vec<_>>();
writeln!(w, " }}").unwrap();
Ok(())
})
.collect::<Result<Vec<_>, _>>()?;
Ok(())
}
}

type LocalDecls = Vec<LocalDecl>;
Expand All @@ -64,6 +87,7 @@ type LocalDecls = Vec<LocalDecl>;
pub struct LocalDecl {
pub ty: Ty,
pub span: Span,
pub mutability: Mutability,
}

#[derive(Clone, Debug)]
Expand Down
Loading
Loading