Skip to content

A ! call's long sequential loop parks between launches, so no dispatch holds the GPU (#942) - #1012

Closed
MattCozendey wants to merge 6 commits into
bendlang:mainfrom
MattCozendey:feature/gpu-launch-budget
Closed

MattCozendey wants to merge 6 commits into
bendlang:mainfrom
MattCozendey:feature/gpu-launch-budget

Conversation

@MattCozendey

@MattCozendey MattCozendey commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

This should fix the Mac crash in #942. Nothing here has run on a GPU, so I'm asking a maintainer to confirm it on a Mac (see the end).

The problem

In #942, a ! call with no parallelism never finished its command buffer, and WindowServer's watchdog took the session down. That program's work is three long sequential loops: fill and fold over 2^28 cells, and a BFS over the same grid. Each loop compiled to a native spin (emit_native), a C loop that runs whole inside one step of work_loop. So the whole run happened inside one dispatch.

The change

Which loops change (00bfaba, a squeeze in the last commit). carb_park walks the call graph from the bang targets and stops at a loop. A loop it reaches with no other loop above it (a fork tree counts as a loop) is a top loop.

  • A top loop is no longer flat, so it compiles as a segment instead of a spin.
  • An inner loop stays a spin. It runs once per turn of the loop above it, and paying a segment's call there costs real time. I first tried converting every loop a bang reaches, and it cost kmeans 3× and raytrace 1.6× on the cores. The narrow rule leaves all 16 runtime benches' emitted C unchanged.

Parking (0f0771d, 308bf77). Each top loop's self-jump emits WL_PARK(N, F, words). Every 4096 jumps it spends one unit of the lane's fuel (FUEL = 64 per launch, so about 262k turns).

  • When the fuel is spent, it pushes the jump's N words and F | N << 16 | seq << 24 onto the lane's stack. Out of line, wl_park copies the stack to the heap and deals it as a frontier task tagged TAG_PRK, bumping H_CURSOR so cube_run launches again.
  • work_loop takes a TAG_PRK task back onto any lane's stack.
  • Pass 0 skips parked tasks, and a lane ends its pass 1 once it has parked. So a launch runs a chain for about one budget at most.
  • The fuel is one word per lane in the alc block, past ALC_LEN, where the host keeps ALC_COLD.

Programs with no top loop pay nothing. #define PARKS says whether a program has any park site. Without one, the resume path, the fuel word and its checks compile out, and the corpus layout is main's.

  • All 16 runtime benches have no park site.
  • For each of them, the preprocessed C (for the cores and for the device) is main's, up to how constants are spelled (2 * 32 against (2 * 32 + 0)).
  • Their bend_dev register counts under NVRTC match main's exactly.

Where parks go. An earlier version parked at the top of work_loop. That stored the whole register bank there and cost up to +12 registers (queens 94→106). Parking at every loop's jump was worse (queens 120). Park sites only at top loops is what keeps everything else at main's kernel.

The --gpu span is bounded (633dbdf). #942 found that an explicit --gpu 8GB was wired whole, because only the default went through gpu_span.

  • gpu_span now returns the device's limit: Metal's recommended working set under its largest buffer, or CUDA's total memory.
  • Both the asked span and the default stop at that limit. An asked span over it prints bend: --gpu is over the device's NMB; using that on stderr.
  • Metal's default stays 2 GB, and the cores' span is unchanged.
  • This bounds one process. Several GPU programs running at once can still add up past the machine's memory.

A build knob (9000e4b). -DFUEL=n sets the budget for a host whose GPU watchdog is tighter than macOS's. The host passes it to the device compiler (NVRTC -D, Metal preprocessor macro), and it's part of the CUDA .gpu cache key.

What I verified

None of this ran on a GPU.

  • A ! call with no available parallelism holds the GPU indefinitely and crashes macOS (WindowServer watchdog) #942's program: fill, bfs and fold compile as segments with one park site each. The kernel compiles with NVRTC 13.3 for sm_89 (72 registers, no spills).
  • Benches and tests: the 16 runtime benches and 8 ! tests compile with NVRTC, also with -DFUEL=16. There are no new warnings and no spills. Every program without a park site has main's register count. The two that have one: symreg 66→72 (same occupancy, since registers are allocated in eights) and gpu_reach_closure_list 55→58.
  • Host code: clang -fsyntax-only -Wall passes for the CUDA host lane and the cores lane, with and without -DFUEL=16, including the span cap.
  • Native test run: I built every runnable test as a native binary (Ubuntu 24.04, clang 18) on main and on this branch. 604 pass on both, with the same verdict for every test.
  • Not compiled here: the Metal host (Objective-C) side of the span cap, which is two lines in gpu_span.
  • Size: comp.ts is 63,489 tokens.

What I could not verify

Limits

  • Inner loops: a loop under another loop, or inside a fork tree's leaf, stays a spin with no park site. A short outer loop around a huge inner loop still holds a launch for each inner call, and so does a fork-tree leaf that loops for a long time. Covering those needs park sites in hot code, which costs registers everywhere (see above).
  • A ! call with no available parallelism holds the GPU indefinitely and crashes macOS (WindowServer watchdog) #942's other point: a warning for ! with no parallelism is left for a follow-up. With this change, such a call no longer holds the GPU, so the warning would only be about performance.

🤖 Generated with Claude Code

@MattCozendey
MattCozendey force-pushed the feature/gpu-launch-budget branch from bf13668 to 2dd7716 Compare September 24, 2026 00:39
MattCozendey and others added 6 commits September 24, 2026 09:23
…holds the GPU (bendlang#942)

A ! call with no parallelism ran its whole chain inside one dispatch, which
never returned and tripped the display's GPU watchdog. Each lane now gets
FUEL * 4096 steps per launch. When they run out, work_loop pushes its bank
and fid | rn | seq onto the lane's stack, copies the stack to the heap and
deals it as a frontier task (TAG_PRK). The next pass 1 takes it back onto
any lane's stack. A device self-loop hands every 4096th turn back to the
switch, so the check covers it too. Pass 0 skips parked tasks, and a lane
stops its pass 1 once it has parked, so each launch runs a task's chain
once at most. The fuel lives in the lane's alc block, one word past
ALC_LEN.

A native spin (a flat def's loop) still runs whole inside its step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…chdog is tighter

FUEL keeps 64 unless the host build defines it. The host passes its value
to the device compiler (NVRTC's -D, Metal's preprocessor macros), and the
CUDA cache key hashes it, so a .gpu built under another budget is compiled
again, not loaded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in, so the device can park it

A spin (a flat def's native loop) runs whole inside one device step, so a
long sequential loop under a ! still held its launch. bendlang#942's program is all
spins: fill, bfs and fold each run 2^28 turns. carb_book now walks from the
bang targets, and a looping def with no loop above it (a fork tree is one)
is no longer flat. It compiles as a segment, whose self-loop parks when the
lane's fuel runs out. An inner loop stays a spin: it runs once per turn of
the loop above it, which cannot afford a segment's call. The broad version
of this rule (every loop a bang reaches) cost kmeans 3x and raytrace 1.6x
on the cores. This one leaves all 16 runtime benches' emitted C unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…p keeps its kernel

Parking at the top of work_loop stored the whole register bank there, which
kept every bank register live through the loop: queens went from 94 to 106
registers and tree-bitonic from 72 to 80. Parking at every loop's self-jump
was worse (queens 120). Now the loops the spin rule already finds (a tail
loop a bang reaches under no other loop) are the only park sites. At the
jump, WL_PARK stores just the jump's N words, then F | N << 16 | seq << 24.
The loop top and WL_SPIN are main's again.

PARKS says whether a program has a park site. Without one, the resume, the
fuel word and its checks compile out, and the layout is main's. All 16
runtime benches have none, so their preprocessed C (cores and device) is
main's up to constant spelling, and their bend_dev registers match main's
exactly. bendlang#942's fill, bfs and fold each park at their jump.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lang#942)

bendlang#942 found an explicit --gpu 8GB wired whole: only the default went through
gpu_span. gpu_span now answers the device's limit (Metal's recommended
working set under its largest buffer, CUDA's total memory). Both the asked
span and the default stop there, and an asked span over it says so on
stderr. Metal's default stays 2GB. The cores' span is unchanged.

This bounds one process. Several GPU programs running at once can still
sum past the machine's memory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
carb_park walks from the bang targets and stops at a loop, since everything
under a loop keeps its flatness and gets no park. That drops the walk's
under flag, its string keys and a FLATS.clear() that nothing needed
(flatness is first asked at emission). corpus_setup's span choice moves to
gpu_fit, where an asked span over the device is simply bytes > top. The
emitted C of the 16 runtime benches, the 8 ! tests and bendlang#942's program is
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@MattCozendey
MattCozendey force-pushed the feature/gpu-launch-budget branch from 2dd7716 to 867f36a Compare September 24, 2026 12:24
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks for taking on #942, and for the detailed write-up of what you could and couldn't verify. I'm going to close this one, but part of it is worth landing separately.

The --gpu span cap is good. Capping an explicit --gpu size at what the device can take is a small, self-contained fix for a real bug from #942. Please send it as its own PR.

The parking mechanism is too much for what it fixes. It adds a new task tag, a park/resume path in work_loop, changes to monk_step and the device pass loop, a PARKS compile switch, extra per-def flags with a new call-graph pass, and a fuel budget with hand-tuned constants and a build knob. That's a lot of new machinery in the hottest part of the runtime, and we try hard to keep that code simple. It also hasn't run on a GPU yet, and no test exercises a park. And it only covers top-level loops: an inner loop or a looping fork-tree leaf can still hold a launch just as long, so the crash is harder to hit but still possible.

The program in #942 is a ! on work with no parallelism. For that, I'd rather go with the simpler fixes suggested in the issue:

  • warn, or fall back to the CPU, when a ! call has no parallelism to give the GPU;
  • give the host a deadline on each GPU launch, and fail cleanly (as a device fault does) instead of waiting forever.

If you'd like to work on one of those, a new PR would be welcome. If you think long sequential chains should really be split across launches on the GPU, please open an issue first. That's a runtime design decision we'd want to make together before any code.

@MattCozendey

Copy link
Copy Markdown
Contributor Author

GPU-cap change: #1047

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.

2 participants