Repository navigation
perf(comp): a statement's call unfolds into a def that is not flat - #2162
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
emit_unfold took a call whose constant arguments decide its matches, but only into a flat def: its residue is an expression. A call in statement position (a tail call, or the one a let binds, where ANF puts every call into a def that is not flat) now unfolds into such a def too. Its residue is a statement, so ANF cuts the calls it makes and they unfold where they stand. There, and only there: a flat argument folds to its end; an op on constants is its constant (a U32 or a Bool, by the op's JS, as a match table's cells are); a def with no match of its own unfolds if its body's tail call does, and a residue's tail call unfolds in place; an argument that calls a def not flat is bound first, so no later one is held across it, and is typed by the arguments before it. A call with no constant argument is not tried. A residue that forks or bangs is refused, and so is one that keeps a call leading back to the def unfolded (the constants must carry a recursion to its end, or it would only be peeled). A try that fails gives its fuel back, and a def whose unfold ran out of fuel is not tried again in that unit. lexer's gen walks a literal template: its frames, its returns and its slot dispatch are gone from a line (53.41G to 39.78G instructions, 8.44G to 6.51G cycles). symreg's gen(5n, h) builds its candidate without a frame. The other 15 benches emit the same C. tests/run/unfold_statements.bend and tests/run/unfold_generic.bend. Co-Authored-By: Claude Fable 5.2 <[email protected]>
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]>
This was referenced Nov 22, 2026
Collaborator
Author
|
Moved to nicolas-abril#17, which keeps it saved off the main queue until it can be accepted. |
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 24, 2026
, bendlang#2224-bendlang#2226, bendlang#2231, bendlang#2285, bendlang#2286, bendlang#2288 and others) One conflict, in emit_unfold: bendlang#2288 annotates an unfolded call's lets with the argument types instantiated by the earlier arguments; bendlang#2162's deep unfold keeps its arguments unannotated there and annotates them the same way after folding (its type walk renamed D, beside bendlang#2288's T). Both kept. Co-Authored-By: Claude Opus 5.5 <[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.
comp.tsis 67,137 ttok, 1,137 over the 66,000 cap (main is 65,966; this adds 1,171), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run.What it buys:
lexerruns 25.5% fewer instructions and 22.9% fewer cycles (its gap to the C twin goes from 1.96x to 1.52x),symreg0.8% fewer instructions; the other 15 benches emit byte-identical C.Independent of the other open drafts: it branches from main
f0cf0a7c. It was found while breaking down whatlexerloses to its C twin: more than half of that gap is the line generator, a recursion over a template that is a literal.What it does
emit_unfoldalready unfolds a call whose constant arguments decide its matches, but only into a flat def, because its residue is emitted as an expression.lexer'sgenwalks a literal template with a call that is not a tail call, so it is not flat: every line paid 39 frames, 39 returns through the segment table, and a comparison chain per template character to pick its slot, all on constants.Now a call in statement position (a tail call, or the call a
letbinds, which is where ANF puts every call into a def that is not flat) unfolds into such a def too. Its residue is a statement, so ANF cuts the calls it makes, and they unfold in turn where they stand. Four things make the unfold go through there, all behind the one flag, so an expression's fold is what it was:slot(105)isId{}by the timeexpandmatches on it.U32or aBool, by the op's JS, as a match table's cells already are):U32.is_eq(105, 105)isTrue{}, and the template positionk + 1is a literal.gen.at(105, t, rest)isexpand(slot(105), 105, t, rest), which isident(n, t, rest).When it does not unfold:
FOLD_FUEL, as for the flat unfold: a unit for every term of every body unfolded, all of it or none. A try that fails gives its fuel back, and a def whose unfold ran out is not tried again in that unit.U32.show's digit loop, with its literal fuel, would be, in every program).For
lexerthe whole ofgen,gen.at,expand,slotandslot.gois gone from a line: its literal characters areSConnodes built in place (the tail" ;"is static), and the seven identifiers and numbers are seven calls with one word, the line's seed, in each frame. It takes 5,451 of the 8,192 fuel, so a template half as long again would stay a call.symreg'sgen(5n, h)builds its 63-node candidate without a frame.Elsewhere it is mostly
"literal" ++ x: Base'sString.appendis not flat, and now a literal in front is consed on directly.Results
One M4, GPU off, instructions retired and cycles from
/usr/bin/time -l, the least of five interleaved runs.lexerThe per-line rows are from builds that generate 64 lines, then lex them, with a counter read between (so their cycles are a little above a plain run's).
The gain was checked against placement, since any change to the C moves the code: both builds at 16 placements (the segments shifted in 4-byte steps), three alternating rounds, the least per placement.
No overlap; -22.9% on the median.
Bench suite. 15 of the 17
bench/runtimeprograms emit byte-identical C. The other two:lexersymregSize and compile time. Over the 883 tests that pass on main, the 17 benches and two more programs, 464 emit different C and 438 the same; the C is 0.02% larger in all (from 0.26x on
reg/show_shared_layoutto 2.16x onbase/list_sort), and compiling them all takes 6% longer. Of the 15 demos with amain.bend, 8 emit the same C and 7 change:app_slash_boss_3d2.92 MB to 3.07 MB of C (+5.1%) and 0.65 s to 0.94 s to emit it;pure_hvm5_mini+2.3% and 0.33 s to 0.42 s;io_http_server+4.0%; the other four shrink by 1% to 3%.Checks
All local, against main
f0cf0a7c:-DBEND_POISONand a count of live heap words at exit, and run at one thread: the same output, the same exit code and the same count as main on every one; then at 8 threads: the same output.tests/run/unfold_statements.bend: a fold over a constant list whose step is not flat; an arm that drops a constant and frees a value; a value the caller keeps, shared by two arguments of a def that walks it, handed to an arm that drops it, and again at its last use; a def with no match that unfolds through its callee, with its character's class decided by ops on constants; a def that forks (it must stay a call); a count too long for the fuel and a call tree that is; Base'sString.appendandList.appendon literals; an unboxed pair out of a def that is not flat; and a template expanded aslexer's is. Its inputs come from a list built at run time, so C folds none of it.tests/run/unfold_generic.bend: a generic def unfolded over a literal list whose other argument is computed, bound and shared, with boxed elements, called from a def whose own type parameter has the same name and place. Both pass on the C lane at 1 and 8 threads, on Metal, in JS, and with poisoned frees and no live word at exit. In the emitted C the segments offold,step,pick,both,wrap,reads,two,gen,glue,twiceandList.appendare gone, and those oftsum,countandfibare not.Bool, or to aU32one too high (the first test andlexerprint wrong); a use the unfold adds not counted (the compiler stops); a residue that forks unfolded into a!def (Metal: stack overflow, the def now runs on the device). Found on the way and fixed: a bare reference to an op (no arguments) folded to a constant, which stopped the compiler ontests/reg/array_clone_runs.bend.bind_deadrecounts the binding at the next statement, so nothing leaks); the argument that calls bound last (slower, not wrong); an argument bound with the callee's own type names, as the flat unfold does (no program I wrote makes it matter: a call nearby marks the same types shared).proof_insertion_sort,proof_numerics,proof_typed_eval,pure_hvm5_mini) print what they print on main.lexer(2.51 s to 1.48 s),symregand the two new tests print the same results with--gpu on.fib-shaped and million-deep recursions on literals, alone and as arguments: same output, same compile time, no unfold.repo.tsfails only on the ttok cap.Not covered
tschave not run.lexer's pin would move.base/list_sort, literal lists sorted). The fuel caps it for a unit, not for a program.lexer's template takes two thirds of it; a longer one unfolds nothing.both(o, w) = kl_sum(w, step(o, w))stays a call for a constanto.Nator anF32is not folded, soident(U32.to_nat(3), ...)is not unrolled.emit_body.++was not measured.🤖 Generated with Claude Code