Repository navigation
A full device lane moves its stack to the heap, so non-tail recursion under ! goes past 1,023 frames - #1819
A full device lane moves its stack to the heap, so non-tail recursion under ! goes past 1,023 frames#1819wakamex wants to merge 1 commit into
Conversation
|
my clanker opened this without me telling them to to be fair to them, they seem confident about it: |
|
needs more work, gonna rent a Mac to test |
|
okay I rented a Mac ($15 minimum, can't rent for less than a day, thanks Apple) and updated the PR |
a67515f to
a17e017
Compare
|
I rebased this onto main (00d29f4) and force-pushed it as a17e017, so it merges cleanly again; the logic is unchanged. It does fix #1393 and #1394 on Metal: the issues' own programs ( But it also introduces regressions that block the merge:
|
… under ! goes past 1,023 frames On main (00d29f4) a lane on the GPU has a 2,048-word stack and the call that fills it posts ERR_DEEP: a non-tail List.append under ! stops at 1,023 frames (bendlang#1393), an unbalanced fork tree such as two!(365n) stops (bendlang#1394), and so do a split-then-append recursion at 2,046 elements and uts-compact. Now a call that may leave no room for the next frame copies the lane's stack to a 16 KB heap block under a 3-word exit frame [the block's task, spills << 32 | words, FID_EXIT]. A return into that frame gives back whole defs, walked down from the top of the block through FID_DEEP (each def's words on the stack when a continuation's fid is on top, from emit_fork, device code only), until about half a stack, and leaves the rest under the exit frame. - Every non-tail device call checks, after its frame is pushed and its arguments are loaded, whether the widest frame pushed by any device segment still fits; that width is taken once over the live device segments, after c_header checks their arity. When it does not fit, the call sets rn = F | 1 << 16 (no call site reads rn) and jumps to work_loop's default case, which spills and dispatches F; any other unknown fid still posts ERR_FIDS. Metal has no goto, so the jump goes through the switch. - A self call compiles to a loop, so it checks after setting the loop's parameters and copies them back to r0.. only to spill; the loop keeps r0.. dead. - FID_EXIT handles a spill frame before it reads FID_T. - lane_spill stops the lane when its block cannot be allocated, before copying, and posts ERR_DEEP past 2^12 nested spills a lane (64 MB of blocks), so runaway recursion under ! stops in about 2 s; about 25 lanes running away at once fill a 2 GB span and get the out-of-memory error first. - FID_DEEP is reserved like FID_T. A restore resumes a lane at about half a stack plus one def's words, and a def holds at most 493 words (a continuation takes at most 247 parameters, and every word a fork keeps to its end counts against its last continuation's), so no push after a restore passes the stack. Host machine code is the same as main for symreg, merkle, queens, raytrace and tree-matmul (objdump -d, addresses aside). tests/compile/bang_lane_spill.bend covers bendlang#1393, bendlang#1394, spills at a fork's second call while it holds words from its first, a loop a few words under the top whose first call spills (6,000 spills when a return gives back the whole stack, 1 here), and a split-then-append recursion; tests/compile/bang_lane_spill_wide.bend covers a def with 443 words on the stack. Both pass on Metal, CUDA, natively, interpreted and as JS, and all 44 runnable tests with a ! pass on a Mac mini M4 and an RTX 3090. On the M4 against main (bend-bench metal/compare.sh), --gpu-build over the 17 bench/runtime programs is 5.9% shorter (symreg 0.270 to 0.258 s, merkle 0.169 to 0.168 s), histogram runs 2.0% slower (its self-call checks; without them it matches main), symreg 4.0% and queens 7.6% faster, and the bendlang#1819 review's program at depth 2,040 takes 0.133 s (0.136 s on main). comp.ts goes from 66,000 to 66,799 ttok (+799).
|
Thanks for the rebase and the review. All six were real. I reworked the spill around the first two and pushed it as one commit, 6fbd597. Numbers are from a Mac mini M4 against main 00d29f4, measured with bend-bench's
Other changes on the way:
What didn't work on the way:
On the M4:
Host machine code is the same as main for symreg, merkle, queens, raytrace and tree-matmul. comp.ts is at 66,799 ttok, 799 over the cap. |
Fixes #1393. Fixes #1394. Follow-up to #1439, which #1809 reverted.
Today on main, a lane on the GPU has a 2,048-word stack (
STAK_LEN), and once it fills, the next call postsERR_DEEPand the run stops. A non-tailList.appendunder!fails at 1,023 frames (#1393), an unbalanced fork tree such astwo!(365n)fails (#1394), a split-then-append recursion fails at 2,046 elements, and Unbalanced Tree Search (UTS) (uts-compact) exits withERR_DEEPon the GPU. The host has no such limit.This PR lets a full lane keep going. A call that may leave no room for the next frame copies the lane's whole stack to a 16 KB heap block and leaves a 3-word exit frame at the stack's base. A return into that frame gives back whole defs, walked down from the top of the block, until about half a stack, and leaves the rest under the exit frame for later returns.
What a call pays
STAT_OFFminus that width (3 to 12 words acrossbench/runtime, 242 in the wide test). The width is taken once over the live device segments, afterc_headerchecks their arity. When the frame doesn't fit, the call setsrn = F | 1 << 16(no call site readsrn) and jumps towork_loop'sdefault:, which spills and dispatchesF; any other unknown fid still postsERR_FIDS. Metal has nogoto, so the jump goes through the switch.r0..only on the spill path; otherwise the loop would keepr0..live on every iteration.lane_spillandlane_unspillare out-of-line device functions.FID_EXIThandles a spill frame before it readsFID_T.Why a lane never writes past its stack
Between two checks a lane pushes at most one frame: a check passes only while the widest frame still fits, and after a call returns its continuation pushes one frame before the next check. A restore resumes the lane at about half a stack plus one def's words. A def has at most 493 words on the stack (a continuation takes at most 247 parameters, and every word a fork keeps to its end counts against its last continuation's), 443 in the wide test, so a lane resumes at about 1,520 words at most, below every threshold. Each def's words on the stack when a continuation's fid is on top (
FID_DEEP, emitted only in device code) come fromemit_fork.Limits
!stops in 1 to 2 s. That is 64 MB of blocks a lane: about 25 lanes running away at once fill a 2 GB span and get the out-of-memory error first. A million-deep recursion takes about 1,000 spills.sawtooth_over, a loop whose callee recurses 1,100 deep, takes 1.82 s on the M4 for 3,000 iterations, where main faults.Effects on shared code
WL_ROOMandWL_FULLexpand to nothing and 0, andFID_DEEPis device-only.objdump -dshows the same host machine code as main for symreg, merkle, queens, raytrace and tree-matmul, apart from addresses.FID_EXITtestswf == FID_EXITfirst.ERR_DEEPnow means 2^12 spills, not a full stack.tests/io/stack_fault_trap.bend's comment says so; its output is unchanged.FID_DEEPjoinsFID_Tamong the reserved names.comp.ts goes from 66,000 to 66,799 ttok (+799), so the repo gate stays red until the cap in
gates/repo.tsgoes up.Tests
tests/compile/bang_lane_spill.bendcovers a 5,000-deepList.append(#1393),two!(365n)(#1394), a fork whose spills come at its second call while it holds words from its first, the review's loop a few words under the top (its first call spills once here; giving back the whole stack spilled 6,000 times), and a split-then-append recursion over 4,096 elements.tests/compile/bang_lane_spill_wide.bendcovers a def with 443 words on the stack. Both pass on Metal, on CUDA, natively, interpreted and as JS. All 44 runnable tests with a!pass on an M4 mini and on an RTX 3090; main fails only the two spill tests, and the same two other tests don't build to a runnable program on either. bend-benchcompiler-checkbuilds and runs every test natively on both: 863 pass on main and 862 here, the same 19 print something else and the same 920 do not build to a runnable program on either; the one difference,tests/io/tcp_send_slow_peer.bend, timed out once while the machine's load average passed 100 and passes 3 of 3 runs alone. Checking and emitting C take the same time in total (0.991x and 0.975x).Metal (Mac mini M4)
Measured with bend-bench's
metal/compare.shand summarized bybend-bench metal-report(how to run it), on a Mac mini M4 (10-core GPU, macOS 26.7.2), main 00d29f4 against this PR:deep!(1000000000n)stops with the deep-stack error after 1.3 s.bench/runtimeprograms (lower is better):./main +RTS --gpu-buildtotals 3.78 s on main and 3.56 s here (5.9% shorter), andbend main.bend -o main13.49 s and 13.24 s (1.9% shorter). symreg's--gpu-buildgoes from 0.270 to 0.258 s and merkle's from 0.169 to 0.168 s. The largest increase is gameoflife (0.137 to 0.143 s, +4.4%); the largest decreases are tree-matmul (-20.0%) and histogram (-16.1%).histogram is 2.0% slower, and that comes from its 6 self-call checks: built without them it runs as fast as main (0.995x over 6 blocks), and with only them it is 2.3% slower. Every form of that check I tried costs histogram the same 2%, including the earlier one before the parameter copy, which makes queens slower than this form does. tree-radix is 0.2% slower. I haven't found why queens, symreg and lexer get faster.
CUDA (RTX 3090, CUDA 13.1)
Against main 00d29f4:
293367 0); main faults.bend_dev: the same as main for queens, symreg, merkle, hashmap, raytrace, lexer and tree-radix; up for histogram (96 to 110), editdist (56 to 66), gameoflife (70 to 75), tree-bitonic (74 to 80), bfs (64 to 68), terrain (60 to 64) and kmeans (114 to 118); down for tree-matmul (92 to 90).