fix(shader): never branch conditionally to one label twice #4580

Merged
coltken95 merged 2 commits from upstream-pr/4580 into main 2026-10-13 03:52:49 +00:00
coltken95 commented 2026-10-12 16:08:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-12T16:08:38Z.


What

SPIR-V 1.6 requires the true and false labels of OpBranchConditional to differ. The recompiler produced the same label for both in two places:

  • SpirvFlowEmitter.cpp: a conditional branch whose targets resolve to the same block, such as s_cbranch_scc0 to the next instruction. It now emits OpBranch, keeping OpLoopMerge on a loop header.
  • SpirvSpecialization.cpp: when specialization folds a branch on a constant but must keep the OpSelectionMerge (the taken path is not linear), it named the taken label twice. For SPIR-V 1.6 modules the branch is now left as it is, so the control flow and the untaken arm stay. Earlier versions, which allow one label twice, keep the previous rewrite and still drop the untaken arm. Pointing the untaken side at the merge block instead is not an option: it adds an edge from the header to the merge block, which the merge block's phis have no operand for, and values from the taken arm no longer dominate it.

With SPIR-V validation enabled these shaders fail to recompile; without SPIRV-Tools (the default build) the invalid module is handed to the driver.

#4431 also touches SpirvSpecialization.cpp; this change applies cleanly on top of it, so either can merge first. #4600 fixes a separate specialization bug and merges cleanly with this one.

Tested

Linux (Ubuntu 24.04, GCC 13.3), homebrew compute shaders, no title.

  • New agc_spirv_branch_targets test: recompiles a zero-offset s_cbranch_scc0 and a store through a descriptor with an unaligned base, and fails if any OpBranchConditional names one label twice. Each case fails without its fix.
  • New testSpecializedSelectionExitSpirv16 in agc_spirv_optimizer_tests: a SPIR-V 1.6 selection folded on OpConstantTrue with a break and a nested selection in the taken arm, and a phi in the merge block. The result must validate for Vulkan 1.3 and be stable under a second specialization. It fails on main (same label) and with a merge-block redirect (phi predecessor count).
  • Recompiling 20000 mutated compute shaders with validation on, together with #4600: 284 were rejected before, none after.
  • All 449 agc_* ctest entries pass in a RelWithDebInfo build with ANYPS5_ENABLE_SPIRV_TOOLS=ON (GPU tests skip without a device).
  • tools/check_conventions.py passes.
  • Windows not built locally.

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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4832001971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4580). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-12T16:08:38Z. --- ### What SPIR-V 1.6 requires the true and false labels of `OpBranchConditional` to differ. The recompiler produced the same label for both in two places: - **`SpirvFlowEmitter.cpp`**: a conditional branch whose targets resolve to the same block, such as `s_cbranch_scc0` to the next instruction. It now emits `OpBranch`, keeping `OpLoopMerge` on a loop header. - **`SpirvSpecialization.cpp`**: when specialization folds a branch on a constant but must keep the `OpSelectionMerge` (the taken path is not linear), it named the taken label twice. For SPIR-V 1.6 modules the branch is now left as it is, so the control flow and the untaken arm stay. Earlier versions, which allow one label twice, keep the previous rewrite and still drop the untaken arm. Pointing the untaken side at the merge block instead is not an option: it adds an edge from the header to the merge block, which the merge block's phis have no operand for, and values from the taken arm no longer dominate it. With SPIR-V validation enabled these shaders fail to recompile; without SPIRV-Tools (the default build) the invalid module is handed to the driver. #4431 also touches `SpirvSpecialization.cpp`; this change applies cleanly on top of it, so either can merge first. #4600 fixes a separate specialization bug and merges cleanly with this one. ### Tested Linux (Ubuntu 24.04, GCC 13.3), homebrew compute shaders, no title. - New `agc_spirv_branch_targets` test: recompiles a zero-offset `s_cbranch_scc0` and a store through a descriptor with an unaligned base, and fails if any `OpBranchConditional` names one label twice. Each case fails without its fix. - New `testSpecializedSelectionExitSpirv16` in `agc_spirv_optimizer_tests`: a SPIR-V 1.6 selection folded on `OpConstantTrue` with a break and a nested selection in the taken arm, and a phi in the merge block. The result must validate for Vulkan 1.3 and be stable under a second specialization. It fails on `main` (same label) and with a merge-block redirect (phi predecessor count). - Recompiling 20000 mutated compute shaders with validation on, together with #4600: 284 were rejected before, none after. - All 449 `agc_*` ctest entries pass in a RelWithDebInfo build with `ANYPS5_ENABLE_SPIRV_TOOLS=ON` (GPU tests skip without a device). - `tools/check_conventions.py` passes. - Windows not built locally. ### Checklist - [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) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb
github-actions[bot] commented 2026-10-12 17:51:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T17:51:09Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4288 core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp L285
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6129611274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4580#issuecomment-6129611274). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T17:51:09Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4288 | `core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp` [L285](https://github.com/boykopovar/AnyPS5/blob/6124bf063f6348e4451ad0b9c67f82acec5bc1c2/core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cpp#L285) | | |
kr4y4 commented 2026-10-13 01:46:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T01:46:36Z.


A Windows run with and without SPIRV-Tools, and the two test shaders on NVIDIA and Intel drivers.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head 1285ee06f merged into main 284a1c334.

  • Default build: agc_spirv_branch_targets passes with --repeat until-fail:3, and the full suite (ctest --timeout 120 --parallel 4) passes 1021 of 1021 on both GPUs. With SpirvFlowEmitter.cpp and SpirvSpecialization.cpp as on main, it fails with s_cbranch_scc0 to the next instruction: OpBranchConditional with the same true and false label.
  • With ANYPS5_ENABLE_SPIRV_TOOLS=ON: agc_spirv_branch_targets and the three agc_spirv_optimizer tests pass with --repeat until-fail:3. Each case needs its own file:
    • With only SpirvFlowEmitter.cpp as on main, the s_cbranch_scc0 case fails validation with In SPIR-V 1.6 or later, True Label and False Label must be different labels. The optimizer tests pass.
    • With only SpirvSpecialization.cpp as on main, the unaligned-store case and specialized SPIR-V 1.6 selection exit fail with the same message.
  • Together with #4600: the branches merge cleanly in either order, and the four tests pass.

On real devices the runtime doesn't target SPIR-V 1.6. VulkanDevice.cpp:3019 sets Vulkan 1.1, with SPIR-V 1.4 when mesh shaders are enabled and 1.3 otherwise, so Target() is 1.4 on the RTX 4070 and 1.3 on the Intel. I recompiled the two shaders from SpirvBranchTargets.cpp with device->Target(). The descriptor was the same, with its base moved to a real buffer and still 3 bytes off alignment. I dispatched both under the Khronos validation layer 1.4.363:

  • On main the two modules have 1 and 3 OpBranchConditional that name one label twice. spirv-val --target-env vulkan1.3 accepts all four modules, the layer reports nothing, and every dispatch completes. The two NVIDIA modules from main, with only the header version changed to 1.6, are rejected with the message above.
  • With this branch, the s_cbranch_scc0 module has no conditional branch, and the unaligned-store module keeps its 3, as the description says for versions before 1.6.

So with today's device target, the modules that reach these drivers are valid. The fix matters for SPIR-V 1.6 targets: the tests now, and the runtime if it moves to Vulkan 1.3.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136279021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4580#issuecomment-6136279021). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-13T01:46:36Z. --- A Windows run with and without SPIRV-Tools, and the two test shaders on NVIDIA and Intel drivers. Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head 1285ee06f merged into `main` 284a1c334. - Default build: `agc_spirv_branch_targets` passes with `--repeat until-fail:3`, and the full suite (`ctest --timeout 120 --parallel 4`) passes 1021 of 1021 on both GPUs. With `SpirvFlowEmitter.cpp` and `SpirvSpecialization.cpp` as on `main`, it fails with `s_cbranch_scc0 to the next instruction: OpBranchConditional with the same true and false label`. - With `ANYPS5_ENABLE_SPIRV_TOOLS=ON`: `agc_spirv_branch_targets` and the three `agc_spirv_optimizer` tests pass with `--repeat until-fail:3`. Each case needs its own file: - With only `SpirvFlowEmitter.cpp` as on `main`, the `s_cbranch_scc0` case fails validation with `In SPIR-V 1.6 or later, True Label and False Label must be different labels`. The optimizer tests pass. - With only `SpirvSpecialization.cpp` as on `main`, the unaligned-store case and `specialized SPIR-V 1.6 selection exit` fail with the same message. - Together with #4600: the branches merge cleanly in either order, and the four tests pass. On real devices the runtime doesn't target SPIR-V 1.6. `VulkanDevice.cpp:3019` sets Vulkan 1.1, with SPIR-V 1.4 when mesh shaders are enabled and 1.3 otherwise, so `Target()` is 1.4 on the RTX 4070 and 1.3 on the Intel. I recompiled the two shaders from `SpirvBranchTargets.cpp` with `device->Target()`. The descriptor was the same, with its base moved to a real buffer and still 3 bytes off alignment. I dispatched both under the Khronos validation layer 1.4.363: - On `main` the two modules have 1 and 3 `OpBranchConditional` that name one label twice. `spirv-val --target-env vulkan1.3` accepts all four modules, the layer reports nothing, and every dispatch completes. The two NVIDIA modules from `main`, with only the header version changed to 1.6, are rejected with the message above. - With this branch, the `s_cbranch_scc0` module has no conditional branch, and the unaligned-store module keeps its 3, as the description says for versions before 1.6. So with today's device target, the modules that reach these drivers are valid. The fix matters for SPIR-V 1.6 targets: the tests now, and the runtime if it moves to Vulkan 1.3. Written with AI assistance.
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!4580
No description provided.