Skip to content

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
bendlang:mainfrom
nicolas-abril:opt/queens
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/queens

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 66,427 ttok, 427 over the 66,000 cap (main 65,995), 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. One commit (90509f4d).

What it does

A let whose 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' solve binds the next sibling's bit and columns before its child call and reads them only in the sibling call after it:

case 1n+g False{} False{} Stat0{}:
  +cc = U32.and(full, U32.not(U32.or(nc, U32.or(nl, nr))))   # the child call reads cc, cb, cn
  +cb = U32.and(cc, U32.sub(0, cc))
  +cn = U32.or(nc, cb)
  +b2 = U32.and(sib, U32.sub(0, sib))                        # only the sibling call reads b2, n2
  +n2 = U32.or(cols, b2)
  solve(g, U32.is_zero(sib), U32.is_eq(n2, full), U32.sub(sib, b2), n2, .., cols, ld, rd, full,
    solve(g, U32.is_zero(cc), U32.is_eq(cn, full), .., nc, nl, nr, full, Stat0{}, sols, U32.inc(nodes)), 0, 0)

On main every child call stores g, sib, cols, ld, rd, full, b2, n2 and the continuation's id (9 words) and reads 8 back; sib and cols are in the frame anyway, so b2 and n2 are two stores and two loads that three ALU operations replace. Here the frame is 7 words.

The rule

let_sink(x, ty), called by emit_body before anf, so the C, Metal and JS lanes all take it:

  • x binds one name to a tree of U32 operations (U32.* with a C template; all are total, division and modulo by zero included) over variables and constants;
  • the lets after x, if any, are such lets too, and what follows them is a call that pushes a frame (not flat; one binding, so a parallel let is left alone);
  • the call does not read x, and every variable x reads is read after the call.

x then moves to just after the call. In a chain each let moves as far as it can: above, cc, cb, cn stay and b2, n2 move.

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 Nat arithmetic (which can stop at 2^48) never moves: tests/run/let_sink_abort.bend has 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:

build instructions cycles
main 89.82G 21.81 / 22.12 / 22.29G
this PR 87.57G (-2.5%) 22.12 / 22.28 / 22.37G
opt/integration (the 14 drafts) 71.33G 18.50 / 18.56 / 18.71G
opt/integration + this change 66.81G (-6.3%) 18.10 / 18.21 / 18.39G (-1.9%)

On main this saves instructions and no cycles (the median is 0.7% higher, the ranges overlap): main's solve still sets up a C frame and tests seq on every entry, and the two words do not show. On opt/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

  • the 3 programs whose C changes and the two new tests: C at default threads, JS and the interpreter print the same as main; the memory check (-DBEND_POISON, live heap words counted at exit) gives 0 live words at 1 and at 8 threads, as main; queens at full size on C at 1 and 8 threads and on Metal (full and small sizes);
  • every other test emits main's C, so its native-lane status is main's;
  • 7 wrong versions of the pass: the 4 that change results (moving a let the call reads, dropping the let, putting it inside the call's argument, moving a let that calls a def) are each caught by the new tests; the other 3 only change which lets move (past a parallel let, any intrinsic, without the liveness test).

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 on opt/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 on opt/integration that applies to main; the other, an answered call that pushes no frame (-13% instructions, -3.3% cycles on #2177), needs #2177 and is on opt/queens-early of nicolas-abril/bend. Tools and numbers: bench/pgo-queens there.

🤖 Generated with Claude Code

…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]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#25, which keeps it saved off the main queue until it can be accepted.

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