Skip to content

fix Region.clone() to use reverse-post-order traversal#635

Merged
neelay893 merged 5 commits intomainfrom
neelay/fix-region-clone
Apr 13, 2026
Merged

fix Region.clone() to use reverse-post-order traversal#635
neelay893 merged 5 commits intomainfrom
neelay/fix-region-clone

Conversation

@neelay893
Copy link
Copy Markdown
Contributor

@neelay893 neelay893 commented Apr 9, 2026

Aggressive fold over multiple iterations (fixpoint), can sometimes produce a region with an out of order block sequence (even if the CFG is still well formed). This can cause issues when using Region.clone(), where a block using some SSA value is cloned before the block defining it, resulting in the use SSA still pointing to the old object.

This PR changes Region.clone() to traverse blocks in reverse-post-order to avoid this adds a SortBlocks rewrite rule to sort the blocks in each region in reverse-post-order. SortBlocks is applied at the end of the CompactifyRegion pass.

neelay893 and others added 3 commits April 9, 2026 17:02
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Region.clone() previously iterated blocks in index order, which could
leave cloned operands pointing at the original region when aggressive
fold produced out-of-definition-order block layouts. Now clones
statements in reverse post-order of the CFG, and raises ValueError
if an in-region def is used before being cloned.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 9, 2026

Codecov Report

❌ Patch coverage is 97.91667% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/kirin/rewrite/sort_blocks.py 97.14% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11493 10331 90% 0% 🟢

New Files

File Coverage Status
src/kirin/rewrite/sort_blocks.py 97% 🟢
TOTAL 97% 🟢

Modified Files

File Coverage Status
src/kirin/ir/nodes/region.py 80% 🟢
src/kirin/rewrite/_init_.py 100% 🟢
src/kirin/rewrite/compactify.py 98% 🟢
TOTAL 93% 🟢

updated for commit: 9c1117e by action🐍

@neelay893 neelay893 requested a review from Roger-luo April 9, 2026 22:01
@neelay893 neelay893 marked this pull request as ready for review April 9, 2026 22:02
Copy link
Copy Markdown
Collaborator

@Roger-luo Roger-luo left a comment

Choose a reason for hiding this comment

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

I think this is a good change to canonicalize the order of iteration, but I believe it should not be part of the IR iterator but should be a rewrite pass - otherwise we need to pay the price everytime we try to iterate the blocks

Move the RPO traversal out of Region.clone() into a dedicated
SortBlocks rewrite rule that mutates the block sequence in-place.
Integrate it into CompactifyRegion so blocks are sorted after
CFG compactification. Region.clone() now iterates self.blocks
(which are already in RPO order) with a safety guard that raises
ValueError if an in-region def is encountered before being cloned.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Roger-luo Roger-luo added area: IR Area: issues related to the IR module. category: bug Category: this is a bug or something isn't working as expected. labels Apr 10, 2026
- test_sort_blocks_fixes_scrambled_region: manually scrambles block
  order, runs SortBlocks, verifies clone produces self-contained region
- test_region_clone_raises_on_unsorted_blocks: scrambles blocks on a
  folded method and asserts Region.clone raises ValueError
- Add comment in SortBlocks noting reliance on up-to-date CFG

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@neelay893 neelay893 self-assigned this Apr 10, 2026
@neelay893 neelay893 requested a review from Roger-luo April 10, 2026 14:55
@Roger-luo
Copy link
Copy Markdown
Collaborator

@neelay893 can you share your claude prompts in the PR for future reference? the PR itself looks good.

@neelay893
Copy link
Copy Markdown
Contributor Author

Claude prompts:

❯ there is an issue w/ Region.clone() where the blocks are traversed in index order, not topologically sorted CFG order. This is usually fine, but can cause problems when fixpointing aggressive fold. See kirin_aggressive_fold_mwe.py for an example, and kirin_region_clone_bug.md for a longer description.
[I'd previously asked codex to debug and issue and generate an MWE and markdown file describing the bug]

[it then made a plan which we went back and forth on for a bit; I can try to find the full chat history if this is of interest]

❯ please review the changes

❯ please refactor the RPO traversal into a rewrite pass that mutates the block sequence within the region, such that subsequent traversals will be in the correct order

@neelay893 neelay893 merged commit d600bcc into main Apr 13, 2026
12 of 13 checks passed
@neelay893 neelay893 deleted the neelay/fix-region-clone branch April 13, 2026 15:23
Roger-luo pushed a commit that referenced this pull request Apr 13, 2026
Aggressive fold over multiple iterations (fixpoint), can sometimes
produce a region with an out of order block sequence (even if the CFG is
still well formed). This can cause issues when using `Region.clone()`,
where a block using some SSA value is cloned before the block defining
it, resulting in the use SSA still pointing to the old object.

This PR ~~changes `Region.clone()` to traverse blocks in
reverse-post-order to avoid this~~ adds a `SortBlocks` rewrite rule to
sort the blocks in each region in reverse-post-order. `SortBlocks` is
applied at the end of the `CompactifyRegion` pass.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: IR Area: issues related to the IR module. category: bug Category: this is a bug or something isn't working as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants