fix(shader): keep the back-edge of a loop whose continue block is dead #4600
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4600"
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:36:10Z.
What
Pipeline specialization (
SpirvSpecialization.cpp,prune) replaced every unreachable block withOpUnreachable. When a loop's continue target was unreachable, that removed the header's only back-edge, and validation rejects the module: "Loop header is targeted by 0 back-edge blocks but the standard requires exactly one". This happens when the loop condition compares two constants, e.g.s_cmp_lt_u32with an inline constant and a literal: either the emitter already exits the latch unconditionally, or specialization folds the latch's branch. Without SPIRV-Tools (the default build) the invalid module is handed to the driver.An unreachable continue target of a live loop header now keeps an
OpBranchto its header, as SPIRV-Tools' dead branch elimination does. The header's phis keep an operand for that predecessor, naming the phi itself: the edge is never taken and the continue block's values are gone, and the validator requires an operand for every predecessor, reachable or not.The phi rewrite now compares contents rather than sizes, since replacing that operand keeps the size.
Independent of #4580 and compatible with #4431 (its cleanup removes only unreferenced
OpUnreachableblocks; the kept continue block is referenced byOpLoopMerge); both merge cleanly with this branch.Tested
Linux (Ubuntu 24.04, GCC 13.3), homebrew compute shaders, no title.
agc_spirv_optimizer_tests(testSpecializedDeadContinue): a loop whose latch exits onOpConstantFalse, and one whose continue target is already unreachable, both with a header phi fed by the continue block. Both fail validation after specialization without the change and pass with it; a second specialization leaves the result unchanged.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:18Z.
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.cppL243Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T01:46:35Z.
A Windows run of the new case with
ANYPS5_ENABLE_SPIRV_TOOLS=ON, since the CI build jobs were skipped for this PR.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
52b679c55merged intomain284a1c334.agc_spirv_optimizer,agc_spirv_optimizer_fullandagc_spirv_optimizer_nonepass with--repeat until-fail:3. WithSpirvSpecialization.cppas onmain, both new cases fail in all three:Loop header '12[%12]' is targeted by 0 back-edge blocks but the standard requires exactly one.ctest --timeout 120 --parallel 4) passes 1020 of 1020 on both GPUs.agc_spirv_branch_targetspass with--repeat until-fail:3.Written with AI assistance.