Repository navigation
Remove the Metal lane spill (#1439): bend -o of a ! program built 17% slower - #1809
Merged
Merged
Conversation
… 17% slower #1439 moved a full lane stack to the heap, so non-tail recursion under `!` no longer faulted at 1023 frames on Metal (#1393, #1394). Its lane_spill joins the device code every `!` program compiles, and made the Metal GPU program build (`./main +RTS --gpu-build`, part of `bend -o`) 17% slower, 0.257s -> 0.300s on an M4 mini; the perf gate's COMPILER column went past 1.15x (symreg 1.16x, merkle 1.17x, two gate runs out of two). Bisected over v2.0.37..main on one mini (median of 7 `bend main.bend -o main`): symreg 0.747s at 10256a7, 0.793s at 40402b4. This removes only the spill: WL_ROOM loses its depth argument and its lane_spill call, lane_spill goes, the work loop no longer unspills at FID_EXIT, and the spill test goes. #1439's other changes (the token cuts in comp.ts) stay. #1393 and #1394 are open again until the spill returns in a form whose cost only a program that needs it pays. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
bend -o of a ! program 17% slower to buildbend -o of a ! program built 17% slower
Lorenzobattistela
force-pushed
the
revert-1439-lane-spill
branch
3 times, most recently
from
October 21, 2026 13:26
8b9c457 to
805f7b6
Compare
wakamex
added a commit
to wakamex/bend
that referenced
this pull request
Oct 22, 2026
…on-tail recursion under ! no longer faults at 1023 frames (bendlang#1393, bendlang#1394) Today on main (de26a91, after bendlang#1809 reverted bendlang#1439), emit_fork emits WL_ROOM(N) before pushing each sequential continuation frame, and WL_ROOM(N) posts ERR_DEEP and returns 0 on DEVICE once a lane's 2048-word stack fills, faulting at 1023 frames of List.append (bendlang#1393) or on an unbalanced fork tree (bendlang#1394). bendlang#1439 (40402b4) replaced WL_ROOM(N) with WL_ROOM(N, D) calling a 4-argument lane_spill(e, sp, N, D) at every sequential call site while the caller segment's local variables were still live, followed by a second branch if (!(sp = ...)) { err_post(e.mem, ERR_DEEP); return 0; }, and added a separate unspill block in WL_CASE(FID_EXIT). That grew the device CFG at every WL_ROOM site (27 device sites in symreg, 8 in merkle) and slowed bend -o's --gpu-build step by 17% on symreg and merkle (bendlang#1809). This change moves the stack-room check to emit_jump, after the sequential frame push and after r0..rn are loaded (where all caller segment local variables are dead and the entire continuation frame is already on the stack below sp), and makes WL_ROOM(F) on DEVICE jump to one cold sp_spill: label at the bottom of switch (fid) in work_loop: - emit_fork removes WL_ROOM(fr.length) before STK(j) = w and passes push = true to emit_jump(sc, cargs, c.k!, !fork && c.b, true). No other emit_jump caller passes push = true, so tail jumps and self-tail loops without a frame push emit no WL_ROOM. - emit_jump with push = true emits WL_ROOM(seg_ref(sc, fid)) right after r0..rn are assigned, before WL_JMP or WL_AGAIN. Self-recursive calls in emit_fork still set sc.seg.spin = true and take WL_AGAIN on the normal path when the stack is not full. - Because WL_ROOM runs after STK(j) = w; WL_PUSHN(fr.length) and after r0..rn are loaded, all caller segment locals are dead and the caller's complete continuation frame sits below sp. In bendlang#1439, WL_ROOM ran before WL_PUSHN, so spilling at a second fork call (such as walk in tests/compile/bang_lane_spill.bend) split the earlier call's stack words from the new frame's words across the exit frame unless lane_spill took D and copied D words back above the exit frame. Emitting WL_ROOM after WL_PUSHN places the exit frame above the complete continuation frame, so lane_spill(e, sp) needs neither N nor D. - On !DEVICE, WL_ROOM(F) expands to nothing (host SEQ-CPU and PAR-CPU machine code is byte-for-byte identical). - On DEVICE, WL_ROOM(F) checks if (sp >= e.mem + STAT_OFF - 247 * CUBE) { fid = (F); goto sp_spill; }. Because comp.ts caps each continuation segment's parameter count at WIDE (247), a single frame push adds at most WIDE + 1 (248) words, so STAK_LEN - WIDE (1801 words) is a static watermark: starting below 1801 words, pushing a frame reaches at most 2048 words (STAK_LEN), and every word below sp belongs to a complete pushed frame. - At the bottom of switch (fid) in work_loop on DEVICE, inside default: after return 0;, sp_spill: sp = lane_spill(e, sp); calls FAR DEV Term* lane_spill(Env e, DEV Term* sp) at a single call site where only e, sp, fid, and r0..rn are live, then exits switch (fid) to loop back to WL_SPIN -> switch (fid) without placing a label before switch (fid). - In WL_CASE(FID_EXIT), unspilling shares the existing WL_ARGS + heap_free + WL_TAKE block with continuation dispatch under cont != TERM_HOLE && ((DEVICE && wf == FID_EXIT) || fid_resw(wf)), ending with if (DEVICE && wf == FID_EXIT) { WL_RETN(n); }. comp.ts goes from 66,000 to 66,268 ttok (+268 ttok, vs +283 in bendlang#1439; main is at the 66,000 cap after bendlang#1809 kept bendlang#1439's token cuts and bendlang#1542 used the 283 tokens).
wakamex
added a commit
to wakamex/bend
that referenced
this pull request
Oct 22, 2026
…il recursion under ! no longer faults at 1023 frames (bendlang#1393, bendlang#1394) A lane's stack on the device holds 2,048 words (STAK_LEN). On main a call that finds it full posts ERR_DEEP: a non-tail List.append under ! stops at 1,023 frames (bendlang#1393), and so does an unbalanced fork tree such as two!(365n) (bendlang#1394). Now a call that finds too little room moves the lane's whole stack to a heap block under a 3-word exit frame, and FID_EXIT moves it back when the lane returns to that frame. bendlang#1439 did this with a 4-argument lane_spill call inlined at every call site, which made the Metal GPU build of symreg and merkle 17% slower (bendlang#1809). Here a call site costs one compare and a jump: - emit_jump checks after the callee's arguments are in r0..rn, where the caller's locals are dead and its whole frame is on the stack. - On the device, WL_ROOM(F) takes WL_JMP to F with bit 16 set (FIDs stay under 65,536); work_loop's default case clears the bit, calls lane_spill and dispatches F. Metal rejects goto and labels, so the jump goes through the switch. - FID_EXIT unspills through its existing WL_ARGS, heap_free and WL_TAKE block, testing for a spilt frame with WL_SPILT(F), which is 0 on the host. A restored stack resumes near the top, and the continuation pushes the next call's frame before any check, so one frame of room is not enough. emit_fork records in FL.room the most words one fork chain pushes (its held words plus a fid per call), and WL_ROOM spills when less than that remains. Every def is entered with that much room, so its chain fits after a spill and a spilled stack never exceeds STAK_LEN words. Host machine code is the same as main for symreg, merkle, queens, raytrace and tree-matmul (objdump -d, addresses aside). The device no longer posts ERR_DEEP: runaway recursion under ! spills until the heap runs out and fails with ERR_HEAP. tests/io/stack_fault_trap.bend's comment says so. tests/compile/bang_lane_spill.bend covers bendlang#1393, bendlang#1394, a spill at a fork's second call, and tall, whose fork chain pushes 449 words; with one frame of room the second tall! faulted on CUDA. It passes on Metal (Mac mini M4), on CUDA (RTX 3090), natively, interpreted and as JS, and every runnable ! test passes on both GPUs. On the M4, symreg's --gpu-build takes 0.267 s on main and 0.252 s here, merkle's 0.163 s and 0.156 s, and none of the 17 bench/runtime programs runs slower. comp.ts goes from 66,000 to 66,377 ttok (+377).
Merged
Lorenzobattistela
pushed a commit
to wakamex/bend
that referenced
this pull request
Nov 2, 2026
…il recursion under ! no longer faults at 1023 frames (bendlang#1393, bendlang#1394) A lane's stack on the device holds 2,048 words (STAK_LEN). On main a call that finds it full posts ERR_DEEP: a non-tail List.append under ! stops at 1,023 frames (bendlang#1393), and so does an unbalanced fork tree such as two!(365n) (bendlang#1394). Now a call that finds too little room moves the lane's whole stack to a heap block under a 3-word exit frame, and FID_EXIT moves it back when the lane returns to that frame. bendlang#1439 did this with a 4-argument lane_spill call inlined at every call site, which made the Metal GPU build of symreg and merkle 17% slower (bendlang#1809). Here a call site costs one compare and a jump: - emit_jump checks after the callee's arguments are in r0..rn, where the caller's locals are dead and its whole frame is on the stack. - On the device, WL_ROOM(F) takes WL_JMP to F with bit 16 set (FIDs stay under 65,536); work_loop's default case clears the bit, calls lane_spill and dispatches F. Metal rejects goto and labels, so the jump goes through the switch. - FID_EXIT unspills through its existing WL_ARGS, heap_free and WL_TAKE block, testing for a spilt frame with WL_SPILT(F), which is 0 on the host. A restored stack resumes near the top, and the continuation pushes the next call's frame before any check, so one frame of room is not enough. emit_fork records in FL.room the most words one fork chain pushes (its held words plus a fid per call), and WL_ROOM spills when less than that remains. Every def is entered with that much room, so its chain fits after a spill and a spilled stack never exceeds STAK_LEN words. Host machine code is the same as main for symreg, merkle, queens, raytrace and tree-matmul (objdump -d, addresses aside). The device no longer posts ERR_DEEP: runaway recursion under ! spills until the heap runs out and fails with ERR_HEAP. tests/io/stack_fault_trap.bend's comment says so. tests/compile/bang_lane_spill.bend covers bendlang#1393, bendlang#1394, a spill at a fork's second call, and tall, whose fork chain pushes 449 words; with one frame of room the second tall! faulted on CUDA. It passes on Metal (Mac mini M4), on CUDA (RTX 3090), natively, interpreted and as JS, and every runnable ! test passes on both GPUs. On the M4, symreg's --gpu-build takes 0.267 s on main and 0.252 s here, merkle's 0.163 s and 0.156 s, and none of the 17 bench/runtime programs runs slower. comp.ts goes from 66,000 to 66,377 ttok (+377).
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.
Removes the functional part of #1439 (40402b4, shipped in 2.0.38); its other changes (the token cuts across
comp.ts) stay.What goes
WL_ROOM(N, D)→WL_ROOM(N): a full lane errors withERR_DEEPagain instead of callinglane_spilllane_spillitselfFID_EXITtests/compile/bang_lane_spill.bendWhy
The perf gate fails its COMPILER column on main, two runs out of two: symreg 1.16x, merkle 1.17x.
Bisected over
v2.0.37..mainon one M4 mini (median of 7bend main.bend -o mainofbench/runtime/symregafter a warm build; each measure repeated within ~1%): 0.747s at10256a7b, 0.793s at40402b42, and the same on a second mini. The emitted C and itsccstep cost the same at both; the whole ~45 ms is the Metal GPU program's build (./main +RTS --gpu-build, whichbend -oruns for any!program): 0.257s → 0.300s (+17%), sincelane_spilljoins the device code.With this PR, on the same mini: main 0.791 / 0.795s, this branch 0.749 / 0.750s.
Gates
bend --checkupprocesses from an earlier test-gate shard at 100% CPU each, the likeliest cause.Cost
#1393 and #1394 come back: non-tail recursion under
!faults at 1023 frames on Metal. The spill can return in a form whose cost only a program that needs it pays.🤖 Generated with Claude Code