Repository navigation
fix(shader): never branch conditionally to one label twice - #4580
Conversation
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
|
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.
|
|
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
On real devices the runtime doesn't target SPIR-V 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. |
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