Skip to content

Commit 1cc3e92

Browse files
jbertholddkcumming
andauthored
Option to generate a dot file representing the smir-json (#34)
In this first version we don't draw the statements of blocks, just the block structure and caller/callee relations between blocks/items. In terms of interface, the `main` program now has options `--json` and `--dot` which have to come first, and we default to `--json` if they are absent. Output example: (main calls `foo(0)` calls `bar(0)`) ![image](https://github.com/user-attachments/assets/114bb9cb-e21a-4fd2-af4a-e2f8f0c87c1c) The HS version had more color but was otherwise the same: ![image](https://github.com/user-attachments/assets/c391e9a7-4129-4702-a3c2-feb8a896ed78) --------- Co-authored-by: Daniel Cumming <124537596+dkcumming@users.noreply.github.com>
1 parent 5314e1f commit 1cc3e92

File tree

7 files changed

+452
-58
lines changed

7 files changed

+452
-58
lines changed

Cargo.lock

Lines changed: 25 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
dot-writer = "0.1.4"
1011
tracing = "0.1"
1112
# serde = { version = "=1.0.202", features = ["derive"] }
1213
# serde_cbor = "0.11"

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ To generate stable MIR output without building a binary, you can invoke the tool
3939
./run.sh -Z no-codegen <crate_root>
4040
```
4141

42+
There is experimental support for rendering the Stable-MIR items and their basic blocks as a
43+
call graph in graphviz' dot format.
44+
45+
To produce a dot file `*.smir.dot` (instead of `*.smir.json`), one can invoke the driver with
46+
_first_ argument `--dot`. When using `--json` as the first argument, the `*.smir.json` file
47+
will be written. Any other strings given as first argument will be passed to the compiler
48+
(like all subsequent arguments).
49+
4250
There are a few environment variables that can be set to control the tools output:
4351

4452
1. `LINK_ITEMS` - add entries to the link-time `functions` map for each monomorphic item in the crate;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(rustc_private)]
22
pub mod driver;
3+
pub mod mk_graph;
34
pub mod printer;
45
pub use driver::stable_mir_driver;
56
pub use printer::*;

src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ pub mod driver;
44
pub mod printer;
55
use driver::stable_mir_driver;
66
use printer::emit_smir;
7+
use smir_pretty::mk_graph::emit_dotfile;
78

89
fn main() {
9-
let args: Vec<_> = env::args().collect();
10-
stable_mir_driver(&args, emit_smir)
10+
let mut args: Vec<String> = env::args().collect();
11+
12+
match args.get(1) {
13+
None =>
14+
stable_mir_driver(&args, emit_smir), // backward compatibility
15+
Some(arg) if arg == "--json" => {
16+
args.remove(1);
17+
stable_mir_driver(&args, emit_smir)
18+
}
19+
Some(arg) if arg == "--dot" => {
20+
args.remove(1);
21+
stable_mir_driver(&args, emit_dotfile)
22+
}
23+
Some(_other) =>
24+
stable_mir_driver(&args, emit_smir), // backward compatibility
25+
}
1126
}

0 commit comments

Comments
 (0)