Commit 41c5f09
committed
feat(tco): self-recursive tail-call marker + LLVM
Adds a new `tco` pass that walks every function after the
optimisation fixed-point and flips `is_tail = true` on direct
self-recursive `Call`s whose result flows into the block's `Return`
terminator with no intervening work. The LLVM backend then emits
the `tail` flag on the corresponding `CallInst`, letting LLVM's
sibling-call optimisation collapse the recursion when the ABI and
stack layout allow it.
Scope is restricted to self-recursive direct calls. A broader
"every tail-position Call" scope was tried first and regressed the
fib bench ~45 % exec: the single `main → fib(40)` call was the only
mark in that program, and LLVM's IPA inliner reacted to the marker
in a way that slowed fib's hot loop body (the marker perturbed
inlining decisions for reasons unrelated to TCO). Self-recursion
also matches the case where a future HIR-level loop-rewrite pass
could fully eliminate the call frame, so the narrow scope is
where the next payoff naturally lands.
Cranelift's `return_call` lowering is half-wired (it'd
double-terminate the block alongside the HIR Return terminator) and
was unreachable before this commit. The three Cranelift call arms
that read `is_tail` now ignore it and fall back to standard `call`
until the terminator-skip plumbing lands separately. The HIR mark
is still useful — LLVM honours it today.
No bench delta on the current kernels (none use tail recursion); 7
new unit tests cover the marker's shape recognition.tail hint1 parent 5a6a6f3 commit 41c5f09
4 files changed
Lines changed: 377 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2126 | 2126 | | |
2127 | 2127 | | |
2128 | 2128 | | |
2129 | | - | |
| 2129 | + | |
2130 | 2130 | | |
2131 | 2131 | | |
2132 | 2132 | | |
| |||
6200 | 6200 | | |
6201 | 6201 | | |
6202 | 6202 | | |
6203 | | - | |
| 6203 | + | |
6204 | 6204 | | |
6205 | 6205 | | |
6206 | 6206 | | |
| |||
6212 | 6212 | | |
6213 | 6213 | | |
6214 | 6214 | | |
6215 | | - | |
6216 | | - | |
6217 | | - | |
6218 | | - | |
6219 | | - | |
| 6215 | + | |
| 6216 | + | |
| 6217 | + | |
| 6218 | + | |
| 6219 | + | |
| 6220 | + | |
| 6221 | + | |
| 6222 | + | |
| 6223 | + | |
| 6224 | + | |
| 6225 | + | |
6220 | 6226 | | |
6221 | 6227 | | |
6222 | 6228 | | |
| |||
6257 | 6263 | | |
6258 | 6264 | | |
6259 | 6265 | | |
6260 | | - | |
6261 | | - | |
6262 | | - | |
6263 | | - | |
6264 | | - | |
6265 | | - | |
6266 | | - | |
| 6266 | + | |
| 6267 | + | |
| 6268 | + | |
| 6269 | + | |
| 6270 | + | |
6267 | 6271 | | |
6268 | 6272 | | |
6269 | 6273 | | |
| |||
7068 | 7072 | | |
7069 | 7073 | | |
7070 | 7074 | | |
7071 | | - | |
7072 | | - | |
7073 | | - | |
7074 | | - | |
7075 | | - | |
| 7075 | + | |
| 7076 | + | |
| 7077 | + | |
| 7078 | + | |
7076 | 7079 | | |
7077 | 7080 | | |
7078 | 7081 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
1681 | 1682 | | |
1682 | 1683 | | |
1683 | 1684 | | |
| 1685 | + | |
1684 | 1686 | | |
1685 | 1687 | | |
1686 | 1688 | | |
| |||
1886 | 1888 | | |
1887 | 1889 | | |
1888 | 1890 | | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
1889 | 1908 | | |
1890 | 1909 | | |
1891 | 1910 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1270 | 1270 | | |
1271 | 1271 | | |
1272 | 1272 | | |
1273 | | - | |
| 1273 | + | |
1274 | 1274 | | |
1275 | | - | |
| 1275 | + | |
1276 | 1276 | | |
1277 | 1277 | | |
1278 | 1278 | | |
| |||
2991 | 2991 | | |
2992 | 2992 | | |
2993 | 2993 | | |
| 2994 | + | |
2994 | 2995 | | |
2995 | 2996 | | |
2996 | 2997 | | |
| |||
3017 | 3018 | | |
3018 | 3019 | | |
3019 | 3020 | | |
| 3021 | + | |
| 3022 | + | |
| 3023 | + | |
| 3024 | + | |
| 3025 | + | |
| 3026 | + | |
| 3027 | + | |
| 3028 | + | |
| 3029 | + | |
| 3030 | + | |
| 3031 | + | |
3020 | 3032 | | |
3021 | 3033 | | |
3022 | 3034 | | |
| |||
3122 | 3134 | | |
3123 | 3135 | | |
3124 | 3136 | | |
| 3137 | + | |
| 3138 | + | |
| 3139 | + | |
3125 | 3140 | | |
3126 | 3141 | | |
3127 | 3142 | | |
| |||
0 commit comments