feat(shader): implement ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 #385

Merged
DotDebian merged 3 commits from upstream-pr/385 into main 2026-10-06 21:00:00 +00:00
DotDebian commented 2026-10-02 17:02:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-02T17:02:03Z.


What

ds_wrxchg2_rtn_b32 (DS 0x2e) and ds_wrxchg2st64_rtn_b32 (DS 0x2f) threw DS opcode is not supported.

Both swap data0 with the dword at addr + offset0 * S and data1 with the dword at addr + offset1 * S, and return the two previous values in vdst and vdst + 1. S is 4 bytes, or 256 bytes for the st64 form, as for ds_read2_b32 / ds_read2st64_b32.

Sources: AMD's machine-readable RDNA2 ISA specification (description, operand shape: 64-bit VDST, ADDR, DATA0, DATA1) and LLVM's DSInstructions.td (encodings, v[n:n+1], addr, data0, data1 offset0 offset1).

Implementation: no new IR. The translation emits one SharedAtomicSwap32 per location, the op ds_wrxchg_rtn_b32 already uses, so EXEC masking, bounds and the LDS barrier are the existing ones. All three sources are read before the destination is written.

Rejected at decode time instead of guessed:

  • offset0 == offset1: the specification does not say what the two results and the final memory value are when both offsets name the same location.
  • vdst == 255: the second result has no register.

The 64-bit forms (ds_wrxchg_rtn_b64, ds_wrxchg2_rtn_b64, ds_wrxchg2st64_rtn_b64) are not in this PR: LDS is an array of dwords here, and two 32-bit swaps are not a 64-bit atomic exchange.

Not measured on hardware. The swaps of the two locations are two separate atomics, not one atomic pair.

Test agc_driver_ds_wrxchg2: one wave32 compute shader, every lane on its own LDS dwords. It checks both returned values and both memory locations for:

  • ds_wrxchg2_rtn_b32 with offset0 < offset1
  • ds_wrxchg2st64_rtn_b32 with offset0 > offset1
  • a destination pair that is also the two data registers
  • EXEC = low 16 lanes, set by v_cmpx_gt_u32 on the thread id so that it does not depend on the host subgroup size: inactive lanes keep their registers and their memory

It also checks the two rejections by their message.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, full ctest: 109/109 pass.

  • Without the change agc_driver_ds_wrxchg2 fails with DS opcode is not supported.
  • With data0 and data1 swapped in the translation it fails with lane 0 ds_wrxchg2_rtn_b32 offset0 memory is 0x40000003, expected 0x30000002.

No title was run.

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. #320 and #334 touch the same DS tables; a trial merge with #334 is clean.
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4719177531 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/385). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-02T17:02:03Z. --- ### What `ds_wrxchg2_rtn_b32` (DS 0x2e) and `ds_wrxchg2st64_rtn_b32` (DS 0x2f) threw `DS opcode is not supported`. Both swap `data0` with the dword at `addr + offset0 * S` and `data1` with the dword at `addr + offset1 * S`, and return the two previous values in `vdst` and `vdst + 1`. `S` is 4 bytes, or 256 bytes for the st64 form, as for `ds_read2_b32` / `ds_read2st64_b32`. Sources: AMD's machine-readable RDNA2 ISA specification (description, operand shape: 64-bit `VDST`, `ADDR`, `DATA0`, `DATA1`) and LLVM's `DSInstructions.td` (encodings, `v[n:n+1], addr, data0, data1 offset0 offset1`). Implementation: no new IR. The translation emits one `SharedAtomicSwap32` per location, the op `ds_wrxchg_rtn_b32` already uses, so EXEC masking, bounds and the LDS barrier are the existing ones. All three sources are read before the destination is written. Rejected at decode time instead of guessed: - `offset0 == offset1`: the specification does not say what the two results and the final memory value are when both offsets name the same location. - `vdst == 255`: the second result has no register. The 64-bit forms (`ds_wrxchg_rtn_b64`, `ds_wrxchg2_rtn_b64`, `ds_wrxchg2st64_rtn_b64`) are not in this PR: LDS is an array of dwords here, and two 32-bit swaps are not a 64-bit atomic exchange. Not measured on hardware. The swaps of the two locations are two separate atomics, not one atomic pair. Test `agc_driver_ds_wrxchg2`: one wave32 compute shader, every lane on its own LDS dwords. It checks both returned values and both memory locations for: - `ds_wrxchg2_rtn_b32` with `offset0 < offset1` - `ds_wrxchg2st64_rtn_b32` with `offset0 > offset1` - a destination pair that is also the two data registers - EXEC = low 16 lanes, set by `v_cmpx_gt_u32` on the thread id so that it does not depend on the host subgroup size: inactive lanes keep their registers and their memory It also checks the two rejections by their message. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, full `ctest`: 109/109 pass. - Without the change `agc_driver_ds_wrxchg2` fails with `DS opcode is not supported`. - With `data0` and `data1` swapped in the translation it fails with `lane 0 ds_wrxchg2_rtn_b32 offset0 memory is 0x40000003, expected 0x30000002`. No title was run. ### 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. #320 and #334 touch the same DS tables; a trial merge with #334 is clean. - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-02 17:03:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T17:03:03Z.


Progress report (5b35909 → 12008dc)

📈 GPU shader instructions: 96.66% (+0.18%, +2 implemented instructions)

✅ 2 implemented
Encoding Name
DS DS_WRXCHG2ST64_RTN_B32
DS DS_WRXCHG2_RTN_B32
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5957333532 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/385#issuecomment-5957333532). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T17:03:03Z. --- <!-- progress-report --> ### Progress report (5b35909 → 12008dc) 📈 **GPU shader instructions**: 96.66% (+0.18%, +2 implemented instructions) <details> <summary>✅ 2 implemented</summary> | Encoding | Name | | - | - | | DS | `DS_WRXCHG2ST64_RTN_B32` | | DS | `DS_WRXCHG2_RTN_B32` | </details>
oneandonlydean commented 2026-10-04 07:10:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:10:12Z.


@boykopovar this one needs your call: is it OK to implement ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 as two separate 32-bit atomic swaps? Within one wave the results are right: the encodings match LLVM's DSInstructions.td, and the test covers EXEC and a destination that aliases the data registers. Between waves, another wave can see one location swapped and the other not yet. Whether the hardware makes the pair atomic isn't documented, and only code racing across waves could tell, so this is about how strict exact-or-throw should be rather than a bug. The author states the gap in the PR body and kept the 64-bit forms out for a similar reason.

My read: acceptable, as long as the gap goes into TechnicalDebt (CONTRIBUTING asks for known gaps there, and right now it's only in the PR body).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977587112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/385#issuecomment-5977587112). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:10:12Z. --- @boykopovar this one needs your call: is it OK to implement `ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32` as two separate 32-bit atomic swaps? Within one wave the results are right: the encodings match LLVM's `DSInstructions.td`, and the test covers EXEC and a destination that aliases the data registers. Between waves, another wave can see one location swapped and the other not yet. Whether the hardware makes the pair atomic isn't documented, and only code racing across waves could tell, so this is about how strict exact-or-throw should be rather than a bug. The author states the gap in the PR body and kept the 64-bit forms out for a similar reason. My read: acceptable, as long as the gap goes into TechnicalDebt (CONTRIBUTING asks for known gaps there, and right now it's only in the PR body).
adriadam10 commented 2026-10-06 08:05:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-06T08:05:29Z.


@oneandonlydean measured on RDNA2 hardware (gfx1035), for the open question here:

  • With each lane on its own dwords (this PR's test), ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 behave as described: vdst gets the old dword at offset0 * S, vdst + 1 the old dword at offset1 * S, memory ends with data0/data1, S = 4 or 256 bytes.
  • When lanes overlap (lane n's offset1 address = lane n+1's offset0 address), the hardware does every lane's first exchange before any lane's second one. Two separate atomics per invocation give the same result only if the invocations run in lockstep. That isn't guaranteed: lavapipe uses 8-wide subgroups, so across subgroups it can differ.
  • offset0 == offset1 is well defined on hardware: the two swaps happen in order, so vdst = old value, vdst + 1 = data0, and memory ends as data1. Throwing for it is exact-or-throw, so not a blocker; but it could be translated as two sequential swaps.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6012068879 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/385#issuecomment-6012068879). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-06T08:05:29Z. --- @oneandonlydean measured on RDNA2 hardware (gfx1035), for the open question here: - With each lane on its own dwords (this PR's test), `ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32` behave as described: vdst gets the old dword at offset0 * S, vdst + 1 the old dword at offset1 * S, memory ends with data0/data1, S = 4 or 256 bytes. - When lanes overlap (lane n's offset1 address = lane n+1's offset0 address), the hardware does every lane's first exchange before any lane's second one. Two separate atomics per invocation give the same result only if the invocations run in lockstep. That isn't guaranteed: lavapipe uses 8-wide subgroups, so across subgroups it can differ. - `offset0 == offset1` is well defined on hardware: the two swaps happen in order, so vdst = old value, vdst + 1 = data0, and memory ends as data1. Throwing for it is exact-or-throw, so not a blocker; but it could be translated as two sequential swaps.
adriadam10 commented 2026-10-06 20:48:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-06T20:48:52Z.


Rebased on main and added the TechnicalDebt line @oneandonlydean asked for (12008dc), with what we measured on gfx1035: hardware does every lane's first exchange before any lane's second one, so two atomics per invocation can differ when lanes overlap addresses. Build and full ctest green on RADV; agc_driver_ds_wrxchg2 also passes on CI's lavapipe (0.44 s cold). Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6025175979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/385#issuecomment-6025175979). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-06T20:48:52Z. --- Rebased on main and added the TechnicalDebt line @oneandonlydean asked for (12008dc), with what we measured on gfx1035: hardware does every lane's first exchange before any lane's second one, so two atomics per invocation can differ when lanes overlap addresses. Build and full ctest green on RADV; `agc_driver_ds_wrxchg2` also passes on CI's lavapipe (0.44 s cold). 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!385
No description provided.