-
Notifications
You must be signed in to change notification settings - Fork 3
Simple O(1) inlining rewrite #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Roger-luo
merged 3 commits into
main
from
rafaelha/simple-inlining-with-linear-time-complexity
Oct 29, 2025
Merged
Simple O(1) inlining rewrite #552
Roger-luo
merged 3 commits into
main
from
rafaelha/simple-inlining-with-linear-time-complexity
Oct 29, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
|
Contributor
|
Looks like you need to check for None or cast to satisfy pyright (src/kirin/rewrite/inline.py:198-199). |
cduck
reviewed
Oct 28, 2025
Co-authored-by: Casey Duckering <cduck@users.noreply.github.com>
Roger-luo
approved these changes
Oct 29, 2025
Roger-luo
pushed a commit
that referenced
this pull request
Oct 29, 2025
The inline pass (as part of a `Walk` rewrite) has O(n^2) time complexity
which is a performance issue.
An extra factor of `n` is coming from `inline.py` where each block is
split in two, and the inline region is inserted in the middle. Splitting
blocks in two comes with the extra O(n) factor:
```python
after_block = ir.Block()
stmt = call_like.next_stmt
while stmt is not None:
stmt.detach()
after_block.stmts.append(stmt)
stmt = call_like.next_stmt
```
This PR introduces a partial workaround. Simple regions with just a
single block are inlined by inserting all of their statements directly.
Since statements form a linked list, this is O(1).
For my test case, I observe that this fix reduces runtime and brings the
time complexity of the inline pass back to O(n).
<img width="489" height="358" alt="image"
src="https://github.com/user-attachments/assets/3b11560f-670f-45d3-84b0-0959693f47b4"
/>
However, we should refactor the inline pass to scale linearly even in
the general case.
---------
Co-authored-by: Casey Duckering <cduck@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The inline pass (as part of a
Walkrewrite) has O(n^2) time complexity which is a performance issue.An extra factor of
nis coming frominline.pywhere each block is split in two, and the inline region is inserted in the middle. Splitting blocks in two comes with the extra O(n) factor:This PR introduces a partial workaround. Simple regions with just a single block are inlined by inserting all of their statements directly. Since statements form a linked list, this is O(1).
For my test case, I observe that this fix reduces runtime and brings the time complexity of the inline pass back to O(n).
However, we should refactor the inline pass to scale linearly even in the general case.