Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
coltken95:fix/shader-same-target-branch
Oct 13, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
coltken95:fix/shader-same-target-branch

Conversation

@coltken95

@coltken95 coltken95 commented Oct 12, 2026 •

Copy link
Copy Markdown

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 fix(shader): keep the back-edge of a loop whose continue block is dead #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

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

- The structured terminator emitter, for a conditional branch whose
  targets are the same block, such as s_cbranch_scc0 to the next
  instruction. It now emits OpBranch, keeping OpLoopMerge on a loop
  header.
- Pipeline specialization, when it folded a branch on a constant and
  kept the selection merge because the taken path is not linear. It
  wrote the taken label twice; the untaken side now names the
  selection's merge block, which the constant condition never takes.

Found by recompiling mutated compute shaders with SPIR-V validation on;
before this, 169 of 20000 were rejected for this rule, and builds
without SPIRV-Tools handed the module to the driver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb
… one label

The previous commit pointed the untaken side of a folded branch that
keeps its selection merge at the merge block. That adds an edge from the
header to the merge block: the merge block's phis have no operand for it,
and values from the taken arm no longer dominate the merge block, as
fuzzing with validation on found once the repeated label stopped hiding
it.

For SPIR-V 1.6 the branch is now left as it is, so the control flow and
the untaken arm stay; earlier versions keep naming the taken label twice,
which they allow, and drop the untaken arm as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb
@github-actions github-actions Bot added build shader core/shader agc libSceAgc* labels Oct 12, 2026
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

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

@kr4y4

kr4y4 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

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 1285ee0 merged into main 284a1c3.

  • 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 fix(shader): keep the back-edge of a loop whose continue block is dead #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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants