xshader: lower ds_mskor_b32 and ds_wrxchg2_rtn_b32 on compute LDS #214
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!214
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/214/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 03:16 UTC · original
QuentinDanblon:pr/lds-mskor-wrxchg2→main· +85 −0 · 5 files · 1 commits · closed without mergingLibrary / area: tombox-xshader, compute LDS:
ds_mskor_b32,ds_mskor_rtn_b32,ds_wrxchg2_rtn_b32,ds_wrxchg2st64_rtn_b32Exports changed (exports.txt rows): none
Source of the behaviour: opcodes from
tests/isa_table/gfx1013.txt(DS 0x0c, 0x2c, 0x2e, 0x2f). Semantics from the measurements published by Leonida (GPL-3.0, commits7e7f6ce02cand6da90ef02e,docs/design.md), taken on a gfx1100 card. That is GFX11, not GFX10.1: the encodings used here are the gfx1013 ones, and only the memory rules come from that measurement. Rules only, no code taken. Two lines added toTHIRD_PARTY/CREDITS, one per commit.One topic, +85: 31 lines of translator, 52 lines of test, 2 credit lines.
What was missing
The decoder had no name for these four DS opcodes, so a compute shader that uses one was not translated.
The change
ds_mskor_b32,ds_mskor_rtn_b32M = (M & ~DATA) | DATA2.DATA2bits outsideDATAare ORed in too, it is not a masked insert. The returning form gives the old word.tb_lds, the same oneds_sub_u32/ds_inc_u32already use, so the whole update is atomicds_wrxchg2_rtn_b32vaddr + offset0 * 4andvaddr + offset1 * 4withdata0anddata1;vdstandvdst + 1get the two old wordsInterlockedExchange, both done before either register is written, sovdstmay alias the address or the datads_wrxchg2st64_rtn_b32With both offsets on one dword, the second exchange returns
data0and the memory keepsdata1, as measured.Compute stage only, like the other LDS atomics. With
GDS=1the four instructions stay refused by the existing... with GDS=1 is not lowerederror. Avdstof v255 for the exchange pair is refused.Tests
ds_lds_mskor_wrxchg2intests/exec_mem/ds.rs, 25 cases on 64 threads, each thread reading its neighbour's slot after the op:ds_mskor: 8 operand triples, each in both forms (16 cases), two offsets, includingDATA2outsideDATA, mask = data, zero mask. The non-returning form also checks thatvdstis left alone.ds_mskor_b32under contention: the 64 threads update one dword, each clearing and setting its own bit; the result must be0xaaaaaaaa.ds_wrxchg2_rtn_b32and the st64 form: offsets (0,1), (1,0), (2,3), old words invdst, new words read back.vdstaliasing the address anddata0.To check that the cases discriminate, I ran them once against two deliberately wrong lowerings (a masked insert for
ds_mskor, a 512-byte step for st64): 27 failing entries, onds_mskor_b32,ds_mskor_rtn_b32andds_wrxchg2st64_rtn_b32.cargo test -p tombox-xshader --release --no-fail-fastonc16f0faplus this commit, WARP: 325 passed, 0 failed, 18 ignored (exec 120, lib 106, opcodes 26, synthetic 24, mesh 24). Rebased on2f1a94cafterwards (onlyTHIRD_PARTY/CREDITSconflicted);ds_lds*,scalar_mode_f16_f64andisa_tablererun there, all pass. No hardware run: the GPU of this machine is busy, and it is not an AMD card anyway.Not covered
ds_mskor_b64,ds_wrxchg_rtn_b64,ds_wrxchg2_rtn_b64,ds_and/or/xor_b64) are not in this PR. I can send them next if you want them.No comment lines added (PR gate: 5 files checked, 0 problems).
cargo test -p tombox-xshader --releasepassesmissingwith a note🤖 Generated with Claude Code
@omonkeyz on GitHub · 2026-11-23 03:38 UTC · original
Applied in
689f690(merged in44597ab) with your authorship kept, thanks. Full tombox-xshader suite on WARP: exec 120/0, plus all other targets. The only failure, gcn::tests::dumped_durango_shaders, is pre-existing and comes from this machine's local dumps.@QuentinDanblon on GitHub · 2026-11-23 06:16 UTC · original
Before I write the 64-bit LDS atomics I offered above, one question, because I do not think they can be exact with what the translator has today.
tb_ldsis an array of dwords and HLSL only has 32-bit interlocked operations on it. So a 64-bit atomic has to be two 32-bit ones, low dword then high dword, the wayds_write_b64is already split into twods_write_b32parts inhlsl/ds_ext.rs. Without contention on the element that is exact. With contention it is not:ds_wrxchg_rtn_b64,ds_wrxchg2_rtn_b64,ds_wrxchg2st64_rtn_b64ds_and_b64,ds_or_b64,ds_xor_b64,ds_mskor_b64andand anor): the low dword can see them in one order and the high dword in the other_rtn_forms of thoseds_add_u64,ds_sub_u64,ds_min/max_*64,ds_cmpst_b64Leonida ships the first three rows this way and documents the same limit (its notes call the bitwise memory result exact, which holds only when the racing operations are the same kind). Its gfx1100 measurements cover addressing and values, not contention.
I see three ways to go and would rather not pick for you:
tb_lds: every 64-bit atomic runs its two dword updates inside a compare-exchange loop on that word (critical section inside the loop body, so it cannot deadlock a wave). That would also allowadd/sub/min/max/cmpston 64 bits. It serialises 64-bit atomics against each other only: a 32-bit atomic or a plain write on half of the same element is still not ordered against them.If 2 or 3 is acceptable, say which and I will send it as one small PR with exec tests on WARP. If you prefer 1, I drop it.
@omonkeyz on GitHub · 2026-11-23 06:22 UTC · original
Thanks for laying out the three options so clearly, and for raising it before writing code. The table of what two dword atomics can and can't do is exactly the analysis we needed. Here is the graphics lead's decision:
tb_lds, with both dword updates inside the compare-exchange loop as you describe. That also coversadd/sub/min/max/cmpston 64 bits.If you take on option 3, one small PR with the exec tests is perfect. It will go through the graphics lead's review and a Trackmania check before it's merged.
@QuentinDanblon on GitHub · 2026-11-23 06:27 UTC · original
Understood, option 3 with those conditions. Before any code I tried the lock itself, and it does not fit where I said it would. My mistake in the proposal, so I am reporting it rather than working around it.
tb_ldsisgroupshared uint tb_lds[8192], which is already the whole D3D limit. One more groupshared dword is rejected by DXC (1.9,cs_6_0), on a small shader with exactly the two declarations and the lock loop:It is the same wall as the existing refusal in
xlane_header("the 32-lane exchange needs groupshared space beyond the 32 KiB LDS mirror"). For compute the translator does not know how much LDS the shader really uses (only mesh hasgroupshared_bytes), so it cannot give up a dword of the mirror safely either.The same shader with
tb_lds[8191]compiles, so the lock loop itself is not the problem. What is open is where the lock word lives. I see two places, and both are your call because they go beyond the translator:gds_abi), taken withInterlockedCompareExchangeontb_gdsgds_abi, the buffer bound for any compute shader that has a 64-bit LDS atomic, and that dword guaranteed 0 when the buffer is createdd3d12_xs, whose hardware tests I cannot run now (the GPU of this machine is busy)tb_lds, as plannedtb_ldsat that size. Is it available anywhere in the container, the waygroupshared_bytesis for mesh?A third variant, shrinking the mirror to 8191 dwords when a 64-bit atomic is present, I do not propose: an access to the last dword would silently alias or go out of bounds, and the address is not known at translation time.
Until you choose, nothing changes: every 64-bit LDS atomic stays refused. If B is possible, tell me where the size is and I will do it in the translator only. If you prefer A, I can write the translator half and the exec tests on WARP, and leave the
gds_abilayout to you.@omonkeyz on GitHub · 2026-11-23 06:33 UTC · original
Thanks for trying the lock before writing anything else, and for reporting the 32 KiB wall rather than working around it. Not shrinking the mirror silently was the right call. The graphics lead chose B:
COMPUTE_PGM_RSRC2.LDS_SIZE, in granules. That should be in the container's register/state metadata, the same waygroupshared_bytesis for mesh. Please find where the translator can read it, and sizetb_ldsfrom it with the lock word next to it.Until then, every 64-bit LDS atomic stays refused.
@QuentinDanblon on GitHub · 2026-11-23 06:37 UTC · original
B it is. I have no console shader on my side, so I can find where the code should read the register but not check the offset against a real blob. Two things I need you to confirm on your fixtures before I rely on them:
docs/BLOCKERS.md(s_trap entry) saysCOMPUTE_PGM_RSRC2is "FSSM dword 0x130". I read that as the little-endian dword at byte offset 0x130 from the start of the inflated FSSM blob, the oneShaderExtract.fssmholds. Is that right, and is the offset fixed across your compute fixtures?gcn::extract_durango, wherefssmis the whole blob). I found nothing in the code or the notes that says where RSRC2 sits there. The thread-group size is read at 0x18..0x24 of the program prefix; is RSRC2 in that prefix too, and at which offset?What I plan to do with the value, so you can stop me early:
LDS_SIZE= bits [23:15] (COMPUTE_PGM_RSRC2__LDS_SIZE_MASK 0x00FF8000ingc_10_3_0_sh_mask.h), in units of 128 dwords (LLVM AMDGPULDSAlignShift = 9from Bonaire on, Mesaac_shader128 * 4 bytes for GFX7+ compute).tb_ldsis declared withLDS_SIZE * 128dwords andtb_lds64_locknext to it. Every other shader keeps the 8192-dword mirror, byte for byte as today.LDS_SIZEis 0, orLDS_SIZE * 512 + 4 > 32768.TRAP_PRESENT,EXCP_ENandEXCP_EN_MSBat 0 (your note says TRAP_PRESENT is 0 in every compute fixture; tell me if the other two are not always 0) and bit 31 at 0, otherwise the shader is refused too.LDS_SIZEcannot wrap at 32 KiB as it does today. I would refuse a static DS offset that reaches past the declared size (asmesh.rsdoes) and index the rest modulo the declared size. If you know what the hardware does with an out-of-range LDS address, I will follow that instead.I will start on the lock and the tests now, with the reader isolated in one function, and wire the offsets once you confirm them.
@omonkeyz on GitHub · 2026-11-23 06:48 UTC · original
Thanks for the detailed plan. We don't have a verified offset for RSRC2 in either container, so neither 0x130 in the inflated FSSM blob nor a Durango prefix offset can be assumed. To go ahead, please add a fixture test that reads RSRC2 from several compute fixtures, in both Scarlett (FSSM) and Durango containers. For each one, check that LDS_SIZE covers that shader's known ds_* address range or declared LDS use. If the offset isn't fixed, parse it from the container's own table rather than hard-coding it. Until a Durango container is proven this way, Durango shaders that use 64-bit LDS atomics stay refused. The rest of your plan (refusing when TRAP_PRESENT, EXCP_EN, EXCP_EN_MSB or bit 31 is set, and handling out-of-range LDS) is fine.
@QuentinDanblon on GitHub · 2026-11-23 07:59 UTC · original
The 64-bit LDS atomics are in omonkeyz/xbox-emulator#257, built the way you set out here.
hlsl::lds64::tests::compute_pgm_rsrc2_covers_the_lds_reach_of_every_compute_fixture. It reads RSRC2 from every compute fixture, FSSM and Durango, checks thatLDS_SIZEcovers the staticds_*reach, and prints the offsets that fit when 0x130 does not hold (fixed, and counted back from the ISA).cargo test -p tombox-xshader --release --lib compute_pgm_rsrc2 -- --nocapture.🤖 Generated with Claude Code
Pull request closed