feat(shader): add LDS conditional exchange #1198

Merged
Sanjaysrinivas merged 3 commits from upstream-pr/1198 into main 2026-10-08 09:34:32 +00:00
Sanjaysrinivas commented 2026-10-07 17:20:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-07T17:20:58Z.


What

Shaders containing DS_CONDXCHG32_RTN_B64 (DS opcode 0x7e) fail decoding on main. Add decoding and LDS translation through two existing 32-bit masked atomic updates. Each dword returns its original value and receives the input's low 31 bits only when the input's high bit is set.

Add the combined 16-bit instruction offset to the original VGPR address through the shared atomic path. Out-of-range accesses return zero and leave LDS unchanged. The address is not masked to 64 KiB or rounded down. Snapshot both data dwords and the address before writing destinations so overlapping VGPRs work. Preserve inactive EXEC lanes; reject GDS and VGPR pairs extending beyond v255.

Semantics evidence:

  • AMD's machine-readable ISA, amdgpu_isa_rdna1.xml and amdgpu_isa_rdna2.xml, instruction DS_CONDXCHG32_RTN_B64: encoding, operand widths and the two conditional 32-bit exchanges.
  • adriadam10's gfx1035 hardware review: aligned return values, conditional bit clearing, 16-bit offsets and out-of-range accesses measured on all 32 lanes. Misaligned accesses fault the wave; the recompiler does not model that fault and the execution test uses only 8-byte aligned addresses.
  • Existing Radeon 780M (gfx1103, RDNA3) measurements recorded in TechnicalDebt.md confirmed aligned returns, conditional bit clearing, EXEC masking and operand overlap in wave32 and wave64; contended cases passed per-dword conservation checks.

Contended lane ordering, pair-wide indivisibility, interaction with locked 64-bit LDS atomics, gfx1013 and allocated-VGPR EXEC nullification remain documented limitations. The recompiler's existing source-version hashing invalidates affected shader caches; no new IR opcode, backend lock or dependency is added.

Related prior work: #897, closed and unmerged. This implementation uses the existing 32-bit atomic path.

Tested

Revision 34d4cf17, Ubuntu 24.04 under WSL2, GCC 13.3, Release with BUILD_TESTING=ON, Mesa llvmpipe (LLVM 20.1.2, subgroup size 8). Vulkan execution required a usable device and disabled the shader cache.

  • The revised regression test fails against the old address mask: lane 0 returns existing LDS data where zero is required. It passes after the fix in three consecutive CTest repetitions.
  • All 18 conditional-exchange configurations cover wave32, wave64 and wave64 with a forced 32-lane target; conditional writes and bit clearing, offsets 0, 0x120 and 0xfff8, aligned in-range/out-of-range addresses, unchanged LDS and guard words, operand overlap, inactive EXEC lanes and per-dword contention. Decoder rejection and v254:v255 boundary checks also pass.
  • Related LDS execution tests: 5 passed, 1 skipped, 0 failed. lds_addtid requires subgroups of at least 32 lanes.
  • Full Release build and the libs target passed. Full CTest: 378 passed, 22 skipped, 1 failed out of 401. The sole failure is the previously documented guest_access abort with its working directory on the Windows-mounted filesystem; the identical rebuilt executable passes from WSL ext4 /tmp. The full suite is not reported as green. agc_driver_flat_store passed.
  • Linux tools/check_conventions.py --base origin/main and git diff --check passed. The branch merges cleanly with upstream main at 34c20eed.

No game title or console hardware was tested. The gfx1035 evidence above comes from the reviewer's hardware measurements; local regression execution uses Vulkan llvmpipe.

Checklist

  • Based on main; merges cleanly with current main at 34c20eed; 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 (no new third-party code)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4776051186 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1198). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-07T17:20:58Z. --- ### What Shaders containing `DS_CONDXCHG32_RTN_B64` (DS opcode `0x7e`) fail decoding on main. Add decoding and LDS translation through two existing 32-bit masked atomic updates. Each dword returns its original value and receives the input's low 31 bits only when the input's high bit is set. Add the combined 16-bit instruction offset to the original VGPR address through the shared atomic path. Out-of-range accesses return zero and leave LDS unchanged. The address is not masked to 64 KiB or rounded down. Snapshot both data dwords and the address before writing destinations so overlapping VGPRs work. Preserve inactive EXEC lanes; reject GDS and VGPR pairs extending beyond v255. Semantics evidence: - AMD's [machine-readable ISA](https://gpuopen.com/machine-readable-isa/), `amdgpu_isa_rdna1.xml` and `amdgpu_isa_rdna2.xml`, instruction `DS_CONDXCHG32_RTN_B64`: encoding, operand widths and the two conditional 32-bit exchanges. - [adriadam10's gfx1035 hardware review](https://github.com/boykopovar/AnyPS5/pull/1198#pullrequestreview-5454127706): aligned return values, conditional bit clearing, 16-bit offsets and out-of-range accesses measured on all 32 lanes. Misaligned accesses fault the wave; the recompiler does not model that fault and the execution test uses only 8-byte aligned addresses. - Existing Radeon 780M (gfx1103, RDNA3) measurements recorded in `TechnicalDebt.md` confirmed aligned returns, conditional bit clearing, EXEC masking and operand overlap in wave32 and wave64; contended cases passed per-dword conservation checks. Contended lane ordering, pair-wide indivisibility, interaction with locked 64-bit LDS atomics, gfx1013 and allocated-VGPR EXEC nullification remain documented limitations. The recompiler's existing source-version hashing invalidates affected shader caches; no new IR opcode, backend lock or dependency is added. Related prior work: #897, closed and unmerged. This implementation uses the existing 32-bit atomic path. ### Tested Revision `34d4cf17`, Ubuntu 24.04 under WSL2, GCC 13.3, Release with `BUILD_TESTING=ON`, Mesa llvmpipe (LLVM 20.1.2, subgroup size 8). Vulkan execution required a usable device and disabled the shader cache. - The revised regression test fails against the old address mask: lane 0 returns existing LDS data where zero is required. It passes after the fix in three consecutive CTest repetitions. - All 18 conditional-exchange configurations cover wave32, wave64 and wave64 with a forced 32-lane target; conditional writes and bit clearing, offsets `0`, `0x120` and `0xfff8`, aligned in-range/out-of-range addresses, unchanged LDS and guard words, operand overlap, inactive EXEC lanes and per-dword contention. Decoder rejection and v254:v255 boundary checks also pass. - Related LDS execution tests: 5 passed, 1 skipped, 0 failed. `lds_addtid` requires subgroups of at least 32 lanes. - Full Release build and the `libs` target passed. Full CTest: **378 passed, 22 skipped, 1 failed** out of 401. The sole failure is the previously documented `guest_access` abort with its working directory on the Windows-mounted filesystem; the identical rebuilt executable passes from WSL ext4 `/tmp`. The full suite is not reported as green. `agc_driver_flat_store` passed. - Linux `tools/check_conventions.py --base origin/main` and `git diff --check` passed. The branch merges cleanly with upstream `main` at `34c20eed`. No game title or console hardware was tested. The gfx1035 evidence above comes from the reviewer's hardware measurements; local regression execution uses Vulkan llvmpipe. ### Checklist - [x] Based on `main`; merges cleanly with current `main` at `34c20eed`; 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 - [x] New third-party code is a submodule built from source (no new third-party code) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-08 05:44:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T05:44:52Z.


Progress report (ac951f8 → 34d4cf1)

📈 GPU shader instructions: 97.08% (+0.08%, +1 implemented instructions)

✅ 1 implemented
Encoding Name
DS DS_CONDXCHG32_RTN_B64
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6053233088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1198#issuecomment-6053233088). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T05:44:52Z. --- <!-- progress-report --> ### Progress report (ac951f8 → 34d4cf1) 📈 **GPU shader instructions**: 97.08% (+0.08%, +1 implemented instructions) <details> <summary>✅ 1 implemented</summary> | Encoding | Name | | - | - | | DS | `DS_CONDXCHG32_RTN_B64` | </details>
adriadam10 commented 2026-10-08 08:55:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:55:43Z.

Review: CHANGES_REQUESTED


Measured on RDNA2 (gfx1035) with our HW oracle. Aligned, in-range rows match bit for bit on all 32 lanes: both old dwords are returned, each dword is written with its low 31 bits only when bit 31 is set (0, 0x7fffffff, 0x80000000 and 0xffffffff included), and the 16-bit offset combines correctly (0x120, 0xfff8). Build and ctest pass on the merge with main, and the new test fails without the change. One thing doesn't match:

  • SharedMemoryInstructions.cpp:235: the & 0xfff8 mask wraps the address at 64 KiB. On hardware, vaddr + offset >= 0x10000 is out of range: both returns are 0 and the LDS is left unchanged. That is the same as ds_read_b64 at 0x10000. With the mask, the address wraps around and the PR writes into the LDS. All 32 lanes differ for vaddr 0x10000, vaddr 0x10008, vaddr 8 + offset 0xfff8, and vaddr 0xfee0 + offset 0x120. The shared-atomic path on main already returns 0 and skips the write outside the LDS (EmitAtomicAccess). Dropping the mask and passing the offset the way dsAtomic64 does (memory.offset = instruction offset, +4 for the second dword) should give the hardware result.
  • The low bits aren't masked on hardware either. A ds_condxchg32_rtn_b64 at an address that isn't 8-byte aligned (+1, +2, +4) faults the wave, as ds_add_rtn_u32 at +2 and ds_add_rtn_u64 / ds_wrxchg_rtn_b64 at +4 do. The recompiler can't model that, so the test shouldn't pin a result for it.
  • LdsCondxchg.cpp:107: in[5] pins both the 64 KiB wrap (+ 0x10000) and the alignment (tid & 7). Please replace them with an out-of-range row (returns 0, guard words unchanged) and keep only 8-byte aligned addresses.
  • TechnicalDebt.md:250: the in-range semantics are now measured on gfx1035, so the line can say so and drop the "address mask follows RDNA3" part. Contended ordering and gfx1013 are still unmeasured.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5454127706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1198#pullrequestreview-5454127706). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:55:43Z. **Review:** CHANGES_REQUESTED --- Measured on RDNA2 (gfx1035) with our HW oracle. Aligned, in-range rows match bit for bit on all 32 lanes: both old dwords are returned, each dword is written with its low 31 bits only when bit 31 is set (0, 0x7fffffff, 0x80000000 and 0xffffffff included), and the 16-bit offset combines correctly (0x120, 0xfff8). Build and ctest pass on the merge with main, and the new test fails without the change. One thing doesn't match: - `SharedMemoryInstructions.cpp:235`: the `& 0xfff8` mask wraps the address at 64 KiB. On hardware, `vaddr + offset >= 0x10000` is out of range: both returns are 0 and the LDS is left unchanged. That is the same as `ds_read_b64` at 0x10000. With the mask, the address wraps around and the PR writes into the LDS. All 32 lanes differ for vaddr 0x10000, vaddr 0x10008, vaddr 8 + offset 0xfff8, and vaddr 0xfee0 + offset 0x120. The shared-atomic path on main already returns 0 and skips the write outside the LDS (`EmitAtomicAccess`). Dropping the mask and passing the offset the way `dsAtomic64` does (`memory.offset` = instruction offset, +4 for the second dword) should give the hardware result. - The low bits aren't masked on hardware either. A `ds_condxchg32_rtn_b64` at an address that isn't 8-byte aligned (+1, +2, +4) faults the wave, as `ds_add_rtn_u32` at +2 and `ds_add_rtn_u64` / `ds_wrxchg_rtn_b64` at +4 do. The recompiler can't model that, so the test shouldn't pin a result for it. - `LdsCondxchg.cpp:107`: `in[5]` pins both the 64 KiB wrap (`+ 0x10000`) and the alignment (`tid & 7`). Please replace them with an out-of-range row (returns 0, guard words unchanged) and keep only 8-byte aligned addresses. - `TechnicalDebt.md:250`: the in-range semantics are now measured on gfx1035, so the line can say so and drop the "address mask follows RDNA3" part. Contended ordering and gfx1013 are still unmeasured.
adriadam10 commented 2026-10-08 09:34:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:34:29Z.

Review: APPROVED


Checked the fix for the 64 KiB wrap: vaddr + offset >= 0x10000 now returns 0 and leaves LDS untouched, matching what we measured on gfx1035. Build and ctest pass on the merge with main, and the new test fails without the change (negative check). Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5454555783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1198#pullrequestreview-5454555783). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:34:29Z. **Review:** APPROVED --- Checked the fix for the 64 KiB wrap: `vaddr + offset >= 0x10000` now returns 0 and leaves LDS untouched, matching what we measured on gfx1035. Build and ctest pass on the merge with main, and the new test fails without the change (negative check). Merging once CI 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!1198
No description provided.