xshader: 64-bit LDS atomics under a group lock, LDS sized from COMPUTE_PGM_RSRC2 #257
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!257
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/257/head"
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?
@QuentinDanblon on GitHub · 2026-11-23 07:59 UTC · original
QuentinDanblon:pr/lds-atomic64→main· +731 −8 · 7 files · 2 commits · closed without mergingLibrary / 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
6877ac6canda15e10f7(GPL-3.0) for the dword order (low first), thewrxchg2addressing and the returned VGPR pair: rules only, no code taken, two lines added toTHIRD_PARTY/CREDITS.COMPUTE_PGM_RSRC2field layout fromgc_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_ldssized fromCOMPUTE_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
tb_lds64_lock: read both dwords, compute, write both dwords, release.tb_ldsis declared withLDS_SIZE * 128dwords instead of 8192, indices wrap with% ninstead of& 8191u, andmainstarts 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:
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.Other refusals
COMPUTE_PGM_RSRC2not found in the container.TRAP_PRESENT,EXCP_EN,EXCP_EN_MSBor bit 31 set (mask0xff006040): the dword is not trusted as an LDS size.LDS_SIZEis 0.LDS_SIZE * 512 + 4 > 32768: no room for the lock word under the groupshared limit.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:
docs/BLOCKERS.md("FSSM dword 0x130") only; not provenFSSM sub-version N has no known locationThe 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.dxbcunderanalysis/shadersand under the Durango dump directory (TOMBOX_SHADER_DUMPSor%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:
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:
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_SIZEcan be larger than the reach and still be right. Durango stays refused whatever the test prints.The LDS index wrap
& 8191u(or% nin 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:ds_add_u64, odd lanesds_sub_u64in the two arms of a branch (and the reverse).Mutations, to show the tests see what they claim to see:
k = 0, release removed)ds_lds_atomic64_sizingcovers the refusals above (register not found, guard bits,LDS_SIZE0, 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.rscover 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_sizingalso 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 thebreakform on hardware.Not covered
Hardware: only the
ds_tests oftests/exec.rswere run on a real GPU (TOMBOX_EXEC_HW=1, NVIDIA RTX 5090): 23 passed, 0 failed,ds_lds_atomic64andds_lds_atomic64_sizingincluded. 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-xshaderpasses: onbd73e23(rebased onf89e410, r32l), the full suite on WARP, every target: lib 118 passed, 0 failed, 4 ignored;tests/exec.rs130 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
missingwith a note🤖 Generated with Claude Code
@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 oftests/exec.rs: 23 passed, 0 failed. That includes the 102 cases ofds_lds_atomic64(the 7 contended ones too) andds_lds_atomic64_sizing.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
@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).
@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_fixtureat your heade318596, unchanged, with--ignored.TOMBOX_SHADER_DUMPSpointed 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:
0xff006040);[0x138]. 0x138 is in the candidate list of all 580.0a61380a5533d389(ReduceCS, 8x8x1):0x00000bf0: LDS 0, and bit 6 is set, which is one of the must-be-zero bits;0x00008990: USER_SGPR 8, TGID_X/Y on, TIDIG_COMP_CNT 1, LDS_SIZE 1 (512 bytes), which matches itsnumthreadsand LDS use;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:
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.
@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 r32lf89e410):FSSM sub-version N has no known location). 0x130 for sub-version 5 still rests only on the BLOCKERS s_trap entry.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:
& 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, fulltombox-xshadersuite on WARP: lib 118/0 (4 ignored),tests/exec.rs130/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.@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 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.
@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
op64from this PR's test: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_b64at offset pairs (0,1), (3,1), (2,0) and (1,5) match:LDS_GRANULE_DWORDS = 128matches the hardware. Out-of-range dynamic addresses read 0 on hardware instead of wrapping. The PR keeps the existing& 8191u/% nwrap 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 withTOMBOX_EXEC_HW=1. That includesds_lds_atomic64: 31 mnemonics verified, 0 failing, 102 caseswith the contended cases, andds_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:64gives 8 bytes,_b8/_u8/_i8gives 1, and 16-bit names give 2;ds_read_b96/ds_write_b96andds_read_b128/ds_write_b128count 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_b128at offsetn*4 - 4reaches 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:
(Measured and reviewed with an AI assistant, Claude.)
Pull request closed