Skip to content

Rewrite For loop over IList to a ranged For loop for better unrolling. #597

Merged
Roger-luo merged 4 commits intomainfrom
phil/rewrite-ilist-loop-to-range
Feb 25, 2026
Merged

Rewrite For loop over IList to a ranged For loop for better unrolling. #597
Roger-luo merged 4 commits intomainfrom
phil/rewrite-ilist-loop-to-range

Conversation

@weinbe58
Copy link
Copy Markdown
Member

@weinbe58 weinbe58 commented Jan 8, 2026

In this PR I implement a rewrite that effectively rewrites:

for ele in ilist_value:
    ...

to

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.

@weinbe58 weinbe58 changed the title Rewrite For loop over Ilist to a ranged For loop for better folding. Rewrite For loop over IList to a ranged For loop for better folding. Jan 8, 2026
@weinbe58 weinbe58 changed the title Rewrite For loop over IList to a ranged For loop for better folding. Rewrite For loop over IList to a ranged For loop for better unrolling. Jan 8, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 8, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11324 10178 90% 0% 🟢

New Files

File Coverage Status
src/kirin/dialects/ilist/rewrite/to_range_loop.py 100% 🟢
TOTAL 100% 🟢

Modified Files

File Coverage Status
src/kirin/dialects/ilist/rewrite/_init_.py 100% 🟢
TOTAL 100% 🟢

updated for commit: 84dd733 by action🐍

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 8, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-02-25 05:37 UTC

@weinbe58 weinbe58 requested a review from Roger-luo January 8, 2026 15:20
@weinbe58 weinbe58 added P-Low Priority: low priority issues and tasks. category: enhancement Category: this is an enhancement of an existing feature. labels Jan 8, 2026
@weinbe58 weinbe58 mentioned this pull request Jan 8, 2026
@Roger-luo Roger-luo requested a review from Copilot February 25, 2026 05:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.ToRangeFor rewrite 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.

Comment thread src/kirin/dialects/ilist/rewrite/to_range_loop.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Roger-luo Roger-luo merged commit 47cdf79 into main Feb 25, 2026
12 of 13 checks passed
@Roger-luo Roger-luo deleted the phil/rewrite-ilist-loop-to-range branch February 25, 2026 05:36
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: enhancement Category: this is an enhancement of an existing feature. P-Low Priority: low priority issues and tasks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants