Skip to content

rdna2spirv: opt-in --loop-budget N turns a runaway loop into a finished shader instead of a device loss - #6067

Open
tedin7 wants to merge 2 commits into
ItsNotRexed:mainfrom
tedin7:xcl-x17
Open

tedin7 wants to merge 2 commits into
ItsNotRexed:mainfrom
tedin7:xcl-x17

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #5934 (comment).

A translated loop that never exits hangs the GPU; --loop-budget N turns that into a finished shader

The idea came from reading XCL's design notes (a per-lane iteration budget on generated loops). Nothing here is taken from its code; the numbers below are Leonida's own.

The defect on current main (561b65a)

Leonida has no guard on a translated loop: only a BVH step cap in one Lanterns stage and the 120 s test timeout. A synthetic compute shader (a lane-uniform loop that steps by 2 until it equals an odd number read from memory at run time, so no compiler can fold it) was translated with the unchanged translator and run with vk_compute_test on an RTX 5080 (Linux, driver 580.65.06):

  • vkQueueWaitIdle returned VK_ERROR_DEVICE_LOST (-4), after 4.7 s in one run and after 29.8 s in another; the next run on the same GPU was fine.
  • On Windows the same loop is a TDR.
  • A counting loop with a constant or masked bound is folded by the driver compiler (2^31 trips in 0.27 s), so only a loop with no reachable exit shows it.

The change

  • tools/rdna2spirv.py --loop-budget N (0 = off, the default). Each invocation gets a Function variable %loop_budget; every time a loop goes round again it takes one unit, and a loop with none left leaves instead of repeating. The loops of one invocation share the budget.
  • Covers the structured form (the loop's closing branch) and the dispatch-loop form (a jump to the same or an earlier block). Not covered: --wave64-emulate and barrier-round schedulers, and the atomic retry loops. index.json records loop_budget for the shaders it was applied to.
  • After the budget runs out the lane stops looping and what follows the loop is skipped (%pc still names the loop header), so the result is wrong but the shader ends.
  • Off, the output does not change: the translator's output for all 239 fixtures of the suite is byte-identical to main's.
  • scripts/run_gpu_tests.py: LEONIDA_LOOP_BUDGET=N translates every fixture with a budget; the new fixture cs_loop_budget (a loop with no exit) is always translated with 65,536.
  • Docs: a row in docs/design.md and a paragraph in docs/testing.md.

Tests

  • tests/semantic/test_loop_budget.py (CPU, stdlib only), 5 tests: without a budget the runaway loop does not finish; with a budget it ends after exactly N trips; two loops share the budget (10 trips in the first leave 91 for the second at N = 100); a budget far above a correct loop's trips leaves its result alone; the same in the dispatch-loop form. Against main's translator 3 of the 5 fail (the runaway loop never finishes), after the change all 5 pass.
  • GPU, RTX 5080: cs_loop_budget PASS native and with LEONIDA_W64_EMULATE=1 (out[0] = 131072, the rest untouched). The same fixture translated without the flag: DEVICE_LOST as above.
  • Full run_gpu_tests.py: native 184/233 passed, 48 skipped, 1 failure (ms_wave_info_s3, same SPIR-V as main so not caused by this change); with LEONIDA_LOOP_BUDGET=65536 the verdict of every shader is the same; LEONIDA_W64_EMULATE=1 215/233, 17 skipped, same single failure, and the same again with the budget.

Cost

A tight loop pays for the extra counter. Four scalar instructions per trip, 61,166 trips, 2,000,000 groups (RTX 5080): 1.23 s unguarded, 1.92 s with a budget of 65,536 (+55%); 200,000 groups 0.41 s against 0.46 s. A loop with a heavier body pays less. That is why it is opt-in.

Open

  • Wave64 emulation, barrier rounds and loops with a barrier are not guarded.
  • Whether make_game.py should pass a budget by default on a device that resets on a hang is left to the maintainers; this PR only adds the switch.

Review fixes (DeepSeek)

  • With `--wave64-emulate` the structured plan shared the guarded-loop emitter but never declared `%loop_budget` (invalid SPIR-V on a multi-block loop). Now only the plain structured form guards; a test translates such a loop with emulation and a budget, runs spirv-val, and fails on the previous commit.
  • Barrier loops are left unguarded; a test checks both forms and that a barrier-free loop in the same shader is still guarded.
  • `run_gpu_tests.py` keeps the first nonzero translator exit code across the budgeted fixtures.

Reviewed by a second model (DeepSeek) before opening; its three points are fixed in aa90efe6. No game files. AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested aa90efe6 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_loop_budget.py" -> OK

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@Etr0p

Etr0p commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested aa90efe6 on GPUs: RTX 5070 Ti Laptop (NVIDIA 621.24, subgroup 32) and Radeon 610M (RDNA2, AMD 26.11.2, native wave64), Windows 11 26200, test programs built with MSVC 14.44 at 561b65a8 (this PR changes no runtime source): pass.

  • run_gpu_tests.py --filter cs_loop_budget on both GPUs, native and LEONIDA_W64_EMULATE=1: 4/4 pass. out[0..7] = 131072 then seven 4008636142, as the fixture's header expects. No hang, no device loss.
  • Full run_gpu_tests.py on each GPU, without and with LEONIDA_LOOP_BUDGET=65536 (every shader under a budget): identical results, so the budget changes nothing that finishes.
  • tests/semantic/test_loop_budget.py: 7 OK.

AI-assisted (Claude Code). Synthetic fixtures only, no game files.

@sennecools

Copy link
Copy Markdown

Tested aa90efe6 on RX 9070 XT (RDNA4 gfx1201, AMD Windows driver 32.0.32029.2010, subgroup 32..64), Windows 11 26300, on top of 561b65a8: pass. This is the first RDNA4 result and the first on the AMD Windows compiler.

I did not run the unguarded loop or LEONIDA_W64_EMULATE=1.

AI-assisted. Synthetic fixtures only, no game files.

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.

4 participants