Rollup merge of #39891 - shepmaster:emit-mir, r=nikomatsakis

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 https://github.com/rust-lang/rust/issues/31847#issuecomment-279179057, so I figured a prototype implementation could help provide a seed to talk about.
This commit is contained in:
Corey Farwell 2017-03-22 23:37:58 -04:00 committed by GitHub
commit 916c0b83cc
5 changed files with 32 additions and 0 deletions

View File

@ -82,6 +82,7 @@ pub enum OutputType {
Bitcode,
Assembly,
LlvmAssembly,
Mir,
Metadata,
Object,
Exe,
@ -96,6 +97,7 @@ impl OutputType {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Metadata => false,
}
@ -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",
@ -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",
@ -172,6 +176,7 @@ impl OutputTypes {
OutputType::Bitcode |
OutputType::Assembly |
OutputType::LlvmAssembly |
OutputType::Mir |
OutputType::Object |
OutputType::Exe => true,
OutputType::Metadata |
@ -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,

View File

@ -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))
})??
};

View File

@ -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};
@ -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(())
}

View File

@ -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);

View File

@ -741,6 +741,7 @@ pub fn run_passes(sess: &Session,
modules_config.emit_obj = true;
metadata_config.emit_obj = true;
},
OutputType::Mir => {}
OutputType::DepInfo => {}
}
}
@ -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 => {}