Skip to content

rdna2spirv: bound loop visits at the structured exit (SHADER-LOOP-GUARD) - #1101

Closed
Moriz82 wants to merge 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/shader-loop-guard
Closed

Moriz82 wants to merge 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/shader-loop-guard

Conversation

@Moriz82

@Moriz82 Moriz82 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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

@Plaaasma

Copy link
Copy Markdown
Contributor

Native Windows check, AI-assisted (Claude Code). Windows 11, i9-13900K, 64 GB, RTX 4070 Ti 12 GB (NVIDIA 630.20), MSVC 2022, Vulkan; Python 3.10, llvm-mc (AMDGPU) and spirv-val on PATH.

Head 10ad8809b:

  • python -I scripts/run_semantic_tests.py tests/semantic -p test_loop_iteration_guard.py -v: 3/3 OK in 0.47 s (test_finite_and_nonterminating, test_inner_budget_resets_on_each_entry, test_dispatch_loop_also_stops)
  • ... -p test_control_flow_branches.py: 33/33 OK in 0.27 s
  • python -m unittest test_make_game_translator_flags (from tests/): 34/34 OK in 0.91 s
  • full suite, python -I scripts/run_semantic_tests.py tests/semantic --timeout 600: 471 files, 6171 tests, 0 files failed in 189 s (32 jobs)

No failures. (No runtime/ctest targets in this PR, so nothing built.)

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Audited and verified. Implements structured loop iteration bounding in ools/spirv_loop_guard.py, instrumenting loop headers with iteration counter phis and redirecting back edges to the loop merge block when the iteration budget is exhausted. Correctly handles split latches and OpPhi incoming values. Approved.

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean-Room Audit — PR #1101 (SHADER-LOOP-GUARD)

Clean loop visit bound injection (tools/spirv_loop_guard.py) guarding structured loop merge blocks (default 65,536 iterations) with --loop-limit configuration wired cleanly through rdna2spirv.py and make_game.py. Synthetic .asm fixture and semantic tests verified clean-room (0 game data).

Verdict: APPROVED

@whoalin1

Copy link
Copy Markdown
Contributor

AI-assisted independent GPU verification (overnight shift, worker w4).

Environment: Windows, Git Bash; runtime built RelWithDebInfo (Ninja, VS2022 BuildTools, SDK 10.0.26100, Vulkan SDK 1.4.363.0, LLVM_MC=WSL llvm-21, spirv-val/glslangValidator on PATH so the translated-draw ctest targets were built).

AMD Radeon iGPU (RDNA2 gfx1035, driver 24.10.24, Vulkan 1.3.280, 128B push constants):
VK_LOADER_DRIVERS_SELECT=*amd-vulkan* LEONIDA_VK_DEVICE=radeon VK_LOADER_LAYERS_DISABLE=~implicit~ LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1 py -3.14 -I scripts/run_gpu_tests.py --work <dir>

NVIDIA RTX 4070 SUPER (driver 610.88):
VK_LOADER_DRIVERS_SELECT=*nv-vk* LEONIDA_VK_DEVICE=4070 VK_LOADER_LAYERS_DISABLE=~implicit~ py -3.14 -I scripts/run_gpu_tests.py --work <dir>

  • 203/252 passed, 49 skipped, 0 failed. 0 validation errors.

Translator tests:

  • test_loop_iteration_guard.py: 3/3 OK; test_control_flow_branches.py: 33/33 OK; test_make_game_translator_flags.py: 34/34 OK.
  • Full suite py -3.14 -I scripts/run_semantic_tests.py tests/semantic → 471 files, 6171 tests, 0 failed.

Verdict: verified. Loop-visit bounding at the structured exit causes no regressions on either GPU; the only AMD failures are the documented mesh-shader driver crashes.

@iFeyz

iFeyz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

AMD run (the gap the description lists): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, MSVC 2022 RelWithDebInfo.

Head 10ad8809b:

  • scripts/run_gpu_tests.py with this translator: 261 of 261 shaders translated and spirv-val-clean, 236 of 252 pass, 16 skipped, 0 failed (native, subgroup 64).
  • test_control_flow_branches.py (33), test_loop_iteration_guard.py (3), tests/test_make_game_translator_flags.py (34): OK.

@Deedlyy

Deedlyy commented Nov 24, 2026

Copy link
Copy Markdown

Recommendation: [MERGE]

Reasoning: Adds a structured loop-iteration guard pass (tools/spirv_loop_guard.py) so a non-terminating shader loop exits at its structured exit instead of hanging the GPU. Also touches .github/workflows/runtime.yml, make_game.py and the cmake deps, so a maintainer should eyeball the workflow change. Very well verified: full semantic suite 6171 tests, NVIDIA, AMD RDNA2 iGPU and RX 9070 XT runs with no new failures. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main. Merge once the queued checks are green.

Impact level: 6/10

@akimfomin

akimfomin commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Moriz82, PortBridge used the boundary work from the merged XCL #2034 to check Continued's loop guard on exact 10ad8809 and main 3ff2a2a. Both count the first real header visit and leave a finite acyclic backward-address branch uncharged.

One different result appears only on a forced exit. A finite synthetic Wave64 loop would naturally leave after five visits. With --loop-limit 4, native64 and emulated64 run four body writes. Native64 skips the post-loop marker store, while emulated64 writes it on all 64 lanes. The modes agree on natural exits within the budget, including four visits at limit four.

At the forced merge, native64 checks the retained guest PC before entering the post-loop block. Emulated64 enters that block directly. Both generated modules passed Vulkan 1.3 validation. The exact checks executed in the CPU interpreter. Is the difference in post-loop side effects intended when the limit fires? Fixture and exact logs

Vulkan follow-up on RTX 4070 Ti SUPER (driver 617.14). On exact 10ad8809, natural three-visit exits wrote 3/42 in both modes. With limit 4 and a natural exit at 5, all lanes wrote count 4. Native Wave32 at host subgroup 32 left the post-loop marker untouched in all 32 lanes, while Wave64 emulation on host subgroup 32 wrote 42 in all 64 lanes. Native Wave32 with the limit disabled wrote 5/42. All five dispatches completed with zero Khronos diagnostics. The guard withheld the limit-disabled emulated dispatch at process admission. Its CPU interpreter and SPIR-V validation checks passed. Source and raw results

AI assistance from OpenAI Codex (GPT-6 family) for the cross-project review and synthetic verification.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
A loop with an unreachable exit can keep a translated invocation running.
Count header visits per invocation and loop entry. At the configured
bound, take the structured merge. The default is 65536 visits and zero
disables the guard.

The unbounded dispatch fixture timed out at both wave widths on main.
The guarded fixture terminated and preserved finite results in budget.
All 6171 semantic tests ran with no failing files. The Windows runtime
build, tracked-file guard and dependency checks passed.

AI-assisted (Codex and Claude Code agents).

Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Moriz82
Moriz82 force-pushed the split/shader-loop-guard branch from 10ad880 to 30baacf Compare November 25, 2026 03:40
@Moriz82

Moriz82 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

@akimfomin Thanks for the boundary fixture. I reran it on current main at bfef489c. At limit 4 with a natural exit at 5, both modes wrote count 4. Native skipped the marker and emulation wrote 42. All natural exits in the fixture agreed. #984 already brought the guard into main. The rebased head 30baacf9 documents the forced-exit behavior and adds coverage that make_game.py forwards the loop limit. The loop, structured-control-flow, and flag suites passed 3, 33, and 35 tests respectively.

AI-assisted.

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@aslan2142 aslan2142 added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@Moriz82 Moriz82 closed this by deleting the head repository Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants