Repository navigation
Defs that tail-call each other in a cycle run as one loop: a head's spin holds the other defs' bodies - #2188
Draft
nicolas-abril wants to merge 1 commit into
Draft
nicolas-abril wants to merge 1 commit into
nicolas-abril wants to merge 1 commit into
Conversation
On the C lane a tail-call cycle across defs was never flat: each def was a segment, a call into it from an expression a frame and a return, and its callers were not flat either. A cycle is now flat when one of its defs is a head (every other def is named once in the cycle), no def of it calls another in an argument, and their other callees are flat. The head's spin holds each other def's body in place of its call, by the path a tail call to a def with one call site already takes, and a call back to the head is the spin's jump. A def that is no head gets a spin that calls the head's. Base's String.contains, String.starts_with, String.eq, U32.show, Nat.show, U32.read, Nat.read, U32.hex, String.trim_start and Map.diff are such cycles; the JS lane already ran them as one loop. 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 66,233 ttok, 233 over the 66,000 cap (main 65,981, +252), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run.What it does
Bend has no
ifand cannot match on a local, so a test of a computed value is a helper def that matches it and tail-calls back. Base'sString.containsis one:A def whose only calls to itself are tail calls is flat: the C lane emits it as a spin (a C function with a loop), and a call to it is a C call. A tail-call cycle across two or more defs was never flat, so every def of the cycle was a segment, every call into it from an expression was a frame and a return through the segment table, and every def that called it stopped being flat too (
U32.show,Nat.show,String.starts_with,Map.get.leaf, ..). The JS lane already runs such a cycle as one loop over its defs' bodies ($pc).Now a cycle is flat too, and runs as one spin, when one of its defs is a head: every other def of the cycle is named from one place in the cycle. The head's spin holds each other def's body in place of the one call to it, and a call back to the head is the spin's own jump.
String.containsis emitted aswhere main emitted four segments (
String.contains, its continuation afterString.starts_with,String.starts_withandString.starts_with.go), with a frame pushed, three jumps and a return through the segment table each turn. There is no selector and no new C construct: it is the body a def with a tail call to itself already gets, with the helper's test where its call was.The rule
loop_of(d)(already there, for the JS lane) is d's strongly connected component in the graph of tail calls.loop_head) is a def such that every other def of the cycle is named exactly once inside the cycle (a count ofRefs per pair of defs, kept byfile_bookbeside the site counts it already keeps). Then every turn goes through the head, each other def hangs from it by its one call, and the head's spin holds each body once: the spin is as long as the defs together, never longer.flat_of) when it has a head, no def of it forks, bangs or calls a def of the cycle in an argument, and every def they call outside the cycle is flat. A cycle that is not flat is compiled exactly as on main.emit_body), a tail call to another def of the cycle emits that def's body with the arguments as its parameters, through the path a tail call to a def with one call site already takes in a segment (emit_argsas for a jump, thenemit_body). A tail call to the head is the self-jump.hop, below, sinceloopalso calls itself) gets a spin that calls the spins of the defs it tail-calls: one C call per entry, the turns run in the head's spin. The chain of such calls follows the one-call tree down from a def to a head, so it is no deeper than the cycle has defs, and no spin of a cycle calls itself.!) calls the spin, as it does for any flat def.How a cycle can arise, and the count before building
Mutual recursion is refused by the checker (a def may only call one above it), with two ways round: a def may call an unfilled
lawbefore the def that fills it, which only Base's own defs and@unsafedefs may do; and an@unsafedef may call a def below it. So every cycle is in Base, through a law, or has an@unsafedef in it.A counting pass over the checked terms (a hook in a scratch copy of the compiler): every def of Base, the 909 tests that build, 18 demo programs, the 17
bench/runtimeprograms and the 22 koru kernels (korulang/koru-benchmarks,suites/osprey-compute-kernels/bend). A cycle is a strongly connected component of the tail-call graph with two or more defs.@unsafe)tests/run/js_group_args). No helper has a caller outside its cycle.loopintests/run/unsafe_tail_cycle, which calls itself as well). So every cycle without a call in an argument has a head, and all 15 become loops.wnf/wnf_appin three tests,count/backin one) are left alone.Base's ten, and what reaches them:
U32.show.go/.fin(all 17 benches, for the one number they print; 5 demo programs; 520 tests),Nat.show.fin/.go(all 22 kernels, for their one number; 7 demo programs; 222 tests),String.eq/.go(underMap.get,Map.has,Map.pop; 4 demo programs, 29 tests),Map.diff/.fin(2 demo programs),U32.read.go/.if(4),Nat.read.go/.if(3),String.contains/.ifandString.starts_with.go/.if(thetextstatskernel, 7 tests),U32.hex.go/.fin(11 tests),String.trim_start/.if(1 test).On a hot path: one program.
textstatsspends 75% of its instructions inString.contains. Everywhere else the cycle runs once per printed number or per parsed argument.Bounds by hand, before the compiler work
The cycle's own cost, separated from borrowing: the same algorithm with the same ownership, the helper folded into its caller in the source as a flag parameter (the one way Bend can spell it), built with main. Instructions retired, one thread.
textstats(Base'sString.containsandString.starts_withcopied into the file, verbatim and merged)isqrtwith a helper def (iter/iter.if), against its flag spelling doing the same workprimeswith helper defs (a ring of three, andgo/go.if), against the kernel's flag spellingtextstats: a verbatim copy of Base's four defs in the kernel's file emits the same C as Base's (205.27M both). So of the 5.4x between main and the hand-written flat twin over the same lists (38.0M, measured before this work), the cycle is 6%; the rest is ownership:String.containsowns both strings, so every position keeps a count on the tail and on the pattern, rebuilds the node it matched and hands it toString.starts_with, which frees it node by node. That is unchanged here (the merged def's parameters are owned exactly as the two defs' were).isqrt: the kernel's own flag spelling computes the next iterate twice; both columns compute it once (+y = ..in the helper spelling, aletunder the flag's arm in the other). With this PR the helper spelling emits the flag spelling's loop statement for statement, less the flag.primes: the helper spelling does one division less per number (it testsd * d > nbeforen % d), yet after this PR it still runs more instructions than the flag spelling on main. That is clang, not the cycle:Nat.modisPair.sndof the native divmod, whose test for a boxed half clang compiles to a compare chain in one loop and an early branch in the other (17 instructions a turn against 13, read off the assembly). With that test gone (Nat.div and Nat.mod are one native division each: Base drops the other half as a Nat #2173 + Nat.mul divides to test for overflow only when an operand reaches 2^32 #2174 applied to both sides) the helper spelling is ahead: see below.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.textstats(koru kernel, unchanged source)isqrt, helper spelling (helper also called from outside)isqrt, helper spelling (entered at the head)primes, helper spellingisqrt, helper spelling, on #2173 + #2174primes, helper spelling, on #2173 + #2174The flag spellings on the same sweeps:
isqrt187.5M and 59.7M / 59.8M / 60.1M on main, 129.0M and 47.2M / 47.5M / 47.8M on #2173 + #2174 (the helper spelling is now level with it);primes112.5M and 34.1M / 34.6M / 34.7M on main, 84.2M and 23.2M / 23.5M / 23.7M on #2173 + #2174 (the helper spelling is now ahead in both). So the natural spelling of a computed test no longer costs anything against flag parameters.The 22 koru kernels all emit different C, because each prints its one number with
Nat.show. The least of 5 interleaved runs:textstats205.15M to 193.14M instructions (-5.9%); 19 others within 0.2% (the worstprimes, -0.19%, andwordfreq, +0.12%). Two run more instructions at the same cycles:josephus92.1M to 112.1M (+21.7%; cycles 127.2M and 127.0M) andnestedloop231.4M to 247.1M (+6.8%; cycles 147.8M and 147.7M). Their loops are textually identical C in the two builds; what changed is thatNat.show, now a spin, is inlined by clang into the C function that holds the loop, and clang then compiles the same loop with more instructions a turn (two more injosephus). Withspin_Nat_showkept out of line by hand both are back at main's count (92.1M, 231.5M). Over 16 placements their cycles are 0.985 to 1.013 and 0.975 to 1.081 of main's (medians +0.2% and -0.05%). The same two kernels move the same way under #2173 (josephus, +10.8% instructions, no cycles) and #2174 (nestedloop).Base's functions, in loops over values made at run time (
micro/in the scratch files; same sweep, 2 rounds):U32.showof 1M numbersNat.showof 1M numbersString.contains,starts_with,eqon 200-character stringsU32.read,Nat.read,String.trim_startof one shared stringU32.showandNat.showin one statementThe last two rows run fewer instructions and more cycles at every placement. See "Not covered": a call to a def that is now flat runs where it is written in its statement, where main ran it first.
Bend's suite. All 17
bench/runtimeprograms emit different C, because each prints its one number withU32.show, which is now a spin. Instructions retired, one thread, the least of 2 interleaved runs:bfsnbodyeditdistqueensgameofliferaytracehashmapsymreghistogramterrainkmeanstree-bitoniclexertree-matmulmandelbrottree-radixmerkleThe worst is +0.002% (
bfs,nbody), the best -0.11% (tree-radix); every output is equal. In cycles the single runs of that table movededitdistby -20% andnbodyby +5.7%, which is placement: over 16 placementseditdistis 8,449.7M / 8,452.0M / 10,621.0M on main and 8,450.2M / 8,451.7M / 10,624.3M here (one placement in 16 costs 25% in either build, as before), andnbody21,330.7M / 22,051.8M / 22,694.4M and 21,323.6M / 22,114.4M / 22,722.1M. The other 15 were within 0.9% in the single runs and were not swept.Compile time and code size. Of the 966 programs that build (909 tests, 18 demo programs, 17 benches, 22 kernels), 791 emit different C: 743 tests, 9 demo programs, all 17 benches and all 22 kernels, nearly all through
U32.showandNat.show. 81 defs are new spins, and the programs that change have 22,147 segments where they had 25,607.tests/base/u32_divmod), the most one grows +0.27% (tests/io/channel_fork_join).app_slash_boss_3d: 2,928,604 to 2,887,214 bytes.INLINE(underSPIN_FARcharacters), none isFARorDFAR.app_slash_boss_3d, the largest (2.9 MB of C), emits in 0.69 s with either (the least of 5), and clang's user time on its C is 6.15 s and 5.95 s (the quietest of 3 pairs).Cost
+252 ttok, measured by removing each part from the final file:
flat_of(the cycle's defs checked together, the head asked for, no call in an argument) 96,loop_head64, the comment 50, the held call inemit_body26, the count of names per pair of defs infile_book16. No new emitter path: the held body goes through the path a tail call to a def with one call site takes on main.The other design: one spin with several entries
The alternative is what the JS lane does: the bodies of the cycle's defs in one C loop under a selector, a tail call inside the cycle sets the selector and the parameters and goes round. I built it in a scratch copy to compare (not in this PR):
comp.tsSegif (pc == 0) { .. } else if (pc == 1) { .. }in the loop,pc = 1; continue;at each callprimes, helper spellingisqrt, helper spellingtextstatsThe selector costs nothing only where clang threads every
pc = k; continueto its arm. It does onisqrtandtextstatsand does not onprimes(a ring of three called from a cycle of two), which comes out slower than main's segments. A loop whose speed rests on clang's jump threading is not something to emit, and the selector version costs 2.5 times the tokens, so the head version is the one here. What it gives up is the cycle with no head, which nothing in Base, the tests, the demos, the benches or the kernels has. (textstatsrunning 3% fewer instructions under the selector version is clang too: the two builds differ only in those two spins, which do the same work.)What merging changes besides the loop
tail_cycle_ownershas a helper that a caller outside lends a string and the cycle hands one it made; with the held call lending like a plain call instead, that test frees twice). One step out, a tail call into the cycle from a flat def is now a call to a spin, which lends where main's jump handed the value over; no parameter in the corpus changes because of that. A hook that prints every boxed parameter's ownership once the facts settle finds 962 of the 966 programs with every parameter owned or lent as on main, and four that differ, in six parameters:demos/io_rollback_netcode/walkers_demoandwalkers_test:String.eq's second parameter is owned where main lent it.Post.dropreadsnameinString.eq(.., name)and passes it to its own recursion. On mainString.eqwas not flat, ran first and was lentname; now the recursion runs first, with a kept copy, andString.eqis the last reader, so it is handed the string to free. This is the order change of "Not covered" showing in ownership: which use of a+value is the last one can move.tests/base/string_contains:String.starts_with's pattern is owned where main lent it. The test's literal calls are now unfolded, down toString.starts_with.go.if("..", "..", ..), and a literal argument makes its parameter owned, as it does on main for any call.tests/gfx/fullscreen_input:want's parameter is lent where main owned it (not traced).tail_cycle_shapes'col.if,tail_cycle_owners'wk.ifandgt.if), it gets a spin of its own: a loop of its own if it is a head too (a cycle of two with one call each way has two heads, and each def's spin holds the other), a call to the head's spin if not. The emitter's unfolding of a call with a literal argument can also name a helper from outside (rev(U32, n, [1, 2, 3, 4, 5], [])becomes a call torev.ifat the call site).SPIN_FAR(8,192) characters isINLINE, a longer one is a call on the device (DFAR) and, if it loops over F32 math, on the host too (FAR). The head's spin is as long as the cycle's defs together. In the 966 programs no spin changes class: all 81 new spins areINLINE.!. A def with a fork or a!call is not flat, so a cycle with one is compiled as on main (fk.a/fk.bintail_cycle_callers). A cycle called from a fork's branches or with!runs through its defs' segments, which call the spin: same output on the CPU and on Metal (tail_cycle_callers:tree!,ev!,ev.if!). The 62 programs with a!call whose C changes (44 tests, the 17 benches, one demo) were built whole by both compilers, each with its GPU program, and run with--gpu on: the same result from both on all 62.tail_cycle_callers).@unsafecycles with a call in an argument. Left alone (4 in the tests). Merging one is sound for the values it computes but would move the recursion to the C stack: the deliberately wrong version that merges them prints the right lines until the recursion is deep (tail_cycle_callersruns 20,000 frames in sixteen forked leaves, and that version dies on a pool thread's 512 KB stack).Checked
Against main (8c329c1), on one M4:
The native lane (every
tests/**/*.bendwith#|lines: build with-o bin, run with--gpu offat the default thread count, compare stdout): 886 pass on main, 889 here (the three new tests), 953 do not build (they expect an error), 21 differ and 1 times out on both, and no status differs over the 1,874 files they share.Memory. For every program whose C differs, both C files are patched to count live heap words at exit, built with
-DBEND_POISON(a freed word is poisoned) and run: the 743 tests, 16 of the benches and 5 demo programs print the same lines, exit with the same code and leave the same count of live words, and so do the three new tests and six scratch programs. Not decided by it:tree-bitonic(over a minute at-O1with either compiler; its-O3output is equal), the relayserver.bend(it never ends),app_slash_boss_3dandwalkers_demo(both builds exit at once with the same code, 45, in a session with no window to open), andwalkers_test, which needs a relay: run by hand on a fresh local relay, two bots in one room print the same three lines with either compiler, as the demo promises (the hashes change from run to run on main too).The JS lane.
-o x.jsemits byte-identical JS with both compilers for all 909 tests and all 35 demo and bench programs, alone and after a C emit in the same process. The rule is reached from the JS lane only throughflat_ofin a match table's rows, andfile_bookopens every term in main's order, so the probes and the order of a cycle's defs under$pcare main's.The gate's in-process checks, run by hand with both compilers: the comptime twins emit the same C, and the four state fixtures compile to the same bytes and leave the same probes over the gate's eleven steps.
tscoverbend2andgates: the same 10 errors on main and here, none in the changed lines.New tests, each on four lanes with both compilers (C at
-O1with poisoned frees, C at-O3on every core, JS,bend file.bend), all eight runs equal:tests/run/tail_cycle_shapes.bend: a ring of three entered at each def, a helper with a caller outside, a self call beside a cross call, a def named from two places, a cycle with no head; 3,000,000 turns each.tests/run/tail_cycle_owners.bend: a matched node the helper rebuilds, reusable lists, a value one def builds and the other answers, a string lent to the whole cycle, parameters that swap, a helper lent a string from outside and handed one to own, a record of words, an erased type parameter, parameters that differ in count and kind, a Nat rebuilt as matched, an Array written and read in place, and Base's own cycles on text made at run time.tests/run/tail_cycle_callers.bend: the cycle under a fork, under!, as a function value, behind a partial application and a closure, answering a closure, in a literal call; and the four kinds of cycle that must stay as they were.IO.now, so nothing is folded away). One loop each:r0,r1,r2,col,loop,two.b,keep,look,mk,cnt,swp.if,inc,fibp,rev,rev.if,mix.a,mix.c,dn,fill,asum,wk,wk.if,gt,ev,cl.a,cl.b,even,odd,out,kp. A spin that calls a head's:col.if,hop,two,two.a,swp,gt.if,ev.if,ev.end. Segments, as on main:pa,pb,nt.a,nt.b,fk.a,fk.b,hv,oc.a,oc.b,dp.a,dp.b,oq.a,oq.b.Deliberately wrong versions of the rule, each run against the three new tests, five older tests with cycles and six scratch programs, with live words counted. Twelve are caught by the new tests:
callerscallerscallerscallersshapesshapes,ownersshapes,ownersshapes,ownersownersTwo are not wrong and no
#|line can see them: a segment that holds the bodies too (the same loop, in the segment), and sites counted over the whole program instead of inside the cycle (fewer cycles become loops).Not run: the cluster gates (
test,perf), CUDA, Linux, x86, clang other than Apple clang 17.Not measured together with the other open drafts on this ground. #2162 (statements unfold into defs that are not flat) is independent of this: nothing here unfolds a helper into a def that is not flat, a body is held only in the spin of a head. #2175 leaves a def in a tail cycle alone and still does. What is left of
textstatsis ownership (#2148's ground):String.containsandString.starts_withown their strings before and after.Not covered
Every way the behaviour can differ from main:
U32.show,Nat.show,String.starts_with,Map.get.leaf, ..) therefore move from the first group to the second. Values do not depend on it. Two things can:@unsafe): the outcome can be the other one's.Nat.add(len(U32.show(x)), len(Nat.show(y)))built both strings and then walked both on main; here it builds and walks the first, then the second: 6.5% fewer instructions and 23.5% to 24.8% more cycles at every placement. Spelled with twolets (main's order) the same program is at 153.5M cycles against main's 155.5M. Each call alone is level (0.992 to 1.016 and 0.992 to 1.006). The reading that the walks' frees and the next build's allocations become one chain through the free list is inferred, not traced. A second micro-program (a+string read byU32.readand byNat.read) runs 1.8% fewer instructions and 3.2% to 6.8% more cycles; its three parts alone are each faster (-1.2% to -1.7% cycles), every parameter's ownership is the same in both builds, and spelling the order out withlets recovers half of it (+3.0%). Not traced further.FOLD_FUEL), so it now does for a cycle's defs:even(10n)in an argument is computed by the compiler, andrev(U32, n, [1, 2, 3, 4, 5], [])loses its first turn (the call site namesrev.ifwith the rest of the list). An unfolded call does not evaluate an argument its arm never reads, where the JS lane andbend file.benddo (The native lane skips an argument thatbend file.bendand the JS lane evaluate, when the call's selector is a literal: one prints 100, the others stop with the Nat message #2181, on main for every flat def): a program that relied on such an argument failing behaves as The native lane skips an argument thatbend file.bendand the JS lane evaluate, when the call's selector is a literal: one prints 100, the others stop with the Nat message #2181 describes for more defs than before.seqclear, a jump into a segment that cannot fork is handed back to the scheduler as a task, which main did for a jump into a cycle's def from another segment; a call to a spin is not, and the spin runs to its end where it was called, as every flat loop does. On the device a spin polls for a failure every 4,096 turns (WL_SPIN), as before.fcallsf.iffrom two arms andf.ifcallsffrom two), or two defs each call themselves. Holding a body twice would take the first; the selector design would take both. Nothing in the corpus has either.🤖 Generated with Claude Code