Skip to content

Commit 08b1fe3

Browse files
committed
add graphvis DOT files to dump mir directory
1 parent d5d4494 commit 08b1fe3

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/librustc/session/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10571057
"dump MIR state at various points in translation"),
10581058
dump_mir_dir: Option<String> = (None, parse_opt_string, [UNTRACKED],
10591059
"the directory the MIR is dumped into"),
1060+
dump_mir_graphviz: bool = (false, parse_bool, [UNTRACKED],
1061+
"additionally to `.mir` files, create graphviz `.dot` files"),
10601062
dump_mir_exclude_pass_number: bool = (false, parse_bool, [UNTRACKED],
10611063
"if set, exclude the pass number when dumping MIR (used in tests)"),
10621064
mir_emit_validate: usize = (0, parse_uint, [TRACKED],
@@ -2650,6 +2652,8 @@ mod tests {
26502652
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
26512653
opts.debugging_opts.dump_mir_dir = Some(String::from("abc"));
26522654
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
2655+
opts.debugging_opts.dump_mir_graphviz = true;
2656+
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
26532657

26542658
// Make sure changing a [TRACKED] option changes the hash
26552659
opts = reference.clone();

src/librustc_mir/util/graphviz.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,38 @@ pub fn write_mir_graphviz<'a, 'tcx, W>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
3030
for def_id in dump_mir_def_ids(tcx, single) {
3131
let nodeid = tcx.hir.as_local_node_id(def_id).unwrap();
3232
let mir = &tcx.optimized_mir(def_id);
33+
write_mir_fn_graphviz(tcx, nodeid, mir, w)?;
34+
}
35+
Ok(())
36+
}
3337

34-
writeln!(w, "digraph Mir_{} {{", nodeid)?;
38+
/// Write a graphviz DOT graph of the MIR.
39+
pub fn write_mir_fn_graphviz<'a, 'tcx, W>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
40+
nodeid: NodeId,
41+
mir: &Mir,
42+
w: &mut W) -> io::Result<()>
43+
where W: Write
44+
{
45+
writeln!(w, "digraph Mir_{} {{", nodeid)?;
3546

36-
// Global graph properties
37-
writeln!(w, r#" graph [fontname="monospace"];"#)?;
38-
writeln!(w, r#" node [fontname="monospace"];"#)?;
39-
writeln!(w, r#" edge [fontname="monospace"];"#)?;
47+
// Global graph properties
48+
writeln!(w, r#" graph [fontname="monospace"];"#)?;
49+
writeln!(w, r#" node [fontname="monospace"];"#)?;
50+
writeln!(w, r#" edge [fontname="monospace"];"#)?;
4051

41-
// Graph label
42-
write_graph_label(tcx, nodeid, mir, w)?;
52+
// Graph label
53+
write_graph_label(tcx, nodeid, mir, w)?;
4354

44-
// Nodes
45-
for (block, _) in mir.basic_blocks().iter_enumerated() {
46-
write_node(block, mir, w)?;
47-
}
55+
// Nodes
56+
for (block, _) in mir.basic_blocks().iter_enumerated() {
57+
write_node(block, mir, w)?;
58+
}
4859

49-
// Edges
50-
for (source, _) in mir.basic_blocks().iter_enumerated() {
51-
write_edges(source, mir, w)?;
52-
}
53-
writeln!(w, "}}")?
60+
// Edges
61+
for (source, _) in mir.basic_blocks().iter_enumerated() {
62+
write_edges(source, mir, w)?;
5463
}
55-
Ok(())
64+
writeln!(w, "}}")
5665
}
5766

5867
/// Write a graphviz HTML-styled label for the given basic block, with

src/librustc_mir/util/pretty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::fmt::Display;
2020
use std::fs;
2121
use std::io::{self, Write};
2222
use std::path::{PathBuf, Path};
23+
use super::graphviz::write_mir_fn_graphviz;
2324

2425
const INDENT: &'static str = " ";
2526
/// Alignment for lining up comments following MIR statements
@@ -128,6 +129,14 @@ fn dump_matched_mir_node<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
128129
write_mir_fn(tcx, source, mir, &mut file)?;
129130
Ok(())
130131
});
132+
133+
if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
134+
file_path.set_extension("dot");
135+
let _ = fs::File::create(&file_path).and_then(|mut file| {
136+
write_mir_fn_graphviz(tcx, source.item_id(), mir, &mut file)?;
137+
Ok(())
138+
});
139+
}
131140
}
132141

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

0 commit comments

Comments
 (0)