Repository navigation
A let of U32 operations moves below a non-tail call that does not read it - #2217
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
…ot read it A let whose value is U32 operations (total: division and modulo by zero are defined) moves below the next call that pushes a frame, when the call does not read it and every variable it reads is read after the call. The frame then keeps those operands instead of the result, and the continuation recomputes it. queens' solve pushes 9 words a frame on main and 7 here (b2 and n2 are recomputed from sib and cols). let_sink runs in emit_body before anf, on the checked terms, so the C, Metal and JS lanes all take it. A let that aborts or calls a def is never moved (tests/run/let_sink_abort.bend). 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
… 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
Draft
Collaborator
Author
|
Moved to nicolas-abril#25, which keeps it saved off the main queue until it can be accepted. |
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,427 ttok, 427 over the 66,000 cap (main 65,995), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. One commit (90509f4d).What it does
A
letwhose value is U32 operations moves below the next call that pushes a frame, when the call does not read it and every variable it reads is read after the call anyway. The frame keeps those operands instead of the result, and the continuation computes it again.queens'solvebinds the next sibling's bit and columns before its child call and reads them only in the sibling call after it:On main every child call stores
g, sib, cols, ld, rd, full, b2, n2and the continuation's id (9 words) and reads 8 back;sibandcolsare in the frame anyway, sob2andn2are two stores and two loads that three ALU operations replace. Here the frame is 7 words.The rule
let_sink(x, ty), called byemit_bodybeforeanf, so the C, Metal and JS lanes all take it:U32.*with a C template; all are total, division and modulo by zero included) over variables and constants;letis left alone);x then moves to just after the call. In a chain each let moves as far as it can: above,
cc,cb,cnstay andb2,n2move.Why it is sound: a U32 operation cannot stop the program, and values do not change, so computing it after the call gives the same word; the call does not read it; its operands are the same variables, still bound after the call since they are read there. A let that calls a def or does
Natarithmetic (which can stop at 2^48) never moves:tests/run/let_sink_abort.bendhas a let that must stop the program before a call that never returns.Reach
Of 1,940 programs (17 benches, 18 demos, the tests, 22 koru kernels, 7 Perceus ports), 975 emit C on both; 3 emit different C:
queens,tests/reg/wnf_memo_bound_key,tests/run/expr_interpreter. No demo, kernel or Perceus port changes. A variant that also moves a let past later independent lets reaches nothing more among the benches, demos, kernels and ports, so it is not in.Measured
queens, one thread, GPU off, M4, quiet machine; cycles over 16 placements x 2 rounds, least / median / most:opt/integration(the 14 drafts)opt/integration+ this changeOn main this saves instructions and no cycles (the median is 0.7% higher, the ranges overlap): main's
solvestill sets up a C frame and testsseqon every entry, and the two words do not show. Onopt/integration, where #2176 and #2177 have removed that, it is -1.9% at every placement. The 16 other benches emit the same C as main, so their instructions are unchanged.Checks
-DBEND_POISON, live heap words counted at exit) gives 0 live words at 1 and at 8 threads, as main;queensat full size on C at 1 and 8 threads and on Metal (full and small sizes);Not run: the cluster gates, CUDA, Linux, x86,
tsc.Context
From a study of
queens' gap to its C twin (1.46x in cycles on main, 1.22x onopt/integration): a ladder of C twins taking Bend's constraints one at a time, frame counters, hand edits and a profile-guided bound. Of the causes the compiler can remove by itself, recomputing these frame words is the one over 5% of instructions onopt/integrationthat applies to main; the other, an answered call that pushes no frame (-13% instructions, -3.3% cycles on #2177), needs #2177 and is onopt/queens-earlyof nicolas-abril/bend. Tools and numbers:bench/pgo-queensthere.🤖 Generated with Claude Code