Repository navigation
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
Draft
nicolas-abril wants to merge 3 commits into
Draft
nicolas-abril wants to merge 3 commits into
nicolas-abril wants to merge 3 commits into
Conversation
… 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>
…rs the sum as it stands acc_def turned an arm that answers 0n into Nat.add(acc, 0n), which the C lane tests against 2^48 - 1 although acc is a Nat already (and for U32 an add of 0). Such an arm now answers the sum itself. Instructions on the koru kernels with the open drafts merged (opt/integration): coins 459.0M to 429.5M, fib 399.3M to 387.9M. Of the 17 benches only histogram's C changes (two lines), same output. tests/run/sum_loop_zero.bend: one and two calls, an arm that answers another literal, a computed 0, U32, and a sum that lands on 2^48 - 1 before the arm that answers 0. comp.ts is 67,091 ttok. Co-Authored-By: Claude Fable 5.2 <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,091 ttok, 1,091 over the 66,000 cap (main 65,966), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. Two commits: the first (e1b6a3e5, 66,444 ttok) took one call underNat.add; the second (2707077e, first opened as #2184) replaces its mechanism and takes more; a third (81ca8718) stops an arm that answers 0 from adding it (its own section below). This description covers the first two.What it does
The first commit turned one shape into a loop: a tail
Nat.addof one call to the def itself, or1n+f(..). This takes the same idea as far as the counts below justify:letfirst, adds inside adds, successors over an add and an add over successors;U32.addas well asNat.add;fib,pascal,coinsandbinarytrees;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 (the first commit'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.fibis compiled as if it had been writtenwhich is the shape the
hanoikernel already has by hand. With one call under the add there is no call left andd~accis a C loop, as in the first commit.Because the sum is now an ordinary parameter, the first commit's emitter code is gone (
acc_of,acc_on, theu64 acclocal, the wrappedemit_putand the branch inemit_body): one rewrite on the checked terms infile_book(acc_def) buildsd~acc, and the emitter compiles it like any def, so frames, borrows, spares and forks need no case of their own. For the first commit's own shapes the emitted loop is the same statements withaccas a parameter.The rule
acc_def(d), C lane only:Nator aU32, and calls itself somewhere outside a tail position.Nat.add/U32.add, under successors, or through aletthat binds exactly that call and whose name is read once.d~acc: the other operands are added toacc(bound before the calls), each call takes the sum in turn, the last one as a tail call; a plain tail call to d passesaccon; any other answervisacc + v. d itself isd~acc(0, ..).!, in a closure, in a fork, a bound name read twice), the def is left exactly as it was.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. (The first commit'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.let. Same sum; the call now runs where its name is read, not at itslet. Taken only when the name is read once, so no call runs twice.1n+xisNat.add(1n, x); nesting is associativity.@unsafedefs 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/runtimeprograms and the 22 koru kernels (korulang/koru-benchmarks,suites/osprey-compute-kernels/bend). Distinct defs that have the shape and that the first commit does not take; "reached" means the def would actually become a loop (it does not fork, has no other call to itself).Nat.add, another spelling (let-bound, nested)U32.addVoices.countinapp_slash_boss_3d(at most 24 voices)U32.xor/Bool.and/Bool.orPeer.hasin the netcode demo)U32.mul,and,or,min,max,Nat.min,Nat.maxNat.mul(unsound: a later zero)Nat.pow,Word.to_natin BaseNat.addfib,pascal(binom),coins(ways): the whole kernel;binarytrees(check): one of its two walks. AlsoMap.sizein BaseU32.addesizeinsymreg, once per candidate. 34 fork (the benches'batchdrivers), 3 mix operators (symreg'seval)U32.mul/xor/or,Bool.andsymreg'seval(mixed, below),ascendinginpure_par_sortd + k * f(rest))Word.to_nat, which isNat)So the two-call case is where the hot code is, the other spellings are needed by it (
fibandcoinsbind their calls withlet,checknests its adds), andU32.addis wide but cold: it costs 10 tokens on top, so it is in.hanoiis not reached: it already carries its sum in a parameter.As built: under the first commit, 31 defs outside Base are loops (plus
List.lengthandString.length); here 93 are loops and 38 more carry the sum across a call that stays or a callee that is not flat (plusMap.size). 176 of the 965 programs that build emit different C from the first commit: 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:fibbinarytreespascalcoinsKeeping the sum in the frame instead (
fib(pp, Nat.add(acc, fib(p, 0n)))) gives -2.9% onfib, which is why the calls take the sum.symreg'sevaladds 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 15nops 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.fibpascalcoinsbinarytreesThe three kernels the first commit already reached keep its numbers (against the first commit, same sweep):
collatz167.2M and 167.2M instructions, cycles 0.985 to 1.004;digitsum166.7M and 166.7M, 0.956 to 1.038;textstats201.4M and 201.4M, 0.991 to 1.020. The other 15 kernels emit identical C.Those three against main, as measured for the first commit (the least of 9 interleaved runs; cycles over 16 placements as this / main):
digitsumcollatztextstats(String.length)With #2176 and #2177 (which attack the same recursions from the call and return side). Instructions, and the median cycles over 16 placements:
fibbinarytreespascalcoinsIn instructions the gains add on all four (within 1.5 points of the product on three;
fiboverlaps: -23.3% and -24.7% give -36.4%). In cycles they add onfibandbinarytrees. Onpascalthey 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). Oncoins#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.hanoiandtakare not reached and emit identical C.Bend's suite. 15 of the 17
bench/runtimeprograms emit byte-identical C to the first commit.histogram(half, taken by the first commit, now throughhalf~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 underU32.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 ofevaltextually identical in the two builds: that is the segment order shifting (nineesizesegments become six, placed beforeeval), the layout sensitivity of this bench, not a gain of the rule.Compile time:
app_slash_boss_3d680 ms and 669 ms; the 161 tests whose C changes, 14.23 s and 14.43 s in total (+1.4%).The third commit: an arm that answers 0 answers the sum as it stands
The rewrite turned an arm that answers
0nintoNat.add(acc, 0n), which the C lane tests against 2^48-1 althoughaccis a Nat already (forU32, an add of 0). Such an arm now answersaccitself; in the emitted C,nat_chk(e, _acc_0 + 0ull)becomes_acc_0. +49 ttok.Instructions, one thread, against the second commit:
coins519.9M to 490.3M (-5.7%),fib473.1M to 461.7M (-2.4%);collatzchanges C at equal instructions;pascalanddigitsumemit the same C. No cycles claim (not swept). Of the 17 benches onlyhistogram's C changes (two lines, same output, 44,365.7M and 44,384.4M instructions, within run-to-run spread).Checks, against the second commit: the native lane (886 pass there, 887 here, no status differs); the memory check (
-DBEND_POISON, live words at exit) on the 127 tests whose C differs, all the same output, exit code and live count; a new testtests/run/sum_loop_zero.bend(one and two calls, an arm that answers another literal, a computed 0,U32, and a sum that lands on 2^48-1 before the arm that answers 0), which prints the values computed apart in Python on the interpreter, JS and C, on main, the second commit and this one, and ends with 0 live words; a wrong version that skips every constant fails it.Cost
Measured by removing each part from the final file (ttok):
Nat.add, nested adds,d~accand its caller (the first commit's one-call version was 478)letcollatz, below)U32.add166 of the total are the comment.
Checks
All local, against the first commit (
e1b6a3e5) unless said:#|lines): 884 pass on the first commit, 886 here (the two new stdout tests); no existing test's status differs.-DBEND_POISONand 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 the first commit and against main.stack_fault_trap, whose source changed.!whose C changes, an adversarial file with the def behind!, and the new tests print the same with--gpu onand--gpu off, under both compilers.proof_numericsprints the same.bend file.bend, as JS under bun, and on the C lane of main, the first commit and this branch; the numbers were computed apart in Python:tests/run/sum_loop_shapes.bend: one call in every spelling,U32past 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.!, as a function value and as a template, and a pair of@unsafedefs in a tail cycle run for 800 million rounds.subtaken foradd,F32taken, the sum dropped where the def answers, operands dropped, a name bound once but read twice taken (times out:2^40calls), 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.bendfaults the machine stack with aU32recursion 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 the first commit) 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,collatzran 6% fewer instructions than the first commit and 14% more cycles at every placement (clang merges the two arms' tails); binding the sum before the calls gives the first commit's code back. (3) Behind!, a recursion deeper than the device's 1,023 frames faults on Metal on main and on the first commit (bound!(5000n), the let-bound sum of the tests); a def taken here loops, so it prints its answer.Not covered
tscwas not run. No timing at 8 threads.U32.xor(1 def),Bool.and(23) andBool.or(1) have the shape in tests and two demos and nothing hot; no def has it formul,and,or,min,max.Nat.mulandF32are unsound. Affine sums (d + k * f(rest)) have two defs.symreg'seval.a b = f(l) f(r)thenNat.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.Natsum 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@unsafedef) never returns, in one of those stops with the Nat message here.letruns where its name is read, after anyletbetween. If two of them fail, which one stops the program can differ.@unsafeallows it), a round holds two frames where it held one.🤖 Generated with Claude Code