Skip to content

Commit 2d3b966

Browse files
committed
Auto merge of #45497 - mikhail-m1:dump-mir-graphviz, r=nikomatsakis
add graphvis DOT files to dump mir directory r? @nikomatsakis
2 parents f0fe716 + b2c2ba4 commit 2d3b966

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+
"in addition 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],
@@ -2609,6 +2611,8 @@ mod tests {
26092611
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
26102612
opts.debugging_opts.dump_mir_dir = Some(String::from("abc"));
26112613
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
2614+
opts.debugging_opts.dump_mir_graphviz = true;
2615+
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
26122616

26132617
// Make sure changing a [TRACKED] option changes the hash
26142618
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
@@ -149,6 +150,14 @@ where
149150
extra_data(PassWhere::AfterCFG, &mut file)?;
150151
Ok(())
151152
});
153+
154+
if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
155+
file_path.set_extension("dot");
156+
let _ = fs::File::create(&file_path).and_then(|mut file| {
157+
write_mir_fn_graphviz(tcx, source.item_id(), mir, &mut file)?;
158+
Ok(())
159+
});
160+
}
152161
}
153162

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

0 commit comments

Comments
 (0)