Skip to content

A tail call into a tree runs the callee's one answering arm in place (ackermann 24.0 to 18.4 ms) - #45

Merged
nicolas-abril merged 5 commits into
opt/integrationfrom
opt/cont-self-return
Nov 27, 2026
Merged

nicolas-abril merged 5 commits into
opt/integrationfrom
opt/cont-self-return

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

2026-11-30: several answering arms, where the jump they save is a call (eccd4201, test 7c8d55b5; 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_k1 99%, queens' solve_k1 59%, fib's fib_acc_k1 38%, respelled pascal's binom_acc_k1 48%. 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-return e72ea92, 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_k1 resumes with ack(mp, r), and ack tests m == 0 and returns n + 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 own WL_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.

  WL_CASE(FID_ack_k1)
    WL_POPN(1);
    Term _mp_1 = STK(0);
    Term _r_0 = r0;
    ...
    r0 = _mp_1;
    r1 = _r_0;
    if (!DEVICE) {          // ack's own arm
      Term _m_0 = r0;
      Term _n_0 = r1;
      if (_m_0 == 0) {
        r0 = nat_chk(e, _n_0 + 1ull);
        WL_RETN(1);
      }
    }
    WL_JMP(FID_ack);

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-ml fib +7%; respelled pascal +20% cycles at +18% instructions. Those multi-arm copies did win elsewhere (coins -15.8%, runtime queens -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 perceus deriv's mul (1,550 lines of arms, +56% C) out.

Results

M4, one thread, GPU off; instructions one run, cycles median of 5; base opt/integration 418a706. 22 of the 244 bend-benchmarks programs emit different C:

program instructions base csr Δ cycles base csr Δ ok
awfy/cd/main.bend (apart) 576.5M 576.5M +0.01% 96.6M 97.0M +0.44% ok
clbg/binary-trees/main.bend (apart) 862.8M 932.9M +8.13% 175.8M 170.5M -3.05% ok
closures/delaunay-animation/main.bend 20073.5M 20079.7M +0.03% 4899.9M 4895.8M -0.08% ok
koka-extra/rbtree-clrs/main.bend (apart) 137.2M 137.6M +0.29% 67.0M 66.5M -0.78% ok
koka-extra/rbtree-del/main.bend 10149.9M 10152.1M +0.02% 2432.3M 2436.1M +0.16% ok
koka-extra/rbtree-fip/main.bend (apart) 113.5M 113.8M +0.31% 51.5M 51.4M -0.24% ok
koka-extra/tmap-std/main.bend (apart) 38.6M 39.1M +1.08% 9.6M 9.6M +0.64% ok
koka-extra/tmap/main.bend (apart) 61.2M 61.2M -0.04% 14.2M 14.3M +0.40% ok
osprey/ackermann/bend/ackermann.bend 481.8M 414.8M -13.90% 95.5M 73.0M -23.56% ok
osprey/binarytrees/bend-par/binarytrees.bend (apart) 13.5M 13.5M +0.39% 4.5M 4.5M +0.15% ok
osprey/binarytrees/bend/binarytrees.bend (apart) 13.5M 14.0M +3.62% 5.1M 4.9M -3.13% ok
osprey/hanoi/bend/hanoi.bend 532.6M 508.3M -4.56% 91.8M 94.3M +2.80% ok
osprey/pascal/bend/pascal.bend 1723.8M 1986.4M +15.24% 378.9M 350.0M -7.63% ok
osprey/tak/bend-par/tak.bend 693.0M 627.2M -9.50% 124.1M 103.8M -16.39% ok
osprey/tak/bend/tak.bend 569.2M 550.5M -3.27% 124.7M 108.9M -12.65% ok
parallel-ml/delaunay-animation/main.bend 20064.4M 20064.6M +0.00% 4886.1M 4881.1M -0.10% ok
parallel-ml/ocaml-binarytrees5/main.bend (apart) 2051.2M 2233.9M +8.91% 409.2M 395.6M -3.33% ok
perceus/rbtree-ck-unsafe/main.bend (apart) 21221.2M 21212.5M -0.04% 5192.6M 5162.7M -0.58% ok
perceus/rbtree-ck/main.bend (apart) 22635.7M 22632.5M -0.01% 5191.1M 5199.7M +0.17% ok
perceus/rbtree-unsafe/main.bend 7467.8M 7469.3M +0.02% 1941.7M 1945.6M +0.20% ok
perceus/rbtree/main.bend 7840.2M 7842.7M +0.03% 1928.0M 1925.4M -0.14% ok
respelled/queens/r3.bend 41778.8M 41359.7M -1.00% 16329.2M 15970.3M -2.20% ok

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

  • Native lane (1,124 tests): every status equal to 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.
  • Memory (-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_pair and reg/array_keep_cow, leave 2 words on both); the new test 0.
  • New test 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.
  • Wrong versions (run on the first commit): arms placed before the call's arguments are set, an arm that jumps into the callee with its answer, the callee's parameter reads left out, the arm's test dropped: all four caught (wrong lines, traps or a C build failure).
  • GPU (Metal, --gpu on and --gpu off --threads 10, 3 runs): respelled/queens/r3 and the 5 bang tests whose C changes print their expected lines. The copies are if (!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).
  • Not run: cluster gates, CUDA, Linux/x86, tsc.

🤖 Generated with Claude Code

nicolas-abril and others added 5 commits November 24, 2026 23:58
…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]>
@nicolas-abril
nicolas-abril merged commit 46e0418 into opt/integration Nov 27, 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]>
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.

1 participant