fix(shader): read the half-wave scans of wave64 reductions on any host subgroup #1268
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1268"
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 Celegans12 on 2026-10-07T20:36:23Z.
What
GFX10 lowers a wave64 reduction like
WaveActiveMinto a DPProw_shr1/2/4/8 scan andv_permlanex16_b32under EXEC -1, then joins lanes 31 and 63. The console fills lanes outside the entry EXEC with the identity. On the host they can be helpers or never-launched invocations, so on 64 lanes (RADV) the emulated scan reads 0 or stale values, and on 32 lanes lane 63 throws (#997).EmitReadLanenow recognises the sequence on any host for UMin, UMax, SMin, SMax, IAdd, AND and OR when the key is the op's identity outside the entry EXEC ballot. On 64 lanes, lanes 31 and 63 becomeOpGroupNonUniform*Reduces over invocations 0-31 and 32-63; on 32 or fewer, lane 31 reduces all keys and lane 63 is the identity.s_wqm_b64, a missing DPP step) keep the old paths. Without subgroup arithmetic, or on a subgroup wider than the wave, the sequence throws; so doesv_permlanex16_b32on 16 lanes or fewer.VulkanDevicereportsGroupNonUniformArithmeticandShaderValidationchecks it. TechnicalDebt notes the one shape that reads past a narrower subgroup.<algorithm>to the same include block, so whichever lands second needs a one-line rebase. #448, #639, #757-#759, #920, #1074 and #1077 touch these files without conflicting.Tested
main7ca17997: configure-DBUILD_TESTING=ON, full build,--target libs,ctest:ctest --output-on-failure)VK_DRIVER_FILES): 418/418 pass;agc_driver_flat_storeis left out there because it takes several minutes on Windows llvmpipeagc_driver_pixel_wave_reduction: exact UMin, UMax and IAdd on partial waves, laid out for the device and for 64 lanes, which on 32 or 8 real lanes is RADV'spswave32situation; the 64-lane layout fails on the previous head.agc_shader_memory: the Reduces on 32 and 64 lanes, near misses. Both andagc_driver_graphicsfail onmain.v_readlane_b32 of lane 63skips in 2,700 s (3,136 without); 0x8000273400 builds.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)Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T05:55:10Z.
Tested on Linux, which the PR lists as not tested. The new
agc_driver_pixel_wave_reductionpasses on NVIDIA and fails on RADV.Nobara 44 (Linux 7.2), GCC 16.2.1, Release,
BUILD_TESTING=ON. This PR merged ontomaindf16c4c2, plus #1329. Without #1329 the NVIDIA proprietary driver fails 100 GPU tests onmainfrom the dma-buf probe.-E agc_driver)-R agc_driver -j1-R agc_driver -j1agc_driver_pixel_wave_reductionfailsOn RADV it fails in every run (6 of 6 in a row). The value it reads changes between runs:
The device log reports
subgroup=64andCompute wave32 programs run on subgroups of 32. WithRADV_PERFTEST=pswave32it still fails 3 of 3, on a different pixel (pixel 776 of a wave of 22 covered pixels read 0, the wave holds 39). The other GPU tests, includingagc_driver_graphics, pass on RADV.Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-08T08:24:05Z.
Thanks for running this on RADV, and for posting both the runs where the value changed and the pswave32 run. Together they pointed straight at a bug in this PR. The reduction was only recognised when the device reports fewer than 64 lanes, and RADV reports 64, even under pswave32, because our fragment pipelines use the fixed subgroup size. So your iGPU ran the plain emulation of the DPP scan,
v_permlanex16_b32andv_readlane_b32. On the console those run under EXEC -1, where lanes without a pixel hold the identity. On the host those lanes are helpers that sit out subgroup operations, or invocations that were never launched. So the permlane read 0 (your UMin "read 0"), and the readlane of lane 63 got whatever that register held (52, 255).0bcd0514 recognises the sequence on any subgroup size. On 64 lanes, lanes 31 and 63 become subgroup reductions over invocations 0-31 and 32-63. Keys outside each half count as the identity, so nothing reads a lane that might not exist. 32-lane devices get the same reductions as before. The test now also lays the program out for 64 lanes on narrower devices, which on my RTX 3080 Laptop is your pswave32 case. The old head failed it the same way ("UMin: pixel 1646 of a wave of 15 covered pixels read 0, the wave holds 219"). The new one passes there, and on lavapipe on Windows and in WSL. What I can't reproduce here is a real 64-lane wave with lanes 32-63 present, which is your default run.
Could you run
agc_driver_pixel_wave_reductionon the 7600X iGPU once more on 0bcd0514, once as is and once withRADV_PERFTEST=pswave32?On RADV I'd expect three "laid out for 64 lanes on 64-lane subgroups" lines (UMin, UMax, IAdd) and "pixel wave reduction tests passed" in both runs.
Details
HostSubgroupNarrowerThanWaveinEmitReadLane(SpirvAluEmitterMath.cpp). It is nowstage != Compute && laneCount == 1, and #997's throw for lanes outside the subgroup still only fires on narrower hosts.subgroupSize64 and allows a required subgroup size only for compute, task and mesh. Our graphics SPIR-V is 1.3 and passes no varying-size flag. So under pswave32 the shader still sees a 64-lane subgroup, but at most 32 invocations, numbered 0-31. Lanes 32-63 never exist there.v_permlanex16_b32emulation takes "source active" from a ballot and returns 0 for a source lane that doesn't run. 0 is the identity for UMax, IAdd and OR, so only UMin, SMin, SMax and AND go wrong that way. The test checks UMin first and stops there, which fits "read 0" in both of your runs.v_readlane_b32emulation is anOpGroupNonUniformShuffle. Its result is undefined for an invocation that isn't active, and as far as I can tell from the Mesa source, ACO turns a constant index intov_readlane_b32, which reads the VGPR whatever is in it. That would explain why the value changed between runs.row_shrsteps also drop partial sums when a lane in the middle of a row is an inactive helper. The new path doesn't use the scan's result at all.Reduce(Select(id >= 32, identity, key))for lane 31 andReduce(Select(id >= 32, key, identity))for lane 63.agc_shader_memorychecks that SPIR-V shape for all seven ops.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T10:20:27Z.
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/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cppL5-6Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T10:48:16Z.
Checked on our side:
v_readlane_b3231/63 match "reduce of lanes 0-31 / 32-63 with the identity outside the entry EXEC" in every case, so this lowering is right.main.agc_driver_pixel_wave_reductionruns on RADV (64 lanes, 516 partial waves) and on CI's lavapipe (8 lanes, 2240 partial waves). All three tests fail without the change.Added one commit of our own: AND and OR rows in
PixelWaveReduction.cpp, so the execution test pins values for 5 of the 7 ops instead of 3. They pass on RADV and lavapipe, and swapping the emitter's And/Or table entries fails them. Rebased onto currentmain; merging once CI is green. #1252 will need the one-line include rebase you mentioned.