Repository navigation
A tail call into a tree runs the callee's one answering arm in place (ackermann 24.0 to 18.4 ms) - #45
Merged
Merged
Conversation
…in place bendlang#2177 answers a non-tail call into a tree (a def that calls itself twice on one path) at the call: the callee's returning arms run there and jump to the continuation. A tail call had no such copy, so a continuation that resumes the recursion (ackermann's ack_k1, which tail-calls ack) jumped to the callee, which tested its argument and returned: three taken branches per return where Koru takes two. Now a tail call into a tree def from another segment runs the callee's answering arms (the same ones bendlang#2177 copies) after its arguments are set, host only; an arm's return is the callee's own WL_RETN, on the same stack. Arms of more than 48 lines in all stay in the callee (perceus deriv's mul: 1,550 lines, its C +56% without the bound). ackermann (osprey) 94.9M to 72.5M cycles, 22.3 to 17.1 ms (Koru 17.3). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t has one With two or more answering arms the copy repeats a chain of tests that the callee runs again on every miss: fib (osprey) +8.5% cycles at every one of 16 code positions, parallel-ml fib +7%, respelled pascal +20% (+18% instructions). With one arm: ackermann -23.8% cycles, tak -11.5% (-15.6% bend-par), osprey pascal -7.8%, queens r3 -2.1%, no program over +0.4% cycles. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Dec 1, 2026
nicolas-abril
added a commit
that referenced
this pull request
Dec 1, 2026
…33, #34, #36, #39, #44, #61-#68): record their heads Each is a port onto main of integration's own branch, its content already here (integration keeps the original commits; the ports add type fixes, line wraps, moved pieces and #68's book_passes, applied in the previous commit). Groups: #61 (#9 #11 #15 #32 #41 #42), #62 (#26 #56), #63 (#48 #50 and compile-time), #64 (#19 #20 #45), #67 (#55 #58), #68 (#54); par-defer (#43) is now part of #34, lazy-startup's parts are in #16 and #34. This merge records their heads (-s ours: no content change). Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
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.
2026-11-30: several answering arms, where the jump they save is a call (
eccd4201, test7c8d55b5; merged into integration at 3aeb542;comp.ts+211 ttok, 120,491 to 120,702). Copying every arm lost for two reasons, found by splitting the copies by site and sweeping 16 code positions: (1) a copy inside a continuation that bendlang#2177 already copies into the callee saves no call (its jump is the callee's own loop back-edge) and only reshapes that loop (coins +25%, fib +4%); (2) in a continuation segment clang laid the copied arms out as the fall-through, so a miss took two taken branches before the callee ran the tests again (fib_acc_k1:cbnz+b). Hit rates (counting build): coins'ways_acc_k199%, queens'solve_k159%, fib'sfib_acc_k138%, respelled pascal'sbinom_acc_k148%. Rule: a callee with one answering arm keeps the old copy everywhere; with several, the arms are copied only outside the copied continuations, never into a continuation that a copied arm also jumps to (respelled pascal's, which loses with any copy, +7% to +21%, even a single merged test without the frame push), and with each test wrapped in__builtin_expect(test, 0)so a miss falls through to the jump. 12 of 244 programs emit different C; 16-position sweeps vs integration: coins -18.7%, parallel-ml fib -5.0% (10 threads -5.7% wall), runtime queens -3.7%, respelled queens r2 -2.9%, r1 -1.9%, fib 0.0%, symreg +1.5%, deriv +1.3%, tree-radix 0.0%, tree-bitonic -0.1%, set apart tmap +0.6%, tmap-std +0.7%; geomean over the 10 not set apart -3.2% cycles, -1.0% instructions. No program over +1.6%. Cluster gate 1979/1979 (on the merge), memory check 0 live words at 1 and 8 threads on the 12 tests whose C changes, Metal lane ok on the 6 GPU programs whose C changes. Wrong versions caught by the new test: tests inverted, a chain's last test dropped, the first two parameters swapped. Results in~/Software/bend-opt-results/cont-multi/.Merged into integration at 46e0418 (
opt/cont-self-returne72ea92, after merging integration 244e960; lane statuses equal to integration's plus the new test, 243 of 244 benchmark programs print expected at 1 thread and the 98 parallel ports at 10; kostya/json exceeds the 4 GB cap on integration too).Stacks on
opt/integration(418a706).comp.ts+331 ttok (101,434 to 101,765).What it does
bendlang#2177 answers a non-tail call into a tree (a def that calls itself twice on one path) at the call: the callee's returning arms run there and jump to the continuation. A tail call had no such copy, so a continuation that resumes the recursion jumped into the callee only for the callee to test its argument and return. In ackermann,
ack_k1resumes withack(mp, r), andacktestsm == 0and returnsn + 1: three taken branches per return where Koru takes two (OPTIMIZATIONS.md §44).Now a tail call into a tree def from another segment runs the callee's answering arm after its arguments are set, host only (
if (!DEVICE)); the arm's return is the callee's ownWL_RETN, on the same stack, so nothing about frames changes. The arm is the one bendlang#2177 already copies (seg_base), and it is copied only when the callee has exactly one answering arm of at most 48 lines.Why one arm
The first commit copied every answering arm. With two or more, a miss runs a chain of tests that the callee runs again:
fib(osprey,fib_acc_k1) +8.5% cycles, worse at every one of 16 code positions (1.018x to 1.166x); parallel-mlfib+7%; respelledpascal+20% cycles at +18% instructions. Those multi-arm copies did win elsewhere (coins-15.8%, runtimequeens-5.9%,symreg-3.2%), but I found no static rule that separates them from the losers, so they are left out (a follow-up). The 48-line bound keeps perceusderiv'smul(1,550 lines of arms, +56% C) out.Results
M4, one thread, GPU off; instructions one run, cycles median of 5; base
opt/integration418a706. 22 of the 244 bend-benchmarks programs emit different C:geomean (not set apart, 11 programs): instructions -1.77%, cycles -5.79%
Osprey board (koru-benchmarks' kernels, hyperfine, ms): ackermann 24.0 to 18.4 (Koru 18.3), tak 31.0 to 27.4 (Koru 29.4), pascal 95.1 to 88.1 (Koru 27.2), hanoi 22.8 to 23.4 (Koru 26.8). On a quiet machine earlier: ackermann 22.3 to 17.1 (Koru 17.3).
No program over +3% cycles; the largest are hanoi +2.8% at -4.6% instructions and set-apart tmap-std +0.6%.
Checks
opt/integration's (974 pass, 27 FAIL there too: the tests whose expected lines include stderr or exit codes, 123 that do not build on either); the new test passes at 1 and 8 threads.-DBEND_POISON, live words at 1 and 8 threads): the 74 tests whose C changes give the same output, exit code and live-word count as integration at 1 and 8 threads (two of them,reg/array_copy_pairandreg/array_keep_cow, leave 2 words on both); the new test 0.tests/run/call_returns_tail.bend: tail calls into trees from a continuation into its own def (ack, a tree fold), into another def (an arm that builds a node, a three-word result), from a def's entry, from a fork's join (a task at 8 threads), and the shapes that must stay (an arm that drops a parameter, a recursion down a list). Its lines are integration's; a counting build takes 9.37M tail-arm returns (ack_k1 9.36M, the fork join 1,087, a three-word result 299, ...), the same at 1 and 8 threads.--gpu onand--gpu off --threads 10, 3 runs):respelled/queens/r3and the 5 bang tests whose C changes print their expected lines. The copies areif (!DEVICE)blocks; r3's GPU wall 1.626 s to 1.672 s (+2.8%; the device-compiled text differs only by those dead blocks; measured on the previous integration base).tsc.🤖 Generated with Claude Code