Skip to content

Generalize interpreter traversal over CFG, Block, DiGraph, and UnGraph bodies#684

Open
zhenrongliew wants to merge 5 commits into
rustfrom
dl/generalize-body
Open

Generalize interpreter traversal over CFG, Block, DiGraph, and UnGraph bodies#684
zhenrongliew wants to merge 5 commits into
rustfrom
dl/generalize-body

Conversation

@zhenrongliew

Copy link
Copy Markdown
Collaborator

Generalize interpreter body traversal beyond CFGs

Part of #667. This addresses the second follow-up identified in the issue:

Investigate why the interpreter framework is specialized to Region/CFG. It should support bodies such as Block, DiGraph, and UnGraph, demonstrated with a language combining computational graphs and regular SSA IR.

Depends on the Region → CFG rename PR.

Summary

  • Introduces a closed Body vocabulary covering:
    • Cfg
    • Block
    • DiGraph
    • UnGraph
  • Allows callable statements to own CFG, Block, DiGraph, or UnGraph bodies.
  • Separates body representation traversal from invocation context:
    • CfgFrame traverses CFGs.
    • BlockFrame traverses one Block.
    • DiGraphFrame traverses a directed computational graph.
    • CallFrame manages callee resolution, activation lifetime, returns, and caller result binding.
  • Adds a default topological DiGraph walker and reports an error for directed cycles.
  • Provides no universal UnGraph walker. A compiler supplies UnGraph scheduling policy through FrameBuild::from_ungraph_entry; otherwise the interpreter reports NoDefaultWalker.
  • Adds a mixed test language combining ordinary CFG SSA, callable Blocks, directed computational graphs, structured Blocks, and callable UnGraph policy.

Scope

Body remains an intentionally closed enum of Kirin’s built-in body representations. The extensibility point for UnGraph is its traversal policy, since an undirected graph has no inherent execution order.

The remaining toy-lang sparse/dense liveness simplification from #667 is left for a separate PR.

This commit introduces a new test file `body_kinds.rs` that contains acceptance tests for the generic interpreter bodies. The tests cover various scenarios including:
- A Cfg-bodied `main` calling a DiGraph-bodied function.
- A statement inside a Cfg block that owns a DiGraph body.
- A linear callable with a flat instruction list.
- Ensuring that graph nodes run in topological order.

These tests aim to validate the interaction between Cfg-SSA code and DiGraph computational graphs, ensuring correct execution and error handling.
- Renamed `BodyFrame` to `BlockFrame` and adjusted related documentation to reflect the new terminology.
- Enhanced the `ToyFrame` enum to include `BlockFrame` and `CfgFrame`, replacing `BodyFrame`.
- Updated `FrameBuild` implementations in `ToyFrame` and `TracingFrame` to accommodate the new frame types.
- Revised tests in `tests/body_kinds.rs` to ensure compatibility with the updated frame structure.
- Added comprehensive tests for structured control flow (SCF) operations, including `scf.if` and `scf.for`, demonstrating their integration with the new frame types.
- Implemented a custom callable-UnGraph policy to handle ungraph traversal, ensuring proper execution order and output handling.
@zhenrongliew
zhenrongliew requested a review from Roger-luo July 15, 2026 19:22
@zhenrongliew zhenrongliew added area: interpreter Area: issues related to the interpreter. area: Rust Area: issues and pull requests related to Rust code. area: Kirin-rs Area: Rust-version Kirin related components. labels Jul 15, 2026
Base automatically changed from dl/rename-region-to-cfg to rust July 17, 2026 04:11
Resolve conflicts from the Region->CFG rename (#683, now in rust):
- Apply the same Cfg->CFG / CFGs->CFG naming to this branch's changes.
- Keep this branch's restructure of concrete/frames.rs into concrete/frames/
  (drop the stale single-file frames.rs that rust only renamed).
- Drop now-unused CFG imports surfaced by the merge.

All tests (1234) and doctests pass; fmt and clippy clean.
/// are structurally legal IR but have no single-pass execution order.
pub struct DiGraphWalkQuery(pub kirin_ir::DiGraph);

impl<S, L> StageAction<S, L> for DiGraphWalkQuery

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remind me, what is a StageAction?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StageAction lets the interpreter access the specific stage-local DiGraph. In this case it resolves the graph and builds the walk plan that DiGraphFrame executes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: interpreter Area: issues related to the interpreter. area: Kirin-rs Area: Rust-version Kirin related components. area: Rust Area: issues and pull requests related to Rust code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants