Commit 10b4f47
authored
Rafaelha/fix
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.has_done_something and fixpoint logic (#545)1 parent 537525f commit 10b4f47
File tree
12 files changed
+55
-36
lines changed- src/kirin
- dialects/ilist/rewrite
- passes
- rewrite
- test
- analysis/dataflow/typeinfer
- dialects
- testing
12 files changed
+55
-36
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
| |||
53 | 57 | | |
54 | 58 | | |
55 | 59 | | |
56 | | - | |
57 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
58 | 69 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
| 36 | + | |
36 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
37 | 42 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
25 | | - | |
26 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
27 | 31 | | |
28 | 32 | | |
29 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | | - | |
| 88 | + | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
This file was deleted.
0 commit comments