fix(shader): fold fixed 32-lane vertex bounds #5914

Merged
fdfawef324 merged 2 commits from upstream-pr/5914 into main 2026-10-16 11:47:57 +00:00
fdfawef324 commented 2026-10-14 15:32:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-14T15:32:04Z.


What

For a vertex shader targeting a fixed 32-lane host subgroup and SPIR-V below 1.6, fold a lane word SelectU32(LaneId < 32, low, high) to its low word when both words are constants. This lets the existing constant and dead-code passes remove a constant lower-half export mask before later safety checks.

Lane words selected from words that are not constants, such as the words of the entry EXEC ballot, keep their LaneId < 32 test. The half-wave scan matcher of the SPIR-V backend recognises a lane word by that select. An earlier head of this PR folded the compare itself. The matcher then missed a wave64 vertex scan keyed by the entry EXEC, and agc_shader_memory failed.

Partial masks and observable lane outputs retain their lane dependence. The fold excludes other stages, unknown/64/single-lane subgroup requests and unknown/SPIR-V 1.6 module versions. This is the independent optimizer follow-up requested in #5466; it has no stencil replay or disk-cache changes.

Tested

Windows Server 2025, RTX 3060, GCC 15.2, build-dev Release (-O3), rebased on main 5050a23f3.

  • agc_constant_bit_test passes. Disabling only this fold makes the fixture fail.
    • The new case a lane word selected from ballot words keeps its lower-half test fails with the earlier fold, which replaced every LaneId < 32 compare.
  • Controls retain partial and single-bit masks, direct lane outputs and lane words from ballot words. They also reject the excluded subgroup, module-version and stage contracts.
  • agc_shader_memory passes.
    • With the earlier fold, on main 7289938ce, it failed in verifyHalfWaveReduction, on the wave64 vertex UMin scan at 32 lanes: v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation.
    • On that main it also passed with the fold call removed from Recompiler.cpp, which matches main's behaviour.
  • 78 ctest registrations of 67 recompiler, vertex, lane and wave test executables ran on the RTX 3060: 77 pass and 1 is skipped. They include:
    • the recompiler tests agc_shader_memory, agc_constant_bit_test, agc_constant_phi, agc_masked_select, agc_scalar_select_mask, agc_integer_narrowing, agc_structurizer, agc_spirv_module, agc_validate_program, agc_pass_hazards, agc_shader_disk_cache and request_serializer
    • the GPU vertex tests agc_driver_graphics, agc_driver_stencil_replay, agc_driver_ngg_vertex_wave_reduction, agc_driver_ngg_vertex_wave_info, agc_driver_vertex_clip_cull_draw, agc_driver_vertex_fetch, agc_vertex_layer_export, agc_embedded_vertex_fetch, agc_vertex_input and agc_driver_tessellation
    • the GPU lane and wave tests agc_driver_read_lane, agc_driver_write_lane, agc_driver_read_first_lane, agc_driver_wave32_subgroup, agc_driver_wave32_exec_lane_bits, agc_driver_pixel_wave_reduction and agc_vertex_barrier
    • The skipped test is agc_driver_stencil_ref_export, because the device lacks VK_EXT_shader_stencil_export.
  • The conventions check from main's tools/check_conventions.py passes.
  • Not run: lavapipe, Linux, other GPUs and any title. No title advancement is claimed.

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; silent stubs are listed in their library's technical debt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: constant vertex lane-mask preparation only; no title advancement claimed
  • GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100
  • AI-assisted: yes

Co-Authored-By: Codex [email protected]

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4860289874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5914). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-14T15:32:04Z. --- ### What For a vertex shader targeting a fixed 32-lane host subgroup and SPIR-V below 1.6, fold a lane word `SelectU32(LaneId < 32, low, high)` to its low word when both words are constants. This lets the existing constant and dead-code passes remove a constant lower-half export mask before later safety checks. Lane words selected from words that are not constants, such as the words of the entry EXEC ballot, keep their `LaneId < 32` test. The half-wave scan matcher of the SPIR-V backend recognises a lane word by that select. An earlier head of this PR folded the compare itself. The matcher then missed a wave64 vertex scan keyed by the entry EXEC, and `agc_shader_memory` failed. Partial masks and observable lane outputs retain their lane dependence. The fold excludes other stages, unknown/64/single-lane subgroup requests and unknown/SPIR-V 1.6 module versions. This is the independent optimizer follow-up requested in #5466; it has no stencil replay or disk-cache changes. ### Tested Windows Server 2025, RTX 3060, GCC 15.2, `build-dev` Release (`-O3`), rebased on main `5050a23f3`. - `agc_constant_bit_test` passes. Disabling only this fold makes the fixture fail. - The new case `a lane word selected from ballot words keeps its lower-half test` fails with the earlier fold, which replaced every `LaneId < 32` compare. - Controls retain partial and single-bit masks, direct lane outputs and lane words from ballot words. They also reject the excluded subgroup, module-version and stage contracts. - `agc_shader_memory` passes. - With the earlier fold, on main `7289938ce`, it failed in `verifyHalfWaveReduction`, on the wave64 vertex UMin scan at 32 lanes: `v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation`. - On that main it also passed with the fold call removed from `Recompiler.cpp`, which matches main's behaviour. - 78 ctest registrations of 67 recompiler, vertex, lane and wave test executables ran on the RTX 3060: 77 pass and 1 is skipped. They include: - the recompiler tests `agc_shader_memory`, `agc_constant_bit_test`, `agc_constant_phi`, `agc_masked_select`, `agc_scalar_select_mask`, `agc_integer_narrowing`, `agc_structurizer`, `agc_spirv_module`, `agc_validate_program`, `agc_pass_hazards`, `agc_shader_disk_cache` and `request_serializer` - the GPU vertex tests `agc_driver_graphics`, `agc_driver_stencil_replay`, `agc_driver_ngg_vertex_wave_reduction`, `agc_driver_ngg_vertex_wave_info`, `agc_driver_vertex_clip_cull_draw`, `agc_driver_vertex_fetch`, `agc_vertex_layer_export`, `agc_embedded_vertex_fetch`, `agc_vertex_input` and `agc_driver_tessellation` - the GPU lane and wave tests `agc_driver_read_lane`, `agc_driver_write_lane`, `agc_driver_read_first_lane`, `agc_driver_wave32_subgroup`, `agc_driver_wave32_exec_lane_bits`, `agc_driver_pixel_wave_reduction` and `agc_vertex_barrier` - The skipped test is `agc_driver_stencil_ref_export`, because the device lacks `VK_EXT_shader_stencil_export`. - The conventions check from main's `tools/check_conventions.py` passes. - Not run: lavapipe, Linux, other GPUs and any title. No title advancement is claimed. ### 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 - [x] Unimplemented paths throw; silent stubs are listed in their library's technical debt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: constant vertex lane-mask preparation only; no title advancement claimed - [x] GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100 - [x] AI-assisted: yes Co-Authored-By: Codex <[email protected]>
fdfawef324 commented 2026-10-14 21:10:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-14T21:10:18Z.


Rebased onto main 7289938ce without conflicts and fixed a regression, now at 238093ec7. The PR stays in draft.

Before (1d9ad1c38 rebased onto 7289938ce)

  • agc_shader_memory failed: ShaderRecompiler::Recompile: SPIR-V emission failed: hash=0x65536 stage=1 opcode=ReadLane reason=v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation. This is verifyHalfWaveReduction's wave64 vertex UMin scan at 32 lanes.
  • With only the FoldVertexLaneBounds call removed from Recompiler.cpp, the test passed, so the fold caused the failure.
  • Cause: the fold replaced every LaneId < 32 compare with true. The half-wave scan matcher in SpirvAluEmitterMath.cpp (LaneBitWords/EntryLaneWalk) recognises a lane word as SelectU32(LaneId < 32, low, high). After the fold, the entry EXEC lane word became the low ballot word alone. The matcher then failed to prove the high half, and the raw lane-63 readlane reached SPIR-V emission.

Fix (238093ec7)

  • FoldVertexLaneBounds now folds the lane word select itself to its low word, and only when both words are constants.
  • Lane words from ballots or other values that are not constants keep their compare, so the matcher sees the shape it expects.
  • New ConstantBitTest case: a lane word selected from ballot words keeps its lower-half test. It fails with the previous fold.

After, on Windows Server 2025, NVIDIA GeForce RTX 3060 with driver 591.86, GCC 15.2 -O3 Release:

  • agc_shader_memory and agc_constant_bit_test pass.
  • 60 ctest registrations of 40 recompiler and vertex test executables ran: 59 pass, and agc_driver_stencil_ref_export is skipped because the device lacks VK_EXT_shader_stencil_export. They include agc_driver_graphics, agc_driver_stencil_replay, agc_driver_ngg_vertex_wave_reduction, agc_structurizer, agc_shader_disk_cache and agc_masked_select.
  • The conventions check passes.

The description was updated to match the narrower fold and the tests run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6178577688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5914#issuecomment-6178577688). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-14T21:10:18Z. --- Rebased onto main `7289938ce` without conflicts and fixed a regression, now at `238093ec7`. The PR stays in draft. **Before (`1d9ad1c38` rebased onto `7289938ce`)** - `agc_shader_memory` failed: `ShaderRecompiler::Recompile: SPIR-V emission failed: hash=0x65536 stage=1 opcode=ReadLane reason=v_readlane_b32 of lane 63 is outside the 32-lane host subgroup that runs this wave64 program at one lane per invocation`. This is `verifyHalfWaveReduction`'s wave64 vertex UMin scan at 32 lanes. - With only the `FoldVertexLaneBounds` call removed from `Recompiler.cpp`, the test passed, so the fold caused the failure. - Cause: the fold replaced every `LaneId < 32` compare with true. The half-wave scan matcher in `SpirvAluEmitterMath.cpp` (`LaneBitWords`/`EntryLaneWalk`) recognises a lane word as `SelectU32(LaneId < 32, low, high)`. After the fold, the entry EXEC lane word became the low ballot word alone. The matcher then failed to prove the high half, and the raw lane-63 readlane reached SPIR-V emission. **Fix (`238093ec7`)** - `FoldVertexLaneBounds` now folds the lane word select itself to its low word, and only when both words are constants. - Lane words from ballots or other values that are not constants keep their compare, so the matcher sees the shape it expects. - New `ConstantBitTest` case: `a lane word selected from ballot words keeps its lower-half test`. It fails with the previous fold. **After**, on Windows Server 2025, NVIDIA GeForce RTX 3060 with driver 591.86, GCC 15.2 `-O3` Release: - `agc_shader_memory` and `agc_constant_bit_test` pass. - 60 ctest registrations of 40 recompiler and vertex test executables ran: 59 pass, and `agc_driver_stencil_ref_export` is skipped because the device lacks `VK_EXT_shader_stencil_export`. They include `agc_driver_graphics`, `agc_driver_stencil_replay`, `agc_driver_ngg_vertex_wave_reduction`, `agc_structurizer`, `agc_shader_disk_cache` and `agc_masked_select`. - The conventions check passes. The description was updated to match the narrower fold and the tests run. AI-assisted: yes.
fdfawef324 commented 2026-10-15 11:57:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-15T11:57:43Z.


Rebased onto main 5050a23f3 (236+ commits since 7289938ce, including recompiler and SPIR-V backend changes) without conflicts, now at d353c6bcc. git range-diff shows both commits unchanged.

On Windows Server 2025, NVIDIA GeForce RTX 3060 with driver 591.86, GCC 15.2 -O3 Release, at d353c6bcc:

  • 78 ctest registrations of 67 recompiler, vertex, lane and wave test executables: 77 pass, and agc_driver_stencil_ref_export is skipped because the device lacks VK_EXT_shader_stencil_export. They include agc_shader_memory, agc_constant_bit_test, agc_driver_stencil_replay and agc_driver_ngg_vertex_wave_reduction.
  • With FoldVertexLaneBounds forced to return false, agc_constant_bit_test fails on the four folding cases; restored, it passes.
  • The conventions check from main's tools/check_conventions.py passes.

The description now names the new base and the larger test set.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191489605 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5914#issuecomment-6191489605). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-15T11:57:43Z. --- Rebased onto main `5050a23f3` (236+ commits since `7289938ce`, including recompiler and SPIR-V backend changes) without conflicts, now at `d353c6bcc`. `git range-diff` shows both commits unchanged. On Windows Server 2025, NVIDIA GeForce RTX 3060 with driver 591.86, GCC 15.2 `-O3` Release, at `d353c6bcc`: - 78 ctest registrations of 67 recompiler, vertex, lane and wave test executables: 77 pass, and `agc_driver_stencil_ref_export` is skipped because the device lacks `VK_EXT_shader_stencil_export`. They include `agc_shader_memory`, `agc_constant_bit_test`, `agc_driver_stencil_replay` and `agc_driver_ngg_vertex_wave_reduction`. - With `FoldVertexLaneBounds` forced to return false, `agc_constant_bit_test` fails on the four folding cases; restored, it passes. - The conventions check from main's `tools/check_conventions.py` passes. The description now names the new base and the larger test set. AI-assisted: yes
github-actions[bot] commented 2026-10-15 12:11:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:11:33Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4072 core/shader/recompiler/Recompiler.cpp
PR 4650 core/shader/recompiler/Recompiler.cpp
PR 5500 core/shader/recompiler/Optimization/src/ConstantFolder.cpp
core/shader/recompiler/Recompiler.cpp
PR 5609 core/shader/recompiler/Recompiler.cpp
PR 5656 core/shader/recompiler/Recompiler.cpp
PR 6177 core/shader/recompiler/Recompiler.cpp
PR 6222 core/shader/recompiler/Recompiler.cpp
PR 6503 core/shader/recompiler/Recompiler.cpp
PR 6539 core/shader/recompiler/Recompiler.cpp
PR 6612 core/shader/recompiler/Recompiler.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191738244 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5914#issuecomment-6191738244). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:11:33Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4072](https://www.github.com/boykopovar/AnyPS5/pull/4072) | `core/shader/recompiler/Recompiler.cpp` | | [PR 4650](https://www.github.com/boykopovar/AnyPS5/pull/4650) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5500](https://www.github.com/boykopovar/AnyPS5/pull/5500) | `core/shader/recompiler/Optimization/src/ConstantFolder.cpp`<br>`core/shader/recompiler/Recompiler.cpp` | | [PR 5609](https://www.github.com/boykopovar/AnyPS5/pull/5609) | `core/shader/recompiler/Recompiler.cpp` | | [PR 5656](https://www.github.com/boykopovar/AnyPS5/pull/5656) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6177](https://www.github.com/boykopovar/AnyPS5/pull/6177) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6503](https://www.github.com/boykopovar/AnyPS5/pull/6503) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6539](https://www.github.com/boykopovar/AnyPS5/pull/6539) | `core/shader/recompiler/Recompiler.cpp` | | [PR 6612](https://www.github.com/boykopovar/AnyPS5/pull/6612) | `core/shader/recompiler/Recompiler.cpp` |
adriadam10 commented 2026-10-16 11:30:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T11:30:52Z.


Checked the fold (vertex stage, fixed 32-lane host subgroup, SPIR-V < 1.6, constant words only), its controls (partial/single-bit masks, ballot-word selects, other stages, subgroup and version contracts) and ran agc_constant_bit_test and agc_shader_memory on the PR merged onto current main; both pass. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6214539589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5914#issuecomment-6214539589). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T11:30:52Z. --- Checked the fold (vertex stage, fixed 32-lane host subgroup, SPIR-V < 1.6, constant words only), its controls (partial/single-bit masks, ballot-word selects, other stages, subgroup and version contracts) and ran `agc_constant_bit_test` and `agc_shader_memory` on the PR merged onto current main; both pass. Merging once the full Build is green.
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!5914
No description provided.