xshader: 64-bit LDS atomics under a group lock, LDS sized from COMPUTE_PGM_RSRC2 #257

Closed
github-bot wants to merge 2 commits from gh-pr/257/head into gh-pr/257/base
Owner

@⁠QuentinDanblon on GitHub · 2026-11-23 07:59 UTC · original
QuentinDanblon:pr/lds-atomic64 → main · +731 −8 · 7 files · 2 commits · closed without merging

Library / area: tombox-xshader, compute LDS (src/hlsl/lds64.rs, src/hlsl.rs, src/rdna2/mnem.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: AMD RDNA2 ISA document and LLVM AMDGPU for the operations (same definitions as the 32-bit forms, on a 64-bit element). Leonida 6877ac6c and a15e10f7 (GPL-3.0) for the dword order (low first), the wrxchg2 addressing and the returned VGPR pair: rules only, no code taken, two lines added to THIRD_PARTY/CREDITS. COMPUTE_PGM_RSRC2 field layout from gc_10_3_0_sh_mask.h, LDS granule of 128 dwords from LLVM AMDGPU and Mesa.

This is option B from the #214 thread: 64-bit LDS atomics under a lock, tb_lds sized from COMPUTE_PGM_RSRC2.LDS_SIZE, lock word next to it.

Do not merge before the fixture test below has passed on your fixtures. I have no console shader. GDAgent ran it on a sub-version 6 title (see the thread), which moved the sub-version 6 location to 0x138; sub-version 5 is still unproven. Everything else was run on WARP, and the ds_ tests also on an NVIDIA GPU.

What it does

  • Names the 31 DS opcodes 0x40..0x4c, 0x50, 0x60..0x70 and translates them in compute shaders: add, sub, rsub, inc, dec, min/max (signed and unsigned), and, or, xor, mskor, cmpst, wrxchg, wrxchg2, wrxchg2st64, with and without return.
  • Each one runs inside a spin lock on one extra groupshared word, tb_lds64_lock: read both dwords, compute, write both dwords, release.
  • Only a compute shader that contains one of these instructions changes. For it, tb_lds is declared with LDS_SIZE * 128 dwords instead of 8192, indices wrap with % n instead of & 8191u, and main starts by zeroing the lock word and a group sync. Every other shader is emitted byte for byte as before.

The guarantee, as you asked it to be documented

64-bit LDS atomics are serialised only against each other. A 32-bit atomic or a plain write to the same dwords is outside that guarantee. This is written in the generated HLSL next to the declaration of tb_lds64_lock (the gate does not allow a Rust comment) and in the refusal message.

How the translator keeps a title out of the unguaranteed case:

  • At brace depth 0 and outside a loop, a group sync separates a 64-bit atomic from every other LDS access before and after it, so nothing 32-bit can run against it.
  • In a branch or a loop no group sync is possible. A shader that has a 64-bit atomic in a branch or a loop and any other LDS access in a branch or a loop is refused: a 64-bit LDS atomic sits in a branch or a loop of a shader that has another LDS access in a branch or a loop: 64-bit LDS atomics are serialised only against each other, and no group sync can separate them there.
  • This refusal is wider than needed: it does not look at addresses, and it also refuses when the two accesses sit in different constructs that could be separated. I preferred refusing too much.
  • Two 64-bit atomics in the two arms of a branch are accepted: that is the case the lock covers.

Other refusals

  • COMPUTE_PGM_RSRC2 not found in the container.
  • TRAP_PRESENT, EXCP_EN, EXCP_EN_MSB or bit 31 set (mask 0xff006040): the dword is not trusted as an LDS size.
  • LDS_SIZE is 0.
  • LDS_SIZE * 512 + 4 > 32768: no room for the lock word under the groupshared limit.
  • A static DS offset in the shader reaches past the declared size.
  • Durango container (no proven location, see below).
  • Mesh shader.

A dynamic address past the declared size wraps with % n. The 8192-dword mirror wraps the same way today with & 8191u; I did not change that policy.

Where RSRC2 is read, and what is proven

The reader picks the location from the FSSM sub-version, the little-endian dword at byte 0x10 of the inflated blob, and takes the dword only if it lies before the ISA preamble:

sub-version byte evidence
5 0x130 the s_trap entry of docs/BLOCKERS.md ("FSSM dword 0x130") only; not proven
6 0x138 GDAgent's run of the fixture test on one title (this thread): 0x138 fits all 580 LDS-using compute shaders of the first 1,851, 0x130 fits 1; their own checker: 0x138 consistent for 3,232 of 3,233 compute shaders
any other, or none refused: FSSM sub-version N has no known location

The fixture test hlsl::lds64::tests::compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture (gated like the other fixture tests, tombox_fixtures_shaders) walks every .dxbc under analysis/shaders and under the Durango dump directory (TOMBOX_SHADER_DUMPS or %TEMP% ombox_shaders), keeps the compute shaders, and for each one computes the static LDS reach from its own DS instructions (largest offset plus element size, GDS forms left out). It prints the sub-version, the ISA offset, the reach, what the reader takes (or why it refuses), and every dword before the ISA that would fit as RSRC2, as fixed offsets and counted back from the ISA.

It fails if:

  • an FSSM compute fixture is refused, or the dword at its sub-version's location has a must-be-zero bit set, an LDS size below the reach, or a non-zero LDS size in a shader with no LDS instruction;
  • the reader returns a value for a Durango container;
  • fewer than two FSSM compute fixtures were found, or none that uses LDS ("not proven");
  • for a sub-version, the reader's offset is not among the offsets that fit every LDS-using fixture of that sub-version.

At the end it prints, per sub-version, the offsets that fit every LDS-using fixture. If your titles are sub-version 5, this is the run that proves or disproves 0x130.

To run it:

cargo test -p tombox-xshader --release --lib compute_pgm_rsrc2 -- --ignored --nocapture

Limits, stated plainly: a shader with no LDS instruction is weak evidence (many small dwords pass), so the proof rests on the fixtures that use LDS. The reach only counts static offsets, so a real LDS_SIZE can be larger than the reach and still be right. Durango stays refused whatever the test prints.

The LDS index wrap

& 8191u (or % n in a shader with a 64-bit LDS atomic) is now chosen when each LDS access is emitted, from the size planned before the body is translated. The earlier text replacement over the finished body is gone. A shader without a 64-bit LDS atomic is emitted exactly as before.

WARP evidence

ds_lds_atomic64, 102 cases, 64 threads:

  • 95 uncontended: every operation, with and without return, each thread on its own element, edge values included (carry across the dword boundary, signed against unsigned min/max, inc and dec at their limits, cmpst hit and miss).
  • 5 contended, same instruction: 64 lanes on one element with add, xor, max_u, min_i, sub.
  • 2 contended, mixed: 64 lanes on one element, even lanes ds_add_u64, odd lanes ds_sub_u64 in the two arms of a branch (and the reverse).

Mutations, to show the tests see what they claim to see:

mutation result
lock removed (compare-exchange replaced by k = 0, release removed) the 7 contended cases fail, the 95 uncontended pass
no group sync after the lock block (a bug of my first version, before the mixed cases existed) the 5 same-instruction contended cases failed, with the value left by the first 4-lane packet

ds_lds_atomic64_sizing covers the refusals above (register not found, guard bits, LDS_SIZE 0, no room, static offset past the size, the branch refusal with a plain write, a plain read and a 32-bit atomic in the other arm), the accepted branch with two 64-bit atomics, the declarations, and that a shader without a 64-bit atomic is emitted as before with or without a container. The mesh refusal has no test.

Two unit tests in lds64.rs cover the RSRC2 reader (sub-versions 5 and 6, a sub-version 6 container with the dword planted at 0x130, unknown sub-versions 0, 4, 7 and 0xffffffff, before the ISA only, Durango refused) and the scan used by the fixture test, on synthetic containers. ds_lds_atomic64_sizing also covers an unknown sub-version and sub-version 6. With sub-version 6 mapped to 0x130 (local change, not committed) both fail.

A WARP finding you may want to know

The first version of the lock loop was for (;;) { cmpxchg; if (k == 0) { body; release; break; } }. DXC 1.9 compiles it to a one-block loop that holds only the compare-exchange, which is correct DXIL. WARP runs a thread group in packets of 4 lanes and never lets lanes 1 to 3 of a packet leave that loop: the unbounded form hangs, a bounded form lets only threads 0, 4, 8, ... win. With a flag and no break (bool g = true; while (g) { cmpxchg; if (k == 0) { body; release; g = false; } }) the body stays inside the loop and all 64 threads get through. The PR uses the flag form. I did not test the break form on hardware.

Not covered

  • Hardware: only the ds_ tests of tests/exec.rs were run on a real GPU (TOMBOX_EXEC_HW=1, NVIDIA RTX 5090): 23 passed, 0 failed, ds_lds_atomic64 and ds_lds_atomic64_sizing included. No AMD card, and no other hardware run.

  • WARP runs 4-lane packets, and from what I measured one packet after another (inferred, not proven), so parallelism across packets is not exercised. In the mixed case, lanes with the same operation run together.

  • The RSRC2 location, as above.

  • The Trackmania check and the graphics lead's review are yours.

  • cargo test -p tombox-xshader passes: on bd73e23 (rebased on f89e410, r32l), the full suite on WARP, every target: lib 118 passed, 0 failed, 4 ignored; tests/exec.rs 130 passed, 0 failed, 2 ignored; all other targets passed. The fixture tests are ignored here (no fixtures).

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

🤖 Generated with Claude Code

**@⁠QuentinDanblon** on GitHub · 2026-11-23 07:59 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257) `QuentinDanblon:pr/lds-atomic64` → `main` · +731 −8 · 7 files · 2 commits · closed without merging **Library / area:** `tombox-xshader`, compute LDS (`src/hlsl/lds64.rs`, `src/hlsl.rs`, `src/rdna2/mnem.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** AMD RDNA2 ISA document and LLVM AMDGPU for the operations (same definitions as the 32-bit forms, on a 64-bit element). Leonida `6877ac6c` and `a15e10f7` (GPL-3.0) for the dword order (low first), the `wrxchg2` addressing and the returned VGPR pair: rules only, no code taken, two lines added to `THIRD_PARTY/CREDITS`. `COMPUTE_PGM_RSRC2` field layout from `gc_10_3_0_sh_mask.h`, LDS granule of 128 dwords from LLVM AMDGPU and Mesa. This is option B from the #214 thread: 64-bit LDS atomics under a lock, `tb_lds` sized from `COMPUTE_PGM_RSRC2.LDS_SIZE`, lock word next to it. **Do not merge before the fixture test below has passed on your fixtures.** I have no console shader. GDAgent ran it on a sub-version 6 title (see the thread), which moved the sub-version 6 location to 0x138; sub-version 5 is still unproven. Everything else was run on WARP, and the `ds_` tests also on an NVIDIA GPU. ### What it does - Names the 31 DS opcodes 0x40..0x4c, 0x50, 0x60..0x70 and translates them in compute shaders: add, sub, rsub, inc, dec, min/max (signed and unsigned), and, or, xor, mskor, cmpst, wrxchg, wrxchg2, wrxchg2st64, with and without return. - Each one runs inside a spin lock on one extra groupshared word, `tb_lds64_lock`: read both dwords, compute, write both dwords, release. - Only a compute shader that contains one of these instructions changes. For it, `tb_lds` is declared with `LDS_SIZE * 128` dwords instead of 8192, indices wrap with `% n` instead of `& 8191u`, and `main` starts by zeroing the lock word and a group sync. Every other shader is emitted byte for byte as before. ### The guarantee, as you asked it to be documented 64-bit LDS atomics are serialised only against each other. A 32-bit atomic or a plain write to the same dwords is outside that guarantee. This is written in the generated HLSL next to the declaration of `tb_lds64_lock` (the gate does not allow a Rust comment) and in the refusal message. How the translator keeps a title out of the unguaranteed case: - At brace depth 0 and outside a loop, a group sync separates a 64-bit atomic from every other LDS access before and after it, so nothing 32-bit can run against it. - In a branch or a loop no group sync is possible. A shader that has a 64-bit atomic in a branch or a loop **and** any other LDS access in a branch or a loop is refused: `a 64-bit LDS atomic sits in a branch or a loop of a shader that has another LDS access in a branch or a loop: 64-bit LDS atomics are serialised only against each other, and no group sync can separate them there`. - This refusal is wider than needed: it does not look at addresses, and it also refuses when the two accesses sit in different constructs that could be separated. I preferred refusing too much. - Two 64-bit atomics in the two arms of a branch are accepted: that is the case the lock covers. ### Other refusals - `COMPUTE_PGM_RSRC2` not found in the container. - `TRAP_PRESENT`, `EXCP_EN`, `EXCP_EN_MSB` or bit 31 set (mask `0xff006040`): the dword is not trusted as an LDS size. - `LDS_SIZE` is 0. - `LDS_SIZE * 512 + 4 > 32768`: no room for the lock word under the groupshared limit. - A static DS offset in the shader reaches past the declared size. - Durango container (no proven location, see below). - Mesh shader. A dynamic address past the declared size wraps with `% n`. The 8192-dword mirror wraps the same way today with `& 8191u`; I did not change that policy. ### Where RSRC2 is read, and what is proven The reader picks the location from the FSSM sub-version, the little-endian dword at byte 0x10 of the inflated blob, and takes the dword only if it lies before the ISA preamble: | sub-version | byte | evidence | |---|---|---| | 5 | 0x130 | the s_trap entry of `docs/BLOCKERS.md` ("FSSM dword 0x130") only; not proven | | 6 | 0x138 | GDAgent's run of the fixture test on one title (this thread): 0x138 fits all 580 LDS-using compute shaders of the first 1,851, 0x130 fits 1; their own checker: 0x138 consistent for 3,232 of 3,233 compute shaders | | any other, or none | | refused: `FSSM sub-version N has no known location` | The fixture test `hlsl::lds64::tests::compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture` (gated like the other fixture tests, `tombox_fixtures_shaders`) walks every `.dxbc` under `analysis/shaders` and under the Durango dump directory (`TOMBOX_SHADER_DUMPS` or `%TEMP% ombox_shaders`), keeps the compute shaders, and for each one computes the static LDS reach from its own DS instructions (largest offset plus element size, GDS forms left out). It prints the sub-version, the ISA offset, the reach, what the reader takes (or why it refuses), and every dword before the ISA that would fit as RSRC2, as fixed offsets and counted back from the ISA. It fails if: - an FSSM compute fixture is refused, or the dword at its sub-version's location has a must-be-zero bit set, an LDS size below the reach, or a non-zero LDS size in a shader with no LDS instruction; - the reader returns a value for a Durango container; - fewer than two FSSM compute fixtures were found, or none that uses LDS ("not proven"); - for a sub-version, the reader's offset is not among the offsets that fit every LDS-using fixture of that sub-version. At the end it prints, per sub-version, the offsets that fit every LDS-using fixture. If your titles are sub-version 5, this is the run that proves or disproves 0x130. To run it: ``` cargo test -p tombox-xshader --release --lib compute_pgm_rsrc2 -- --ignored --nocapture ``` Limits, stated plainly: a shader with no LDS instruction is weak evidence (many small dwords pass), so the proof rests on the fixtures that use LDS. The reach only counts static offsets, so a real `LDS_SIZE` can be larger than the reach and still be right. Durango stays refused whatever the test prints. ### The LDS index wrap `& 8191u` (or `% n` in a shader with a 64-bit LDS atomic) is now chosen when each LDS access is emitted, from the size planned before the body is translated. The earlier text replacement over the finished body is gone. A shader without a 64-bit LDS atomic is emitted exactly as before. ### WARP evidence `ds_lds_atomic64`, 102 cases, 64 threads: - 95 uncontended: every operation, with and without return, each thread on its own element, edge values included (carry across the dword boundary, signed against unsigned min/max, inc and dec at their limits, cmpst hit and miss). - 5 contended, same instruction: 64 lanes on one element with add, xor, max_u, min_i, sub. - 2 contended, mixed: 64 lanes on one element, even lanes `ds_add_u64`, odd lanes `ds_sub_u64` in the two arms of a branch (and the reverse). Mutations, to show the tests see what they claim to see: | mutation | result | |---|---| | lock removed (compare-exchange replaced by `k = 0`, release removed) | the 7 contended cases fail, the 95 uncontended pass | | no group sync after the lock block (a bug of my first version, before the mixed cases existed) | the 5 same-instruction contended cases failed, with the value left by the first 4-lane packet | `ds_lds_atomic64_sizing` covers the refusals above (register not found, guard bits, `LDS_SIZE` 0, no room, static offset past the size, the branch refusal with a plain write, a plain read and a 32-bit atomic in the other arm), the accepted branch with two 64-bit atomics, the declarations, and that a shader without a 64-bit atomic is emitted as before with or without a container. The mesh refusal has no test. Two unit tests in `lds64.rs` cover the RSRC2 reader (sub-versions 5 and 6, a sub-version 6 container with the dword planted at 0x130, unknown sub-versions 0, 4, 7 and 0xffffffff, before the ISA only, Durango refused) and the scan used by the fixture test, on synthetic containers. `ds_lds_atomic64_sizing` also covers an unknown sub-version and sub-version 6. With sub-version 6 mapped to 0x130 (local change, not committed) both fail. ### A WARP finding you may want to know The first version of the lock loop was `for (;;) { cmpxchg; if (k == 0) { body; release; break; } }`. DXC 1.9 compiles it to a one-block loop that holds only the compare-exchange, which is correct DXIL. WARP runs a thread group in packets of 4 lanes and never lets lanes 1 to 3 of a packet leave that loop: the unbounded form hangs, a bounded form lets only threads 0, 4, 8, ... win. With a flag and no break (`bool g = true; while (g) { cmpxchg; if (k == 0) { body; release; g = false; } }`) the body stays inside the loop and all 64 threads get through. The PR uses the flag form. I did not test the `break` form on hardware. ### Not covered - Hardware: only the `ds_` tests of `tests/exec.rs` were run on a real GPU (`TOMBOX_EXEC_HW=1`, NVIDIA RTX 5090): 23 passed, 0 failed, `ds_lds_atomic64` and `ds_lds_atomic64_sizing` included. No AMD card, and no other hardware run. - WARP runs 4-lane packets, and from what I measured one packet after another (inferred, not proven), so parallelism across packets is not exercised. In the mixed case, lanes with the same operation run together. - The RSRC2 location, as above. - The Trackmania check and the graphics lead's review are yours. - [x] `cargo test -p tombox-xshader` passes: on `bd73e23` (rebased on `f89e410`, r32l), the full suite on WARP, every target: lib 118 passed, 0 failed, 4 ignored; `tests/exec.rs` 130 passed, 0 failed, 2 ignored; all other targets passed. The fixture tests are ignored here (no fixtures). - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The 31 DS opcodes 0x40..0x4c, 0x50, 0x60..0x70 (add, sub, rsub, inc, dec,
min, max, and, or, xor, mskor, cmpst, wrxchg, wrxchg2, wrxchg2st64 on a
64-bit LDS element, with and without return) are named and translated in
compute shaders.

Each one runs inside a spin lock on one extra groupshared word, so two
64-bit atomics never interleave on the two dwords of an element. They are
serialised against each other only: a 32-bit atomic or a plain write to
the same dwords is outside that guarantee. At brace depth 0 a group sync
separates the atomic from every other LDS access. A shader that has a
64-bit atomic in a branch or a loop and another LDS access in a branch or
a loop is refused.

tb_lds is declared with the size the shader states in
COMPUTE_PGM_RSRC2.LDS_SIZE and the lock word sits next to it. The shader
is refused when that register is not found, when TRAP_PRESENT, EXCP_EN,
EXCP_EN_MSB or bit 31 is set, when LDS_SIZE is 0, when the lock word does
not fit under the 32768-byte groupshared limit, or when a static DS
offset reaches past the declared size. Durango containers and mesh
shaders are refused.

The register is read at byte 0x130 of the inflated FSSM blob, before the
ISA. That location is not proven: the fixture test
compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture checks it
against every compute fixture and prints the offsets that fit when it
does not hold. It needs the local fixtures and was not run here.

The lock loop is written with a flag and no break: with a break, DXC
emits a one-block loop holding only the compare-exchange, and WARP never
lets lanes 1 to 3 of a 4-lane packet leave it.

Tests: ds_lds_atomic64 (102 cases on WARP: 95 uncontended, 5 with 64
lanes racing on one element with the same instruction, 2 with 64 lanes
racing with add and sub mixed), ds_lds_atomic64_sizing, and two unit
tests of the RSRC2 reader and of the offset scan.

Behaviour sources: AMD RDNA2 ISA document and LLVM AMDGPU for the
operations; Leonida 6877ac6c and a15e10f7 for the dword order, the
wrxchg2 addressing and the returned pair (rules only, no code taken).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The reader took the dword at byte 0x130 of every FSSM container. GDAgent
ran this PR's fixture test on the compute shaders of an Xbox Series
title whose containers all carry sub-version 6 in the dword at byte 0x10
(PR #257 thread). There, 0x130 fits the static LDS reach of 1 of 580
LDS-using shaders and 0x138 fits all 580; with their own checker, 0x138
is consistent for 3,232 of the title's 3,233 compute shaders.

- The dword at byte 0x10 now picks the location: sub-version 5 reads
  byte 0x130, sub-version 6 reads byte 0x138. Any other sub-version, or
  a container too short to hold one, is refused with "FSSM sub-version N
  has no known location". 0x130 for sub-version 5 still rests only on
  the docs/BLOCKERS.md s_trap entry and is not proven here.
- The fixture test checks each FSSM fixture against the offset of its
  own sub-version, and prints the offsets that fit every LDS-using
  fixture separately for each sub-version.

The LDS index wrap (& 8191u, or % n in a shader with a 64-bit LDS
atomic) is now chosen when each access is emitted, from the size planned
before the body is translated, instead of a text replacement over the
finished body. A shader without a 64-bit LDS atomic is emitted exactly
as before.

Tests: the reader unit test covers both sub-versions, a sub-version 6
container with the dword planted at 0x130 (refused: the dword at 0x138
is not a valid RSRC2), unknown sub-versions 0, 4, 7 and 0xffffffff, and
Durango. ds_lds_atomic64_sizing adds an unknown sub-version, a
sub-version 6 container read at 0x138, and one with nothing at 0x138.
Checked that both tests fail with sub-version 6 mapped to 0x130.

Source: measurements posted by GDAgent on PR #257 (sub-version 6 only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:16:09 +00:00
github-bot closed this pull request 2026-11-26 06:16:09 +00:00
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-23 08:17 UTC · original

Two runs added to the description since I opened this, both on the pushed commit:

  • TOMBOX_EXEC_HW=1, NVIDIA RTX 5090, ds_ tests of tests/exec.rs: 23 passed, 0 failed. That includes the 102 cases of ds_lds_atomic64 (the 7 contended ones too) and ds_lds_atomic64_sizing.
  • WARP, the whole of tests/exec.rs: 129 passed, 0 failed, 2 ignored.

Still not run: the fixture test (no console shader here), and nothing on an AMD card.

🤖 Generated with Claude Code

**@⁠QuentinDanblon** on GitHub · 2026-11-23 08:17 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7095282698) Two runs added to the description since I opened this, both on the pushed commit: - `TOMBOX_EXEC_HW=1`, NVIDIA RTX 5090, `ds_` tests of `tests/exec.rs`: 23 passed, 0 failed. That includes the 102 cases of `ds_lds_atomic64` (the 7 contended ones too) and `ds_lds_atomic64_sizing`. - WARP, the whole of `tests/exec.rs`: 129 passed, 0 failed, 2 ignored. Still not run: the fixture test (no console shader here), and nothing on an AMD card. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 13:31 UTC · original

Holding as your description asks, until the fixture test has passed on real console shaders. Two other points to settle then: the LDS size read from offset 0x130 of the shader container is unproven, and the LDS index rewrite (& 8191u] -> % n]) is a text replacement over the whole body. The spin lock and the cmpst/mskor operand order look right. It will need a rebase on #240/#245 (and #204 if that lands first).

**@⁠omonkeyz** on GitHub · 2026-11-23 13:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7102654485) Holding as your description asks, until the fixture test has passed on real console shaders. Two other points to settle then: the LDS size read from offset 0x130 of the shader container is unproven, and the LDS index rewrite (& 8191u] -> % n]) is a text replacement over the whole body. The spin lock and the cmpst/mskor operand order look right. It will need a rebase on #240/#245 (and #204 if that lands first).
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 15:02 UTC · original

Hi @⁠QuentinDanblon, I ran the fixture test from this PR on real console shaders. It points to byte 0x138, not 0x130, for the title I have here.

What I ran: hlsl::lds64::tests::compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture at your head e318596, unchanged, with --ignored. TOMBOX_SHADER_DUMPS pointed at the unique shader containers of The Matrix Awakens (Xbox Series X build). All of them are FSSM, with header dword 0x10 = 6 and the ISA at 0x150. Native Windows 11, CPU only.

Result: I had to stop the run part-way for an unrelated reason (machine load), after 1,851 compute shaders in file-name order. 580 of them have LDS instructions:

  • The dword at 0x130 fits the static LDS reach in 1 of the 580. In the other 579 it fails the reader's own rules:
    • 377 have must-be-zero bits set (0xff006040);
    • 202 declare less LDS than the shader reaches, usually 0.
  • The "offsets that fit all of those" intersection from your test is exactly [0x138]. 0x138 is in the candidate list of all 580.
  • Example, 0a61380a5533d389 (ReduceCS, 8x8x1):
    • 0x130 holds 0x00000bf0: LDS 0, and bit 6 is set, which is one of the must-be-zero bits;
    • 0x138 holds 0x00008990: USER_SGPR 8, TGID_X/Y on, TIDIG_COMP_CNT 1, LDS_SIZE 1 (512 bytes), which matches its numthreads and LDS use;
    • the candidate offsets your test prints for it are 0x78, 0xd4, 0x100, 0x138.

An earlier full pass with my own checker covered all 3,233 compute shaders of the title with the same rules:

offset consistent failures
0x138 3,232 of 3,233 the one miss fails only the TIDIG-vs-numthreads check
0x130 702 2,025 with must-be-zero bits set, 360 LDS mismatches

LDS_SIZE at 0x138 is non-zero exactly in the shaders that have LDS instructions.

That fits the sub-version note in your description: with header dword 0x10 = 6, the block sits 8 bytes later than 0x130. As written, the reader would refuse or mis-size every compute shader of this title that it is applied to. Reading 0x130 for sub-version 5 and 0x138 for sub-version 6, keyed on the dword at 0x10, would cover both, if your source for 0x130 is a sub-version 5 title. I could only check sub-version 6, because that is all this title has.

Happy to rerun the full set on a new head if that helps.

**@⁠GDAgent** on GitHub · 2026-11-23 15:02 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7104920487) Hi @⁠QuentinDanblon, I ran the fixture test from this PR on real console shaders. It points to byte 0x138, not 0x130, for the title I have here. **What I ran:** `hlsl::lds64::tests::compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture` at your head `e318596`, unchanged, with `--ignored`. `TOMBOX_SHADER_DUMPS` pointed at the unique shader containers of The Matrix Awakens (Xbox Series X build). All of them are FSSM, with header dword 0x10 = 6 and the ISA at 0x150. Native Windows 11, CPU only. **Result:** I had to stop the run part-way for an unrelated reason (machine load), after 1,851 compute shaders in file-name order. 580 of them have LDS instructions: - The dword at 0x130 fits the static LDS reach in 1 of the 580. In the other 579 it fails the reader's own rules: - 377 have must-be-zero bits set (`0xff006040`); - 202 declare less LDS than the shader reaches, usually 0. - The "offsets that fit all of those" intersection from your test is exactly `[0x138]`. 0x138 is in the candidate list of all 580. - Example, `0a61380a5533d389` (`ReduceCS`, 8x8x1): - 0x130 holds `0x00000bf0`: LDS 0, and bit 6 is set, which is one of the must-be-zero bits; - 0x138 holds `0x00008990`: USER_SGPR 8, TGID_X/Y on, TIDIG_COMP_CNT 1, LDS_SIZE 1 (512 bytes), which matches its `numthreads` and LDS use; - the candidate offsets your test prints for it are `0x78, 0xd4, 0x100, 0x138`. An earlier full pass with my own checker covered all 3,233 compute shaders of the title with the same rules: | offset | consistent | failures | |---|---|---| | 0x138 | 3,232 of 3,233 | the one miss fails only the TIDIG-vs-numthreads check | | 0x130 | 702 | 2,025 with must-be-zero bits set, 360 LDS mismatches | LDS_SIZE at 0x138 is non-zero exactly in the shaders that have LDS instructions. That fits the sub-version note in your description: with header dword 0x10 = 6, the block sits 8 bytes later than 0x130. As written, the reader would refuse or mis-size every compute shader of this title that it is applied to. Reading 0x130 for sub-version 5 and 0x138 for sub-version 6, keyed on the dword at 0x10, would cover both, if your source for 0x130 is a sub-version 5 title. I could only check sub-version 6, because that is all this title has. Happy to rerun the full set on a new head if that helps.
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-23 16:44 UTC · original

@⁠GDAgent thanks, that run settles it for sub-version 6. Pushed bd73e23 (branch rebased on r32l f89e410):

  • The reader now keys the location on the dword at byte 0x10: sub-version 5 reads 0x130, sub-version 6 reads 0x138, any other sub-version is refused (FSSM sub-version N has no known location). 0x130 for sub-version 5 still rests only on the BLOCKERS s_trap entry.
  • The fixture test checks each fixture against its own sub-version's offset and prints the fitting offsets per sub-version. Run it with cargo test -p tombox-xshader --release --lib compute_pgm_rsrc2 -- --ignored --nocapture. A rerun on your set would confirm 0x138 end to end through the reader itself.

@⁠omonkeyz, on your two other points:

  • LDS size at 0x130: now per sub-version, as above. If your fixtures are sub-version 5, the test output on them is what proves or disproves 0x130.
  • The & 8191u] -> % n] text replacement is gone. The wrap is chosen when each LDS access is emitted, from the size planned before the body is translated. Shaders without a 64-bit LDS atomic are emitted exactly as before.

On bd73e23, full tombox-xshader suite on WARP: lib 118/0 (4 ignored), tests/exec.rs 130/0 (2 ignored), all other targets pass. Both RSRC2 tests fail with sub-version 6 mapped to 0x130 (local check). #240/#245/#204 are still open, so the branch sits on main; I'll rebase when they land. The description is updated.

**@⁠QuentinDanblon** on GitHub · 2026-11-23 16:44 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7107593825) @⁠GDAgent thanks, that run settles it for sub-version 6. Pushed `bd73e23` (branch rebased on r32l `f89e410`): - The reader now keys the location on the dword at byte 0x10: sub-version 5 reads 0x130, sub-version 6 reads 0x138, any other sub-version is refused (`FSSM sub-version N has no known location`). 0x130 for sub-version 5 still rests only on the BLOCKERS s_trap entry. - The fixture test checks each fixture against its own sub-version's offset and prints the fitting offsets per sub-version. Run it with `cargo test -p tombox-xshader --release --lib compute_pgm_rsrc2 -- --ignored --nocapture`. A rerun on your set would confirm 0x138 end to end through the reader itself. @⁠omonkeyz, on your two other points: - LDS size at 0x130: now per sub-version, as above. If your fixtures are sub-version 5, the test output on them is what proves or disproves 0x130. - The `& 8191u]` -> `% n]` text replacement is gone. The wrap is chosen when each LDS access is emitted, from the size planned before the body is translated. Shaders without a 64-bit LDS atomic are emitted exactly as before. On `bd73e23`, full `tombox-xshader` suite on WARP: lib 118/0 (4 ignored), `tests/exec.rs` 130/0 (2 ignored), all other targets pass. Both RSRC2 tests fail with sub-version 6 mapped to 0x130 (local check). #240/#245/#204 are still open, so the branch sits on main; I'll rebase when they land. The description is updated.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:48 UTC · original

Applied on main in b5cd984 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:48 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7164323946) Applied on main in b5cd984 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 14:01 UTC · original

Follow-up after applying: on our local set of real console compute shaders, compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture fails for 7 fixtures. In those, the ISA starts before byte 0x130, so the reader reports COMPUTE_PGM_RSRC2 as not found (the 'byte 0x130 is not before the ISA' error). Translation of shaders without 64-bit LDS atomics is unaffected, since the size is only read when such an atomic is present, and those were refused before this PR as well. So this is a coverage gap rather than a regression: sub-version 5 containers with a short header need another proven location (or a size taken from PSV/numthreads data) before 64-bit LDS atomics can be trusted there. A follow-up PR would be welcome.

**@⁠omonkeyz** on GitHub · 2026-11-25 14:01 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#issuecomment-7164643061) Follow-up after applying: on our local set of real console compute shaders, compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture fails for 7 fixtures. In those, the ISA starts before byte 0x130, so the reader reports COMPUTE_PGM_RSRC2 as not found (the 'byte 0x130 is not before the ISA' error). Translation of shaders without 64-bit LDS atomics is unaffected, since the size is only read when such an atomic is present, and those were refused before this PR as well. So this is a coverage gap rather than a regression: sub-version 5 containers with a short header need another proven location (or a size taken from PSV/numthreads data) before 64-bit LDS atomics can be trusted there. A follow-up PR would be welcome.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 15:09 UTC · original · reviewed

Scope: RDNA2 silicon semantics, a hardware run of this PR's tests on an AMD card, and a full read of the diff. It doesn't cover the open COMPUTE_PGM_RSRC2 location question, which needs console containers, so this is a comment, not a merge recommendation.

Silicon semantics. Recorded on a gfx1036 iGPU (Ryzen 5 9600X, ROCm/HIP 5.7.1), which is RDNA2, not gfx1013. Raw wave32 kernels ran twice with identical results, compared against op64 from this PR's test:

  • All 14 ops with and without return, plus ds_wrxchg_rtn_b64, 29 kernels: every case matches, 4,096 random 64-bit values each (edges included, and o == d in a quarter of the cases). The returned value is the old 64-bit word, low dword first. That includes the operand roles the review asked about:
    • cmpst: data0 is the compare value, and data1 is stored when it matches;
    • mskor: (o & ~data0) | data1.
  • ds_wrxchg2_rtn_b64 / ds_wrxchg2st64_rtn_b64 at offset pairs (0,1), (3,1), (2,0) and (1,5) match:
    • the units are 8 and 512 bytes;
    • data0 goes to offset0 and data1 to offset1;
    • the old pairs come back in v[n:n+1] for offset0 and v[n+2:n+3] for offset1.
  • LDS allocation: a kernel's LDS is usable up to the next 512-byte boundary (4 → 512, 516 → 1024, 1028 → 1536 bytes), and accesses past that read 0. So LDS_GRANULE_DWORDS = 128 matches the hardware. Out-of-range dynamic addresses read 0 on hardware instead of wrapping. The PR keeps the existing & 8191u / % n wrap policy and says so, so that isn't a change here.

An AMD run of this PR's tests. On head bd73e23, Windows 11, DXC 1.9.2609, AMD Radeon RX 9060 XT:

  • --test exec ds_: 23 passed, 0 failed on WARP and with TOMBOX_EXEC_HW=1. That includes ds_lds_atomic64: 31 mnemonics verified, 0 failing, 102 cases with the contended cases, and ds_lds_atomic64_sizing.
  • --lib lds64: 2 passed, 1 ignored (the fixture test).

One issue from reading the diff. ds_reach() sizes the element from the mnemonic:

  • 64 gives 8 bytes, _b8/_u8/_i8 gives 1, and 16-bit names give 2;
  • everything else gives 4, so ds_read_b96 / ds_write_b96 and ds_read_b128 / ds_write_b128 count as 4 bytes instead of 12 and 16.

A static offset whose access ends past the declared LDS is then not refused by the "static LDS offset reaches byte N" check. For example, ds_write_b128 at offset n*4 - 4 reaches 12 bytes past the end. #240, which adds those opcodes, has been applied on your side, so they'll be decoded. Adding _b96 → 12 and _b128 → 16 before the default would close it.

The rest reads correctly to me:

  • The spin lock does its work and releases inside the branch where the compare-exchange succeeds, so a wave can't deadlock on its own lanes.
  • The RSRC2 LDS_SIZE field is (w >> 15) & 0x1ff.
  • The wrxchg2 return-pair layout matches the hardware.

(Measured and reviewed with an AI assistant, Claude.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 15:09 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/257#pullrequestreview-6041847162) · **reviewed** **Scope:** RDNA2 silicon semantics, a hardware run of this PR's tests on an AMD card, and a full read of the diff. It doesn't cover the open COMPUTE_PGM_RSRC2 location question, which needs console containers, so this is a comment, not a merge recommendation. **Silicon semantics.** Recorded on a gfx1036 iGPU (Ryzen 5 9600X, ROCm/HIP 5.7.1), which is RDNA2, not gfx1013. Raw wave32 kernels ran twice with identical results, compared against `op64` from this PR's test: - All 14 ops with and without return, plus `ds_wrxchg_rtn_b64`, 29 kernels: every case matches, 4,096 random 64-bit values each (edges included, and o == d in a quarter of the cases). The returned value is the old 64-bit word, low dword first. That includes the operand roles the review asked about: - `cmpst`: data0 is the compare value, and data1 is stored when it matches; - `mskor`: (o & ~data0) | data1. - `ds_wrxchg2_rtn_b64` / `ds_wrxchg2st64_rtn_b64` at offset pairs (0,1), (3,1), (2,0) and (1,5) match: - the units are 8 and 512 bytes; - data0 goes to offset0 and data1 to offset1; - the old pairs come back in v[n:n+1] for offset0 and v[n+2:n+3] for offset1. - **LDS allocation:** a kernel's LDS is usable up to the next 512-byte boundary (4 → 512, 516 → 1024, 1028 → 1536 bytes), and accesses past that read 0. So `LDS_GRANULE_DWORDS = 128` matches the hardware. Out-of-range dynamic addresses read 0 on hardware instead of wrapping. The PR keeps the existing `& 8191u` / `% n` wrap policy and says so, so that isn't a change here. **An AMD run of this PR's tests.** On head `bd73e23`, Windows 11, DXC 1.9.2609, AMD Radeon RX 9060 XT: - `--test exec ds_`: 23 passed, 0 failed on WARP and with `TOMBOX_EXEC_HW=1`. That includes `ds_lds_atomic64: 31 mnemonics verified, 0 failing, 102 cases` with the contended cases, and `ds_lds_atomic64_sizing`. - `--lib lds64`: 2 passed, 1 ignored (the fixture test). **One issue from reading the diff.** `ds_reach()` sizes the element from the mnemonic: - `64` gives 8 bytes, `_b8`/`_u8`/`_i8` gives 1, and 16-bit names give 2; - **everything else gives 4**, so `ds_read_b96` / `ds_write_b96` and `ds_read_b128` / `ds_write_b128` count as 4 bytes instead of 12 and 16. A static offset whose access ends past the declared LDS is then not refused by the "static LDS offset reaches byte N" check. For example, `ds_write_b128` at offset `n*4 - 4` reaches 12 bytes past the end. #240, which adds those opcodes, has been applied on your side, so they'll be decoded. Adding `_b96` → 12 and `_b128` → 16 before the default would close it. The rest reads correctly to me: - The spin lock does its work and releases inside the branch where the compare-exchange succeeds, so a wave can't deadlock on its own lanes. - The RSRC2 LDS_SIZE field is (w >> 15) & 0x1ff. - The wrxchg2 return-pair layout matches the hardware. (Measured and reviewed with an AI assistant, Claude.)

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.