rdna2spirv: bound loop visits at the structured exit (SHADER-LOOP-GUARD) #1101
Closed
leonida/mandy
Moriz82 wants to merge 0 commits from
split/shader-loop-guard into main
pull from: split/shader-loop-guard
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:github-pr/3349
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!1101
Loading…
Reference in a new issue
No description provided.
Delete branch "split/shader-loop-guard"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request closed
This pull request cannot be reopened because the branch was deleted.
Rebased onto
bfef489cat30baacf9. 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
3aa975421for the regression comparison. The child has one commit on that base.LoopGuardTests.test_finite_and_nonterminatingLoopGuardTests.test_dispatch_loop_also_stopsLoopGuardTests.test_inner_budget_resets_on_each_entryMainWiringTest.test_explicit_loop_limit_reaches_the_shaders_stagepython3 -I scripts/run_semantic_tests.py tests/semantic -p test_loop_iteration_guard.py -j 2python3 -I scripts/run_semantic_tests.py tests/semantic -p test_control_flow_branches.py -j 2python3 -I tests/test_make_game_translator_flags.pypython3 -I tests/test_cmake_translator_deps.pyandpython3 -I tests/test_runtime_paths.pypython3 -I -m compileall -q make_game.py tools testspython3 -I <file>orpython3 -I -m pytest -q <file>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 2python3 -I scripts/check_no_game_data.pycmake -S runtime -B runtime/build/bootandcmake --build runtime/build/boot --target gta6rt -j6For the synthetic loop,
spirv-valaccepted 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:
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
docs/design.md.🤖 Generated with Claude Code
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 spython -m unittest test_make_game_translator_flags(fromtests/): 34/34 OK in 0.91 spython -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.)
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.
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-limitconfiguration wired cleanly throughrdna2spirv.pyandmake_game.py. Synthetic.asmfixture and semantic tests verified clean-room (0 game data).Verdict: APPROVED
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>ms_clip,ms_wave_info_s3(SHADER-167; fixes pending in open PRs #802/#808).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>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.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.
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.pywith this translator: 261 of 261 shaders translated andspirv-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.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.pyand 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 withmain. Merge once the queued checks are green.Impact level: 6/10
Moriz82, PortBridge used the boundary work from the merged XCL #2034 to check Continued's loop guard on exact
10ad8809and main3ff2a2a. 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 wrote3/42in both modes. With limit 4 and a natural exit at 5, all lanes wrote count4. Native Wave32 at host subgroup 32 left the post-loop marker untouched in all 32 lanes, while Wave64 emulation on host subgroup 32 wrote42in all 64 lanes. Native Wave32 with the limit disabled wrote5/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 resultsAI assistance from OpenAI Codex (GPT-6 family) for the cross-project review and synthetic verification.
@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 head30baacf9documents the forced-exit behavior and adds coverage thatmake_game.pyforwards the loop limit. The loop, structured-control-flow, and flag suites passed 3, 33, and 35 tests respectively.AI-assisted.