[PR #1101] [CLOSED] rdna2spirv: bound loop visits at the structured exit (SHADER-LOOP-GUARD) #1136

Closed
opened 2026-11-26 20:20:12 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1101
Author: @Moriz82
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: split/shader-loop-guard


📝 Commits (1)

  • 30baacf rdna2spirv: bound loop visits at the structured exit (SHADER-LOOP-GUARD)

📊 Changes

2 files changed (+7 additions, -1 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/test_make_game_translator_flags.py (+6 -0)

📄 Description

Rebased onto bfef489c at 30baacf9. Main already includes the loop guard. This diff tests the build flag forwarding and documents the native and emulated forced-exit behavior reported below. The three focused suites passed 71 tests.

Claimed in the coordination issue as: SHADER-LOOP-GUARD (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: #984 is the parent integration. This child merges cleanly beside #1068 and #1073. Either can land first.

What and why

A compute invocation can keep taking a loop back edge when its input makes the exit condition unreachable. On Linux, a synthetic shader that added two on each visit could never reach its odd target. With the guard disabled, both the 32-lane and 64-lane cases exceeded the interpreter's 10,000-step limit in structured and dispatch modes.

The translator now counts visits to each structured loop header per invocation. Entering the loop starts a fresh counter. At the configured limit, a back edge goes to the loop's merge block. A dispatch-loop visit counts one dispatch iteration. Both the translator and the shader build accept --loop-limit N. The default is 65,536. Set it to zero to restore the unbounded output.

This also truncates finite loops that need more visits. With a budget of 16, every lane reached a target of 6 normally. For targets of 1 and 34, every lane reported 32 in structured mode and 30 in dispatch mode. For nested loops with three visits to each header per entry, every lane reported all nine inner iterations. A merge phi with no value on a newly forced exit receives an undefined value on that edge.

The structured-branch test interpreter now reads header phis from the entering edge, as the new counters require. The build digest, fixture build dependencies and runtime workflow path filters include the new translator module.

Tested

Applied only the new tests and their synthetic fixture to pristine main 3aa975421 for the regression comparison. The child has one commit on that base.

Test Main plus tests Child
LoopGuardTests.test_finite_and_nonterminating Both wave-width subcases failed because the module had no guard. Both passed. Finite output matched the unbounded control, and the unreachable target stopped.
LoopGuardTests.test_dispatch_loop_also_stops Both wave-width subcases exceeded 10,000 interpreter steps. Both passed. All 32 or 64 lanes finished.
LoopGuardTests.test_inner_budget_resets_on_each_entry Passed on the finite control. Passed at a budget of 3.
MainWiringTest.test_explicit_loop_limit_reaches_the_shaders_stage Both explicit settings, 0 and 16, were rejected by the build CLI. Both reached the shader stage.
Command or check Result
python3 -I scripts/run_semantic_tests.py tests/semantic -p test_loop_iteration_guard.py -j 2 3 methods. Main had one passing method, one failing method and one method with interpreter timeouts. All 3 passed on the child.
python3 -I scripts/run_semantic_tests.py tests/semantic -p test_control_flow_branches.py -j 2 33 passed.
python3 -I tests/test_make_game_translator_flags.py 34 passed.
python3 -I tests/test_cmake_translator_deps.py and python3 -I tests/test_runtime_paths.py 5 passed in each file.
python3 -I -m compileall -q make_game.py tools tests Passed.
Full Python CI loop over 520 nonsemantic files using python3 -I <file> or python3 -I -m pytest -q <file> One failing file on each tree, tests/test_imhex_patterns.py. The same 6 of its 9 cases failed. Zero new failing files.
python3 -I scripts/run_semantic_tests.py tests/semantic -j 2 Main ran 6,168 tests in 470 files. The child ran 6,171 in 471 files, twice, in 504 s and 322 s. Zero failing files on each run.
python3 -I scripts/check_no_game_data.py Passed, 2,465 tracked entries.
cmake -S runtime -B runtime/build/boot and cmake --build runtime/build/boot --target gta6rt -j6 Windows x86-64 runtime target built incrementally on Linux.

For the synthetic loop, spirv-val accepted all 24 combinations of lane width, structured or dispatch lowering, peephole optimization on or off, and limits 0, 16 or 65,536. Nine additional modules with exit phis validated at limits 0, 1 and 4,294,967,295. A loop-free control was byte-identical. On the translator CLI, limit zero reproduced main's 7,116-byte binary and shader index. The default and explicit 65,536 produced the same 7,220-byte binary and index.

The following repository fixtures were also built on main and the child:

out=$(mktemp -d)
LEONIDA_BARY_FALLBACK=0 python3 -I runtime/test/shaders/build.py "$out" \
  cs_barrier_loop cs_imgatomic cs_imgatomic3d cs_imgatomic_ext cs_lds_atomics_ext \
  ps_imgatomic ps_w64_b_ballot_atomic ps_w64_b_waterfall ps_w64_c_waterfall_count \
  vs_w64_b_ballot_atomic vs_w64_b_waterfall

All 11 synthetic modules translated and validated on each tree. Nine contained loops. Main had zero guard counters across 41 loop headers, and the default child had 41 across the same 41 headers.

GPUs tested: none. Validation used CPU interpreters and SPIRV-Tools on Linux. Native Windows, Wine, NVIDIA GPU, AMD GPU, Intel GPU and game boots were not run.

AI-assisted: yes

Checklist

  • One commit on current upstream main, with a clean merge into main and beside the related published branches.
  • One translator bound, its build wiring and its tests.
  • Synthetic fixtures and a passing game-data check.
  • Translator contract and confirmation in docs/design.md.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1101 **Author:** [@Moriz82](https://github.com/Moriz82) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `split/shader-loop-guard` --- ### 📝 Commits (1) - [`30baacf`](https://github.com/ubiquitarianism/leonida/commit/30baacf9b83198de3205a538359beee14791e06a) rdna2spirv: bound loop visits at the structured exit (SHADER-LOOP-GUARD) ### 📊 Changes **2 files changed** (+7 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/test_make_game_translator_flags.py` (+6 -0) </details> ### 📄 Description **Rebased onto `bfef489c` at `30baacf9`.** Main already includes the loop guard. This diff tests the build flag forwarding and documents the native and emulated forced-exit behavior reported below. The three focused suites passed 71 tests. Claimed in the coordination issue as: SHADER-LOOP-GUARD ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7140556817)). Part of the split of #984 into small pull requests. Depends on: none Overlaps: #984 is the parent integration. This child merges cleanly beside #1068 and #1073. Either can land first. ### What and why A compute invocation can keep taking a loop back edge when its input makes the exit condition unreachable. On Linux, a synthetic shader that added two on each visit could never reach its odd target. With the guard disabled, both the 32-lane and 64-lane cases exceeded the interpreter's 10,000-step limit in structured and dispatch modes. The translator now counts visits to each structured loop header per invocation. Entering the loop starts a fresh counter. At the configured limit, a back edge goes to the loop's merge block. A dispatch-loop visit counts one dispatch iteration. Both the translator and the shader build accept `--loop-limit N`. The default is 65,536. Set it to zero to restore the unbounded output. This also truncates finite loops that need more visits. With a budget of 16, every lane reached a target of 6 normally. For targets of 1 and 34, every lane reported 32 in structured mode and 30 in dispatch mode. For nested loops with three visits to each header per entry, every lane reported all nine inner iterations. A merge phi with no value on a newly forced exit receives an undefined value on that edge. The structured-branch test interpreter now reads header phis from the entering edge, as the new counters require. The build digest, fixture build dependencies and runtime workflow path filters include the new translator module. ### Tested Applied only the new tests and their synthetic fixture to pristine main `3aa975421` for the regression comparison. The child has one commit on that base. | Test | Main plus tests | Child | |---|---|---| | `LoopGuardTests.test_finite_and_nonterminating` | Both wave-width subcases failed because the module had no guard. | Both passed. Finite output matched the unbounded control, and the unreachable target stopped. | | `LoopGuardTests.test_dispatch_loop_also_stops` | Both wave-width subcases exceeded 10,000 interpreter steps. | Both passed. All 32 or 64 lanes finished. | | `LoopGuardTests.test_inner_budget_resets_on_each_entry` | Passed on the finite control. | Passed at a budget of 3. | | `MainWiringTest.test_explicit_loop_limit_reaches_the_shaders_stage` | Both explicit settings, 0 and 16, were rejected by the build CLI. | Both reached the shader stage. | | Command or check | Result | |---|---| | `python3 -I scripts/run_semantic_tests.py tests/semantic -p test_loop_iteration_guard.py -j 2` | 3 methods. Main had one passing method, one failing method and one method with interpreter timeouts. All 3 passed on the child. | | `python3 -I scripts/run_semantic_tests.py tests/semantic -p test_control_flow_branches.py -j 2` | 33 passed. | | `python3 -I tests/test_make_game_translator_flags.py` | 34 passed. | | `python3 -I tests/test_cmake_translator_deps.py` and `python3 -I tests/test_runtime_paths.py` | 5 passed in each file. | | `python3 -I -m compileall -q make_game.py tools tests` | Passed. | | Full Python CI loop over 520 nonsemantic files using `python3 -I <file>` or `python3 -I -m pytest -q <file>` | One failing file on each tree, `tests/test_imhex_patterns.py`. The same 6 of its 9 cases failed. Zero new failing files. | | `python3 -I scripts/run_semantic_tests.py tests/semantic -j 2` | Main ran 6,168 tests in 470 files. The child ran 6,171 in 471 files, twice, in 504 s and 322 s. Zero failing files on each run. | | `python3 -I scripts/check_no_game_data.py` | Passed, 2,465 tracked entries. | | `cmake -S runtime -B runtime/build/boot` and `cmake --build runtime/build/boot --target gta6rt -j6` | Windows x86-64 runtime target built incrementally on Linux. | For the synthetic loop, `spirv-val` accepted all 24 combinations of lane width, structured or dispatch lowering, peephole optimization on or off, and limits 0, 16 or 65,536. Nine additional modules with exit phis validated at limits 0, 1 and 4,294,967,295. A loop-free control was byte-identical. On the translator CLI, limit zero reproduced main's 7,116-byte binary and shader index. The default and explicit 65,536 produced the same 7,220-byte binary and index. The following repository fixtures were also built on main and the child: ```sh out=$(mktemp -d) LEONIDA_BARY_FALLBACK=0 python3 -I runtime/test/shaders/build.py "$out" \ cs_barrier_loop cs_imgatomic cs_imgatomic3d cs_imgatomic_ext cs_lds_atomics_ext \ ps_imgatomic ps_w64_b_ballot_atomic ps_w64_b_waterfall ps_w64_c_waterfall_count \ vs_w64_b_ballot_atomic vs_w64_b_waterfall ``` All 11 synthetic modules translated and validated on each tree. Nine contained loops. Main had zero guard counters across 41 loop headers, and the default child had 41 across the same 41 headers. GPUs tested: none. Validation used CPU interpreters and SPIRV-Tools on Linux. Native Windows, Wine, NVIDIA GPU, AMD GPU, Intel GPU and game boots were not run. AI-assisted: yes ### Checklist - [x] One commit on current upstream main, with a clean merge into main and beside the related published branches. - [x] One translator bound, its build wiring and its tests. - [x] Synthetic fixtures and a passing game-data check. - [x] Translator contract and confirmation in `docs/design.md`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:20:12 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1136
No description provided.