fix(shader): preserve read lane dominance across branches #8089
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8089"
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 MarcZX10 on 2026-10-19T20:57:20Z.
What
Fixes #8050.
ReadLaneEliminatorcould replace a spilledReadLanewith a value written only inside a branch, even when that value did not dominate a read after the merge. The rewritten read now follows the value through control-flow phis, usingUndefU32on paths without a store. The direct rewrite remains for stores that dominate the read. The regression covers both cases.Tested
upstream/main9127ea36: builtagc_read_lane_elimination_tests. On the test-only commit,ctest -R '^agc_read_lane_elimination$'fails on the branch-local-store assertion. With the implementation commit, it passes (1/1).git diff --checkandpython3 tools/check_conventions.py --base upstream/main --head HEAD: passed.ReadLaneEliminator.cpporReadLaneElimination.cpp. It could not compare many PRs that also change the AGCCMakeLists.txt; I manually reviewed those patches and found unrelated source lists and test registrations.8c9161eac, an ancestor of current main. The issue report does not state the OS, and I did not rerun GPU tests on this host. Its reported patch has the same patch ID as this implementation.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:31:32Z.
Windows 11 26300, WinLibs GCC 15.2.0, Release,
BUILD_TESTING=ON,main02fb1ce53with this PR (45c217d4a) merged. RTX 5070 Ti (617.42) and AMD Radeon iGPU1002:13c0(RDNA 2, AMD Windows driver 32.0.21042.62),ANYPS5_REQUIRE_VULKAN=1.agc_read_lane_eliminationpasses. WithReadLaneEliminator.cppreverted tomainit fails ona lane value stored inside a branch reaches the read after the merge through a phi, and the dominating-store case still passes.ctest -R "agc|shader|spirv|recomp" -j6: RTX 834 tests, onlyagc_driver_pass_input_hazardfails, which fails onmainhere too. iGPU 834 tests, onlyagc_driver_mul_lo_u16_high_halfandagc_driver_sgpr_pattern_fill_keysfail, both also onmainhere. No new failures. (agc_driver_image_array_window_past_layer_limitdoesn't compile with MinGW onmain(alignas(65536)), so it is Not Run on both sides.)One case the dominance check still lets through:
dominates()returns true when the value and the read are in the same block, without checking their order. A single-block loop where the spill is read before the value is stored in the same iteration, built with this test'sBuilder:With this PR,
Eliminaterewrites the read (rewrittenReads=1) tostoreditself, which is instruction 3 ofloopwhile the read is instruction 1. So the read now usesstoredbefore it is defined. The first iteration should read the uninitialized slot (UndefU32) and later iterations the previous iteration'sstored, i.e. a phi inloop.maingives the same rewrite, so this is not a regression. It is the same dominance problem in a loop, though.dependsOnonly rejects it whenstoredis computed from the read, as with a spilled loop counter. A fix probably needsAtto tell a block's value at its start (for the read) from its value at its end (for a phi operand from a back edge).AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-20T19:00:46Z.
First Linux run of this PR, on AMD with RADV. Head
45c217d4amerged ontomain2fee39f1b(clean merge), built in full and compared with the samemain, on the same machine.AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.
Full suite (
ANYPS5_REQUIRE_VULKAN=1 ctest -j8) atc4d6c686f: 1716/1717 with this PR, against 1715/1716 onmain. The one failure is the same on both, so this PR adds none:agc_driver_guest_memory_lifetime(#7784, which #7849 addresses).Comparing the JUnit output with
main: the new entry,agc_read_lane_elimination, runs and passes. No other entry is added, and none is skipped or removed, so the change in read lane dominance doesn't alter any GPU test result here.GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:22:43Z.
Follow-up to Wachi35676’s same-block loop example: I pushed
31630fba75to make the unique-value rewrite point-sensitive within its block. A candidate instruction after theReadLaneno longer counts as available at the read; the rewrite builds a phi from the entry value and the loop back-edge store. The new regression checks the first-iterationUndefU32input and the back-edge store input.Validation on this head:
tools/check_compile.pycompiled both changed translation units;git diff --checkandtools/check_conventions.py --base upstream/mainpass. I could not executeagc_read_lane_elimination_testslocally: the stale build directory would rebuild 271 prerequisite objects and this filesystem has 21 MiB available, so I stopped that build before it could fill the disk. The runtime assertion result is pending CI or a supported build host.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T19:30:26Z.
Thanks for
31630fba75. Making availability point-sensitive is the right fix for the same-block loop case: a store after theReadLaneshould no longer count as available at the read, and the back-edge lookup correctly picks up the store as the block's exit value.One problem with where the new phi goes.
At(block, before)now inserts it withInsertInstructionBefore(before, &phi), directly before theReadLane, andbeforeis passed for every rewrite, not only when the value is defined in the same block. So in the branch-merge case from45c217d4af, if any non-phi instruction comes before the read in the merge block, the phi ends up after it. The SPIR-V emitter rejects that (SpirvFlowEmitter.cpp, "appears after a non-Phi instruction"), and I didn't find a later pass that moves phis back to the front. The current tests don't hit it: the new loop test's read sits right after the only phi, and neither test emits SPIR-V.Since the value isn't defined earlier in the block in either case, the value at the read equals the value at block entry, so the phi can always go at the block front (after any existing phis), as before. Could you keep the availability check, place the phi at the front, and add a case where a non-phi instruction precedes the
ReadLane, checking that the inserted phi comes before every non-phi (or emitting SPIR-V for it)?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:48:58Z.
Thanks for catching this. I added a non-phi instruction before the branch-merge
ReadLaneregression and verified the generated phi is placed before the first non-phi instruction. The placement rule now inserts after any existing phis at the block front, including phis created recursively for predecessor paths. The fix and regression are two commits on this PR:78acdedb76and204cbc558d. Both changed translation units compile with the existing/tmp/build-8050-currentcompile commands;git diff --checkandtools/check_conventions.py --base upstream/mainpass. I could not run the focused CTest:agc_read_lane_eliminationis registered, but its executable is missing from that build directory, and only 31 MB is free for a rebuild. The updated hosted checks should provide runtime/build validation.Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-20T20:35:58Z.
@MarcZX10 the run you couldn't do: current head
204cbc558(with31630fba75,78acdedb76and204cbc558d) merged ontomainaa1b1bfce(clean merge), built in full.AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.
agc_read_lane_elimination: passes 3 of 3, and again under the Khronos validation layer.ANYPS5_REQUIRE_VULKAN=1 ctest -j8) at1df694e5b: 1734/1735. The one failure isagc_driver_guest_memory_lifetime(#7784), which fails onmaintoo, so this head adds none.GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T21:27:14Z.
Thanks for building the current head and running the read-lane elimination case three times, including with the Khronos validation layer. The full-suite result also isolates the sole failure to the already known #7784 failure on main; I have not run these GPU tests locally.