]> git.lizzy.rs Git - rust.git/commitdiff
add graphvis DOT files to dump mir directory
authorMikhail Modin <mikhailm1@gmail.com>
Tue, 24 Oct 2017 11:48:39 +0000 (14:48 +0300)
committerYour Name <mikhailm1@gmail.com>
Thu, 26 Oct 2017 08:14:08 +0000 (11:14 +0300)
src/librustc/session/config.rs
src/librustc_mir/util/graphviz.rs
src/librustc_mir/util/pretty.rs

index fc1c5e187ecc7b79d95d67dcd4df548b66f0ad44..db439c0787cbe1767f65964d7245b1219f9fcb60 100644 (file)
@@ -1057,6 +1057,8 @@ fn parse_optimization_fuel(slot: &mut Option<(String, u64)>, v: Option<&str>) ->
           "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],
+          "additionally 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],
@@ -2650,6 +2652,8 @@ fn test_debugging_options_tracking_hash() {
         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();
index cf13a80e677b1266bac748cd8cfd43360ff00758..cec8067530b6119563798853f61f566fc1a0c52b 100644 (file)
@@ -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
index 9e1f05f6d2f777cc2b5893c59227ff9848991237..61914a6fc36d433e556c6dcd15ba28a1e14031e9 100644 (file)
@@ -20,6 +20,7 @@
 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
@@ -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.