Skip to content

Conversation

@cds-amal
Copy link

@cds-amal cds-amal commented Nov 30, 2025

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:

const ?_i32

After:

const [alloc0: Int(I32) = 42]

New CLI usage

cargo run -- --dot file.rs   # Graphviz DOT (existing)
cargo run -- --d2 file.rs    # D2 format (new)

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

Restructure the graph generation to build indices upfront rather than
resolving relationships at traversal time. This improves maintainability
and enables richer constant/allocation information in the output.

Add new data structures for graph context:
- AllocIndex: maps AllocId to processed AllocEntry with descriptions
- AllocEntry: contains alloc metadata and human-readable description
- AllocKind: categorizes allocs as Memory/Static/VTable/Function
- TypeIndex: maps type IDs to display names
- GraphContext: holds all indices and provides rendering methods

Add ALLOCS legend node to graphs:
- Display all GlobalAlloc entries in a yellow info node
- Show allocation ID, type, and decoded value where possible
- Decode string literals as escaped ASCII
- Decode small integers as numeric values

Enhance constant operand labels:
- Show provenance references: `const [alloc0: Int(I32) = 42]`
- Display inline constant values with types: `const 42_Uint(Usize)`
- Show function names for ZeroSized function constants

Add context-aware rendering functions:
- render_stmt_ctx: renders statements with alloc context
- render_rvalue_ctx: renders rvalues with operand context
- render_intrinsic_ctx: renders intrinsics with operand context

Update to_dot_file to use new architecture:
- Build GraphContext before consuming SmirJson
- Pass context to statement/operand rendering
- Use context for call edge argument labels

Add accessor methods to AllocInfo in printer.rs:
- alloc_id(): returns the allocation ID
- ty(): returns the type
- global_alloc(): returns reference to GlobalAlloc
Add alternative graph output format using D2 language alongside existing
DOT format. D2 offers modern diagramming with better text rendering and
can be viewed in browser via d2lang.com playground.

Changes:
- Add --d2 command-line flag for D2 output
- Add to_d2_file() method to SmirJson for D2 rendering
- Add emit_d2file() entry point
- Add render_terminator_ctx() and terminator_targets() helpers
- Add resolve_call_target() method to GraphContext
- Add escape_d2() to handle $ and other special characters in labels

Output includes:
- ALLOCS legend with allocation descriptions
- Function containers with basic blocks
- Block-to-block control flow edges
- Call edges to external functions
- Change &String to &str in short_name() and block_name() signatures
- Extract bytes_to_u64_le() helper for repeated byte-to-int conversion
- Fix expect() messages to properly interpolate path using unwrap_or_else
Extract D2 rendering into focused helper functions:
- render_d2_allocs_legend
- render_d2_function
- render_d2_blocks
- render_d2_block_edges
- render_d2_call_edges
- render_d2_asm
- render_d2_static
@dkcumming
Copy link
Collaborator

Hey @cds-amal looks great, I am excited to give this a go later on. Our CI is currently stuck on Code Quality, this can be fixed by running cargo clippy and cargo fmt and resolving the errors. I will be in touch on this soon, thank you!

@cds-amal cds-amal marked this pull request as ready for review December 2, 2025 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants