Skip to content

Commit e9395d9

Browse files
cds-amaldkcumming
andauthored
Pr/83 (#111)
Summary This PR enhances the MIR graph visualization with richer allocation information and adds D2 as an alternative output format. Key changes: - Index-first architecture: Build `AllocIndex`, `TypeIndex`, and `GraphContext` upfront before graph traversal, enabling context-aware rendering throughout - **ALLOCS legend**: New yellow info node showing all `GlobalAlloc` entries with decoded values (strings as ASCII, small integers as numeric values) - Enhanced constant labels: Constants now show provenance references like `const [alloc0: Int(I32) = 42]` instead of opaque `const ?_i32` - D2 output format (--d2 flag): Alternative to DOT with modern diagramming, viewable in browser at https://play.d2lang.com ## Addresses #83 ## Example Before: ```rust const ?_i32 ``` After: ```rust const [alloc0: Int(I32) = 42] ``` ## New CLI usage ```console cargo run -- --dot file.rs # Graphviz DOT (existing) cargo run -- --d2 file.rs # D2 format (new) ``` ## New Make targets ```console make dot # Generate all dot files make svg # Generate all svg files make png # Generate all png files make d2 # Generate all D2 files ``` ## Test plan - [ ] Run --dot on test programs, verify ALLOCS legend appears - [ ] Run --d2 on test programs, verify output renders in D2 playground - [ ] Verify make integration-test passes --------- Co-authored-by: Daniel Cumming <124537596+dkcumming@users.noreply.github.com>
1 parent b70cb77 commit e9395d9

File tree

11 files changed

+1500
-262
lines changed

11 files changed

+1500
-262
lines changed

Makefile

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ default: build
66
build:
77
cargo build ${RELEASE_FLAG}
88

9-
clean: rustup-clear-toolchain
9+
clean: rustup-clear-toolchain clean-graphs
1010
cargo clean
1111

1212
.PHONY: rustup-clear-toolchain
@@ -69,3 +69,47 @@ test-ui:
6969
exit 1; \
7070
fi
7171
bash tests/ui/run_ui_tests.sh "$$RUST_DIR_ROOT" "${VERBOSE}"
72+
73+
.PHONY: dot svg png d2 clean-graphs
74+
75+
OUTDIR_DOT=output-dot
76+
OUTDIR_SVG=output-svg
77+
OUTDIR_PNG=output-png
78+
OUTDIR_D2=output-d2
79+
80+
clean-graphs:
81+
@rm -rf $(OUTDIR_DOT) $(OUTDIR_SVG) $(OUTDIR_PNG) $(OUTDIR_D2)
82+
83+
dot:
84+
@mkdir -p $(OUTDIR_DOT)
85+
@for rs in $(TESTDIR)/*.rs; do \
86+
name=$$(basename $$rs .rs); \
87+
echo "Generating $$name.smir.dot"; \
88+
cargo run --release -- --dot -Zno-codegen $$rs 2>/dev/null; \
89+
mv $$name.smir.dot $(OUTDIR_DOT)/ 2>/dev/null || true; \
90+
done
91+
92+
svg: dot
93+
@mkdir -p $(OUTDIR_SVG)
94+
@for dotfile in $(OUTDIR_DOT)/*.dot; do \
95+
name=$$(basename $$dotfile .dot); \
96+
echo "Converting $$name.dot -> $$name.svg"; \
97+
dot -Tsvg $$dotfile -o $(OUTDIR_SVG)/$$name.svg; \
98+
done
99+
100+
png: dot
101+
@mkdir -p $(OUTDIR_PNG)
102+
@for dotfile in $(OUTDIR_DOT)/*.dot; do \
103+
name=$$(basename $$dotfile .dot); \
104+
echo "Converting $$name.dot -> $$name.png"; \
105+
dot -Tpng $$dotfile -o $(OUTDIR_PNG)/$$name.png; \
106+
done
107+
108+
d2:
109+
@mkdir -p $(OUTDIR_D2)
110+
@for rs in $(TESTDIR)/*.rs; do \
111+
name=$$(basename $$rs .rs); \
112+
echo "Generating $$name.smir.d2"; \
113+
cargo run --release -- --d2 -Zno-codegen $$rs 2>/dev/null; \
114+
mv $$name.smir.d2 $(OUTDIR_D2)/ 2>/dev/null || true; \
115+
done

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ _first_ argument `--dot`. When using `--json` as the first argument, the `*.smir
3030
will be written. Any other strings given as first argument will be passed to the compiler
3131
(like all subsequent arguments).
3232

33+
To generate visualizations for all test programs:
34+
35+
```shell
36+
make dot # Generate .dot files in output-dot/
37+
make svg # Generate .svg files in output-svg/ (requires graphviz)
38+
make png # Generate .png files in output-png/ (requires graphviz)
39+
make d2 # Generate .d2 files in output-d2/
40+
```
41+
3342
There are a few environment variables that can be set to control the tools output:
3443

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

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod driver;
44
pub mod printer;
55
use driver::stable_mir_driver;
66
use printer::emit_smir;
7-
use stable_mir_json::mk_graph::emit_dotfile;
7+
use stable_mir_json::mk_graph::{emit_d2file, emit_dotfile};
88

99
fn main() {
1010
let mut args: Vec<String> = env::args().collect();
@@ -19,6 +19,10 @@ fn main() {
1919
args.remove(1);
2020
stable_mir_driver(&args, emit_dotfile)
2121
}
22+
Some(arg) if arg == "--d2" => {
23+
args.remove(1);
24+
stable_mir_driver(&args, emit_d2file)
25+
}
2226
Some(_other) => stable_mir_driver(&args, emit_smir), // backward compatibility
2327
}
2428
}

0 commit comments

Comments
 (0)