Skip to content

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
bendlang:mainfrom
nicolas-abril:opt/flat-tail-cycles
Draft

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/flat-tail-cycles

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 66,233 ttok, 233 over the 66,000 cap (main 65,981, +252), 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

Bend has no if and cannot match on a local, so a test of a computed value is a helper def that matches it and tail-calls back. Base's String.contains is one:

def String.contains.if(t: String, p: String, here: Bool) -> Bool:
  match here:
    case False{}:
      String.contains(t, p)
    case True{}:
      True{}

def String.contains(s, p):
  match s:
    case SNil{}:
      String.is_empty(p)
    case SCon{h, t}:
      String.contains.if(t, p, String.starts_with(SCon{h, t}, p))

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.contains is emitted as

INLINE Term spin_String_contains(Env e, THR Term* o, Term r0, Term r1) {
  Term _s_0 = r0;
  Term _p_0 = r1;
  WL_SPIN
    if (term_aux(_s_0) == CID_SNil) {
      ..
    } else {
      ..
      if (spin_String_starts_with(e, _o_1, term_ctr(CID_SCon, _nd_0), _p_0) == 0) {
        return 0;
      }
      if ((u32)_o_1[0] == 0) {          // String.contains.if, in place of its call
        r0 = _f_1;
        r1 = _p_0;
        _s_0 = r0;
        _p_0 = r1;
        WL_AGAIN(spin_String_contains);
      } else {
        term_sink(e, _f_1);
        term_sink(e, _p_0);
        o[0] = 1;
      }
    }
  break;
  }
  return 1;
}

where main emitted four segments (String.contains, its continuation after String.starts_with, String.starts_with and String.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.
  • A head of a cycle (loop_head) is a def such that every other def of the cycle is named exactly once inside the cycle (a count of Refs per pair of defs, kept by file_book beside 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.
  • A cycle is flat (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.
  • In the spin of a head (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_args as for a jump, then emit_body). A tail call to the head is the self-jump.
  • A def of the cycle that is no head (hop, below, since loop also 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.
  • A segment (a def of the cycle run as a task, under a fork or a !) calls the spin, as it does for any flat def.
@unsafe
def hop(n: Nat) -> Nat:        # no head: loop is named twice in the cycle
  loop(n, False{})             # spin_hop calls spin_loop

@unsafe
def loop(n: Nat, b: Bool) -> Nat:   # the head: hop is named once
  match n:
    case 0n:
      0n
    case 1n+p:
      match b:
        case True{}:
          hop(p)               # hop's body here, its call to loop a jump
        case False{}:
          loop(p, True{})      # a jump

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 law before the def that fills it, which only Base's own defs and @unsafe defs may do; and an @unsafe def may call a def below it. So every cycle is in Base, through a law, or has an @unsafe def 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/runtime programs 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.

cycles of two defs of three with a call to a def of the cycle in an argument flat as built
Base 10 10 0 0 10
tests (all @unsafe) 9 8 1 4 5
demos, benches, kernels (their own defs) 0
  • Entered from outside at one def in 18 of the 19, at each of its three defs in one (a ring in tests/run/js_group_args). No helper has a caller outside its cycle.
  • Named from one place in the cycle: every def but one (loop in tests/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.
  • The 4 with a call in an argument (wnf / wnf_app in three tests, count / back in 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 (under Map.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 / .if and String.starts_with.go / .if (the textstats kernel, 7 tests), U32.hex.go / .fin (11 tests), String.trim_start / .if (1 test).

On a hot path: one program. textstats spends 75% of its instructions in String.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.

program cycle spelling, main one def, by hand, main cycle spelling, this PR
textstats (Base's String.contains and String.starts_with copied into the file, verbatim and merged) 205.3M 192.4M -6.3% 193.2M
isqrt with a helper def (iter / iter.if), against its flag spelling doing the same work 234.4M 187.8M -19.9% 187.8M
primes with helper defs (a ring of three, and go / go.if), against the kernel's flag spelling 165.1M 112.7M -31.7% 134.5M
  • textstats: 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.contains owns both strings, so every position keeps a count on the tail and on the pattern, rebuilds the node it matched and hands it to String.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, a let under 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 tests d * d > n before n % d), yet after this PR it still runs more instructions than the flag spelling on main. That is clang, not the cycle: Nat.mod is Pair.snd of 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 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.

program instructions, main this cycles, main this this / main
textstats (koru kernel, unchanged source) 205.0M 193.0M -5.9% 31.1M / 31.5M / 32.8M 28.7M / 29.5M / 30.1M 0.907 to 0.961
isqrt, helper spelling (helper also called from outside) 234.2M 187.5M -19.9% 68.6M / 68.7M / 68.9M 59.7M / 59.8M / 60.1M 0.869 to 0.875
isqrt, helper spelling (entered at the head) 226.5M 198.5M -12.4% 71.1M / 71.2M / 72.3M 65.5M / 65.7M / 66.3M 0.908 to 0.931
primes, helper spelling 164.8M 134.3M -18.6% 35.8M / 36.3M / 36.9M 35.2M / 35.4M / 35.7M 0.959 to 0.990
isqrt, helper spelling, on #2173 + #2174 170.7M 129.1M -24.4% 63.2M / 63.3M / 63.5M 47.3M / 47.5M / 47.7M 0.745 to 0.754
primes, helper spelling, on #2173 + #2174 113.4M 77.3M -31.8% 25.0M / 25.2M / 25.4M 22.8M / 23.1M / 23.4M 0.898 to 0.928

The flag spellings on the same sweeps: isqrt 187.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); primes 112.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: textstats 205.15M to 193.14M instructions (-5.9%); 19 others within 0.2% (the worst primes, -0.19%, and wordfreq, +0.12%). Two run more instructions at the same cycles: josephus 92.1M to 112.1M (+21.7%; cycles 127.2M and 127.0M) and nestedloop 231.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 that Nat.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 in josephus). With spin_Nat_show kept 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):

program instructions, main this cycles, main (median) this this / main
U32.show of 1M numbers 468.1M 432.9M -7.5% 86.7M 86.7M 0.992 to 1.016
Nat.show of 1M numbers 547.4M 497.4M -9.1% 130.7M 130.5M 0.992 to 1.006
String.contains, starts_with, eq on 200-character strings 1,842.7M 1,682.4M -8.7% 338.2M 321.9M 0.928 to 0.987
U32.read, Nat.read, String.trim_start of one shared string 859.7M 844.7M -1.8% 136.1M 143.8M 1.032 to 1.068
U32.show and Nat.show in one statement 1,035.5M 967.9M -6.5% 155.4M 192.9M 1.235 to 1.248

The 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/runtime programs emit different C, because each prints its one number with U32.show, which is now a spin. Instructions retired, one thread, the least of 2 interleaved runs:

bench main this bench main this
bfs 28.746G 28.746G +0.002% nbody 39.787G 39.788G +0.002%
editdist 49.777G 49.777G -0.000% queens 89.815G 89.815G +0.000%
gameoflife 172.350G 172.350G +0.000% raytrace 82.952G 82.953G +0.000%
hashmap 18.496G 18.496G -0.001% symreg 50.766G 50.767G +0.001%
histogram 44.362G 44.362G -0.000% terrain 44.569G 44.568G -0.001%
kmeans 41.577G 41.577G +0.000% tree-bitonic 148.701G 148.702G +0.000%
lexer 53.407G 53.407G -0.000% tree-matmul 62.582G 62.582G -0.001%
mandelbrot 43.446G 43.446G -0.000% tree-radix 13.789G 13.774G -0.110%
merkle 47.320G 47.314G -0.013%

The worst is +0.002% (bfs, nbody), the best -0.11% (tree-radix); every output is equal. In cycles the single runs of that table moved editdist by -20% and nbody by +5.7%, which is placement: over 16 placements editdist is 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), and nbody 21,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.show and Nat.show. 81 defs are new spins, and the programs that change have 22,147 segments where they had 25,607.

  • Emitted C, the programs that change: tests -1.37% in bytes (71.77 MB to 70.79 MB), demos -3.25%, benches -0.41%, kernels -1.21%. The most a program shrinks is -13.4% (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.
  • No spin changes its size class in any program: every new spin is INLINE (under SPIN_FAR characters), none is FAR or DFAR.
  • Compile time: emitting the C of all 966 programs takes 271.4 s on main and 271.8 s here (five at a time, on a loaded machine). 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_head 64, the comment 50, the held call in emit_body 26, the count of names per pair of defs in file_book 16. 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):

this PR (bodies held by a head) one spin with entries
comp.ts +252 ttok +624 ttok, with no comment, no cached Array location in a cycle's spin and an untyped field on Seg
reaches, of the 15 cycles found 15 15
reaches beyond them cycles with a head every cycle without a call in an argument (also one where two defs each call themselves; none found)
emitted C the helper's body where its call was; no selector if (pc == 0) { .. } else if (pc == 1) { .. } in the loop, pc = 1; continue; at each call
primes, helper spelling 134.6M instructions 172.2M (main: 165.1M)
isqrt, helper spelling 187.8M 187.9M
textstats 193.3M 187.4M
emitted C bytes, the three programs 254,387 256,042

The selector costs nothing only where clang threads every pc = k; continue to its arm. It does on isqrt and textstats and does not on primes (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. (textstats running 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

  • Ownership. Borrow facts stay per def and per parameter; nothing is merged in them. A body held in the head's spin takes each argument as it is (owned, or rooted in one of the head's borrowed parameters), and the held call hands ownership over exactly as main's jump into the helper's segment did, so a value the cycle hands a helper to own is still the helper's to free (tail_cycle_owners has 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_demo and walkers_test: String.eq's second parameter is owned where main lent it. Post.drop reads name in String.eq(.., name) and passes it to its own recursion. On main String.eq was not flat, ran first and was lent name; now the recursion runs first, with a kept copy, and String.eq is 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 to String.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).
  • A helper with other callers. None exists in Base, the tests, the demos, the benches or the kernels. When one does (tail_cycle_shapes' col.if, tail_cycle_owners' wk.if and gt.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 to rev.if at the call site).
  • The size class of the merged spin. A spin under SPIN_FAR (8,192) characters is INLINE, 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 are INLINE.
  • Defs that fork or use !. A def with a fork or a ! call is not flat, so a cycle with one is compiled as on main (fk.a / fk.b in tail_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.
  • Closures over a def of the cycle. A def passed as a function, a partial application of either def, a closure that calls the cycle and a def of the cycle that answers a closure calling the other all go through the closure's own segment, which calls the spin (tail_cycle_callers).
  • @unsafe cycles 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_callers runs 20,000 frames in sixteen forked leaves, and that version dies on a pool thread's 512 KB stack).
  • Compile time and code size. Above: no measurable difference in emit time, 1.2% less C.

Checked

Against main (8c329c1), on one M4:

  • The native lane (every tests/**/*.bend with #| lines: build with -o bin, run with --gpu off at 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 -O1 with either compiler; its -O3 output is equal), the relay server.bend (it never ends), app_slash_boss_3d and walkers_demo (both builds exit at once with the same code, 45, in a session with no window to open), and walkers_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.js emits 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 through flat_of in a match table's rows, and file_book opens every term in main's order, so the probes and the order of a cycle's defs under $pc are 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.

  • tsc over bend2 and gates: the same 10 errors on main and here, none in the changed lines.

  • New tests, each on four lanes with both compilers (C at -O1 with poisoned frees, C at -O3 on 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.
    • How each def came out was read off the emitted C (the counts come from 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:

    wrong version caught by how
    a cycle with a call in an argument is merged callers dies on a pool thread's stack
    the other defs of the cycle may fork or call in an argument callers the same
    a def being asked about counts as flat (a def outside calls back into the cycle) callers the same
    only the asked def's callees must be flat callers the C does not compile
    no head needed shapes overflows the C stack at 3,000,000 turns
    flat, but each def calls the other's spin instead of holding its body all three the same
    every def is a head shapes, owners the compiler overflows its own stack (it holds a body inside itself)
    a def named twice in the cycle is still held shapes, owners the same
    a spin that is no head holds the others too shapes, owners the same
    the self-jump is told by the held def, not the spin's all three the same
    the held call lends as a plain call does owners a string is freed twice
    two arguments swap on the way into the held body all three wrong lines, or no end

    Two 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 textstats is ownership (#2148's ground): String.contains and String.starts_with own their strings before and after.

Not covered

Every way the behaviour can differ from main:

  • Order inside a statement. A call to a def that is not flat is cut out of its statement and runs first; a call to a flat def runs where it is written. The defs that become flat here (the cycles, and every def that was not flat only because it called one: 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:
    • Which failure is met first, in a statement where two calls both fail-stop (a Nat past 2^48-1, an absurd match) or one of them never returns (@unsafe): the outcome can be the other one's.
    • Cycles, through the allocator. 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 two lets (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 by U32.read and by Nat.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 with lets recovers half of it (+3.0%). Not traced further.
  • Unfolding reaches the cycles. The emitter unfolds a call to a flat def when a literal argument decides a match (up to FOLD_FUEL), so it now does for a cycle's defs: even(10n) in an argument is computed by the compiler, and rev(U32, n, [1, 2, 3, 4, 5], []) loses its first turn (the call site names rev.if with the rest of the list). An unfolded call does not evaluate an argument its arm never reads, where the JS lane and bend file.bend do (The native lane skips an argument that bend file.bend and 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 that bend file.bend and 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.
  • Frames held. A call into a cycle from an expression pushed a frame on the heap stack and the cycle's turns were jumps between segments; now the call is a C call (inlined or not by clang) and the turns are a C loop. The C stack grows by one frame per call into a spin that clang leaves out of line, and by at most one more per def of the cycle for an entry at a def that is no head; no spin of a cycle calls itself, and a cycle with any call to its own defs in an argument is left on the heap stack.
  • Tasks. With seq clear, 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.
  • Other spellings of the same thing stay as they were: a cycle where no def is a head. That is one where two defs are each named twice (f calls f.if from two arms and f.if calls f from 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.
  • clang. Two kernels run more instructions at equal cycles because clang inlines the print into the function that holds their loop (above). The suite's 17 are within 0.11%.

🤖 Generated with Claude Code

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