Skip to content

feat(shader): implement ds_wrxchg2_rtn_b32 and ds_wrxchg2st64_rtn_b32 - #385

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
DotDebian:feat/ds-wrxchg2-rtn-b32
Oct 6, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
DotDebian:feat/ds-wrxchg2-rtn-b32

Conversation

@DotDebian

@DotDebian DotDebian commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

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

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 2, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@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

Copy link
Copy Markdown
Collaborator

@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.

DotDebian and others added 3 commits October 6, 2026 22:46
Both swap data0 with the dword at addr + offset0 and data1 with the dword
at addr + offset1, and return the two previous values in vdst and vdst + 1.
The offsets are scaled by 4 bytes, or by 256 bytes for the st64 form.
Each location goes through the existing SharedAtomicSwap32.

An encoding whose two offsets are equal is rejected at decode time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The masked case loaded EXEC from a scalar literal. A scalar mask is read
by subgroup lane, so on a host whose subgroups are narrower than the wave
(llvmpipe: 8 lanes) lane 16 took bit 0 and stayed active. v_cmpx_gt_u32
on the thread id gives the same low 16 lanes on any subgroup size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/ds-wrxchg2-rtn-b32 branch from f97ea10 to 12008dc Compare October 6, 2026 20:48
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 merged commit 006c6cb into boykopovar:main Oct 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants