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.constant10%x = py.constant.constant10%body = func.lambdabody(%x) { %hoisted = py.constant.constant2^1(%body_self, %i): %body = func.lambdabody(%x, %hoisted) {
│ %x_1 = func.getfield(%body_self, 0) ^1(%body_self, %i):
│ %4 = py.constant.constant2%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

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

github-actionsBot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

☂️ Code Coverage

current status: ✅

Overall Coverage

StatementsCoveredCoverageThresholdStatus
117401058190%0%🟢

New Files

FileCoverageStatus
src/kirin/dialects/ilist/rewrite/hoist_constant.py91%🟢
TOTAL91%🟢

Modified Files

FileCoverageStatus
src/kirin/dialects/ilist/rewrite/_init_.py100%🟢
TOTAL100%🟢

updated for commit: 33a3166 by action🐍

@github-actions

github-actionsBot 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

codecovBot 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 linesPatch %Lines
src/kirin/dialects/ilist/rewrite/hoist_constant.py91.48%4 Missing ⚠️

📢 Thoughts on this report? Let us know!

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

Labels

area: rewriteArea: 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

@zhenrongliew