Repository navigation
A call whose arguments a recursion never changes is computed once, in a copy of the def that carries the value - #2180
Closed
nicolas-abril wants to merge 1 commit into
Closed
nicolas-abril wants to merge 1 commit into
nicolas-abril wants to merge 1 commit into
Conversation
A def that calls itself may make, on every turn, a call that reads only parameters each self call passes back as they are (binarytrees' go builds and checks the same tree 1,200 times). def_fix, run at the end of file_book on the checked terms, takes the largest such call that yields words and runs a def that loops or is not flat: the arm that holds it binds it at the head of its statement and goes on in k~fix, a copy of the def that takes the value as a last parameter and reads it where the call was. The def keeps its body, so a loop that never reaches that arm never makes the call, and one that reaches it makes it there. Not taken: a call under a lambda, in a partial application, in an unused let or an erased argument, inside a call the emitter unfolds, with a bang or a foreign def in it; a result that is a box; and, in an @unsafe def, a call that reads a parameter holding an Array or anything but datatypes (a write through one handle shows through the others). binarytrees (koru osprey kernel), one thread: 1,639M instructions and 71.9 ms to 13.6M and 1.2 ms. Of 980 programs under bench/runtime, demos, tests and the koru kernels, 6 emit different C: the two binarytrees ports and four tests. comp.ts is 68,310 ttok (cap 66,000). Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 16, 2026
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 22, 2026
… non-tail call that does not read it Conflict in comp.ts, emit_body's choice of the next form (bendlang#2217 against bendlang#2162): bendlang#2162 tries emit_deep on a statement's call before anf, bendlang#2217 tries let_sink on a let first. They apply to disjoint heads (emit_deep only to a call, let_sink only to a let), so both stay: let_sink first, then bendlang#2162's emit_deep-or-anf as before. let_sink only reorders total U32 lets past a frame-pushing call; acc_def (bendlang#2175), def_fix (bendlang#2180) and bendlang#2177's call-site returns rewrite defs before emission and see the lets as written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 22, 2026
mix_sink_calls: bendlang#2217's U32 lets that sink below a non-tail call, in a tree recursion whose returning arms run at the call (bendlang#2177), in sum loops of two and three self calls and one whose arm answers 0 (bendlang#2175), next to a fixed call (bendlang#2180), a let the call reads (stays), and a parallel call. mix_reform_slots: trees in an Array's slots (bendlang#2135: read borrowed, taken and written back) walked by a selector-first lookup and insert (bendlang#2211), split by colour and re-formed (bendlang#2207), lent to a flat def, with the node's U32, Bool and colour in one word (bendlang#2218), in @unsafe helper cycles. Both print main's numbers on C at 1 and 8 threads and on JS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 22, 2026
Collaborator
Author
|
Moved to nicolas-abril#21, which keeps it saved off the main queue until it can be accepted. |
This was referenced Nov 27, 2026
Nat bounds: drop 2^48 limit and zero-divisor tests that static bounds rule out
nicolas-abril/bend#48
Merged
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 68,310 ttok, 2,310 over the 66,000 cap (main is 65,981), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. The branch is on mainb8a88499, one commit behind8c329c1b(#2178), with whichcomp.tsmerges without conflict. Independent of every other open draft; it shareslt_any,FL.madeand thetld_ofline with #2031, word for word, so whichever merges second drops its copy.Reach, said first: this was found on a benchmark built to reward it, and it fires on very little else. Of the 980 programs under
bench/runtime,demos,testsand the 39 Bend ports of the koru osprey kernels that build, 6 emit different C (8 sites): the twobinarytreesports and four tests. No bench of the suite, no demo and no def of Base changes.What it does
A def that calls itself may make, on every turn, a call that reads nothing the turns change:
check(make(13n))builds and walks the same tree 1,200 times. The def now computes it once: the arm that holds the call binds it and goes on ingo~fix, a copy of the def that takes the value as a last parameter and reads it where the call was.The rule (
def_fix, run at the end offile_bookon each def that calls itself, on the checked terms, so both the C and the JS lane take it):letare not fixed.check(make(13n)), notmake(13n)). A call to a flat def without a loop (Bool.not(s),half(n), a constant def) is left: 57 such sites in the corpus, 44 of them a float-literal helper or a constant def.Bool, a pair of numbers). A result that is a box is left where it is; see Not covered for the measurement.gokeeps its body. Only an arm that holds the call binds it, at the head of the statement that holds it, and only self calls of that statement and later ones go to the copy. So a loop that turns zero times, or never reaches that arm, never makes the call; a call that stops the program or does not return does so in the arm that reached it.let, in an erased argument, or inside a call the emitter unfolds at compile time (emit_fold: the unfolded body may drop the argument).@unsafedef a+parameter may hold an Array, and a write through one handle shows through the others; so there a parameter whose type holds an Array, or anything but datatypes (a closure, a type variable), is never fixed. In a checked def a+parameter isData, which holds neither.!, a foreign def, a lambda or aletinside it, or a call to the def itself.Where the line with compile-time folding is: a call the emitter can unfold within
FOLD_FUEL(a flat def on constants,tri(3n, 0n)) is unfolded as before and never taken; one it cannot (check(make(13n)):makeis not flat) is computed once at run time.Results
One M4, one thread, GPU off; instructions retired and cycles from
/usr/bin/time -l, the least of 9 interleaved runs; wall time fromhyperfine -N, 30 runs.binarytreesis the kernel ofkorulang/koru-benchmarks(suites/osprey-compute-kernels/bend), not added to this repo.binarytreescc -O2)check(make(d)))check<make(13n))A Bend program that prints a constant runs 12.2M instructions, so what is left is start-up. The forked port (
bend-par) goes from 165.5 ms to 1.6 ms at the default thread count and from 70.7 ms to 1.3 ms at one thread.A recursion that is no tail call, as a scratch program: a walk of 2^18 nodes that passes
kdown and addstri(k, 0n)at every node runs 286.3M instructions on main and 24.2M here withk= 200; withk= 0, where the call costs nothing, 24.2M on both, so carrying the word is free there.Every other program whose C changes, same method (7 runs):
tests/rfc/share_looptests/reg/array_half_looptests/reg/array_node_looptests/compile/literal_reads(a loop of two turns)Those four tests are loops written around exactly this shape as regression witnesses (a tree shared per turn, an Array built and dropped per turn). They print their lines, but they now make the call once, so what they were written to exercise on every turn runs on the first turn only. They are left as they are here; keeping their point needs the call to read something the turn changes.
The bench suite: all 17
bench/runtimeprograms emit byte-identical C, so nothing was measured there. Demos: the 18 that build emit identical C. Base: rooting all 299 defs of a file that only imports Base (65 call themselves; templates come in through the tests that instantiate them) finds no site.Compile time: no difference above the noise. Timed inside the compiler, the pass takes 0.8 to 1.4 ms of
compile_book's 26 to 54 ms onlexer,terrain,merkleandsymreg, 4.7 ms of 260 ms onpure_hvm5_miniand 6.2 ms of 576 ms onapp_slash_boss_3d(about 1% of a wholebend file.bend -o file.c, checker included).hyperfineon that command, 7 runs each: the median over the 17 benches moves by +0.7% (from -7.3% to +4.8%, which is the spread of the runs),app_slash_boss_3d677.6 ms to 680.4 ms,pure_hvm5_mini337.0 ms to 343.6 ms; emitting C for all 904 tests with#|lines, one after the other, took 130.8 s on main and 131.0 s and 132.8 s here.Emitted C grows by 0.4% to 1.2% in the six programs (the def's body is emitted twice).
Checks
All local, against main
b8a88499:#|lines): 883 pass on main and 884 here (the new test); no status of an existing test differs. It ran before two edits to the pass that change no emitted C (all 980 programs emit byte-identical C before and after them); the two new tests were run again afterwards, on both lanes.bend file.bendand as a built.js.binarytreesports, built with-DBEND_POISONand a count of live heap words at exit: the same output, exit code and count as main.!calls; a second entry through another def with another value; closures (a call under a lambda never applied, a self call under a lambda); a partial application; an unusedletand an unfolded call that drops its argument; boxed and Array parameters; an Array an@unsafeloop writes through one handle and reads through the one it passes back; results of two words and of aBool; aU32loop with a default arm; a def that opens a lambda past its parameters; erased type parameters with function types as type arguments; an IO loop; a!fork tree.@unsafedefs (prints 370 where main prints 750); binding at the head of the arm instead of the statement.!call. Two scratch programs do (a loop that calls itself with a!, and a!fork tree whose leaves run a loop the pass fires on): both print the same with--gpu onand--gpu off, on main and here; the tree's value was computed apart in Python.tests/run/loop_fixed_call.bend, 14 shapes whose numbers were computed apart in Python, every count scaled by the command line's length so nothing folds; the pass fires on 10 of its defs and must leave 5 alone.tests/run/loop_fixed_call_stop.bend: one turn whose arm first binds a call past the Nat wall and then holds a fixed call that never returns stops with the Nat message. Both print their lines on the C lane and underbend file.bend, on main and here. Of the ten wrong versions, the first test fails eight and the second the one that binds at the head of the arm; the tenth (no unfold test) cannot be pinned by a repo test, because main's own lanes disagree on its case: the C lane unfoldspick(boom(k), 0n, acc)and drops the argument, the JS lane evaluates it and stops.tscreports the same 3 errors incomp.tsas on main.Not covered
Every way the order of evaluation or the sharing can differ from main:
Nat.add(boom(i), hang(k))stops with the Nat message on main and never returns here. Statements before it (earlier lets) still run first.Left out:
facts_hot, not traced: a shared value forces its type hot for the whole program. Not clearly a gain, so left out; taking them only where the type is already shared would need the emitter's facts.letbefore the statement that holds the call keeps calling the def itself: only later calls can take the value.@unsafedefs: parameters whose type holds an Array, a closure or a type variable.Cost
+2,329 ttok. Each def the pass fires on is emitted twice.
🤖 Generated with Claude Code