Repository navigation
A call into a tree recursion runs the callee's returning arms itself and jumps straight to its continuation - #2177
Draft
nicolas-abril wants to merge 4 commits into
Draft
nicolas-abril wants to merge 4 commits into
nicolas-abril wants to merge 4 commits into
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
This was referenced Nov 16, 2026
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 20, 2026
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 <noreply@anthropic.com>
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.
comp.tsis 67,191 ttok, 1,191 over the 66,000 cap (main is 65,966; #2176, which this stacks on, is 66,000; the first commit here 66,774, arms under nested tests +205, the frame id's move +212), sorepo-gatefails until that is settled. Two commits were added on 2026-11-16; what they change and cost is in its own section below, and the sections before it describe the first commit as it was measured. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. Stacks on #2176: the first commit here is that PR.What it does
A non-tail call pushes a frame that ends in the id of its continuation and jumps to its callee. When the callee is at a leaf of the recursion, it tests its argument, computes a word, pops the id and jumps back through the segment table. In a tree recursion that is at least half of all calls:
evalinsymregwalks 63 nodes a tree and 32 of them areVarorLit.The call now runs the callee's returning arms itself. After the frame is pushed and the arguments are in the registers, the caller tests them as the callee would; an arm that returns at once runs in place and jumps straight to the continuation the frame names (
WL_POPN(1); WL_JMP(FID_k), where the callee hadWL_RETN). Any other arm falls through to the call, unchanged.emit_forkleaves a//base callee continuationline at the call; once every unit is final,segs_basereplaces it with the callee's parameter reads and the leading chain of its entry segment. The code that runs is the code the callee would have run, on the same registers, so ownership, layouts and frees are the callee's own.WL_RETNand has no other jump, push, loop exit or return. An arm that calls a spin is left out (clang inlines a spin while it has one call site: with such arms copied,kmeansran 1.9% andraytrace3.1% more instructions). An arm of 8 lines or more is left out. The other arms keep their tests with empty bodies, so the chain reads as in the callee.term_calls), the shape whose calls mostly end at once. Without this rule a recursion down a list got the test too, once per element for one hit at the end, andlexerlost 3.1% of its cycles (8.09G to 8.34G, medians of two 16-position sweeps that do not overlap) for no instructions.WL_SEQ, or always after a fork's sequential path). The device compiles the block away.Results
One M4, GPU off, one thread unless said. Instructions retired and cycles from
/usr/bin/time -l, builds run in turn, minimum of 3 to 5 runs. "#2176" is the commit this stacks on.symreginstructionssymregcycles (one placement)symregwall, 8 threadssymregcycles on an E-coresymreggoes from 1.37x behind its twin to ahead of it on a P-core (7.8G to 8.4G cycles across code positions, below, against the twin's 9.01G, which was not swept); on an E-core it is still 1.19x behind.Placement: the gain is mostly cycles, so the three builds were swept over 16 code positions (nops at the head of
work_loop, 4 bytes a step), 3 interleaved rounds, the minimum per position:The ranges do not overlap: the slowest position of this build is 3.8G cycles under the fastest of main. Position by position the ratio runs from 1.47x to 1.67x, median 1.55x. Placement alone moves this build by about 8%.
Counters (Instruments' CPU Counters, whole run):
ret)Half of the returns are gone, which is the 32 leaves in 63.
symregwas never short of prediction (0.02 mispredictions a node) or of cache (no misses): over twelve variants of the bench its cycles follow taken branches (R² 0.95) and not instructions (R² 0.23).Three small recursive kernels (scaled copies of the
fib,hanoiandtakports in koru-benchmarks' osprey-compute-kernels), instructions / cycles at one placement:fib(42)hanoi(29)tak(38, 19, 9)The rest of the suite, against #2176:
merkle-0.03% instructions,tree-bitonic-0.01%,tree-matmul-0.17%,tree-radix+0.04%, cycles within the noise; the other 12 emit the same C.queensis not reached:solve's returning arms are nested under an inner test, not arms of its entry chain.Elsewhere: 52 tests emit different C from #2176.
demos/pure_hvm5_mini's C grows 3.2% (72 arms at 42 calls),demos/proof_typed_eval's 7.4% (20 arms at 13 calls),pure_par_sort's 1.3% andpure_par_sum's 0.4%;app_slash_boss_3dand the other 10 demos have no such call. Emitting a demo's C took the same time in single runs (0.1 s to 1.4 s each).Added 2026-11-16: arms under nested tests, and the frame's id
Two commits on top of the one described above (
9b1dc6ec,74a1c357). Everything in this section is measured against that first commit ("#2177 as it stood",3394908c), on the same M4, GPU off, one thread.What changed
seg_basenow walks the callee's entry as nested blocks instead of its leading chain only: a test below the entry chain is copied with its returning arms. The lines between two tests run a second time in the callee when no arm answers, so a nested test is copied only when those lines just name words (a declaration whose value calls nothing butterm_loc,term_aux,term_peekorU32_BIN); a line that takes a node apart (ctr_take), frees, allocates or calls a spin keeps everything below it in the callee.term_sink). The drop is a call toterm_dropout of line, and it gave the segment the arm was copied to a C frame on every entry (warp_k1intree-bitonicgainedstp x29, x30): with such arms copied,tree-bitonicran 10.2% andtree-radix3.7% more instructions. This also takes such arms away from the entry chain, where the first commit copied them; no bench or kernel had one.WL_JMP(k)where the first commit hadWL_POPN(1); WL_JMP(k)). Not in the callee's own entry segment: there the call closes a loop, and with the store after the test clang vectorises the loop's frame pushes (st4.2dintak, which then ran 5.7% more cycles at every placement for 4.8% fewer instructions). A call that gets no arms is emitted exactly as before.Results against #2177 as it stood
Instructions retired, and cycles over 16 code positions (3 interleaved rounds, the least per position; least / median / most, in G). Another agent was benchmarking on the machine during these sweeps.
tree-matmulqueenstree-radixtree-bitonicsymregmerkleCounters over the whole run (one run each):
tree-matmul4.72G to 4.13G taken branches and 1.889G to 0.247G indirect ones (87% of its returns through the table are gone);queens6.54G to 4.52G taken and 0.857G to 0.551G indirect. Mispredictions barely move (19.7M to 16.9M, 574M to 559M).symregdiffers in 9 of its 46 segments (the continuations ofevalandesize, andfloop): only the frame id's store moved;eval's entry is textually the same.The koru kernels, scaled as above (
pascal30/15,coins1200,binarytrees8000 trees,ackermann3/12,exprtree600 trees):tak(38, 19, 9)fib(42)hanoi(29)pascalcoinsbinarytreesackermannexprtreepascalis the cost:binom's frequent returning arm (n == 0, 42% of its calls) calls two spins and stays in the callee, so the copied tests answer only the rare one (k == 0, 8% of calls) and the rest run them twice. It runs 14.5% more instructions for 3% to 7% fewer cycles.Per widening
9b1dc6ec)queens-15.5%,tree-matmul-19.1%,tree-bitonic-0.1%,tree-radix+1.0%;coins-21.5%,pascal+12.3%tree-matmul+13.9%,queens+4.3%74a1c357)tree-matmul-19.1% to -25.4%,tree-radix+1.0% to -0.3%,tak-9.5%,coins-21.5% to -24.2%,fib-2.0%,binarytrees-1.0%symreg-2.8% (cycles 0.915x to 1.016x, overlapping)tak+5.7% cycles at every position: clang vectorises the pushespascal-8.3% where it is now +14.5%,exprtree-6.9%,tree-bitonic-2.2%raytrace+3.1%,kmeans+2.0%, as first measuredkmeans-0.4% (szip), nothing elsetree-bitonic+10.2%,tree-radix+3.7%lexer's 591M non-tail calls 11% end at once (gen3%,ident22%,num29%);digitsum18%,collatz1%lexerloss standstree-bitonic3.9%,queens0.8%)How the calls of each bench end was counted with a profiling build (every non-tail call tagged with its site, every return with its arm). Of the non-tail calls, those ending at once in an arm under a nested test:
tree-matmul86% of 1.91G,queens36% of 857M,tree-radix40% of 238M,coins50%; in an arm that calls a spin:tree-bitonic45% of 2.34G,pascal42%,raytrace50% andmandelbrot49% (where the arm is the whole leaf's work).With the current #2175 (
opt/nat-accumulateat2707077e, this branch patched on):fib,pascalandbinarytreesare as with #2177 as it stood (same instructions for the first two, -0.4% for the third, cycles in the same ranges).coinsis slightly worse: 11.62G to 12.00G instructions and 2.33 / 2.42 / 2.65G to 2.50 / 2.59 / 2.87G cycles (overlapping). The additions pay onqueensandtree-matmul, which #2175 does not touch.The two known problems
coins(A call into a tree recursion runs the callee's returning arms itself and jumps straight to its continuation #2177 alone 9% slower than main at 13.7% fewer instructions): two things, both seen in the counters. The first commit's block atways' own first call answers almost never (78 of 14.9M calls: that call walksamountdown like a list), and with it clang laysways' entry out so that every entry takes one more branch: 2.00G to 2.80G taken branches for the same 3.60G branches and 0.80G indirect ones. Under that, the kernel is bound by a store and a reload: each level pushes the first call's result and pops it to add, and the cycles stay between 3.9G and 4.4G from 17.2G down to 11.2G instructions. With the continuation's add run at the answering site by hand, the reload goes and the cycles fall from 4.38G to 2.87G. That is what A def whose tail adds calls to itself carries the sum in a parameter: any spelling of the add, U32, and two or more calls #2175 removes for sums; nested arms takecoins' taken branches to 1.60G but not its cycles.pascal(with A fork-free segment takes its sequential path on the host without testing seq #2176, A def whose tail adds calls to itself carries the sum in a parameter: any spelling of the add, U32, and two or more calls #2175'sbinom~accruns 8% more cycles at 12.8% fewer instructions): once A fork-free segment takes its sequential path on the host without testing seq #2176 removes theseqtest from the loop that descendsn, clang vectorises its frame pushes (st3.2d, eight frames a turn; SIMD instructions 0.0001G to 0.079G, taken branches and mispredictions unchanged). With the pushes kept scalar by hand the build is back inside A def whose tail adds calls to itself carries the sum in a parameter: any spelling of the add, U32, and two or more calls #2175's range (3.13 / 3.19 / 3.29G against 2.98 / 3.12 / 3.29G; vectorised 3.33 / 3.38 / 3.45G) at 19% fewer instructions. The same barrier makeshanoi2% to 19% andbinarytrees4% to 12% slower at every position, so no rule is emitted for it.Checks for the additions
All local, against #2177 as it stood (
3394908c):#|lines on both, 21 differ from them and 1 times out on both; no status differs. The new test is the 886th.tree-matmul+14.6%,pure_hvm5_mini+5.1%,queens+4.4%. The JS of all 943 is byte-identical. Emitting all of them took 214.1 s before and 213.5 s now.-DBEND_POISON, at the default thread count and at 8.!call print the same with--gpu onand--gpu off, before and now (the arms are compiled away on the device; the frame id's move is in the device code of 3 of them,fork_leaf_handoff,gpu_markand the new test).tests/run/call_returns_nested.bend: two trees matched together with arms that free and arms that reuse their node, three tests deep, a Bool then two Nats (the shape ofcoins), shared trees read by two walks, a word test under a tag test, one def called by an owner and by a reader, forks, a!call, a comb 3000 deep; and the shapes that must stay: a nested arm that drops a subtree, and one behind lines that take a node apart, also on a tree that a reader and a picker share. Its lines are main's on the C lane (1 and 8 threads), under bun and underbend file.bend. A counting build takes 9,073 returns under a nested test and 20,271 on the entry chain, at 1 and at 8 threads.!call given arms, and arms run while the frame is in a task (seqclear). No test reaches a copied arm withseqclear; that condition is the first commit's.Verdicts
queensandtree-matmul. Alone it lefttree-radix1.0% up in instructions (its cycles overlap); with the frame id's move no bench of the suite runs more instructions.symregthat I could not separate from placement and a loss ontakthat I could.pascal's +14.5% into about -8%, but as it stands it repeats thekmeansandraytracelosses.coinsfrom 4.38G to 2.87G cycles andfibfrom 2.20G to 2.06G (one position each,fib's for 2.4% more instructions); it copies continuation text into every arm.Not covered by the additions
tsc, as above.queens, not intree-radix'merge, which is where the +1.0% was), and whether it vectorises a loop of frame pushes (pascal,tak).Checks of the first commit
All local, against main
f0cf0a7cand against #2176:#|lines on main and here, 21 differ from them and 1 times out on both, no status differs.-DBEND_POISONand a count of live heap words at exit: the 51 of those 52 tests that build give the same output, exit code and count as main. An earlier version of this branch, which answered the calls of every callee and not only of tree-shaped ones (so a superset of these calls), gave the same on all 905 tests that build.tests/run/call_returns.bend: a returning arm that frees the node it owns, one that allocates, one that drops another owned parameter, a three-word result, a Nat match two levels deep, a returning arm written after the calling one, a shared tree read by two walks, a forking def and forks over fork-free ones; and the shapes that must stay as they were (a recursion down a list, two defs that call each other). Its lines are main's; it prints them on the C lane (1 and 8 threads), under bun and underbend file.bend.compile_bookis the C emitter).!call print the same with--gpu onon main and here; the block is under!DEVICE, so the device compiles it away.repo.tsfails only on the ttok cap.Not covered
tscwas not run.Cost
+774 ttok over #2176 for the first commit, +417 for the two added (+1,225 over main).
Where this came from
symreg's gap to its twin was put down to branch prediction. The counters say otherwise: 0.044G mispredictions in 12.4G cycles. What the cycles follow is taken branches, about 1.2 cycles each, and a leaf reached by a call costs four or five of them (the jump in, the tag tests, the return through the table). Answering the leaf at the call leaves one or two. Real C calls were bounded earlier at 1.14x forsymreg; this gives 1.55x inside the segment machinery.🤖 Generated with Claude Code