Skip to content

feat(ir): implement HoistConstant rule for lifting py.Constant in ilist.ForEach|ilist.Map - #704

Open
zhenrongliew wants to merge 2 commits into
mainfrom
dl/hoist-constant
Open

feat(ir): implement HoistConstant rule for lifting py.Constant in ilist.ForEach|ilist.Map#704
zhenrongliew wants to merge 2 commits into
mainfrom
dl/hoist-constant

Conversation

@zhenrongliew

Copy link
Copy Markdown
Collaborator

Adds HoistConstant, a rewrite that lifts py.Constant statements out of the closure bodies passed to ilist.ForEach / ilist.Map, and re-introduces them as closure captures.

# before                                    # after
%x = py.constant.constant 10                %x = py.constant.constant 10
%body = func.lambda body(%x) {              %hoisted = py.constant.constant 2
  ^1(%body_self, %i):                       %body = func.lambda body(%x, %hoisted) {
  │ %x_1 = func.getfield(%body_self, 0)       ^1(%body_self, %i):
  │ %4   = py.constant.constant 2%4   = func.getfield(%body_self, 1)
  │ %5   = py.binop.mult(%x_1, %4)            │ %x_1 = func.getfield(%body_self, 0)
  │ %6   = py.binop.add(%5, %i)               │ %5   = py.binop.mult(%x_1, %4)
}                                             │ %6   = py.binop.add(%5, %i)
py.ilist.map(fn=%body, collection=%2)       }
                                            py.ilist.map(fn=%body, collection=%2)

Tested behavior:

  • ilist.ForEach/ilist.Map` , constants leave the body, captures grow.
  • Loop-variant ops stay put, only constants.
  • py.Constant appear before lambda.
  • Nested fixpoint, hoist through nested closures.
  • Newly folded constants also hoist.
  • Idempotent.

closes #698

@zhenrongliew zhenrongliew added the area: rewrite Area: rewrite rules, pattern matching, and optimizer passes. label Aug 14, 2026
@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

☂️ Code Coverage

current status: ✅

Overall Coverage

Statements Covered Coverage Threshold Status
11740 10581 90% 0% 🟢

New Files

File Coverage Status
src/kirin/dialects/ilist/rewrite/hoist_constant.py 91% 🟢
TOTAL 91% 🟢

Modified Files

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

updated for commit: 33a3166 by action🐍

@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://QuEraComputing.github.io/kirin/pr-preview/pr-704/

Built to branch gh-pages at 2026-08-17 13:22 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/kirin/dialects/ilist/rewrite/hoist_constant.py 91.48% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

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

Labels

area: rewrite Area: rewrite rules, pattern matching, and optimizer passes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite: hoist loop-invariant py.Constant out of closure bodies in ilist.foreach/ilist.map

1 participant