Skip to content

feat(ir): add on-disk compile cache for kernels and implement caching… - #769

Merged
zhenrongliew merged 6 commits into
mainfrom
dl/kernel-compile-cache
Oct 8, 2026
Merged

zhenrongliew merged 6 commits into
mainfrom
dl/kernel-compile-cache

Conversation

@zhenrongliew

Copy link
Copy Markdown
Collaborator

Add an opt-in on-disk cache for compiled kernels

Kernel decorators compile when a module is imported: they lower the function and
run the dialect group's passes, again in every new process, even when nothing
changed. With KIRIN_COMPILE_CACHE_DIR set, each compiled kernel is now saved,
and a later process loads it instead of running the passes, as long as nothing it
depends on has changed. Without the variable, behaviour is unchanged.

Key. A SHA-256 over the lowered IR (which holds the values of the globals the
function reads), the keys of the kernels it calls, the dialect names and
run_pass source, the decorator options, the function's file and line, and the
Python and package versions. Any change gives a new key, so a stale entry is
never found.

Storage. The compiled code is pickled. The kernel itself, its callees and its
dialect group are saved as references and relinked to the live objects on load;
singleton types are rebuilt as the same objects. Files are written atomically.

Limitations

  • Loading a pickle can run code: only point the cache at a directory you trust.
  • Editing an editable install doesn't change its version: clear the cache after
    changing library code.
  • Loaded kernels hold copies of the constants they captured.

Testing

  • test/ir/test_compile_cache.py: four runs in fresh processes (empty cache,
    nothing changed, callee changed, global changed) check which kernels recompile
    and their results.
  • Full suite passes with the cache off, cold and warm.
  • 40 kernels, each calling the previous one: import takes 0.28 s without the
    cache and 0.03 s with a warm cache.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

☂️ Code Coverage

current status: ✅

Overall Coverage

Statements Covered Coverage Threshold Status
12307 11133 90% 0% 🟢

New Files

File Coverage Status
src/kirin/ir/compile_cache.py 94% 🟢
TOTAL 94% 🟢

Modified Files

File Coverage Status
src/kirin/ir/group.py 92% 🟢
TOTAL 92% 🟢

updated for commit: 34d65a0 by action🐍

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-08 01:59 UTC

@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.58333% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/kirin/ir/compile_cache.py 94.24% 13 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread src/kirin/ir/group.py
Comment thread src/kirin/ir/group.py
Comment on lines +158 to +163
if code.source is not None and code.source.lineno_begin != mt.lineno_begin:
file, saved = code.source.file, code.source.lineno_begin
for stmt in code.walk():
source = stmt.source
if source and source.file == file and source.lineno_begin == saved:
source.lineno_begin = mt.lineno_begin

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this work now, but I think we should really redesign the ir.Method and how SourceInfo is. this seems like crazy we need to re-walk the whole IR just to update the lineno offset @Roger-luo @zhenrongliew.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ideally source info is stored in an areana and we only point certain IR node to the arena ID, so you can shift the lineno by iterating a piece of continuous memory without walking the IR. But this is not super beneficial in Python's object model because you don't really have a continuous memory anyways.

@Roger-luo

Copy link
Copy Markdown
Collaborator

can we improve the coverage a bit to get this PR in? @copilot

Co-authored-by: Roger-luo <8445510+Roger-luo@users.noreply.github.com>

Copilot AI commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

can we improve the coverage a bit to get this PR in? @copilot

Added direct tests for cache directory selection and fingerprinting; compile_cache.py coverage is now 50% in the targeted run. Commit: __SHORT_HASH__.

@zhenrongliew
zhenrongliew merged commit a6481d6 into main Oct 8, 2026
11 checks passed
@zhenrongliew
zhenrongliew deleted the dl/kernel-compile-cache branch October 8, 2026 01:58
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Backport results for a6481d6

Succeeded:

zhenrongliew pushed a commit that referenced this pull request Oct 8, 2026
Automated backport of PR #769 (a6481d6)
to `release-0-22`.

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants