feat(shader): implement s_subvector_loop_begin and s_subvector_loop_end #3750

Merged
fma-rc merged 2 commits from upstream-pr/3750 into main 2026-10-11 22:28:11 +00:00
fma-rc commented 2026-10-11 16:26:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-11T16:26:40Z.


What

s_subvector_loop_begin and s_subvector_loop_end (SOPK 0x1b / 0x1c) threw GNM subvector loop instructions are not supported by this translator. Astro's Playroom (PPSA01325) stops on that: a wave64 mesh shader of the title has one such pair around a 32-lane stream compaction.

Both are now translated for wave64 shaders, with D0 the SGPR operand:

begin: if EXEC == 0 jump; elif EXEC_LO == 0: D0 = EXEC_LO; else: D0 = EXEC_HI, EXEC_HI = 0
end:   if EXEC_HI != 0: EXEC_LO = D0; elif D0 == 0: nothing; else: EXEC_HI = D0, D0 = EXEC_LO, EXEC_LO = 0, jump

That is the pseudo-code of AMD's RDNA 2 instruction set reference, with the S0 it tests in the end instruction read as D0. I measured it on a Radeon 610M (gfx1036, wave64; ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0): two loop bodies, one that leaves EXEC alone and one that ends with s_mov_b64 exec, -1 as the title's does, each with seven initial masks. Every row matches the pseudo-code. Script and raw output: https://github.com/fma-rc/AnyPS5/tree/evidence/subvector-loop-oracle/oracle-subvector-loop

Two smaller changes come with it:

  • SpirvSpecialization.cpp: when the loop body ends with s_mov_b64 exec, -1 the back-edge condition is a constant false, and the pass folded that branch away, leaving an OpLoopMerge header with no back edge. NVIDIA 615.78.08 segfaults compiling that. A constant-condition branch whose untaken target is a loop header is now kept.
  • EmbeddedVertexFetch.cpp: both opcodes are listed as exec writers.

Not covered: a wave32 shader and a destination that is not an SGPR throw (not measured). Only a compute shader is tested. A loop whose back-edge block becomes unreachable some other way can still lose its back edge. PS5 hardware was not measured.

Tested

Arch Linux, GCC 16.2.1, Release, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, main a11c8f7f (the "this branch" row was run again after the rebase onto main 0a8b1d32b, same results). New test agc_driver_subvector_loop runs the measured kernel and checks all 14 cases, every lane and output.

RTX 5080 (615.78.08) RADV (Radeon 610M) lavapipe (Mesa 26.2.4)
this branch with the translator change reverted fails, the throw above not run not run
this branch without the SpirvSpecialization.cpp change segfault in the driver pass not run
this branch pass pass skipped, 8-lane subgroups

So upstream's Linux CI will skip the 14 cases. Full ctest on this branch: 833 of 833 on RADV; on the RTX 5080 one failure, agc_driver_emulated_color_compare, which is also red in main's linux CI job. tools/check_conventions.py --base origin/main passes.

Astro's Playroom on the RTX 5080 with this branch, #3669 and #425 merged onto main a11c8f7f: one 300 s run ends at the timeout without the error, where the same build without this branch stops on it after LoadLevelResources: cpuplaza_mini_04. What the title shows on screen was not checked.

Not tested on Windows.

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:4820542166 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3750). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-11T16:26:40Z. --- ### What `s_subvector_loop_begin` and `s_subvector_loop_end` (SOPK 0x1b / 0x1c) threw `GNM subvector loop instructions are not supported by this translator`. Astro's Playroom (PPSA01325) stops on that: a wave64 mesh shader of the title has one such pair around a 32-lane stream compaction. Both are now translated for wave64 shaders, with `D0` the SGPR operand: ``` begin: if EXEC == 0 jump; elif EXEC_LO == 0: D0 = EXEC_LO; else: D0 = EXEC_HI, EXEC_HI = 0 end: if EXEC_HI != 0: EXEC_LO = D0; elif D0 == 0: nothing; else: EXEC_HI = D0, D0 = EXEC_LO, EXEC_LO = 0, jump ``` That is the pseudo-code of AMD's RDNA 2 instruction set reference, with the `S0` it tests in the end instruction read as `D0`. I measured it on a Radeon 610M (gfx1036, wave64; ieee 0, dx10_clamp 1, denorm32 0, denorm16 3, round32 0, round16 0, fp16_overflow 0): two loop bodies, one that leaves EXEC alone and one that ends with `s_mov_b64 exec, -1` as the title's does, each with seven initial masks. Every row matches the pseudo-code. Script and raw output: https://github.com/fma-rc/AnyPS5/tree/evidence/subvector-loop-oracle/oracle-subvector-loop Two smaller changes come with it: - `SpirvSpecialization.cpp`: when the loop body ends with `s_mov_b64 exec, -1` the back-edge condition is a constant false, and the pass folded that branch away, leaving an `OpLoopMerge` header with no back edge. NVIDIA 615.78.08 segfaults compiling that. A constant-condition branch whose untaken target is a loop header is now kept. - `EmbeddedVertexFetch.cpp`: both opcodes are listed as exec writers. Not covered: a wave32 shader and a destination that is not an SGPR throw (not measured). Only a compute shader is tested. A loop whose back-edge block becomes unreachable some other way can still lose its back edge. PS5 hardware was not measured. ### Tested Arch Linux, GCC 16.2.1, Release, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`, `main` a11c8f7f (the "this branch" row was run again after the rebase onto `main` 0a8b1d32b, same results). New test `agc_driver_subvector_loop` runs the measured kernel and checks all 14 cases, every lane and output. | | RTX 5080 (615.78.08) | RADV (Radeon 610M) | lavapipe (Mesa 26.2.4) | |---|---|---|---| | this branch with the translator change reverted | fails, the throw above | not run | not run | | this branch without the `SpirvSpecialization.cpp` change | segfault in the driver | pass | not run | | this branch | pass | pass | skipped, 8-lane subgroups | So upstream's Linux CI will skip the 14 cases. Full ctest on this branch: 833 of 833 on RADV; on the RTX 5080 one failure, `agc_driver_emulated_color_compare`, which is also red in `main`'s `linux` CI job. `tools/check_conventions.py --base origin/main` passes. Astro's Playroom on the RTX 5080 with this branch, #3669 and #425 merged onto `main` a11c8f7f: one 300 s run ends at the timeout without the error, where the same build without this branch stops on it after `LoadLevelResources: cpuplaza_mini_04`. What the title shows on screen was not checked. Not tested on Windows. ### 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
adriadam10 commented 2026-10-11 21:58:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:58:28Z.


Measured both instructions on RDNA2 (gfx1035, wave64): the two loop bodies of the test (EXEC kept / s_mov_b64 exec, -1) over 64 initial masks (the 7 of the test plus edge and random ones), all 9 outputs per lane, match this translation bit for bit. Test passes on RADV with the validation layer clean, and fails without the change.

One correction to the description: on current main the SpirvSpecialization.cpp change is needed on every driver, not just NVIDIA. Without it the recompiler's own SPIR-V validation rejects the module (Loop header ... is targeted by 0 back-edge blocks but the standard requires exactly one), RADV included. I rebased onto main and added one commit that names that rule in the TechnicalDebt line. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114260654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3750#issuecomment-6114260654). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:58:28Z. --- Measured both instructions on RDNA2 (gfx1035, wave64): the two loop bodies of the test (EXEC kept / `s_mov_b64 exec, -1`) over 64 initial masks (the 7 of the test plus edge and random ones), all 9 outputs per lane, match this translation bit for bit. Test passes on RADV with the validation layer clean, and fails without the change. One correction to the description: on current `main` the `SpirvSpecialization.cpp` change is needed on every driver, not just NVIDIA. Without it the recompiler's own SPIR-V validation rejects the module (`Loop header ... is targeted by 0 back-edge blocks but the standard requires exactly one`), RADV included. I rebased onto `main` and added one commit that names that rule in the TechnicalDebt line. Merging.
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!3750
No description provided.