Repository navigation
The JS lane loops tail calls, mutual cycles included, and calls plain defs directly - #1043
Closed
MattCozendey wants to merge 6 commits into
Closed
MattCozendey wants to merge 6 commits into
MattCozendey wants to merge 6 commits into
Conversation
Closed
MattCozendey
force-pushed
the
feature/js-intrinsics-and-trampoline
branch
from
September 24, 2026 20:59
45e34d9 to
85d4268
Compare
Collaborator
|
I don't like that we add all that global state for things only the JS compilation needs, since it's not really the main concern of Bend right now. I think it can be further simplified to remove them. I'm trying to work on it locally, you can give it a try as well if you want. |
JS-only OPERATIONS entries for bool_and, bool_not, bool_pick, u32_min, u32_max, f32_min, f32_max, nat_min and nat_max: each mirrors its base.bend body exactly (pick c a b = c ? a : b; min = a < b ? a : b; max = a < b ? b : a, so F32's NaN answers follow is_lt, and Nat's Succ-peeling recursion becomes one BigInt compare). Before, each call cost a run_jump trampoline into the def and, for min/max, a second one into Bool.pick; now the ternary emits in place. The C lane is untouched: intr_of without js sees no JS-only entry, so fuse, spins and the borrow inference read the same book as before. Verified through WSL on the JS lane: a scratch program exercising every entry through variable and constant arguments prints the same value interpreted and emitted, and 14 tests (num_kit, nat_ops, list_ops, list_fold, list_kit, string_ops, text_ops, float_roundtrip, u32_divmod, rand, tuple_suite, bool_algebra, fold, readback_sugars) pass run and emitted-JS alike. The cluster gates did not run (no ssh from this machine). Co-Authored-By: Claude Fable 5 <[email protected]>
A def is plain when its emitted JS returns no $JMP: a self call or a closure applied in tail position rides the trampoline, so its callers must run_loop; everything else returns through the machine stack, whose depth the def DAG bounds, since defs never call forward. plain_of reads that off the body with term_any's tail flag (a closure body counts as tail, which only errs toward the trampoline). js_call then calls a plain callee directly, tail or not, so the JIT can inline it; a self tail call keeps run_jump, and a tail call to a trampolined callee absorbs its $JMPs with run_loop in place, one bounded frame. The C lane and the device read none of this. Verified through WSL on the JS lane: 73 tests across base, eval and io pass run and emitted-JS alike (three tests/run fails are the @unsafe stderr note the local runner merges into stdout; they fail identically on the parent commit), and a 10M-iteration loop over a three-def chain runs 1.14-1.42 s before, 0.45-0.54 s after, same output and RSS. The cluster gates did not run (no ssh from this machine). Co-Authored-By: Claude Fable 5 <[email protected]>
…d off the same Src walk as C's flat On top of bendlang#974. A def's tail self call is a loop (for (;;) with per-turn const bindings, so a closure keeps its turn's values), as the C lane's flat helpers loop with WL_AGAIN. carb_book's one walk records each def's tail callees in its Src beside deps; plain_of reads them with the same in-progress false guard as flat_of, so a def is plain when every def it tail-calls is: a closure has no source and a call cycle (unsafe defs call forward) is not plain. A plain callee is called directly, every other one keeps main's run_jump in tail position and run_loop elsewhere, so a 10^6-deep mutual tail recursion runs on the JS lane again. The PLAINS map, plain_of's own walk and js_call's foreign branch go; the min/max selectors are one sel_ops row per type and bool_and joins tpl_ops. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
From bendlang#974 (149bce0) and feature/js-cga: Nat/U32/F32/Bool selectors across lanes, unsafe tail cycles (plain and law), mutual tail jumps across arities, closures captured in a loop, and a tail call through a closure. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A tail-call cycle between defs (Tarjan over the Src tails carb_book records) runs as one loop: `$f$loop($pc, $0, ...)` switches on `$pc`, a tail call into the cycle sets `$i` and `$pc` and continues, and each member keeps an entry that enters the loop at its own case. A self loop is the cycle of one, without the switch. Each case binds its parameters afresh, so a closure keeps its own turn's values, and a closure body never continues the enclosing loop. plain_of reads a cycle as one def: it is plain when every def the cycle tail-calls outside itself is, so callers call it directly. An intrinsic callee is no longer recorded as a tail callee, so a def ending in one (U32.add, Nat.min, ...) is plain rather than trampolined. Co-Authored-By: Claude Opus 5.5 <[email protected]>
MattCozendey
force-pushed
the
feature/js-intrinsics-and-trampoline
branch
from
September 25, 2026 12:39
85d4268 to
8565421
Compare
Collaborator
|
Follow-up in #1061: it builds on this PR's commits unchanged and adds one commit on top. The tail loops stay; the new |
Contributor
Author
|
closed in favour of #1061 |
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Sep 25, 2026
…ess, no new Src or File fields, and only Nat.min and Nat.max become intrinsics On top of bendlang#1043, which carries bendlang#1040 and bendlang#974. The same tail loops, with less machinery: - One Tarjan pass over each def's tail callees, self calls included (tail_loops), fills a JS-lane LOOPS map. Src and File are main's again: carb_book records no tails, and plain_of, Src's plain and loop and File's loop go. The loop being emitted is read off fl.seg.def, as C's emit_jump spots a self jump; a closure's segment has no def, so its tail calls never continue the loop. A self loop is the group of one, so js_def's second walk for it goes. - Every tail call into a def is a direct JS call. Tail cycles loop, so a direct tail call can reach its caller again only through a closure, whose run_tail still bounces on the trampoline: run_jump and the plainness fixpoint go. A non-tail call keeps run_loop. - Only nat_min and nat_max stay intrinsics, on both lanes: Base's are unary and recurse min(a, b) deep. The other selectors are cheap direct calls now, and their C side measured nothing (clang inlines them), so they go with tests/base/selector_intrinsics; tests/run/ unsafe_law_tail_cycle repeated unsafe_tail_cycle. JS lane under bun, bendlang#1043 -> this, ms: cycle3 175 -> 176, mutual2 282 -> 282, listacc 148 -> 149, wrapper 137 -> 140, fib 152 -> 157 (a non-tail call keeps run_loop), bounce 263 -> 118 (a tail call into a def that bounces a closure skips the trampoline); node shows the same. bench/runtime/hashmap 5303 -> 5296. Verified: every runnable test, demo and runtime bench through the JS lane under bun prints what bendlang#1043 prints (the removed tests aside); emitted C equals main's but for tests/base/nat_ops and num_kit (Nat.min and Nat.max), which pass; 10^6-step CPS, def/closure ping-pong, List.foldl and IO loops run; gates/repo.ts 46/46 (comp.ts 63446 ttok); tsc reports its 4 existing errors, none in comp.ts. The cluster gates did not run. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
that referenced
this pull request
Sep 25, 2026
A def on a tail cycle, or tail-calling itself, is one JS loop: loop_of finds its cycle (Tarjan over tail callees, cached in LOOPS with the other per-book caches), and js_def emits `let $pc = <its case>; for (;;) switch ($pc)` with each turn's parameters bound afresh, so a closure keeps its own. A tail call into the loop being emitted (fl.seg.def names it, as in C) sets $i and $pc and continues. Every other tail call into a def is a direct JS call: tail cycles loop, so it can reach its caller again only through a closure, whose run_tail still bounces; run_jump goes. Nat.min and Nat.max become one tpl_ops row on both lanes (Base's are unary and recurse min(a, b) deep). Src and File are unchanged. JS lane under bun against the trampoline: a three-def tail cycle 841 -> 176 ms, two-def mutual recursion 895 -> 282, a closure bounce 265 -> 118, fib 316 -> 157. comp.ts +627 ttok. Tests: nat_min_max, js_group_args, js_loop_capture, js_tail_closure, unsafe_tail_cycle. Builds on #974 and #1043 by Mathews Fernando; supersedes #1040 and #1043. The cluster gates did not run. Co-authored-by: Mathews Fernando <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.
Builds on #1040 by @nicolas-abril, which builds on #974. It contains #1040's four commits unchanged, plus two commits. It supersedes #974 and #1040.
What the two commits add
tests/base/nat_min_max.bendandtests/base/selector_intrinsics.bend: the Nat, U32, F32 and Bool selectors across lanes, with NaN, ±0, the U32 extremes and Nats near 10^12;tests/run/unsafe_tail_cycle.bendandtests/run/unsafe_law_tail_cycle.bend: the 10^6-deep mutual tail recursion from the review;tests/run/js_group_args.bend: mutual tail jumps across different arities and entry points;tests/run/js_loop_capture.bend: each closure made in a loop keeps its own turn's value;tests/run/js_tail_closure.bend: a tail call through a closure does not continue the enclosing loop.tailssets thatcarb_bookalready records finds the cycles. It adds no second walk over the terms.$f$loop($pc, $0, …)withfor (;;) switch ($pc). A tail call into the cycle sets$iand$pcand continues. Each member keeps a one-line entry function that enters the loop at its case. A self loop is the cycle of one, without the switch.plain_ofreads a cycle as one def, so callers still call it directly.U32.add,Nat.min, …) is no longer recorded as a tail callee. Before, a def that ended in one was not plain, so its callers used the trampoline.Measurements
JS lane, node on WSL2, one core, medians of three with the builds alternating. Outputs are identical and peak memory is the same on every case.
bench/runtime/hashmapVerification
tests/run/unsafe_forward.bendprint their expected lines on the interpreter, JS and C.tests/runandtests/base: the same results as The JS lane loops a tail self call and calls plain defs directly (on top of #974) #1040 (104 pass; the other 15 differ from a simple local runner in the same way on both, for example an expectedexit 1line).tscreports only the four existing errors, none incomp.ts.Not done yet
gates/repo.tswas not run (ttokis not installed here).comp.tsgrows by 91 lines and shrinks by 22 over The JS lane loops a tail self call and calls plain defs directly (on top of #974) #1040.🤖 Generated with Claude Code