loop guard: one total of loop visits per invocation, on top of each loop's limit (SHADER-LOOP-TOTAL) #2183

Open
github-bot wants to merge 1 commit from github-bot/gh-2183 into main AGit
Owner

@⁠QemmHD on GitHub · 2026-11-26 03:05 UTC · original
ubiquitarianism:claude/loop-total → main · +110 −11 · 8 files · 1 commits · open

tools/spirv_loop_guard.py caps each loop at 65,536 visits, so nested loops multiply their caps. A loop bound computed by a float divide whose divisor is 0 becomes +inf, converts to 0xFFFFFFFF, and an inclusive exit test against it never fails once the counter wraps. A compute program with nested loops of that shape ran at the caps for seconds per dispatch, and the device was lost with no faulting address.

  • --loop-total N (default 1,048,576; 0 = none) adds one count of every guarded back edge per invocation, in a Private variable listed by the entry point. No back edge passes once it reaches N. Each loop keeps its own limit, so a loop that ends by itself is only cut after the whole invocation made N visits.
  • make_game.py passes the flag through and records it with the other translator flags.
  • The default changes the translation of every program with a loop, so a shader library picks it up when it is translated again.

Tests

  • tests/semantic/test_loop_iteration_guard.py: two nested loops that never end stop at the total, in wave32 and emulated wave64; loops that end by themselves are untouched; without a total the per-loop counts multiply.
  • Three test-local SPIR-V interpreters (test_loop_ballot_barrier, test_control_flow_branches, test_wave64_emulate_opt) now give each invocation its Private globals, which every module with a loop has from now on.
  • Full run_semantic_tests suite green on this branch. On an RTX 4070 SUPER, the GPU loop-cap ctests pass, including an inner loop that never ends stopping at the total.

AI-assisted (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf

**@⁠QemmHD** on GitHub · 2026-11-26 03:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2183) `ubiquitarianism:claude/loop-total` → `main` · +110 −11 · 8 files · 1 commits · open `tools/spirv_loop_guard.py` caps each loop at 65,536 visits, so nested loops multiply their caps. A loop bound computed by a float divide whose divisor is 0 becomes +inf, converts to 0xFFFFFFFF, and an inclusive exit test against it never fails once the counter wraps. A compute program with nested loops of that shape ran at the caps for seconds per dispatch, and the device was lost with no faulting address. - `--loop-total N` (default 1,048,576; 0 = none) adds one count of every guarded back edge per invocation, in a Private variable listed by the entry point. No back edge passes once it reaches N. Each loop keeps its own limit, so a loop that ends by itself is only cut after the whole invocation made N visits. - `make_game.py` passes the flag through and records it with the other translator flags. - The default changes the translation of every program with a loop, so a shader library picks it up when it is translated again. **Tests** - `tests/semantic/test_loop_iteration_guard.py`: two nested loops that never end stop at the total, in wave32 and emulated wave64; loops that end by themselves are untouched; without a total the per-loop counts multiply. - Three test-local SPIR-V interpreters (`test_loop_ballot_barrier`, `test_control_flow_branches`, `test_wave64_emulate_opt`) now give each invocation its Private globals, which every module with a loop has from now on. - Full `run_semantic_tests` suite green on this branch. On an RTX 4070 SUPER, the GPU loop-cap ctests pass, including an inner loop that never ends stopping at the total. AI-assisted (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
tools/spirv_loop_guard.py caps each loop at 65,536 visits from every entry, so
nested loops multiply their caps: an inner loop may make 65,536 visits in each
of 65,536 visits of the outer one. A loop bound computed by a float divide
whose divisor is 0 is +inf. It converts to 0xFFFFFFFF, and an inclusive exit
test against it never fails once the counter wraps. A compute program with
nested loops of that kind ran at the caps for seconds a dispatch, and the
device was lost with no faulting address.

--loop-total N (default 1,048,576; 0 = none) adds one count of every guarded
back edge of the invocation, in a Private variable listed by the entry point.
No back edge passes once it reaches N. Each loop keeps its own limit, so a loop
that ends by itself is cut only after the whole invocation made N visits.
make_game.py passes the flag on and records it with the other translator flags.

The default changes the translation of every program with a loop, so a shader
library takes it when it is translated again.

Tests (tests/semantic/test_loop_iteration_guard.py): two nested loops that
never end stop at the total, in wave32 and in emulated wave64 (whose round
scheduler spends from the same total); loops that end by themselves are
untouched; without a total the per-loop counts multiply. Three test-local SPIR-V
interpreters (test_loop_ballot_barrier, test_control_flow_branches,
test_wave64_emulate_opt) now give each invocation its Private globals, which
every module with a loop has from now on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:21:34 +00:00
Author
Owner

@⁠djrolfes on GitHub · 2026-11-26 08:43 UTC · original

Spotted a workgroup-barrier deadlock hazard in tools/spirv_loop_guard.py when --loop-total instruments the outer scheduler loop (%loop_head / %loop_cont) of x_scheduler, x_mini_scheduler, and bar_scheduler:

  • %loop_guard_total is a per-invocation Private counter incremented at every guarded back edge (both inner loops and the outer round loop).
  • In x_scheduler / x_mini_scheduler / bar_scheduler, %loop_cont contains a workgroup OpControlBarrier (%loop_head is explicitly required to stay in workgroup-uniform control flow).
  • If wave A in a workgroup spends more inner-loop iterations than wave B, wave A hits %loop_guard_total first and exits %loop_head to %loop_exit at %loop_cont, while wave B still has %loop_guard_total < total and loops back to %loop_head to wait at %loop_cont's OpControlBarrier forever -> workgroup barrier deadlock on the GPU.

Note also a related bug in bar_scheduler_tail (tools/rdna2spirv.py): when the per-loop guard cuts the inner %xb_in_head loop at LOOP_LIMIT, it exits to %xb_in_merge without setting %done = true (since %xb_stop is still false), so the outer %loop_head sees the invocation as still active and re-enters %xb_in_head with its per-loop count reset, multiplying to LOOP_LIMIT * LOOP_LIMIT (reproduced synthetically with LOOP_LIMIT=8: bar_scheduler executes 55 iterations instead of <= 8).

Keeping %loop_guard_total out of loops whose body contains OpControlBarrier (and marking %done = true / %x_ldone = true on inner-loop guard cut in bar_scheduler_tail / x_scheduler_tail / x_mini_scheduler_tail so the outer round loop exits uniformly) avoids both the barrier deadlock and the N * N multiplication.

AI-assisted review (synthetic repro only, no game data).

**@⁠djrolfes** on GitHub · 2026-11-26 08:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2183#issuecomment-7186932954) Spotted a workgroup-barrier deadlock hazard in `tools/spirv_loop_guard.py` when `--loop-total` instruments the outer scheduler loop (`%loop_head` / `%loop_cont`) of `x_scheduler`, `x_mini_scheduler`, and `bar_scheduler`: - `%loop_guard_total` is a per-invocation `Private` counter incremented at every guarded back edge (both inner loops and the outer round loop). - In `x_scheduler` / `x_mini_scheduler` / `bar_scheduler`, `%loop_cont` contains a workgroup `OpControlBarrier` (`%loop_head` is explicitly required to stay in workgroup-uniform control flow). - If wave A in a workgroup spends more inner-loop iterations than wave B, wave A hits `%loop_guard_total` first and exits `%loop_head` to `%loop_exit` at `%loop_cont`, while wave B still has `%loop_guard_total < total` and loops back to `%loop_head` to wait at `%loop_cont`'s `OpControlBarrier` forever -> workgroup barrier deadlock on the GPU. Note also a related bug in `bar_scheduler_tail` (`tools/rdna2spirv.py`): when the per-loop guard cuts the inner `%xb_in_head` loop at `LOOP_LIMIT`, it exits to `%xb_in_merge` without setting `%done = true` (since `%xb_stop` is still false), so the outer `%loop_head` sees the invocation as still active and re-enters `%xb_in_head` with its per-loop count reset, multiplying to `LOOP_LIMIT * LOOP_LIMIT` (reproduced synthetically with `LOOP_LIMIT=8`: `bar_scheduler` executes 55 iterations instead of <= 8). Keeping `%loop_guard_total` out of loops whose body contains `OpControlBarrier` (and marking `%done = true` / `%x_ldone = true` on inner-loop guard cut in `bar_scheduler_tail` / `x_scheduler_tail` / `x_mini_scheduler_tail` so the outer round loop exits uniformly) avoids both the barrier deadlock and the `N * N` multiplication. AI-assisted review (synthetic repro only, no game data).
Author
Owner

@⁠real-rusher on GitHub · 2026-11-26 10:20 UTC · original

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly into main. Changed semantic tests with this PR, each via unittest discover: test_loop_iteration_guard 6 OK, test_control_flow_branches 33 OK, test_loop_ballot_barrier 15 OK, test_wave64_emulate_opt 33 OK. test_loop_iteration_guard.py against main's translator: 2 errors, so it fails on main and passes with the change; the other three pass on both. compileall clean.

Not run: the whole semantic suite and the translated / spirv-val counts over a corpus (no game data here).

Note for review: it conflicts with #2155 in tools/rdna2spirv.py, so whichever lands later needs a rebase.

AI-assisted: yes (Claude Code)

**@⁠real-rusher** on GitHub · 2026-11-26 10:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2183#issuecomment-7189224454) Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly into main. Changed semantic tests with this PR, each via unittest discover: test_loop_iteration_guard 6 OK, test_control_flow_branches 33 OK, test_loop_ballot_barrier 15 OK, test_wave64_emulate_opt 33 OK. test_loop_iteration_guard.py against main's translator: 2 errors, so it fails on main and passes with the change; the other three pass on both. compileall clean. Not run: the whole semantic suite and the translated / spirv-val counts over a corpus (no game data here). Note for review: it conflicts with #2155 in tools/rdna2spirv.py, so whichever lands later needs a rebase. AI-assisted: yes (Claude Code)
Author
Owner

@⁠Tauwni on GitHub · 2026-11-26 12:02 UTC · original

Tested 88abd1609 on an Apple M3 (macOS 26.5.1) with the three Vulkan drivers there. I ran the full scripts/run_gpu_tests.py in auto mode, against main a73a57e13, with test programs built from main.

Driver main this PR
MoltenVK 1.4.2 195 pass, 70 skip 195 pass, 70 skip
MoltenVK, LEONIDA_W64_EMULATE=1 229 pass, 36 skip 229 pass, 36 skip
KosmicKrisp (Mesa 26.3.1) 190 pass, 73 skip 190 pass, 73 skip
llvmpipe (Mesa 26.3.1) 177 pass, 90 skip 177 pass, 90 skip
llvmpipe, LEONIDA_W64_EMULATE=1 212 pass, 55 skip 212 pass, 55 skip

So the loop-visit total changes nothing for the 268 fixtures on these drivers. The failures are the same on both sides, none new: cs_fma_fused (#1724), cs_imgatomic_ext (#1518) and ps_ssharp (#1774) on MoltenVK; cs_bvh/cs_bvh32, ps_cndmask_sign_mods and ps_valu16_mad_sel (KosmicKrisp's (NAN) constants, #2229) on KosmicKrisp; cs_imgatomic_ext on llvmpipe.

AI-assisted (Claude Code). No game data.

**@⁠Tauwni** on GitHub · 2026-11-26 12:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2183#issuecomment-7191485325) Tested `88abd1609` on an Apple M3 (macOS 26.5.1) with the three Vulkan drivers there. I ran the full `scripts/run_gpu_tests.py` in auto mode, against `main` `a73a57e13`, with test programs built from `main`. | Driver | `main` | this PR | |---|---|---| | MoltenVK 1.4.2 | 195 pass, 70 skip | 195 pass, 70 skip | | MoltenVK, `LEONIDA_W64_EMULATE=1` | 229 pass, 36 skip | 229 pass, 36 skip | | KosmicKrisp (Mesa 26.3.1) | 190 pass, 73 skip | 190 pass, 73 skip | | llvmpipe (Mesa 26.3.1) | 177 pass, 90 skip | 177 pass, 90 skip | | llvmpipe, `LEONIDA_W64_EMULATE=1` | 212 pass, 55 skip | 212 pass, 55 skip | So the loop-visit total changes nothing for the 268 fixtures on these drivers. The failures are the same on both sides, none new: `cs_fma_fused` (#1724), `cs_imgatomic_ext` (#1518) and `ps_ssharp` (#1774) on MoltenVK; `cs_bvh`/`cs_bvh32`, `ps_cndmask_sign_mods` and `ps_valu16_mad_sel` (KosmicKrisp's `(NAN)` constants, #2229) on KosmicKrisp; `cs_imgatomic_ext` on llvmpipe. AI-assisted (Claude Code). No game data.
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 19:05 UTC · original

I tested djrolfes' scheduler-barrier concern on 88abd1609 and it reproduces. In the wave64 round-loop form, --loop-total can make invocations of one workgroup leave the round loop at different rounds.

Setup: CPU only, no game data. I used 24 hand-written NGG mesh programs with 128 threads, so 2 waves. They have loops of every common shape: waterfall v_readfirstlane, execnz and VCC-vote latches, s_barrier + LDS in a loop, loops whose trip count differs between the two waves, and loops that never end. I translated them with --wave64-emulate in the forced round-loop form (STRUCTURED = False, the emulated wave64 rounds the world-load hangs report). Each workgroup ran in spirv_mini_interp.run_workgroup on 32-wide subgroups. It raises when a subgroup op or an OpControlBarrier is not reached by every invocation at the same point.

--loop-total programs that broke uniformity
40 2 of 24: inf_wave1_vote, inf_wave1_barrier
12 3 of 24: vote_exec, wave_rfl_loop, inf_wave1_barrier
5 0 (everyone leaves in the first rounds)

Each failure is subgroup operation not reached by every invocation at the same point. All of them are programs where one wave does more work than the other:

  • a loop that runs forever only in wave 1;
  • a vote loop under an EXEC narrowed to part of wave 0;
  • a v_readfirstlane loop with a different trip count per wave.

The total counts every guarded back edge of the invocation, the chain loop inside the round included. So the busier wave's invocations reach it first and take the round loop's exit, while the other wave's invocations go on to the next exchange and barrier. On a GPU that is a non-uniform OpControlBarrier: undefined behaviour, and in practice usually a hang. That is the thing the total is meant to prevent.

For comparison, main's per-loop guard on the same programs (--loop-limit 40, including the never-ending ones) broke none. Every invocation counts the round loop's iterations identically, so they all leave it together.

A way to keep the total without the hazard: don't let it end a loop that holds a workgroup barrier on its own, invocation by invocation.

  • One option: exempt the round loops (x_scheduler, x_mini_scheduler, bar_scheduler) from the total, so only their per-loop guard applies.
  • The other: make the decision to leave them uniform, for example OR-ing "any invocation over the total" into the counts the round already exchanges after its barrier, so everyone leaves in the same round.

For context: HasiKe's finding in #1608 shows the world-load TDR goes away when the nested loops are bounded tightly. The inner loop there has no barrier. The first option would still bound that inner loop by the total, with no risk to the round loop's barrier.

The harness is test-local Python, about 300 lines. I can contribute it as a semantic test if it helps.

AI-assisted (Claude Code).

**@⁠qhukz13** on GitHub · 2026-11-26 19:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2183#issuecomment-7201178176) I tested djrolfes' scheduler-barrier concern on `88abd1609` and it reproduces. In the wave64 round-loop form, `--loop-total` can make invocations of one workgroup leave the round loop at different rounds. **Setup:** CPU only, no game data. I used 24 hand-written NGG mesh programs with 128 threads, so 2 waves. They have loops of every common shape: waterfall `v_readfirstlane`, `execnz` and VCC-vote latches, `s_barrier` + LDS in a loop, loops whose trip count differs between the two waves, and loops that never end. I translated them with `--wave64-emulate` in the forced round-loop form (`STRUCTURED = False`, the `emulated wave64 rounds` the world-load hangs report). Each workgroup ran in `spirv_mini_interp.run_workgroup` on 32-wide subgroups. It raises when a subgroup op or an `OpControlBarrier` is not reached by every invocation at the same point. | `--loop-total` | programs that broke uniformity | |---|---| | 40 | 2 of 24: `inf_wave1_vote`, `inf_wave1_barrier` | | 12 | 3 of 24: `vote_exec`, `wave_rfl_loop`, `inf_wave1_barrier` | | 5 | 0 (everyone leaves in the first rounds) | Each failure is `subgroup operation not reached by every invocation at the same point`. All of them are programs where one wave does more work than the other: - a loop that runs forever only in wave 1; - a vote loop under an EXEC narrowed to part of wave 0; - a `v_readfirstlane` loop with a different trip count per wave. The total counts every guarded back edge of the invocation, the chain loop inside the round included. So the busier wave's invocations reach it first and take the round loop's exit, while the other wave's invocations go on to the next exchange and barrier. On a GPU that is a non-uniform `OpControlBarrier`: undefined behaviour, and in practice usually a hang. That is the thing the total is meant to prevent. For comparison, main's per-loop guard on the same programs (`--loop-limit 40`, including the never-ending ones) broke none. Every invocation counts the round loop's iterations identically, so they all leave it together. A way to keep the total without the hazard: don't let it end a loop that holds a workgroup barrier on its own, invocation by invocation. - One option: exempt the round loops (`x_scheduler`, `x_mini_scheduler`, `bar_scheduler`) from the total, so only their per-loop guard applies. - The other: make the decision to leave them uniform, for example OR-ing "any invocation over the total" into the counts the round already exchanges after its barrier, so everyone leaves in the same round. For context: HasiKe's finding in #1608 shows the world-load TDR goes away when the nested loops are bounded tightly. The inner loop there has no barrier. The first option would still bound that inner loop by the total, with no risk to the round loop's barrier. The harness is test-local Python, about 300 lines. I can contribute it as a semantic test if it helps. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2183/head:github-bot/gh-2183'
git switch 'github-bot/gh-2183'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2183'
git switch 'github-bot/gh-2183'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2183'
git switch 'github-bot/gh-2183'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2183'
git switch 'main'
git merge --squash 'github-bot/gh-2183'
git switch 'main'
git merge --ff-only 'github-bot/gh-2183'
git switch 'main'
git merge 'github-bot/gh-2183'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.