fix(shader): keep the SDWA word of GCN vector instructions #23128
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23128"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
0xF9is 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,encodeVop2andencodeVopccopy the SDWA word through, as they already do for a literal. Av_cmpxtakes its second destination from SD/SDST (VCC when SD is clear). SDWA on a VOP2 opcode that AnyPS5 promotes to VOP3 (such asv_add_i32, whose carry out RDNA has only in VOP3) now fails as an unsupportedVOP2_SDWAinstruction; before, the RDNA decoder failed with "unsupported scalar source operand code 249".tests/GcnDecode.cpp(agc_gcn_decode) checks, with hand-built encodings:v_cmp_lt_f32writing the SGPR pair named by SDST, and VCC with SD clear;v_cmpx_lt_f32writing EXEC, plus that SGPR pair or VCC;v_add_f32andv_mov_b32with their operands and selects, and SDWAv_cndmask_b32(whose GCN and RDNA opcodes differ) with its operands;std::out_of_range(the error on whichReadRawShaderreads more code);v_add_i32refused asVOP2_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 (s14tos28). For example, the SDWAv_cmp_eq_u32at word 194 has SD set and SDSTs14, and four words later comess_and_saveexec_b64 vcc, s[14:15]. Between them, the SDWAv_cndmask_b32at 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 (SdwaVop12andSdwaVopc, 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) onmain6286d36449. The unit, validation and selected-test runs below ran on 6c02de6867, which differs from this branch only in the debt fragment.agc_gcn_decodepasses.GcnInstructionDecoder.cppchange reversed and the tests kept, it fails with "truncated VOPC SDWA instruction".v_cmpxignoring SD: "SDWA v_cmpx_lt_f32 with SD set did not also write the SGPR pair its SDST names";VOP2_SDWArefusal removed: "SDWA v_add_i32, which AnyPS5 promotes to VOP3, was not refused as such";std::runtime_errorinstead ofstd::out_of_range: "a GCN SDWA instruction without its modifier word was not refused as truncated".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 onmain. The layer is inserted 10 times in each log, with no "not found" line and noVUID-line.tools/select_tests.py selectpicks for the two changed source files (3,223), run withselect_tests.py ctest build --parallel 4 --timeout 120andANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1on the branch and onmain:agc_driver_bda_device,agc_driver_msaa_dcc_unwatch,agc_driver_stencil_plane_memory, andagc_driver_upload_cache_read_only_flush, whose test executable doesn't build here on either side.main5d5ae377c0with #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.0x2f2fb2100, and the decoder's error there, from the stack's diagnostic print, is "truncated VOPC SDWA instruction".0x2f2fb2100decodes, 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 6286d36449passes on this branch, andtools/debt_fold.py --show shaderplaces the new entry after the otherGcnInstructionDecoder.cppentries;--checkpasses.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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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