Repository navigation
loop guard: one total of loop visits per invocation, on top of each loop's limit (SHADER-LOOP-TOTAL) - #2183
loop guard: one total of loop visits per invocation, on top of each loop's limit (SHADER-LOOP-TOTAL)#2183QemmHD wants to merge 1 commit into
Conversation
…op's limit 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
|
Spotted a workgroup-barrier deadlock hazard in
Note also a related bug in Keeping AI-assisted review (synthetic repro only, no game data). |
|
Verified on main a73a57e (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) |
|
Tested
So the loop-visit total changes nothing for the 268 fixtures on these drivers. The failures are the same on both sides, none new: AI-assisted (Claude Code). No game data. |
|
I tested djrolfes' scheduler-barrier concern on 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
Each failure is
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 For comparison, main's per-loop guard on the same programs ( 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.
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). |
tools/spirv_loop_guard.pycaps 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.pypasses the flag through and records it with the other translator flags.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.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.run_semantic_testssuite 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