Skip to content

perf(comp): call returns: a call its callee answers at once runs the continuation in place; fork-free segments drop the seq test; answering arms copied where the jump they save is a call - #64

Draft
nicolas-abril wants to merge 14 commits into
mainfrom
opt/call-returns

Conversation

@nicolas-abril

Copy link
Copy Markdown
Owner

On main (6180be5b)

This PR is a port onto main, on main: 14 commits of its own, with no integration history (head 9ba2365a).
It replaces #19, #20, #45.

Port notes

Checks

  • Cluster gate (every test, every lane): 1877 / 1878 pass. The one failure is io_tcp_text_residue on the interpreter lane, a TCP test that is flaky on the cluster; it passes locally, on this branch and on main.
  • Locally: no new tsc errors against main, no new comp.ts lines over 80, its tests pass on the C and JS lanes (plus 40 lane runs of existing tests; run/array_slab fails on main too).
  • comp.ts: +2598 tokens (ttok -m gpt-3.5-turbo) over main, which is at 65,989 of the 66,000 cap.

Performance was measured on integration when this change merged there (see the original description or the replaced PRs); it has not been re-measured on main.

What it does

The change of the PRs it replaces, ported together:

Their descriptions hold the design and the measurements.

Commits

  • 12f2249f perf(comp): a fork-free segment takes its sequential path on the host
  • be11d3aa perf(comp): a call runs its callee's returning arms at the call
  • ad4404b0 perf(comp): a call runs its callee's returning arms under nested tests too
  • 213eaf17 perf(comp): an answered call neither stores nor pops its frame's id
  • 7ccc035a comp: a call its callee answers at once runs the continuation in place
  • 0aab6040 comp: early continuations leave the push's task path where it was
  • 0e800c95 comp: an early continuation calls no spin and no drop
  • 2667e12e perf(comp): an early continuation takes the callee's answer in its own words
  • 74fce366 perf(comp): on the device a call's frame is pushed at the call, before its arguments
  • 07cd5f4c perf(comp): a tail call into a tree runs the callee's answering arms in place
  • 3b81dc7f perf(comp): a tail call copies the callee's answering arm only when it has one
  • bab4d5af perf(comp): a tail call copies a callee's several answering arms where the jump they save is a call
  • 7ebae258 test: tail calls into defs with several answering arms
  • 9ba2365a fix(port): segs_base's regex matches are non-null where used; lines under 80

🤖 Generated with Claude Code

nicolas-abril and others added 14 commits December 1, 2026 21:58
A segment that reaches no fork tested seq before each frame it pushed and
before each call, and carried the task path it never takes: FID_ENTER and
FID_EXIT set seq for a fork-free task, and a forking segment hands a
fork-free callee to a task of its own. WL_SEQ(F) is seq, or on the host
true for a fork-free segment, so clang drops the test, the task path and
the frame the path's calls forced on every entry.

The one such segment that ran with seq clear is a closure body entered
through a Clo~apply that may fork; it now keeps its frames on the stack
above the task's frame, as a fork-free task does.

symreg -15.8% instructions, queens -6.3%, lexer -2.8% (one thread, M4).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit 7c5c8c4)
A non-tail call pushes a frame and jumps to its callee; when the callee's
match takes an arm that returns at once, the callee pops the frame's id
and jumps back through the segment table. In a tree recursion that is at
least half of the calls. The call now carries the callee's returning
arms: after the frame is pushed and the arguments are in the registers,
an arm that neither calls nor jumps runs in place and jumps straight to
the continuation the frame names. Any other arm falls through to the
call as before.

segs_base copies the arms from the callee's emitted entry segment, so
they run the code the callee would have run on the same registers. Only
for a callee that calls itself twice on one path, on the host, where the
frame is on the stack.

On top of the fork-free sequential path, one thread, M4: symreg -4.5%
instructions and 12.43G to 8.18G cycles (medians of a 16-position sweep;
its C twin runs 9.01G); fib(42) 2.74G to 2.03G cycles, tak 9.75G to 6.11G.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit 3394908)
…s too

seg_base walks the callee's entry as blocks: a test below the entry chain is
copied with its returning arms when the lines before it only name words (the
callee runs them again when no arm answers). An arm that drops a subterm
stays in the callee: the call to term_drop gave the segment it was copied to
a C frame on every entry.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit 9b1dc6e)
Where a call gets its callee's returning arms, the store of the
continuation's id moves below them: an arm that answers jumps to the
continuation with the frame as a return would leave it. Not in the callee's
own entry, where the call closes a loop that clang then vectorises (tak ran
5.7% more cycles), and a call that gets no arms is emitted as before.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit 74a1c35)
On bendlang#2177's call-site arms: when the continuation pops exactly the frame of
the call, pushes none and is at most 48 lines, the copied arms run inside
a do/while(0) block, an answering arm leaves it with a flag set, and a
copy of the continuation runs there with its frame reads taken from the
caller's words. The frame push moves below the arms, so it runs only when
no arm answers. Before, an answered call stored its frame, jumped to the
continuation and read it back.

The continuation pushing no frame is from symreg: copying eval's k1,
which pushes the second call's frame, into eval cost 5% instructions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 1dea074)
A call into another def runs its own task path, when not sequential,
between its frame push and the arms, and that task reads the WL_CONT the
push's task branch sets. Moving the whole push block below the arms gave
the call the previous continuation at 8 threads (fork_ctor_build and
mach_scan_sibling printed wrong numbers and leaked). Only the sequential
push moves now. The test adds calls under parallel lets, which print
2586831047 for 772016070 on the previous commit at 8 threads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 1dca6d1)
As for bendlang#2177's arms: a continuation copied to the call site that calls a
spin gives the spin a second call site, and clang stops inlining it.
cfold's cfold_k3 put spin_cfold_add and spin_cfold_mul out of line (+5%
instructions); tak and deriv lose their small gains with the rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c9575eb)
…n words

The arms copied above a call wrote their answer to r0.., which the path
where no arm answers still jumps with: clang merged the two into a select
before every miss (pascal's binom_k1: three more instructions per call
that the base case does not answer). The answer now goes to wb_r words.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 7beca70)
…e its arguments

segs_base moves a call's frame push (or its id store) below the callee's
arguments, so that the host can run the callee's answering arms first. Those
arms are host-only (!DEVICE), but the move applied in device segments too,
where it only kept the frame's words and the arguments live at once: GPU time
+19% on respelled queens r3, +3% on r2. In a segment the device runs, the push
stays at the call under #if DEVICE and the moved one is #if !DEVICE, so the
host's text is unchanged (byte-identical after unifdef DEVICE=0 on 1,032 tests
and 244 benchmark programs).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4a9436e)
…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]>
(cherry picked from commit ad85d18)
…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]>
(cherry picked from commit b86f24f)
…e the jump they save is a call

The tail-arm copy took only callees with one answering arm: copying every arm
lost on fib (+5.5% cycles), parallel-ml fib (+14%) and respelled pascal (+14%).
Split by site, the losses have two causes. A copy inside a continuation that
is itself copied 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%. A copy in a
continuation segment lays the arms out as the fall-through, so a miss takes
two branches before the callee runs the tests again; with the tests marked
unlikely a miss falls through to the jump. Respelled pascal's continuation is
also entered by a direct jump from the callee's copied arm, where the copy
loses at any shape (+7% to +21%), so such continuations keep their jump.

Now several arms are copied only outside the copied continuations, into a
continuation no copied arm jumps to, with __builtin_expect(test, 0). Sweeps
of 16 code positions against opt/integration: coins -19.1%, parallel-ml fib
-5.2%, runtime queens -3.7%, fib +1.2%, symreg +1.4%, respelled pascal
unchanged C.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit eccd420)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 7c8d55b)
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