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

add graphvis DOT files to dump mir directory #45497

Merged
merged 2 commits into from
Oct 31, 2017
Merged
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
4 changes: 4 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"dump MIR state at various points in translation"),
dump_mir_dir: Option<String> = (None, parse_opt_string, [UNTRACKED],
"the directory the MIR is dumped into"),
dump_mir_graphviz: bool = (false, parse_bool, [UNTRACKED],
"in addition to `.mir` files, create graphviz `.dot` files"),
dump_mir_exclude_pass_number: bool = (false, parse_bool, [UNTRACKED],
"if set, exclude the pass number when dumping MIR (used in tests)"),
mir_emit_validate: usize = (0, parse_uint, [TRACKED],
Expand Down Expand Up @@ -2650,6 +2652,8 @@ mod tests {
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.dump_mir_dir = Some(String::from("abc"));
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
opts.debugging_opts.dump_mir_graphviz = true;
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());

// Make sure changing a [TRACKED] option changes the hash
opts = reference.clone();
Expand Down
43 changes: 26 additions & 17 deletions src/librustc_mir/util/graphviz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,38 @@ pub fn write_mir_graphviz<'a, 'tcx, W>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
for def_id in dump_mir_def_ids(tcx, single) {
let nodeid = tcx.hir.as_local_node_id(def_id).unwrap();
let mir = &tcx.optimized_mir(def_id);
write_mir_fn_graphviz(tcx, nodeid, mir, w)?;
}
Ok(())
}

writeln!(w, "digraph Mir_{} {{", nodeid)?;
/// Write a graphviz DOT graph of the MIR.
pub fn write_mir_fn_graphviz<'a, 'tcx, W>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
nodeid: NodeId,
mir: &Mir,
w: &mut W) -> io::Result<()>
where W: Write
{
writeln!(w, "digraph Mir_{} {{", nodeid)?;

// Global graph properties
writeln!(w, r#" graph [fontname="monospace"];"#)?;
writeln!(w, r#" node [fontname="monospace"];"#)?;
writeln!(w, r#" edge [fontname="monospace"];"#)?;
// Global graph properties
writeln!(w, r#" graph [fontname="monospace"];"#)?;
writeln!(w, r#" node [fontname="monospace"];"#)?;
writeln!(w, r#" edge [fontname="monospace"];"#)?;

// Graph label
write_graph_label(tcx, nodeid, mir, w)?;
// Graph label
write_graph_label(tcx, nodeid, mir, w)?;

// Nodes
for (block, _) in mir.basic_blocks().iter_enumerated() {
write_node(block, mir, w)?;
}
// Nodes
for (block, _) in mir.basic_blocks().iter_enumerated() {
write_node(block, mir, w)?;
}

// Edges
for (source, _) in mir.basic_blocks().iter_enumerated() {
write_edges(source, mir, w)?;
}
writeln!(w, "}}")?
// Edges
for (source, _) in mir.basic_blocks().iter_enumerated() {
write_edges(source, mir, w)?;
}
Ok(())
writeln!(w, "}}")
}

/// Write a graphviz HTML-styled label for the given basic block, with
Expand Down
9 changes: 9 additions & 0 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::fmt::Display;
use std::fs;
use std::io::{self, Write};
use std::path::{PathBuf, Path};
use super::graphviz::write_mir_fn_graphviz;

const INDENT: &'static str = " ";
/// Alignment for lining up comments following MIR statements
Expand Down Expand Up @@ -128,6 +129,14 @@ fn dump_matched_mir_node<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
write_mir_fn(tcx, source, mir, &mut file)?;
Ok(())
});

if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
file_path.set_extension("dot");
let _ = fs::File::create(&file_path).and_then(|mut file| {
write_mir_fn_graphviz(tcx, source.item_id(), mir, &mut file)?;
Ok(())
});
}
}

/// Write out a human-readable textual representation for the given MIR.
Expand Down