fix(shader): never branch conditionally to one label twice #4580
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4580"
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?
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
OpBranchConditionalto 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 ass_cbranch_scc0to the next instruction. It now emitsOpBranch, keepingOpLoopMergeon a loop header.SpirvSpecialization.cpp: when specialization folds a branch on a constant but must keep theOpSelectionMerge(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.
agc_spirv_branch_targetstest: recompiles a zero-offsets_cbranch_scc0and a store through a descriptor with an unaligned base, and fails if anyOpBranchConditionalnames one label twice. Each case fails without its fix.testSpecializedSelectionExitSpirv16inagc_spirv_optimizer_tests: a SPIR-V 1.6 selection folded onOpConstantTruewith 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 onmain(same label) and with a merge-block redirect (phi predecessor count).agc_*ctest entries pass in a RelWithDebInfo build withANYPS5_ENABLE_SPIRV_TOOLS=ON(GPU tests skip without a device).tools/check_conventions.pypasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01VZWLdTjf1S57iLgn2rcaFb
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.
main)core/libs/prx/libSceAgcDriver/tests/SpirvOptimizer.cppL285Imported 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
1285ee06fmerged intomain284a1c334.agc_spirv_branch_targetspasses with--repeat until-fail:3, and the full suite (ctest --timeout 120 --parallel 4) passes 1021 of 1021 on both GPUs. WithSpirvFlowEmitter.cppandSpirvSpecialization.cppas onmain, it fails withs_cbranch_scc0 to the next instruction: OpBranchConditional with the same true and false label.ANYPS5_ENABLE_SPIRV_TOOLS=ON:agc_spirv_branch_targetsand the threeagc_spirv_optimizertests pass with--repeat until-fail:3. Each case needs its own file:SpirvFlowEmitter.cppas onmain, thes_cbranch_scc0case fails validation withIn SPIR-V 1.6 or later, True Label and False Label must be different labels. The optimizer tests pass.SpirvSpecialization.cppas onmain, the unaligned-store case andspecialized SPIR-V 1.6 selection exitfail with the same message.On real devices the runtime doesn't target SPIR-V 1.6.
VulkanDevice.cpp:3019sets Vulkan 1.1, with SPIR-V 1.4 when mesh shaders are enabled and 1.3 otherwise, soTarget()is 1.4 on the RTX 4070 and 1.3 on the Intel. I recompiled the two shaders fromSpirvBranchTargets.cppwithdevice->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:mainthe two modules have 1 and 3OpBranchConditionalthat name one label twice.spirv-val --target-env vulkan1.3accepts all four modules, the layer reports nothing, and every dispatch completes. The two NVIDIA modules frommain, with only the header version changed to 1.6, are rejected with the message above.s_cbranch_scc0module 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.