Skip to content

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 - #2184

Closed
nicolas-abril wants to merge 2 commits into
bendlang:mainfrom
nicolas-abril:opt/accumulate-more
Closed

nicolas-abril wants to merge 2 commits into
bendlang:mainfrom
nicolas-abril:opt/accumulate-more

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Stacks on #2175 (the first commit here is #2175's; review the second). comp.ts is 67,042 ttok, 1,042 over the 66,000 cap (main 65,966; #2175 66,444; this adds 598 to #2175), so repo-gate fails until that is settled. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run.

What it does

#2175 turned one shape into a loop: a tail Nat.add of one call to the def itself, or 1n+f(..). This takes the same idea as far as the counts below justify:

  • any spelling of the add: the call bound by a let first, adds inside adds, successors over an add and an add over successors;
  • U32.add as well as Nat.add;
  • two or more calls under one add: every call but the last stays a call, the last becomes the loop. This is what reaches fib, pascal, coins and binarytrees;
  • a def that is not flat for another reason (its operand calls a recursion, or it tail-calls a def that forks): its own recursion still becomes a loop.
def fib(+n: Nat) -> Nat:          def check(t: Tree) -> Nat:
  match n:                          match t:
    case 0n:                          case Leaf{}:
      0n                                0n
    case 1n+p:                        case Node{l, r}:
      match p:                          Nat.add(1n, Nat.add(check(l), check(r)))
        case 0n:
          1n
        case 1n+pp:
          a = fib(p)
          b = fib(pp)
          Nat.add(a, b)

Two calls need the sum to live across the call that stays, so the sum can no longer be a local of one C loop (#2175's u64 acc). It is a parameter instead: the def goes on in a second def, d~acc, which takes the sum so far first, and a call that stays is handed the sum and answers it grown. fib is compiled as if it had been written

def fib~acc(acc: Nat, +n: Nat) -> Nat:      def fib(+n: Nat) -> Nat:
  match n:                                    fib~acc(0n, n)
    case 0n:
      Nat.add(acc, 0n)
    case 1n+p:
      match p:
        case 0n:
          Nat.add(acc, 1n)
        case 1n+pp:
          fib~acc(fib~acc(acc, p), pp)

which is the shape the hanoi kernel already has by hand. With one call under the add there is no call left and d~acc is a C loop, as in #2175.

Because the sum is now an ordinary parameter, #2175's emitter code is gone (acc_of, acc_on, the u64 acc local, the wrapped emit_put and the branch in emit_body): one rewrite on the checked terms in file_book (acc_def) builds d~acc, and the emitter compiles it like any def, so frames, borrows, spares and forks need no case of their own. For #2175's own shapes the emitted loop is the same statements with acc as a parameter.

The rule

acc_def(d), C lane only:

  • d answers a Nat or a U32, and calls itself somewhere outside a tail position.
  • Every such call is an operand of a tail add: directly, under nested Nat.add / U32.add, under successors, or through a let that binds exactly that call and whose name is read once.
  • Then, in d~acc: the other operands are added to acc (bound before the calls), each call takes the sum in turn, the last one as a tail call; a plain tail call to d passes acc on; any other answer v is acc + v. d itself is d~acc(0, ..).
  • If any reference to d is left after that (a call in an operand, in an argument, behind !, in a closure, in a fork, a bound name read twice), the def is left exactly as it was.
  • Never taken: F32; a def in a tail cycle with another def (its tail calls would become calls); a def with 247 live parameters (no room for one more).

Why the reordering is sound, per operator and shape:

  • Nat.add, one call. The recursion adds from the innermost call out, the loop from the first step on. Add is associative and commutative, and Nats only grow under it, so every value the carried sum takes is at most the answer of the outermost call: it passes 2^48-1 exactly when that answer does, and both stop with the same message. (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 argument.)
  • Nat.add, two or more calls. The same sum is carried through every call of the tree, so the same bound holds: the carried sum is always a sum of some of the terms of the outermost call's answer. Main meets the wall at the last add on the way out at the latest; here it is met as soon as the terms seen so far pass it. Same message, an earlier point in time.
  • U32.add. It wraps, so any order gives the same word and no add can stop the program. Commutativity is used (operands are added before the calls' results) and holds.
  • A call bound by a let. Same sum; the call now runs where its name is read, not at its let. Taken only when the name is read once, so no call runs twice.
  • Nested adds and successors. 1n+x is Nat.add(1n, x); nesting is associativity.
  • A program that stops or does not return in an operand or a call: see "Not covered", which lists every case where the point of death can differ. @unsafe defs are the only ones that may not return.

Reach, counted before building

A counting pass over the checked terms (a hook in a scratch copy of the compiler) over every def of Base, the 908 tests that build, 18 demo programs, the 17 bench/runtime programs and the 22 koru kernels (korulang/koru-benchmarks, suites/osprey-compute-kernels/bend). Distinct defs that have the shape and that #2175 does not take; "reached" means the def would actually become a loop (it does not fork, has no other call to itself).

candidate defs reached on a hot path
one call, Nat.add, another spelling (let-bound, nested) 2 1 none (both are in #2175's own test)
one call, U32.add 101 97 none: 96 in tests, Voices.count in app_slash_boss_3d (at most 24 voices)
one call, U32.xor / Bool.and / Bool.or 1 / 22 / 1 not taken none (tests; Peer.has in the netcode demo)
one call, U32.mul, and, or, min, max, Nat.min, Nat.max 0
one call, Nat.mul (unsound: a later zero) 6 not taken Nat.pow, Word.to_nat in Base
two or more calls, Nat.add 12 6 fib, pascal (binom), coins (ways): the whole kernel; binarytrees (check): one of its two walks. Also Map.size in Base
two or more calls, U32.add 71 34 none: esize in symreg, once per candidate. 34 fork (the benches' batch drivers), 3 mix operators (symreg's eval)
two or more calls, U32.mul / xor / or, Bool.and 6 not taken symreg's eval (mixed, below), ascending in pure_par_sort
affine (d + k * f(rest)) 2 not taken none (one test; Word.to_nat, which is Nat)

So the two-call case is where the hot code is, the other spellings are needed by it (fib and coins bind their calls with let, check nests its adds), and U32.add is wide but cold: it costs 10 tokens on top, so it is in. hanoi is not reached: it already carries its sum in a parameter.

As built: under #2175, 31 defs outside Base are loops (plus List.length and String.length); here 93 are loops and 38 more carry the sum across a call that stays or a callee that is not flat (plus Map.size). 176 of the 965 programs that build emit different C from #2175: 161 tests, 7 kernels, 6 demos, 2 benches.

Bounds by hand, before the compiler work

The four kernels rewritten in the source with the sum passed through the calls (fib(pp, fib(p, acc))), built with main. Instructions retired, one thread:

kernel main by hand this PR
fib 627.9M 482.1M -23.2% 472.7M
binarytrees 1,639.2M 1,364.0M -16.8% 1,344.1M
pascal 2,570.0M 2,043.5M -20.5% 1,976.2M
coins 653.6M 519.7M -20.5% 519.4M

Keeping the sum in the frame instead (fib(pp, Nat.add(acc, fib(p, 0n)))) gives -2.9% on fib, which is why the calls take the sum. symreg's eval adds in one arm and subtracts, multiplies and xors in three others: with only the add arm carried, by hand, it runs +1.3% instructions on main and +5.3% on #2177, so a def with a call to itself outside the add is left alone.

Results

One M4, one thread, GPU off, cc -O3, /usr/bin/time -l. Each build is swept over 16 placements (0 to 15 nops at the head of the text, 4 bytes each), 3 rounds, the least per placement; cycles are least / median / most over the placements, and the last column is the range of this / main over the same placement.

kernel instructions, main and #2175 this cycles, main this this / main
fib 627.7M 472.7M -24.7% 102.1M / 103.2M / 110.3M 79.5M / 81.6M / 87.2M 0.721 to 0.848
pascal 2,569.8M 1,976.2M -23.1% 439.2M / 453.3M / 466.9M 339.1M / 350.9M / 369.5M 0.728 to 0.841
coins 653.4M 519.4M -20.5% 146.8M / 147.0M / 147.2M 91.7M / 101.2M / 103.5M 0.624 to 0.705
binarytrees 1,639.0M 1,344.1M -18.0% 268.8M / 279.1M / 298.5M 235.1M / 236.9M / 248.3M 0.790 to 0.914

The three kernels #2175 already reached keep its numbers (against #2175, same sweep): collatz 167.2M and 167.2M instructions, cycles 0.985 to 1.004; digitsum 166.7M and 166.7M, 0.956 to 1.038; textstats 201.4M and 201.4M, 0.991 to 1.020. The other 15 kernels emit identical C.

With #2176 and #2177 (which attack the same recursions from the call and return side). Instructions, and the median cycles over 16 placements:

kernel main #2176 #2176 + this #2177 #2177 + this this alone
fib 627.7M / 103.3M 585.1M / 99.6M 415.1M / 73.8M 481.4M / 74.1M 399.4M / 68.4M 472.7M / 81.6M
binarytrees 1,639.0M / 278.9M 1,634.4M / 283.6M 1,270.7M / 226.7M 1,477.0M / 234.9M 1,191.9M / 214.1M 1,344.1M / 236.9M
pascal 2,569.8M / 455.1M 2,249.8M / 455.5M 1,723.3M / 379.5M 2,249.8M / 456.0M 1,723.3M / 380.7M 1,976.2M / 350.9M
coins 653.5M / 147.1M 593.7M / 146.9M 474.6M / 102.2M 564.1M / 161.0M 445.0M / 103.2M 519.4M / 101.2M

In instructions the gains add on all four (within 1.5 points of the product on three; fib overlaps: -23.3% and -24.7% give -36.4%). In cycles they add on fib and binarytrees. On pascal they conflict: #2176 removes 12.5% of the instructions at no cycle gain on main, and on top of this PR the build runs 8% more cycles than this PR alone (375.2M to 385.2M over the placements against 339.1M to 369.5M; not traced). On coins #2177 alone is 9% slower than main in cycles (1.036 to 1.115 per placement) and level with this PR once both are in. hanoi and tak are not reached and emit identical C.

Bend's suite. 15 of the 17 bench/runtime programs emit byte-identical C to #2175. histogram (half, taken by #2175, now through half~acc): 44,362.6M and 44,363.6M instructions (+0.002%), cycles 6,271.4M / 6,349.9M / 6,629.4M and 6,270.1M / 6,348.5M / 6,618.4M. symreg (esize, two calls under U32.add, once per candidate): 50,766.6M and 50,760.4M instructions (-0.01%). Its cycles move from 12,201M / 12,639M / 13,423M to 10,733M / 10,922M / 11,131M at every placement, with every segment of eval textually identical in the two builds: that is the segment order shifting (nine esize segments become six, placed before eval), the layout sensitivity of this bench, not a gain of the rule.

Compile time: app_slash_boss_3d 680 ms and 669 ms; the 161 tests whose C changes, 14.23 s and 14.43 s in total (+1.4%).

Cost

Measured by removing each part from the final file (ttok):

part ttok
the rewrite: one or many calls under one Nat.add, nested adds, d~acc and its caller (#2175's one-call version was 478) 786
a call bound by a let 163
successors over and under the add (#2175 had them) 60
the sum bound before the calls (collatz, below) 37
the tail-cycle check, the arity guard 11, 9
U32.add 10
two or more calls 0

166 of the total are the comment.

Checks

All local, against #2175 (e1b6a3e5) unless said:

  • Native lane (every test built and run with the GPU off, compared with its #| lines): 884 pass on 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, 886 here (the two new stdout tests); no existing test's status differs.
  • Memory: every test was emitted by both compilers: 747 emit byte-identical C; the 161 that differ were built with -DBEND_POISON and a count of live heap words at exit, and give the same output, exit code and count, at the default thread count and at one thread (nat_sum_loop, edited here, was compared on its new source). So do the new tests and the adversarial files, against 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 and against main.
  • JS lane: byte-identical on all 959 tests that emit JS (alone, and emitted after the C in one process), apart from stack_fault_trap, whose source changed.
  • Metal: the 8 tests with a ! whose C changes, an adversarial file with the def behind !, and the new tests print the same with --gpu on and --gpu off, under both compilers.
  • Demos: the 6 whose C changes build; proof_numerics prints the same.
  • New tests, each printing its lines under bend file.bend, as JS under bun, and on the C lane of main, 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 and this branch; the numbers were computed apart in Python:
    • tests/run/sum_loop_shapes.bend: one call in every spelling, U32 past 2^32, a tail call to a def that forks, an operand that recurses, an operand that reads a list the call also gets, an absurd arm; and what must stay a recursion: a bound call used twice, U32.sub, F32.add (64 ones under 1e8, where the order changes the answer), a call behind !, a call in a closure.
    • tests/run/sum_loop_calls.bend: two calls bound by lets in either order, nested, under successors, three calls, U32, erased parameters, an operand that consumes a list beside the calls, a tree lent twice and then given, a sum that lands on 2^48-1, the def under a fork with another def beside the sum, a tree twenty thousand deep on either side, an Array of closures called at its leaves; a bound call used twice stays.
    • tests/run/sum_loop_overflow.bend: one past 2^48-1 over a tree stops with the Nat message.
  • Adversarial files (scratch): the same shapes at larger sizes (three million steps, a tree a million deep on each side), the overflow with the extra one at the root and at the last leaf, the def under !, as a function value and as a template, and a pair of @unsafe defs in a tail cycle run for 800 million rounds.
  • Wrong versions: 15 deliberately wrong versions of the rule were built as compilers. The new tests fail 11: sub taken for add, F32 taken, the sum dropped where the def answers, operands dropped, a name bound once but read twice taken (times out: 2^40 calls), successors counted as one, the sum started at 1, only the live arguments rebuilt, an operand left untyped (the compiler refuses), the absurd arm summed (the compiler refuses), only the first call threaded. The tail-cycle check removed is caught only by the 800-million-round scratch file (the stack faults). Taking a fork's call or a call behind ! prints the same and differs only in the C (the fork is gone, the ! is lost): no #| line can see it. Keeping a def with calls left over is not wrong, only useless.

Two tests changed. tests/io/stack_fault_trap.bend faults the machine stack with a U32 recursion whose tail is a sum of one call to itself: that is now a loop, so its result is xored before the adds and the recursion stays one. tests/run/nat_sum_loop.bend (from #2175) asked for fifty thousand steps; on this machine the interpreter and the JS lane (bun 1.3.14, node 26.3.1) fault between thirty and fifty thousand frames, so it asks for twenty thousand.

Found on the way. (1) An operand moved under the new add without its type made the compiler stop with "a type the C facts cannot read" whenever it was a call that is not flat (found by an adversarial file and by tests/reg/array_function_element); every term the rewrite builds is now typed, and a wrong version without it fails the new tests. (2) With the grown sum written as an argument of the tail call, collatz ran 6% fewer instructions than #2175 and 14% more cycles at every placement (clang merges the two arms' tails); binding the sum before the calls gives #2175's code back. (3) Behind !, a recursion deeper than the device's 1,023 frames faults on Metal on main and on #2175 (bound!(5000n), the let-bound sum of the tests); a def taken here loops, so it prints its answer.

Not covered

  • Cluster gates, CUDA, Linux and x86 have not run. tsc was not run. No timing at 8 threads.
  • The JS lane and the interpreter are untouched: there the defs still recurse.
  • Only add. U32.xor (1 def), Bool.and (23) and Bool.or (1) have the shape in tests and two demos and nothing hot; no def has it for mul, and, or, min, max. Nat.mul and F32 are unsound. Affine sums (d + k * f(rest)) have two defs.
  • A def with a call to itself anywhere else (other arms with other operators, a call in an argument) is left alone: measured a loss on symreg's eval.
  • A fork's calls stay a fork (a b = f(l) f(r) then Nat.add(a, b)): 42 defs under an add, the benches' drivers among them. Carrying the sum through would make the calls depend on each other.
  • Every way evaluation can differ from main:
    • The sum is added in another order. A Nat sum that passes 2^48-1 stops with the same message at an earlier point: before calls and operands that main would have run first. So a program that passes the limit and also fails, or (in an @unsafe def) never returns, in one of those stops with the Nat message here.
    • A recursion whose only failure was its depth no longer fails: the last call under each tail add holds no frame.
    • Inside one tail add, the operands that are not calls now run before the calls (main runs the calls first), and the calls run in the order they stand in the add. A call bound by a let runs where its name is read, after any let between. If two of them fail, which one stops the program can differ.
    • A bound call whose name one arm adds and another arm never reads does not run in the arm that never reads it (main runs it before the match).
    • A tail call from a taken def to another def is now a call and an add: one frame while that def runs. Where that def calls back without a tail call (only @unsafe allows it), a round holds two frames where it held one.
    • Ownership can shift at a call site, as in 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: a def that becomes flat is called where its value is needed.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits November 15, 2026 23:44
… a loop

Nat.add(e, f(..)) or 1n+f(..) in tail position was a non-tail call: a frame
on the heap stack and an indirect return per step, where C carries the sum in
a register (String.length, List.length, any count or digit sum written the
plain way). Such a call no longer keeps its def from being flat: the def's
spin holds the sum in acc, adds the other operand at the call and jumps to
itself, and adds acc to whatever it answers. Nats only grow under add, so a
partial sum in either order is at most the whole one and both orders stop at
2^48-1 together. C lane only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A def that answers a Nat or a U32 and whose every call to itself, past its
tail calls, is an operand of a tail add goes on in d~acc, a second def that
takes the sum so far as its first parameter. The other operands join the
sum, each call takes it in turn and answers it grown, and the last call is
a tail call, so one call under the add is a loop and two or more keep every
call but the last. The add may be spelled with nested adds, successors on
either side, or a call bound by a let that is used once. d calls d~acc
with 0.

This replaces the loop-local sum of the previous commit (acc_of and its
four emitter sites) with one rewrite on the checked terms in file_book, C
lane only. Left as they are: F32, a def in a tail cycle with another def, a
call behind !, a fork's calls, and a def with a call to itself anywhere
else.

tests/io/stack_fault_trap.bend xors the result of its self-call before the
adds, so the recursion it needs to fault the stack stays one.
tests/run/nat_sum_loop.bend asks for twenty thousand steps, which the JS
lanes hold.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Folded into #2175: its branch opt/nat-accumulate now ends at this PR's commit (2707077e), and its description is this one's. The branch opt/accumulate-more is kept.

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