Rewrite For loop over IList to a ranged For loop for better unrolling. #597
Merged
Rewrite For loop over IList to a ranged For loop for better unrolling. #597
IList to a ranged For loop for better unrolling. #597Conversation
Ilist to a ranged For loop for better folding. IList to a ranged For loop for better folding.
IList to a ranged For loop for better folding. IList to a ranged For loop for better unrolling.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
☂️ Python Coverage
Overall Coverage
New Files
Modified Files
|
Contributor
|
Open
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new ilist rewrite rule that converts scf.For loops iterating over IList into index-based loops over a range(len(...))-style iterable, enabling subsequent rewrites/passes (notably HintLen + UnrollScf) to more reliably unroll such loops.
Changes:
- Add
ilist.rewrite.ToRangeForrewrite rule to transform element-iteration into range + getitem form. - Export the new rewrite rule from
kirin.dialects.ilist.rewrite. - Add a test that composes
ToRangeFor+HintLen+ folding +UnrollScf+ getitem inlining and validates the final simplified IR shape.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/dialects/test_ilist.py | Adds an end-to-end test validating the new rewrite enables unrolling and getitem inlining. |
| src/kirin/dialects/ilist/rewrite/to_range_loop.py | Implements the ToRangeFor rewrite that rewrites scf.For over IList to a range-based loop with an inserted getitem. |
| src/kirin/dialects/ilist/rewrite/init.py | Exports ToRangeFor from the ilist rewrite package. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Roger-luo
added a commit
that referenced
this pull request
Feb 25, 2026
…ng. (#597) In this PR I implement a rewrite that effectively rewrites: ```python for ele in ilist_value: ... ``` to ```python for i in range(len(ilist_value)): ele = ilist_value[i] ... ``` which is equivalent in terms of execution but is easier to massage into a state that can be unrolled by combining this rewrite with `HintLen` rewrite rule effectively allowing the `iterable` argument of the new `scf.For` statement to be known at compile time even if `ilist_value` is not a constant. The reason I think we should have this rule instead of implementing a specialization in the unroll of the for loop is that this rewrite is independent of the unroll rewrite and so you do not need to worry about a dependency between the unroll rule and the `ilist` dialect. --------- Co-authored-by: Xiu-zhe (Roger) Luo <rluo@quera.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In this PR I implement a rewrite that effectively rewrites:
to
which is equivalent in terms of execution but is easier to massage into a state that can be unrolled by combining this rewrite with
HintLenrewrite rule effectively allowing theiterableargument of the newscf.Forstatement to be known at compile time even ifilist_valueis not a constant.The reason I think we should have this rule instead of implementing a specialization in the unroll of the for loop is that this rewrite is independent of the unroll rewrite and so you do not need to worry about a dependency between the unroll rule and the
ilistdialect.