Skip to content

Commit

Permalink
inline mir in .dot file
Browse files Browse the repository at this point in the history
usage
```
rustc +nightly -Znll-facts -Zdump-mir="nll" main.rs
polonius -a naive --graphviz-file naive.dot --mir-file mir_dump/main.main.-------.nll.0.mir nll-facts/main/
dot -Tsvg naive.dot -o naive.svg
open naive.svg
```
  • Loading branch information
lengyijun committed Oct 16, 2023
1 parent 0a754a9 commit a0a5d53
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 5 deletions.
197 changes: 197 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ polonius-engine = { path = "./polonius-engine" }
log = "0.4"
petgraph = "0.4.13"
pico-args = "0.2"
pest = "2.7.4"
pest_derive = "2.7.4"

[workspace]
9 changes: 8 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::dump;
use crate::dump::Output;
use crate::facts::AllFacts;
use crate::intern;
use crate::mir_parser;
use crate::tab_delim;

const PKG_NAME: &str = env!("CARGO_PKG_NAME");
Expand All @@ -29,6 +30,7 @@ pub struct Options {
output_directory: Option<String>,
fact_dirs: Vec<String>,
liveness_graph_file: Option<String>,
mir_file: Option<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -86,7 +88,11 @@ pub fn main(opt: Options) -> Result<(), Error> {
.expect("Failed to write output");
}
if let Some(ref graphviz_file) = graphviz_file {
dump::graphviz(&output, &all_facts, graphviz_file, tables)
let mir = opt
.mir_file
.as_ref()
.map(|x| mir_parser::parse(Path::new(&x)));
dump::graphviz(&output, &all_facts, graphviz_file, tables, &mir)
.expect("Failed to write GraphViz");
}
if let Some(ref liveness_graph_file) = liveness_graph_file {
Expand Down Expand Up @@ -169,6 +175,7 @@ ARGS:
graphviz_file: arg_from_str(&mut args, "--graphviz-file")?,
output_directory: arg_from_str(&mut args, "-o")?.or(arg_from_str(&mut args, "--output")?),
liveness_graph_file: arg_from_str(&mut args, "--dump-liveness-graph")?,
mir_file: arg_from_str(&mut args, "--mir-file")?,
fact_dirs: args.free().map_err(readable_pico_error)?,
};

Expand Down
27 changes: 23 additions & 4 deletions src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ pub(crate) fn graphviz(
all_facts: &AllFacts,
output_file: &PathBuf,
intern: &InternerTables,
mir: &Option<HashMap<String, Vec<String>>>,
) -> io::Result<()> {
let mut file = File::create(output_file)?;
let mut output_fragments: Vec<String> = Vec::new();
Expand All @@ -522,6 +523,7 @@ pub(crate) fn graphviz(
&inputs_by_point,
&outputs_by_point,
intern,
mir,
)
.into_iter();
output_fragments.extend(graphviz_code);
Expand All @@ -540,6 +542,7 @@ fn graphviz_for_edge(
inputs_by_point: &[HashMap<Point, String>],
outputs_by_point: &[HashMap<Point, String>],
intern: &InternerTables,
mir: &Option<HashMap<String, Vec<String>>>,
) -> Vec<String> {
let mut ret = Vec::new();
maybe_render_point(
Expand All @@ -549,6 +552,7 @@ fn graphviz_for_edge(
outputs_by_point,
&mut ret,
intern,
mir,
);
maybe_render_point(
point2,
Expand All @@ -557,6 +561,7 @@ fn graphviz_for_edge(
outputs_by_point,
&mut ret,
intern,
mir,
);
ret.push(format!(
"\"node{0}\" -> \"node{1}\":f0 [\n id = {2}\n];\n",
Expand All @@ -574,6 +579,7 @@ fn maybe_render_point(
outputs_by_point: &[HashMap<Point, String>],
render_vec: &mut Vec<String>,
intern: &InternerTables,
mir: &Option<HashMap<String, Vec<String>>>,
) {
if seen_points.contains(&point.index()) {
return;
Expand All @@ -592,11 +598,24 @@ fn maybe_render_point(
.collect::<Vec<_>>()
.join(" | ");

render_vec.push(format!("\"node{0}\" [\n label = \"{{ <f0> {1} | INPUTS | {2} | OUTPUTS | {3} }}\"\n shape = \"record\"\n];\n",
let point_str = escape_for_graphviz(Point::table(intern).untern(point));
let (bb_name, offset) = extract(&point_str);
let instr: String = mir
.as_ref()
.and_then(|hm| Some(format!("| {}", escape_for_graphviz(&hm[bb_name][offset]))))
.unwrap_or_default();
render_vec.push(format!("\"node{0}\" [\n label = \"{{ <f0> {point_str} {instr} | INPUTS | {input_tuples} | OUTPUTS | {output_tuples} }}\"\n shape = \"record\"\n];\n",
point.index(),
escape_for_graphviz(Point::table(intern).untern(point)),
&input_tuples,
&output_tuples));
));
}

fn extract(x: &str) -> (&str, usize) {
let a = x.find('(').unwrap();
let b = x.find('[').unwrap();
let c = x.find(']').unwrap();
let bb_name = &x[a + 1..b];
let offset = &x[b + 1..c];
(bb_name, offset.parse().unwrap())
}

fn escape_for_graphviz(s: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod dump;
mod facts;
mod intern;
mod mir_parser;
mod program;
mod tab_delim;
mod test;
Expand Down
Loading

0 comments on commit a0a5d53

Please sign in to comment.