Skip to content

Conversation

@Roger-luo
Copy link
Collaborator

No description provided.

rafaelha and others added 2 commits October 28, 2025 11:03
This PR fixes several issues related to fixpoint rewrites:

1. A subtle bug in `Pass.fixpoint` lead to rewrites being applied for
`max_iter` iterations, even if they had already converged.
2. Several compiler passes were incorrectly returning
`has_done_something=True`, even if they had not done anything, leading
to maxed out fixpoint loops.
3. The CSE rewrite would prematurely exit, leading to more fixpoint
iterations.

I checked that all tests in the kirin test suite lead to fixpoint
rewrites that converge before reaching `max_iter`. It's still possible
that there are more rewrite passes that need to be fixed. We should
consider raising an error or at least a warning if a fixpoint pass does
not converge, as this will immediately point us to any such issues in
the future.

**The default `max_iter` is set to 32. With this fix, many passes now
iterate only once or twice instead of 32 times. So in many cases there
is a 32x speedup. Some passes also contain a fixpoint within a fixpoint.
They can now become 1000x faster.**

I noticed these issues when running the following script
```python
    @qasm2.extended  # O(n) scaling
    def main_fun():
        qreg = qasm2.qreg(1)
        for _ in range(n):
            qasm2.h(qreg[0])

    QASM2().emit(main_fun)  # ~O(n^1.5)
```

This script runs really slow for large `n`. But even more important,
recording the duration of compilation and QASM2 emission, I found
non-linear scaling ~ O(n^1.5). Any compiler pass must be linear in the
program size (or at worst n log n).

<img width="331" height="250" alt="image"
src="https://github.com/user-attachments/assets/c914db82-e2c6-4821-8a0e-b00ab19b432c"
/>

With the fixes in this PR, the execution time of this script is
significantly faster and the computational complexity is back to the
expected O(n):
<img width="331" height="245" alt="image"
src="https://github.com/user-attachments/assets/35480c95-29b5-4117-b796-c4cc891d1cb0"
/>

These issues also affect the runtime of @cduck's flair-to-squin pipeline
- but I think there are additional slowdowns that I'm still looking
into.
@Roger-luo Roger-luo changed the base branch from main to release-0-17 October 28, 2025 15:08
@Roger-luo Roger-luo merged commit 728defc into release-0-17 Oct 28, 2025
3 checks passed
@Roger-luo Roger-luo deleted the roger/fixing-backport branch October 28, 2025 15:08
@github-actions
Copy link
Contributor

PR Preview Action v1.6.2
Preview removed because the pull request was closed.
2025-10-28 15:09 UTC

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants