fix(shader): read the half-wave scans of wave64 reductions on any host subgroup #1268

Merged
Celegans12 merged 2 commits from upstream-pr/1268 into main 2026-10-08 10:58:19 +00:00
Celegans12 commented 2026-10-07 20:36:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-07T20:36:23Z.


What

GFX10 lowers a wave64 reduction like WaveActiveMin to a DPP row_shr 1/2/4/8 scan and v_permlanex16_b32 under 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).

  • EmitReadLane now 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 become OpGroupNonUniform* Reduces over invocations 0-31 and 32-63; on 32 or fewer, lane 31 reduces all keys and lane 63 is the identity.
  • Other shapes (EXEC not -1, keys from a compare or 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 does v_permlanex16_b32 on 16 lanes or fewer.
  • VulkanDevice reports GroupNonUniformArithmetic and ShaderValidation checks it. TechnicalDebt notes the one shape that reads past a narrower subgroup.
  • PPSA26344 v1.008: six pixel programs use it, among them the terrain G-buffer pass (0x8000273400) and four Terrain Clipmap Update programs; about 10 draws a frame are skipped.
  • #1252 adds <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

  • Windows 11, MinGW-w64 GCC 15.2.0, based on main 7ca17997: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:
  • 419/419 tests pass (ctest --output-on-failure)
  • Same build on lavapipe (Mesa 26.2.4 llvmpipe, 8-wide subgroups, VK_DRIVER_FILES): 418/418 pass; agc_driver_flat_store is left out there because it takes several minutes on Windows llvmpipe
  • agc_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's pswave32 situation; the 64-lane layout fails on the previous head.
  • agc_shader_memory: the Reduces on 32 and 64 lanes, near misses. Both and agc_driver_graphics fail on main.
  • RADV (fisherjoey, 7600X iGPU, Mesa 26.2.1) failed the previous head, also with pswave32.
  • PPSA26344 v1.008, previous head (32-lane hosts get the same reductions): no v_readlane_b32 of lane 63 skips in 2,700 s (3,136 without); 0x8000273400 builds.
  • Not tested: RADV with this head, console behaviour.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4778173713 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1268). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-07T20:36:23Z. --- ### What GFX10 lowers a wave64 reduction like `WaveActiveMin` to a DPP `row_shr` 1/2/4/8 scan and `v_permlanex16_b32` under 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). - `EmitReadLane` now 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 become `OpGroupNonUniform*` Reduces over invocations 0-31 and 32-63; on 32 or fewer, lane 31 reduces all keys and lane 63 is the identity. - Other shapes (EXEC not -1, keys from a compare or `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 does `v_permlanex16_b32` on 16 lanes or fewer. - `VulkanDevice` reports `GroupNonUniformArithmetic` and `ShaderValidation` checks it. TechnicalDebt notes the one shape that reads past a narrower subgroup. - PPSA26344 v1.008: six pixel programs use it, among them the terrain G-buffer pass (0x8000273400) and four Terrain Clipmap Update programs; about 10 draws a frame are skipped. - #1252 adds `<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 - Windows 11, MinGW-w64 GCC 15.2.0, based on `main` 7ca17997: configure `-DBUILD_TESTING=ON`, full build, `--target libs`, `ctest`: - 419/419 tests pass (`ctest --output-on-failure`) - Same build on lavapipe (Mesa 26.2.4 llvmpipe, 8-wide subgroups, `VK_DRIVER_FILES`): 418/418 pass; `agc_driver_flat_store` is left out there because it takes several minutes on Windows llvmpipe - `agc_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's `pswave32` situation; the 64-lane layout fails on the previous head. - `agc_shader_memory`: the Reduces on 32 and 64 lanes, near misses. Both and `agc_driver_graphics` fail on `main`. - RADV (fisherjoey, 7600X iGPU, Mesa 26.2.1) failed the previous head, also with pswave32. - PPSA26344 v1.008, previous head (32-lane hosts get the same reductions): no `v_readlane_b32 of lane 63` skips in 2,700 s (3,136 without); 0x8000273400 builds. - Not tested: RADV with this head, console behaviour. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
fisherjoey commented 2026-10-08 05:55:10 +00:00 (Migrated from github.com)

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_reduction passes on NVIDIA and fails on RADV.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main df16c4c2, plus #1329. Without #1329 the NVIDIA proprietary driver fails 100 GPU tests on main from the dma-buf probe.

result
CPU tests (-E agc_driver) 247 / 247
RTX 5070 Ti, driver 595.91.07, -R agc_driver -j1 168 / 168
RADV, Ryzen 5 7600X iGPU (RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.1, -R agc_driver -j1 167 / 168: agc_driver_pixel_wave_reduction fails

On RADV it fails in every run (6 of 6 in a row). The value it reads changes between runs:

AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 0, the wave holds 18
AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 52, the wave holds 18
AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 255, the wave holds 18

The device log reports subgroup=64 and Compute wave32 programs run on subgroups of 32. With RADV_PERFTEST=pswave32 it 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, including agc_driver_graphics, pass on RADV.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6053370780 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1268#issuecomment-6053370780). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T05:55:10Z. --- Tested on Linux, which the PR lists as not tested. The new `agc_driver_pixel_wave_reduction` passes on NVIDIA and fails on RADV. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` df16c4c2, plus #1329. Without #1329 the NVIDIA proprietary driver fails 100 GPU tests on `main` from the dma-buf probe. | | result | |---|---| | CPU tests (`-E agc_driver`) | 247 / 247 | | RTX 5070 Ti, driver 595.91.07, `-R agc_driver -j1` | 168 / 168 | | RADV, Ryzen 5 7600X iGPU (RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.1, `-R agc_driver -j1` | 167 / 168: `agc_driver_pixel_wave_reduction` fails | On RADV it fails in every run (6 of 6 in a row). The value it reads changes between runs: ``` AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 0, the wave holds 18 AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 52, the wave holds 18 AGC graphics: pixel wave reduction UMin: pixel 584 of a wave of 46 covered pixels read 255, the wave holds 18 ``` The device log reports `subgroup=64` and `Compute wave32 programs run on subgroups of 32`. With `RADV_PERFTEST=pswave32` it 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, including `agc_driver_graphics`, pass on RADV.
Celegans12 commented 2026-10-08 08:24:05 +00:00 (Migrated from github.com)

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_b32 and v_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_reduction on the 7600X iGPU once more on 0bcd0514, once as is and once with RADV_PERFTEST=pswave32?

ctest --test-dir build -R '^agc_driver_pixel_wave_reduction$' -V 2>&1 | grep -E 'laid out|tests passed|wave reduction'
RADV_PERFTEST=pswave32 ctest --test-dir build -R '^agc_driver_pixel_wave_reduction$' -V 2>&1 | grep -E 'laid out|tests passed|wave reduction'

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
  • The old gate was HostSubgroupNarrowerThanWave in EmitReadLane (SpirvAluEmitterMath.cpp). It is now stage != Compute && laneCount == 1, and #997's throw for lanes outside the subgroup still only fires on narrower hosts.
  • RADV reports subgroupSize 64 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.
  • With bound_ctrl and no fetch-inactive, the v_permlanex16_b32 emulation 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.
  • The v_readlane_b32 emulation is an OpGroupNonUniformShuffle. 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 into v_readlane_b32, which reads the VGPR whatever is in it. That would explain why the value changed between runs.
  • The DPP row_shr steps 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.
  • On 64 lanes it now emits Reduce(Select(id >= 32, identity, key)) for lane 31 and Reduce(Select(id >= 32, key, identity)) for lane 63. agc_shader_memory checks that SPIR-V shape for all seven ops.
  • Without subgroup arithmetic, or on a subgroup wider than 64 lanes, the sequence now throws instead of falling back to the plain emulation.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055836953 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1268#issuecomment-6055836953). Originally posted by [Celegans12](https://github.com/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_b32` and `v_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_reduction` on the 7600X iGPU once more on 0bcd0514, once as is and once with `RADV_PERFTEST=pswave32`? ``` ctest --test-dir build -R '^agc_driver_pixel_wave_reduction$' -V 2>&1 | grep -E 'laid out|tests passed|wave reduction' RADV_PERFTEST=pswave32 ctest --test-dir build -R '^agc_driver_pixel_wave_reduction$' -V 2>&1 | grep -E 'laid out|tests passed|wave reduction' ``` 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><summary>Details</summary> - The old gate was `HostSubgroupNarrowerThanWave` in `EmitReadLane` (`SpirvAluEmitterMath.cpp`). It is now `stage != Compute && laneCount == 1`, and #997's throw for lanes outside the subgroup still only fires on narrower hosts. - RADV reports `subgroupSize` 64 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. - With bound_ctrl and no fetch-inactive, the `v_permlanex16_b32` emulation 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. - The `v_readlane_b32` emulation is an `OpGroupNonUniformShuffle`. 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 into `v_readlane_b32`, which reads the VGPR whatever is in it. That would explain why the value changed between runs. - The DPP `row_shr` steps 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. - On 64 lanes it now emits `Reduce(Select(id >= 32, identity, key))` for lane 31 and `Reduce(Select(id >= 32, key, identity))` for lane 63. `agc_shader_memory` checks that SPIR-V shape for all seven ops. - Without subgroup arithmetic, or on a subgroup wider than 64 lanes, the sequence now throws instead of falling back to the plain emulation. </details>
github-actions[bot] commented 2026-10-08 10:20:27 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#1252 core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp L5-6
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6057745649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1268#issuecomment-6057745649). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T10:20:27Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1252 | `core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp` [L5-6](https://github.com/boykopovar/AnyPS5/blob/81f31ef1a92f42277b820856eaaec0d98a0ab30f/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp#L5-L6) | | |
adriadam10 commented 2026-10-08 10:48:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T10:48:16Z.


Checked on our side:

  • HW oracle (gfx1035, wave64 kernel running this exact sequence): UMin/UMax/SMin/SMax/IAdd/AND/OR over 64 EXEC masks (full, empty, one half only, lane 31/63 alone, random) with edge keys. v_readlane_b32 31/63 match "reduce of lanes 0-31 / 32-63 with the identity outside the entry EXEC" in every case, so this lowering is right.
  • Build + full ctest with this merged onto main. agc_driver_pixel_wave_reduction runs 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 current main; merging once CI is green. #1252 will need the one-line include rebase you mentioned.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058195701 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1268#issuecomment-6058195701). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T10:48:16Z. --- Checked on our side: - HW oracle (gfx1035, wave64 kernel running this exact sequence): UMin/UMax/SMin/SMax/IAdd/AND/OR over 64 EXEC masks (full, empty, one half only, lane 31/63 alone, random) with edge keys. `v_readlane_b32` 31/63 match "reduce of lanes 0-31 / 32-63 with the identity outside the entry EXEC" in every case, so this lowering is right. - Build + full ctest with this merged onto `main`. `agc_driver_pixel_wave_reduction` runs 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 current `main`; merging once CI is green. #1252 will need the one-line include rebase you mentioned.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1268
No description provided.