fix(shader): keep the SDWA word of GCN vector instructions #23128

Merged
adam25136 merged 1 commit from upstream-pr/23128 into main 2026-11-20 04:49:03 +00:00
adam25136 commented 2026-11-18 19:00:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-11-18T19:00:50Z.


What

PS4 shaders built for the PS4 Pro can use SDWA. A VOP1, VOP2 or VOPC instruction whose first source is 0xF9 is followed by a second word that holds the real first source and the byte or word selects. For compares, that word also holds SD/SDST, which picks VCC or an SGPR pair as the destination. AnyPS5's RDNA decoder already reads that word in this layout.

The GCN path rewrites each GCN instruction into RDNA words before decoding it, and it rewrote an SDWA instruction into one word. The RDNA decoder then found the SDWA word missing ("truncated VOPC SDWA instruction", and the same for VOP1 and VOP2), a std::out_of_range. ReadRawShader, which reads an unregistered shader from guest memory, takes that error as "read more code". It read on until the end of mapped memory or its 1 MiB limit, and stopped with "raw shader program has no reachable end within mapped code or the size limit".

  • GcnInstructionDecoder.cpp: encodeVop1, encodeVop2 and encodeVopc copy the SDWA word through, as they already do for a literal. A v_cmpx takes its second destination from SD/SDST (VCC when SD is clear). SDWA on a VOP2 opcode that AnyPS5 promotes to VOP3 (such as v_add_i32, whose carry out RDNA has only in VOP3) now fails as an unsupported VOP2_SDWA instruction; before, the RDNA decoder failed with "unsupported scalar source operand code 249".
  • tests/GcnDecode.cpp (agc_gcn_decode) checks, with hand-built encodings:
    • SDWA v_cmp_lt_f32 writing the SGPR pair named by SDST, and VCC with SD clear;
    • SDWA v_cmpx_lt_f32 writing EXEC, plus that SGPR pair or VCC;
    • SDWA v_add_f32 and v_mov_b32 with their operands and selects, and SDWA v_cndmask_b32 (whose GCN and RDNA opcodes differ) with its operands;
    • a missing SDWA word, refused as std::out_of_range (the error on which ReadRawShader reads more code);
    • SDWA v_add_i32 refused as VOP2_SDWA.
  • docs/dev/debt/changes/fix-gcn-sdwa.md: records the SDWA layout as not measured on a console.

Evidence for the layout: a PS4 Pro (Neo) compute shader of inFAMOUS Second Son (CUSA00223), at guest address 0x2f2fb2100, uses this form 67 times (13 VOP1, 21 VOP2, 33 VOPC) and no DPP. Every following word has its selects in range (0-6) and its reserved bits (22 and 30) clear, and the 29 compares with SD set name even SGPR pairs (s14 to s28). For example, the SDWA v_cmp_eq_u32 at word 194 has SD set and SDST s14, and four words later comes s_and_saveexec_b64 vcc, s[14:15]. Between them, the SDWA v_cndmask_b32 at word 196 reads VCC as the plain compare at word 193 wrote it, which only works if the SD compare at 194 leaves VCC alone. shadPS4 decodes PS4 SDWA words in the same layout (SdwaVop12 and SdwaVopc, decoded here). The layout is not measured on a console.

Not in this PR: DPP (first source 0xFA) goes through the same one-word rewrite and fails the same way. Some of its GCN controls (wave_shl, wave_rol, wave_shr, wave_ror, row_bcast) exist only on gfx8 and gfx9, not on RDNA (LLVM), so it needs its own change.

Tested

Windows 10 Pro 19045, NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 r7, Release. This branch (8e869690fc) on main 6286d36449. The unit, validation and selected-test runs below ran on 6c02de6867, which differs from this branch only in the debt fragment.

  • agc_gcn_decode passes.
    • With the GcnInstructionDecoder.cpp change reversed and the tests kept, it fails with "truncated VOPC SDWA instruction".
    • Each deliberate break fails it, and the restored code passes:
      • the VOPC SDWA word dropped: "truncated VOPC SDWA instruction";
      • v_cmpx ignoring SD: "SDWA v_cmpx_lt_f32 with SD set did not also write the SGPR pair its SDST names";
      • the VOP2 SDWA word dropped: "truncated VOP2 SDWA instruction";
      • the VOP1 SDWA word dropped: "truncated VOP1 SDWA instruction";
      • the VOP2_SDWA refusal removed: "SDWA v_add_i32, which AnyPS5 promotes to VOP3, was not refused as such";
      • a missing SDWA word thrown as std::runtime_error instead of std::out_of_range: "a GCN SDWA instruction without its modifier word was not refused as truncated".
  • Validation layer (Khronos 1.4.367 from a local folder, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer VK_LOADER_LAYERS_DISABLE=~implicit~), ctest -R 'gcn|Gcn' -V: 13 of 13 pass on the branch and on main. The layer is inserted 10 times in each log, with no "not found" line and no VUID- line.
  • The tests tools/select_tests.py select picks for the two changed source files (3,223), run with select_tests.py ctest build --parallel 4 --timeout 120 and ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 on the branch and on main:
    • The same 4 fail on both: agc_driver_bda_device, agc_driver_msaa_dcc_unwatch, agc_driver_stencil_plane_memory, and agc_driver_upload_cache_read_only_flush, whose test executable doesn't build here on either side.
    • Both skip the same 19 tests.
  • inFAMOUS Second Son (CUSA00223 01.07, my own dump), in PS4 Pro mode (3840x2160 output), one traced start per build, on a local integration stack:
    • The stack: main 5d5ae377c0 with #17711, #18184, #21961 and #22629 merged, plus local changes not submitted: a DiscMap answer, PS4 multisampled-depth import changes, a bypass of the PS4 multisampled-depth write-back, and diagnostic prints.
    • Without this change: the title stops 29.0 s in with "raw shader program has no reachable end within mapped code or the size limit" for compute shader 0x2f2fb2100, and the decoder's error there, from the stack's diagnostic print, is "truncated VOPC SDWA instruction".
    • With this branch's code changes added to that stack: shader 0x2f2fb2100 decodes, and the title stops 34.7 s in, in the recompiler of the same shader, with a separate error ("GetImageResource dword 0 is not a valid runtime value").
  • tools/check_conventions.py --base 6286d36449 passes on this branch, and tools/debt_fold.py --show shader places the new entry after the other GcnInstructionDecoder.cpp entries; --check passes.
  • tools/pr_overlap.py --branch fix/gcn-sdwa (on 2c3109fbc5, an earlier version that changed the same two source files): no overlap with the 1,875 open pull requests (4 touch the same files; none conflicts).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: CUSA00223 #8214
  • GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 Pro 19045
  • AI-assisted: yes

AI-assisted: Claude Code found the cause in the title's shader, wrote the change and its tests, ran the tests and the title, and drafted this description; separate review agents checked it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5382247770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23128). Originally posted by [adam25136](https://github.com/adam25136) on 2026-11-18T19:00:50Z. --- ### What PS4 shaders built for the PS4 Pro can use SDWA. A VOP1, VOP2 or VOPC instruction whose first source is `0xF9` is followed by a second word that holds the real first source and the byte or word selects. For compares, that word also holds SD/SDST, which picks VCC or an SGPR pair as the destination. AnyPS5's RDNA decoder already reads that word in this layout. The GCN path rewrites each GCN instruction into RDNA words before decoding it, and it rewrote an SDWA instruction into one word. The RDNA decoder then found the SDWA word missing ("truncated VOPC SDWA instruction", and the same for VOP1 and VOP2), a `std::out_of_range`. `ReadRawShader`, which reads an unregistered shader from guest memory, takes that error as "read more code". It read on until the end of mapped memory or its 1 MiB limit, and stopped with "raw shader program has no reachable end within mapped code or the size limit". - `GcnInstructionDecoder.cpp`: `encodeVop1`, `encodeVop2` and `encodeVopc` copy the SDWA word through, as they already do for a literal. A `v_cmpx` takes its second destination from SD/SDST (VCC when SD is clear). SDWA on a VOP2 opcode that AnyPS5 promotes to VOP3 (such as `v_add_i32`, whose carry out RDNA has only in VOP3) now fails as an unsupported `VOP2_SDWA` instruction; before, the RDNA decoder failed with "unsupported scalar source operand code 249". - `tests/GcnDecode.cpp` (`agc_gcn_decode`) checks, with hand-built encodings: - SDWA `v_cmp_lt_f32` writing the SGPR pair named by SDST, and VCC with SD clear; - SDWA `v_cmpx_lt_f32` writing EXEC, plus that SGPR pair or VCC; - SDWA `v_add_f32` and `v_mov_b32` with their operands and selects, and SDWA `v_cndmask_b32` (whose GCN and RDNA opcodes differ) with its operands; - a missing SDWA word, refused as `std::out_of_range` (the error on which `ReadRawShader` reads more code); - SDWA `v_add_i32` refused as `VOP2_SDWA`. - `docs/dev/debt/changes/fix-gcn-sdwa.md`: records the SDWA layout as not measured on a console. Evidence for the layout: a PS4 Pro (Neo) compute shader of inFAMOUS Second Son (CUSA00223), at guest address `0x2f2fb2100`, uses this form 67 times (13 VOP1, 21 VOP2, 33 VOPC) and no DPP. Every following word has its selects in range (0-6) and its reserved bits (22 and 30) clear, and the 29 compares with SD set name even SGPR pairs (`s14` to `s28`). For example, the SDWA `v_cmp_eq_u32` at word 194 has SD set and SDST `s14`, and four words later comes `s_and_saveexec_b64 vcc, s[14:15]`. Between them, the SDWA `v_cndmask_b32` at word 196 reads VCC as the plain compare at word 193 wrote it, which only works if the SD compare at 194 leaves VCC alone. shadPS4 decodes PS4 SDWA words in the same layout ([`SdwaVop12` and `SdwaVopc`](https://github.com/shadps4-emu/shadPS4/blob/7c0b846d0579c7c13b4c2d76032227b68615d068/src/shader_recompiler/frontend/instruction.h#L263-L301), [decoded here](https://github.com/shadps4-emu/shadPS4/blob/7c0b846d0579c7c13b4c2d76032227b68615d068/src/shader_recompiler/frontend/decode.cpp#L436-L509)). The layout is not measured on a console. Not in this PR: DPP (first source `0xFA`) goes through the same one-word rewrite and fails the same way. Some of its GCN controls (`wave_shl`, `wave_rol`, `wave_shr`, `wave_ror`, `row_bcast`) exist only on gfx8 and gfx9, not on RDNA ([LLVM](https://github.com/llvm/llvm-project/blob/48d0dc8f61a7680190a144ef89bd80543c0ec4f9/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp#L10305-L10320)), so it needs its own change. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10 Pro 19045, NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 r7, Release. This branch (8e869690fc) on `main` 6286d36449. The unit, validation and selected-test runs below ran on 6c02de6867, which differs from this branch only in the debt fragment. - **`agc_gcn_decode` passes.** - With the `GcnInstructionDecoder.cpp` change reversed and the tests kept, it fails with "truncated VOPC SDWA instruction". - Each deliberate break fails it, and the restored code passes: - the VOPC SDWA word dropped: "truncated VOPC SDWA instruction"; - `v_cmpx` ignoring SD: "SDWA v_cmpx_lt_f32 with SD set did not also write the SGPR pair its SDST names"; - the VOP2 SDWA word dropped: "truncated VOP2 SDWA instruction"; - the VOP1 SDWA word dropped: "truncated VOP1 SDWA instruction"; - the `VOP2_SDWA` refusal removed: "SDWA v_add_i32, which AnyPS5 promotes to VOP3, was not refused as such"; - a missing SDWA word thrown as `std::runtime_error` instead of `std::out_of_range`: "a GCN SDWA instruction without its modifier word was not refused as truncated". - **Validation layer** (Khronos 1.4.367 from a local folder, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer VK_LOADER_LAYERS_DISABLE=~implicit~`), `ctest -R 'gcn|Gcn' -V`: 13 of 13 pass on the branch and on `main`. The layer is inserted 10 times in each log, with no "not found" line and no `VUID-` line. - **The tests `tools/select_tests.py select` picks for the two changed source files** (3,223), run with `select_tests.py ctest build --parallel 4 --timeout 120` and `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` on the branch and on `main`: - The same 4 fail on both: `agc_driver_bda_device`, `agc_driver_msaa_dcc_unwatch`, `agc_driver_stencil_plane_memory`, and `agc_driver_upload_cache_read_only_flush`, whose test executable doesn't build here on either side. - Both skip the same 19 tests. - **inFAMOUS Second Son** (CUSA00223 01.07, my own dump), in PS4 Pro mode (3840x2160 output), one traced start per build, on a local integration stack: - **The stack:** `main` 5d5ae377c0 with #17711, #18184, #21961 and #22629 merged, plus local changes not submitted: a DiscMap answer, PS4 multisampled-depth import changes, a bypass of the PS4 multisampled-depth write-back, and diagnostic prints. - **Without this change:** the title stops 29.0 s in with "raw shader program has no reachable end within mapped code or the size limit" for compute shader `0x2f2fb2100`, and the decoder's error there, from the stack's diagnostic print, is "truncated VOPC SDWA instruction". - **With this branch's code changes added to that stack:** shader `0x2f2fb2100` decodes, and the title stops 34.7 s in, in the recompiler of the same shader, with a separate error ("GetImageResource dword 0 is not a valid runtime value"). - `tools/check_conventions.py --base 6286d36449` passes on this branch, and `tools/debt_fold.py --show shader` places the new entry after the other `GcnInstructionDecoder.cpp` entries; `--check` passes. - `tools/pr_overlap.py --branch fix/gcn-sdwa` (on 2c3109fbc5, an earlier version that changed the same two source files): no overlap with the 1,875 open pull requests (4 touch the same files; none conflicts). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: CUSA00223 #8214 <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 Pro 19045 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> AI-assisted: Claude Code found the cause in the title's shader, wrote the change and its tests, ran the tests and the title, and drafted this description; separate review agents checked it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!23128
No description provided.