Skip to content

rdna2spirv: MUBUF narrow stores (buffer_store_byte, buffer_store_short and their _d16_hi forms) - #6279

Open
LooperSalty wants to merge 1 commit into
ItsNotRexed:mainfrom
LooperSalty:rdna2spirv-mubuf-narrow-stores
Open

LooperSalty wants to merge 1 commit into
ItsNotRexed:mainfrom
LooperSalty:rdna2spirv-mubuf-narrow-stores

Conversation

@LooperSalty

Copy link
Copy Markdown

buffer_store_byte, buffer_store_short, buffer_store_byte_d16_hi and buffer_store_short_d16_hi raised Unsupported. This is the store side of the sub-dword MUBUF loads already on main (buffer_load_ubyte/sbyte/ushort/sshort), and the MUBUF counterpart of global_store_byte/short. Compiled HLSL uses them for sub-dword writes to byte-address buffers.

Claimed in #2300 (the coordination issue has since moved to #6089). Related older PR: #1864 (loads and stores together, written against an older main); this one only adds the four stores, on top of the current buffer_address and buffer_load_sub.

Semantics (RDNA2 ISA, MUBUF)

  • buffer_store_byte / buffer_store_short write the low 8 / 16 bits of vdata; the _d16_hi forms write the same amount from the high half.
  • Address and bounds are Gen.buffer_address(..., elem=1 | 2), the same call buffer_load_ubyte/ushort make, so the OOB_SELECT end check covers the whole access: a short whose second byte is past num_records writes nothing, not even its first byte. Swizzled V#s, offen, idxen, soffset and the instruction offset behave as for the loads.
  • Each byte is merged into its dword with OpAtomicAnd then OpAtomicOr, as the 8/16-bit format stores and global_store_byte do, so neighbouring bytes (possibly another lane's) are kept. A short at byte 3 of a dword puts its second byte in byte 0 of the next dword (step bytes on in a scratch-swizzled buffer, as buffer_load_ushort reads it).
  • Inactive and out-of-range lanes write nothing.
  • lds, tfe, d16 and a multi-register data operand raise Unsupported; other buffer_store_* names are unchanged.

Changes

  • tools/rdna2spirv.py: BUFFER_STORE_SUB and Gen.buffer_store_sub, dispatched next to buffer_load_sub.
  • tests/semantic/fixtures/buffer_store_sub.asm: hand-written program with the final words worked out in its header.
  • tests/semantic/test_buffer_store_sub.py: 14 tests (see below).
  • docs/design.md: new row after "Sub-dword MUBUF loads and GFX10_FORMAT_8_UINT".

What was tested (CPU only, no game files)

  • python -I -m unittest discover -s tests/semantic -p test_buffer_store_sub.py -v: 14 tests OK. They run the translator's SPIR-V in spirv_mini_interp:
    • the .asm fixture (final words 0x22442244, 0x22BBBB11, 0xCCCCCCCC)
    • every op at byte offsets 0..7 of a raw V#, neighbours kept (48 sub-cases)
    • the raw end check (short at the last byte writes nothing, byte at the last byte writes), num_records edge
    • OOB_SELECT 0, 1, 2 at the access end
    • instruction offset, soffset, an inactive lane, two lanes in one dword
    • a scratch-swizzled V# (SWIZZLE_ENABLE, ADD_TID, INDEX_STRIDE 8) with a dword-crossing short on two lanes
    • rejected forms
  • Without the rdna2spirv.py change the new file fails (48 errors); with it all pass.
  • python -m pytest -q tests/semantic on main 561b65a8 plus this change (Windows 11, Python 3.12): 6159 passed, 4 skipped, 1 xfailed, no failures (1 h 41 min). The 14 new tests are part of it.
  • python -m compileall -q make_game.py tools tests, python -I tests/test_semantic_translator_use.py and python -I tests/test_text_write_line_endings.py: OK.
  • spirv-as + spirv-val --target-env vulkan1.2 (Vulkan SDK 1.4.363.0) on the translated fixture, an idxen + run-time V# variant and a swizzled variant, native and --wave64-emulate: all valid.

Not tested

  • No GPU run: there is no runtime/test/shaders fixture or check.py entry for these stores yet, so this is CPU interpreter + spirv-val only. Not measured on AMD hardware. The bounds rule for a store is the load's (elem=1 | 2), taken from the same function, not separately measured.
  • No translation of a corpus slice (no work folder / game files here), so no before/after translated-shader counts. I make no claim about any specific shader or DLL (for example the BVH builder).

AI-assisted (Claude Code): the code, tests and this description were written with Claude Code and reviewed by the submitter.

@ethandepstein-del

Copy link
Copy Markdown

Independent CPU-only review & verification of c7b5c10c2 against 561b65a83 on macOS Apple Silicon (arm64, CPython 3.13.17, no GPU, no spirv-val, no game files):

Tests run on c7b5c10c2:

  • tests/semantic/test_buffer_store_sub.py: 14/14 passed (0.22s)
  • All tests/semantic/test_*buffer*.py: 220/220 passed (27.73s)
  • tests/test_semantic_translator_use.py: 3/3 passed
  • tests/test_text_write_line_endings.py: 3/3 passed
  • git diff --check and compileall clean.

Negative control (tests run against origin/main 561b65a83):

  • test_buffer_store_sub.py: FAILED (errors=48) (Unsupported: buffer_store_byte / buffer_store_short / _d16_hi), confirming all new assertions bite cleanly.

Extra checks beyond the PR's suite:

  • Verified buffer_store_byte + dword-crossing buffer_store_short_d16_hi using an slist V# ([s4,s5,s6,s7]) and glc/slc/dlc modifiers round-tripping through buffer_load_ubyte / buffer_load_ushort (PASS).
  • Verified WAVE64_EMULATE = True with 64 active lanes writing 64 contiguous shorts (buffer_store_short v1, v2, s[4:7], 0 offen) into shared dwords (PASS).
  • Checked tools/wave64_halves.py (NO_DEST, SINKS, add_tid_lane) and tools/rdna2spirv.py reads_all — all already match via the buffer_store prefix.

Notes / limitations:

  • spirv-val and GPU execution were not run in this environment (macOS CPU-only). Code matches buffer_load_sub and global_store_byte/short cleanly (buffer_address(..., elem=nbytes) + per-byte OpAtomicAnd/OpAtomicOr on aligned PSB dwords).

(AI-assisted)

@Halfect

Halfect commented Nov 23, 2026

Copy link
Copy Markdown

Independent Windows validation of exact head c7b5c10c2cd8df57de9825cc7b29f15ce14b2df9 (detached worktree, no source changes):

  • test_buffer_store_sub.py: 14/14 passed.
  • All tests/semantic/test_*buffer*.py: 220 passed, 1569 subtests passed.
  • tests/test_semantic_translator_use.py: 3/3 passed; tests/test_text_write_line_endings.py: 3/3 passed.
  • SPIRV-Tools v2026.4 (spirv-as + spirv-val --target-env vulkan1.2): the hand-written fixture, an idxen variant, and a scratch-swizzled variant all assembled and validated in both native and WAVE64_EMULATE=True modes: 6/6 valid.
  • scripts/check_no_game_data.py and git diff --check upstream/main...HEAD: clean.

This was CPU interpreter + SPIRV-Tools validation only: no GPU execution, no corpus translation, and no game files.

AI-assisted: Kimi Work.

@junaydirfan

Copy link
Copy Markdown

Tested c7b5c10c merged into main 9077af1f on Linux (CachyOS), with LEONIDA_W64_EMULATE=1: pass. This PR has no GPU fixture, so this only shows that nothing else changed.

  • run_gpu_tests.py gives the same result as main on an Intel Raptor Lake-S iGPU with Mesa ANV 26.3.1 (199/232 passed, 33 skipped, 0 failed) and an RTX 4080 Laptop with NVIDIA 620.73.04 (214/232, the one failure being main's ms_wave_info_s3, which rdna2spirv: %ms_counts is stored atomically by whichever wave sends; ms_wave_info_s3 draws nothing on main on most GPUs (SHADER-766) #5741 fixes).
  • CPU: the semantic files that mention the four stores give 47 passed, 193 subtests on the merge (33 and 151 on main), with no failures.
  • The stores themselves haven't run on a GPU yet. A small cs_ fixture would cover that: a byte, a short, both _d16_hi forms, and a short whose second byte is past num_records.

@LooperSalty

Copy link
Copy Markdown
Author

Measurement on the retail executable, as the author of this PR: on main ec0e01c5 (which has #1864), Box16Rehydration already translates without this PR (its listing has 24 buffer_store_short), and the 52 XboxBvhBuilder.dll programs regenerated with this PR merged are byte-identical (cmp on all 52 .spv). With #5680 they clear retail stop 18 either way (Windows 11, RTX 4070 SUPER, details on #5680). So this PR no longer changes anything on the boot path; what it could still add is semantic (bounds on out-of-range stores, the byte-merge behaviour), which I haven't measured against #1864. Maintainers: if #1864 already covers that, I'll close this one as superseded. AI-assisted (Claude Code).

@LukeOsland1

Copy link
Copy Markdown

Checked the resource-end rule at c7b5c10 on native AMD Radeon(TM) Graphics, HIP architecture gfx1036, Windows 11 build 26300, driver 32.0.21042.62. The synthetic raw buffer descriptor exposed 10 bytes (NUM_RECORDS=10, OOB_SELECT=3) inside a 32-byte allocation seeded with known values. For each dispatch I compared all eight dwords against this PR’s semantic lowering.

Both byte-store forms wrote the final valid byte at offset 9 and left the allocation unchanged at offset 10. Both short-store forms wrote bytes 8–9 at offset 8 and left both bytes untouched at offset 9, where the second byte falls outside the descriptor. An in-bounds short at offset 7 crossed the dword boundary successfully. Distinct upper/lower source values also checked the d16_hi selection. The probe included scalar-offset and instruction-offset cases and checked neighboring bytes.

Across wave32 and wave64, 28/28 complete readbacks matched, zero failures or skips. This measures native raw-buffer bounds and source selection on gfx1036. The translator comparison used its semantic execution path. No game files. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants