fix(shader): keep spanned waves past an OpUnreachable under APS5_SPANNED_UNREACHABLE_STAYS #26716

Merged
muhmuhkuh merged 1 commit from upstream-pr/26716 into main 2026-11-28 01:21:48 +00:00
muhmuhkuh commented 2026-11-24 17:00:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by muhmuhkuh on 2026-11-24T17:00:32Z.


What

The spanned-wave uniformity check (SpirvSpannedUniformity.cpp) counts OpUnreachable as an invocation leaving the program, like OpReturn or OpKill. No invocation executes an OpUnreachable, so it is not an exit.

Since #6416, a faulting BDA read no longer stops its invocation; later guest writes run under if (!faulted). When such a write is a runtime image write, the mode switch of that write puts an OpUnreachable default (SpirvImageEmitter.cpp) inside the per-invocation guard. The check then treats everything after the guard as divergent: a Private word stored afterwards (for example the ballot parity of the spanned ballot) becomes non-uniform, the loops on it become non-uniform, and the next workgroup barrier is refused, so the program falls back to two lanes per invocation although every invocation reaches the barrier.

The only OpUnreachable the emitter writes is the default of a switch on the image-mode specialization constant, and ResourceMaterializer::RuntimeImageMode only returns the index of an emitted mode or throws, so that default is never taken.

Behind APS5_SPANNED_UNREACHABLE_STAYS (off by default, read by the device like APS5_NO_SPANNED_SHUFFLE_EXCHANGE, in the shader disk cache key through SpirvTarget), the check no longer counts OpUnreachable as an exit. OpReturn, OpReturnValue, OpKill and OpTerminateInvocation still count. Each program kept spanned this way prints one [spanned-wave] ... keeps spanned waves past an OpUnreachable line with the reason the old rule would have given.

APS5_VERIFY_SPANNED_UNREACHABLE_STAYS turns the switch on and checks, for every program it keeps spanned, that each OpUnreachable of the module is reached only as the default of a switch on a specialization constant (no other branch or case targets it); otherwise the recompile stops and names the block.

The #6416 masking is unchanged: faulted invocations still skip their later writes.

This is a correctness fix of the uniformity check and makes no speed claim. On main only single-wave (64-thread) wave64 compute programs are spanned, so a 256-thread program is not affected: for the captured PPSA02015 compute program 0xc280575600 (16x16 threads) agc_shader_replay gives the same SPIR-V with the switch unset and set (1234866 words, no fallback line). Together with #23244, which runs each wave of a multi-wave workgroup as its own host workgroup, the same program keeps its split form (4 host workgroups per guest workgroup) instead of falling back to two lanes; that is shown offline in a downstream tree, the game measurement follows.

Tested

Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3080 Ti, driver 610.88.

  • ctest --test-dir build -R "^(spanned_uniformity|agc_driver_faulting_wave_invocation|agc_shader_disk_cache.*|agc_shader_memory.*)$" -V with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, same machine and GPU: main 9/9 passed, this PR 9/9 passed, this PR with both switches set 9/9 passed. Each log shows Insert instance layer "VK_LAYER_KHRONOS_validation", none contains was not found but was requested by env var VK_INSTANCE_LAYERS, and none contains a VUID- line, so the PR adds no validation error.
  • spanned_uniformity: a new case builds a per-invocation guard holding a specialization-constant switch with an OpUnreachable default, then a Private store and a loop with a barrier. It is refused when OpUnreachable counts as an exit (the behaviour without this change) and accepted with the switch; a per-invocation OpReturn is still refused with the switch. The verify check accepts a specialization-constant default and rejects a constant-switch default and a block that is also a case target.
  • Mutants, each turns spanned_uniformity red: the change applied without the switch; OpUnreachable counted as an exit again.
  • Downstream tree with #23244: the captured 0xc280575600 request recompiles to the split form (4 groups, 760442 words) with the switch, against the two-lane fallback without it; the verify check passes; a GPU replay of the program through the driver writes the same 1048576 guest bytes in the split form and in the two-lane fallback.
  • tools/pr_overlap.py --branch reports conflicts with #14218, #15548, #17611, #22432 and #25660, each on lines both sides extend: the SpannedBarrierInDivergentFlow declaration (#14218), the last CacheKey.hpp target fields (#15548, #17611), the expect helper of SpannedUniformityTests.cpp (#22432) and the last SpirvEmitter.hpp target options (#25660). None of them changes how OpUnreachable counts. The 92 pull requests it could not compare (they conflict with main themselves) were checked by diff: none touches SpirvSpannedUniformity, and none adds or removes a line with OpUnreachable, SpannedBarrierInDivergentFlow or Exits(.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 610.88, Windows 11 Pro 10.0.26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5476678552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26716). Originally posted by [muhmuhkuh](https://github.com/muhmuhkuh) on 2026-11-24T17:00:32Z. --- ### What The spanned-wave uniformity check (`SpirvSpannedUniformity.cpp`) counts `OpUnreachable` as an invocation leaving the program, like `OpReturn` or `OpKill`. No invocation executes an `OpUnreachable`, so it is not an exit. Since #6416, a faulting BDA read no longer stops its invocation; later guest writes run under `if (!faulted)`. When such a write is a runtime image write, the mode switch of that write puts an `OpUnreachable` default (`SpirvImageEmitter.cpp`) inside the per-invocation guard. The check then treats everything after the guard as divergent: a Private word stored afterwards (for example the ballot parity of the spanned ballot) becomes non-uniform, the loops on it become non-uniform, and the next workgroup barrier is refused, so the program falls back to two lanes per invocation although every invocation reaches the barrier. The only `OpUnreachable` the emitter writes is the default of a switch on the image-mode specialization constant, and `ResourceMaterializer::RuntimeImageMode` only returns the index of an emitted mode or throws, so that default is never taken. Behind `APS5_SPANNED_UNREACHABLE_STAYS` (off by default, read by the device like `APS5_NO_SPANNED_SHUFFLE_EXCHANGE`, in the shader disk cache key through `SpirvTarget`), the check no longer counts `OpUnreachable` as an exit. `OpReturn`, `OpReturnValue`, `OpKill` and `OpTerminateInvocation` still count. Each program kept spanned this way prints one `[spanned-wave] ... keeps spanned waves past an OpUnreachable` line with the reason the old rule would have given. `APS5_VERIFY_SPANNED_UNREACHABLE_STAYS` turns the switch on and checks, for every program it keeps spanned, that each `OpUnreachable` of the module is reached only as the default of a switch on a specialization constant (no other branch or case targets it); otherwise the recompile stops and names the block. The #6416 masking is unchanged: faulted invocations still skip their later writes. This is a correctness fix of the uniformity check and makes no speed claim. On `main` only single-wave (64-thread) wave64 compute programs are spanned, so a 256-thread program is not affected: for the captured PPSA02015 compute program `0xc280575600` (16x16 threads) `agc_shader_replay` gives the same SPIR-V with the switch unset and set (1234866 words, no fallback line). Together with #23244, which runs each wave of a multi-wave workgroup as its own host workgroup, the same program keeps its split form (4 host workgroups per guest workgroup) instead of falling back to two lanes; that is shown offline in a downstream tree, the game measurement follows. ### Tested Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3080 Ti, driver 610.88. - `ctest --test-dir build -R "^(spanned_uniformity|agc_driver_faulting_wave_invocation|agc_shader_disk_cache.*|agc_shader_memory.*)$" -V` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, same machine and GPU: `main` 9/9 passed, this PR 9/9 passed, this PR with both switches set 9/9 passed. Each log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"`, none contains `was not found but was requested by env var VK_INSTANCE_LAYERS`, and none contains a `VUID-` line, so the PR adds no validation error. - `spanned_uniformity`: a new case builds a per-invocation guard holding a specialization-constant switch with an `OpUnreachable` default, then a Private store and a loop with a barrier. It is refused when `OpUnreachable` counts as an exit (the behaviour without this change) and accepted with the switch; a per-invocation `OpReturn` is still refused with the switch. The verify check accepts a specialization-constant default and rejects a constant-switch default and a block that is also a case target. - Mutants, each turns `spanned_uniformity` red: the change applied without the switch; `OpUnreachable` counted as an exit again. - Downstream tree with #23244: the captured `0xc280575600` request recompiles to the split form (4 groups, 760442 words) with the switch, against the two-lane fallback without it; the verify check passes; a GPU replay of the program through the driver writes the same 1048576 guest bytes in the split form and in the two-lane fallback. - `tools/pr_overlap.py --branch` reports conflicts with #14218, #15548, #17611, #22432 and #25660, each on lines both sides extend: the `SpannedBarrierInDivergentFlow` declaration (#14218), the last `CacheKey.hpp` target fields (#15548, #17611), the `expect` helper of `SpannedUniformityTests.cpp` (#22432) and the last `SpirvEmitter.hpp` target options (#25660). None of them changes how `OpUnreachable` counts. The 92 pull requests it could not compare (they conflict with `main` themselves) were checked by diff: none touches `SpirvSpannedUniformity`, and none adds or removes a line with `OpUnreachable`, `SpannedBarrierInDivergentFlow` or `Exits(`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 610.88, Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-25 07:02:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T07:02:31Z.

Review: COMMENTED


Reviewed — comments

Comments are limited to unverified runtime behavior and one verifier edge case; no blocking defect was confirmed in the reviewed code.

What I ran

Read the prepared diff and targeted source context at head 436d4ec95bd924e22339b1912cabe8c551f4d65c using repo_read.py. No build or tests were run.

Results

Check Result Note
Diff and changed source review passed Targeted review completed
Tests and build not run Reported runs are author supplied

Findings

Blocking

None.

Non-blocking

  • core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp:572: the verifier classifies a switch as specialization-driven when the selector itself is an OpSpecConstant. SPIR-V also permits derived selector expressions based on specialization constants; those valid switches will be rejected by verification even when their default OpUnreachable cannot be selected at runtime. Consider recognizing spec-constant operations or documenting this narrower verifier rule.

Not verified

  • VulkanDevice.cpp is excluded from the review folder due to size; only the changed diff lines were inspected. Its surrounding function and callers were not read.
  • No build, test, or GPU replay was run. The results in the PR description are author reported.
  • The downstream multi-wave behavior and broader interactions with other pull requests were not independently verified.
  • Read full text for ShaderDiskCache.cpp, CacheKey.hpp, Recompiler.cpp, Recompiler.hpp, SpirvEmitter.hpp, SpirvSpannedUniformity.hpp, SpirvEmitter.cpp, SpirvSpannedUniformity.cpp, SpannedUniformityTests.cpp, and ENVIRONMENT.md. VulkanDevice.cpp full text was excluded by size; no other changed file was excluded.

To get this to a good state

Nothing is required. Optionally broaden verifier handling or clarify that only direct specialization-constant selectors are accepted.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6053200606 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26716#pullrequestreview-6053200606). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-25T07:02:31Z. **Review:** COMMENTED --- # Reviewed — comments Comments are limited to unverified runtime behavior and one verifier edge case; no blocking defect was confirmed in the reviewed code. ## What I ran Read the prepared diff and targeted source context at head `436d4ec95bd924e22339b1912cabe8c551f4d65c` using `repo_read.py`. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source review | passed | Targeted review completed | | Tests and build | not run | Reported runs are author supplied | ## Findings ### Blocking None. ### Non-blocking - `core/shader/recompiler/SpirvBackend/src/SpirvSpannedUniformity.cpp:572`: the verifier classifies a switch as specialization-driven when the selector itself is an `OpSpecConstant`. SPIR-V also permits derived selector expressions based on specialization constants; those valid switches will be rejected by verification even when their default `OpUnreachable` cannot be selected at runtime. Consider recognizing spec-constant operations or documenting this narrower verifier rule. ## Not verified - `VulkanDevice.cpp` is excluded from the review folder due to size; only the changed diff lines were inspected. Its surrounding function and callers were not read. - No build, test, or GPU replay was run. The results in the PR description are author reported. - The downstream multi-wave behavior and broader interactions with other pull requests were not independently verified. - Read full text for `ShaderDiskCache.cpp`, `CacheKey.hpp`, `Recompiler.cpp`, `Recompiler.hpp`, `SpirvEmitter.hpp`, `SpirvSpannedUniformity.hpp`, `SpirvEmitter.cpp`, `SpirvSpannedUniformity.cpp`, `SpannedUniformityTests.cpp`, and `ENVIRONMENT.md`. `VulkanDevice.cpp` full text was excluded by size; no other changed file was excluded. ## To get this to a good state Nothing is required. Optionally broaden verifier handling or clarify that only direct specialization-constant selectors are accepted.
MarcZX10 commented 2026-11-26 07:24:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T07:24:07Z.

Review: COMMENTED


Reviewed — no blocking findings

The reviewed implementation and its targeted tests support the stated opt-in behavior; the full Vulkan device file was not available in the review folder, so its surrounding context was not verified.

What I ran

Reviewed head f946bf453799a05e08d808ba82f842223cf4cb6b using the prepared diff and repo_read.py for changed-file context. No commands or tests were run.

Results

Check Result Note
Diff and changed-file inventory passed Reviewed prepared artifacts
Targeted source inspection passed Included changed files inspected
Automated tests not run No test execution performed

Findings

Blocking

None.

Non-blocking

None.

Not verified

core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp was excluded because it was too large for the review folder; only the changed lines shown in the diff were available. Runtime behavior and the author-reported test results were not independently verified. The other changed files listed in manifest.json were included and inspected in relevant portions.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6070001759 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/26716#pullrequestreview-6070001759). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T07:24:07Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The reviewed implementation and its targeted tests support the stated opt-in behavior; the full Vulkan device file was not available in the review folder, so its surrounding context was not verified. ## What I ran Reviewed head `f946bf453799a05e08d808ba82f842223cf4cb6b` using the prepared diff and `repo_read.py` for changed-file context. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed-file inventory | passed | Reviewed prepared artifacts | | Targeted source inspection | passed | Included changed files inspected | | Automated tests | not run | No test execution performed | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` was excluded because it was too large for the review folder; only the changed lines shown in the diff were available. Runtime behavior and the author-reported test results were not independently verified. The other changed files listed in `manifest.json` were included and inspected in relevant portions. ## To get this to a good state Nothing is required.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
boykopovar/AnyPS5!26716
No description provided.