Skip to content

Commit

Permalink
Rollup merge of rust-lang#39891 - shepmaster:emit-mir, r=nikomatsakis
Browse files Browse the repository at this point in the history
Teach rustc --emit=mir

I'm opening this PR to discuss:

1. Is this a good idea?
1. Is this a good implementation?

I'm sure people will have opinions on both points!

This spawned from rust-lang#31847 (comment), so I figured a prototype implementation could help provide a seed to talk about.
  • Loading branch information
frewsxcv authored Mar 23, 2017
2 parents c62e532 + 4ddedf7 commit 916c0b8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum OutputType {
Bitcode,
Assembly,
LlvmAssembly,
Mir,
Metadata,
Object,
Exe,
Expand All @@ -96,6 +97,7 @@ impl OutputType {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Metadata => false,
}
Expand All @@ -106,6 +108,7 @@ impl OutputType {
OutputType::Bitcode => "llvm-bc",
OutputType::Assembly => "asm",
OutputType::LlvmAssembly => "llvm-ir",
OutputType::Mir => "mir",
OutputType::Object => "obj",
OutputType::Metadata => "metadata",
OutputType::Exe => "link",
Expand All @@ -118,6 +121,7 @@ impl OutputType {
OutputType::Bitcode => "bc",
OutputType::Assembly => "s",
OutputType::LlvmAssembly => "ll",
OutputType::Mir => "mir",
OutputType::Object => "o",
OutputType::Metadata => "rmeta",
OutputType::DepInfo => "d",
Expand Down Expand Up @@ -172,6 +176,7 @@ impl OutputTypes {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Exe => true,
OutputType::Metadata |
Expand Down Expand Up @@ -1370,6 +1375,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
let output_type = match parts.next().unwrap() {
"asm" => OutputType::Assembly,
"llvm-ir" => OutputType::LlvmAssembly,
"mir" => OutputType::Mir,
"llvm-bc" => OutputType::Bitcode,
"obj" => OutputType::Object,
"metadata" => OutputType::Metadata,
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ pub fn compile_input(sess: &Session,
tcx.print_debug_stats();
}

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
}
}

Ok((outputs, trans))
})??
};
Expand Down
14 changes: 14 additions & 0 deletions src/librustc_mir/transform/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
//! This pass just dumps MIR at a specified point.

use std::fmt;
use std::fs::File;
use std::io;

use rustc::session::config::{OutputFilenames, OutputType};
use rustc::ty::TyCtxt;
use rustc::mir::*;
use rustc::mir::transform::{Pass, MirPass, MirPassHook, MirSource};
Expand Down Expand Up @@ -70,3 +73,14 @@ impl<'tcx> MirPassHook<'tcx> for DumpMir {
}

impl<'b> Pass for DumpMir {}

pub fn emit_mir<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
outputs: &OutputFilenames)
-> io::Result<()>
{
let path = outputs.path(OutputType::Mir);
let mut f = File::create(&path)?;
mir_util::write_mir_pretty(tcx, tcx.maps.mir.borrow().keys().into_iter(), &mut f)?;
Ok(())
}
3 changes: 3 additions & 0 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub fn write_mir_pretty<'a, 'b, 'tcx, I>(tcx: TyCtxt<'b, 'tcx, 'tcx>,
-> io::Result<()>
where I: Iterator<Item=DefId>, 'tcx: 'a
{
writeln!(w, "// WARNING: This output format is intended for human consumers only")?;
writeln!(w, "// and is subject to change without notice. Knock yourself out.")?;

let mut first = true;
for def_id in iter.filter(DefId::is_local) {
let mir = &tcx.item_mir(def_id);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ pub fn run_passes(sess: &Session,
modules_config.emit_obj = true;
metadata_config.emit_obj = true;
},
OutputType::Mir => {}
OutputType::DepInfo => {}
}
}
Expand Down Expand Up @@ -880,6 +881,7 @@ pub fn run_passes(sess: &Session,
user_wants_objects = true;
copy_if_one_unit(OutputType::Object, true);
}
OutputType::Mir |
OutputType::Metadata |
OutputType::Exe |
OutputType::DepInfo => {}
Expand Down

0 comments on commit 916c0b8

Please sign in to comment.