Skip to content

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

Merged
zhenrongliew merged 2 commits into
mainfrom
dl/hoist-constant
Sep 25, 2026
Merged

zhenrongliew merged 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
Preview removed because the pull request was closed.
2026-09-25 15:46 UTC

@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!

@neelay893 neelay893 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@zhenrongliew
zhenrongliew merged commit c70e2d3 into main Sep 25, 2026
11 checks passed
@zhenrongliew
zhenrongliew deleted the dl/hoist-constant branch September 25, 2026 15:45
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

2 participants