fix(shader): read relative SGPRs through a one-hot mux #928

Merged
MichonGoddijn231849 merged 1 commit from upstream-pr/928 into main 2026-10-07 09:16:36 +00:00
MichonGoddijn231849 commented 2026-10-06 18:09:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-06T18:09:24Z.


What

s_movrels_b32/b64 and s_movrelsd_2_b32 read their source SGPR through a chain of selects keyed on the M0 offset. The relative destination write is a select between that value and the old register. When the old register also appears in the read chain, the NVIDIA shader compiler (RTX 5080, driver 610.74) gets the nesting wrong:

%in  = OpSelect %uint %b %x %y
%dst = OpSelect %uint %a %in %x      ; evaluated as select(a || b, y, x), should be select(a && !b, y, x)

In agc_driver_scalar_sop1 this made s_movrelsd_2_b32 overwrite s13 with s8 in every lane whose source offset was 3. The SPIR-V is valid and the translation is correct. Rewriting the select with inverted conditions, a branch with a phi, a masked xor or a small function-local array all fold back into the same wrong result.

The change builds the relative read as an OR of select(offset == k, s[base + k], 0) terms. Offsets outside [1, count) still read the base register, as before. The result is the same, and the nesting the compiler mishandles no longer forms.

Tested

Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 610.74), MinGW-w64 GCC 15.2.0.

  • ctest -R "agc_|spirv|recompiler" (174 tests): agc_driver_scalar_sop1 failed before, everything passes now.
  • agc_shader_replay --spv over 110 captured recompile requests (GTA V, Demon's Souls and others) gives byte-identical SPIR-V before and after (none of them uses a scalar relative-source move).

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4762801882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/928). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-06T18:09:24Z. --- ### What `s_movrels_b32/b64` and `s_movrelsd_2_b32` read their source SGPR through a chain of selects keyed on the M0 offset. The relative destination write is a select between that value and the old register. When the old register also appears in the read chain, the NVIDIA shader compiler (RTX 5080, driver 610.74) gets the nesting wrong: ``` %in = OpSelect %uint %b %x %y %dst = OpSelect %uint %a %in %x ; evaluated as select(a || b, y, x), should be select(a && !b, y, x) ``` In `agc_driver_scalar_sop1` this made `s_movrelsd_2_b32` overwrite s13 with s8 in every lane whose source offset was 3. The SPIR-V is valid and the translation is correct. Rewriting the select with inverted conditions, a branch with a phi, a masked xor or a small function-local array all fold back into the same wrong result. The change builds the relative read as an OR of `select(offset == k, s[base + k], 0)` terms. Offsets outside `[1, count)` still read the base register, as before. The result is the same, and the nesting the compiler mishandles no longer forms. ### Tested Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 610.74), MinGW-w64 GCC 15.2.0. - `ctest -R "agc_|spirv|recompiler"` (174 tests): `agc_driver_scalar_sop1` failed before, everything passes now. - `agc_shader_replay --spv` over 110 captured recompile requests (GTA V, Demon's Souls and others) gives byte-identical SPIR-V before and after (none of them uses a scalar relative-source move). ### 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 (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
fisherjoey commented 2026-10-06 20:04:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-06T20:04:57Z.


Tested on Linux with NVIDIA as well, since the PR was verified on Windows only.

  • Fedora 44 (Nobara), kernel 7.2, GCC 16.2.1, RTX 5070 Ti, driver 595.91.07
  • main at 5895ea72: agc_driver_scalar_sop1 fails with lane 3 movrelsd_2_b32 s13 is 0x00007fff, expected 0xd4cbd76d
  • main + this PR (177a452c): passes in 3 of 3 runs. Full ctest (359 tests, with #833 also merged) passes.

The miscompile is also present in the 595 Linux driver, not only 610.x on Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6024461368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/928#issuecomment-6024461368). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-06T20:04:57Z. --- Tested on Linux with NVIDIA as well, since the PR was verified on Windows only. - Fedora 44 (Nobara), kernel 7.2, GCC 16.2.1, RTX 5070 Ti, driver 595.91.07 - `main` at 5895ea72: `agc_driver_scalar_sop1` fails with `lane 3 movrelsd_2_b32 s13 is 0x00007fff, expected 0xd4cbd76d` - `main` + this PR (177a452c): passes in 3 of 3 runs. Full ctest (359 tests, with #833 also merged) passes. The miscompile is also present in the 595 Linux driver, not only 610.x on Windows.
MichonGoddijn231849 commented 2026-10-07 08:17:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-07T08:17:27Z.


@fisherjoey thanks for checking on Linux. Good to know the 595 driver has the same miscompile, so it's not a Windows-only or 610-only thing.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6033899336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/928#issuecomment-6033899336). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-07T08:17:27Z. --- @fisherjoey thanks for checking on Linux. Good to know the 595 driver has the same miscompile, so it's not a Windows-only or 610-only thing.
adriadam10 commented 2026-10-07 09:16:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T09:16:33Z.


Checked the new mux against main's select chain for every offset (0, in range, out of range, base = last SGPR), the s_movrels_b32/b64 and s_movrelsd_2_b32 forms on the HW oracle (gfx1035, incl. src = dst = 3), full ctest on RADV and agc_driver_scalar_sop1 on CI's lavapipe; NVIDIA confirmed by @fisherjoey. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6034856450 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/928#issuecomment-6034856450). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T09:16:33Z. --- Checked the new mux against main's select chain for every offset (0, in range, out of range, base = last SGPR), the s_movrels_b32/b64 and s_movrelsd_2_b32 forms on the HW oracle (gfx1035, incl. src = dst = 3), full ctest on RADV and `agc_driver_scalar_sop1` on CI's lavapipe; NVIDIA confirmed by @fisherjoey. 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!928
No description provided.