Skip to content

rdna2spirv: sub-dword global stores take VDATA first, with a GPU fixture - #4160

Open
YaBulochka wants to merge 1 commit into
ItsNotRexed:mainfrom
YaBulochka:gpu-global-sub-dword
Open

YaBulochka wants to merge 1 commit into
ItsNotRexed:mainfrom
YaBulochka:gpu-global-sub-dword

Conversation

@YaBulochka

Copy link
Copy Markdown
Contributor

A correctness fix found by taking the four sub-dword global stores to real hardware.

The bug. LLVM prints the sub-dword stores as global_store_byte/short[_d16_hi] vdata, vaddr, saddr: VDATA first, unlike global_store_dword, whose syntax is vaddr, vdata, saddr. The translator applied the global_store_dword order to all of them, so it used the address as the data and the data as the address. On an RX 7600 the ISA-correct store of 0x5A at 0x84 (v1 = 0x5A, v2 = 0x84, global_store_byte v1, v2) landed as byte 0x84 at 0x5A; after the one-line fix the same shader writes 0x5A at 0x84. The four affected ops are global_store_byte, global_store_byte_d16_hi, global_store_short and global_store_short_d16_hi. The six sub-dword loads already used the right order and are unchanged.

Why nothing caught it. tests/semantic/test_global_sub_dword.py wrote its store bodies with the address in v1 and the data in v2 and had its oracle expect exactly that, so the test encoded the same swap as the translator. That is the failure mode the maintainers flagged for semantic tests: a model written in the test that the translator can satisfy while being wrong about the ISA. This PR corrects the test bodies to the ISA order (v1 = data, v2 = address, including the VGPR-pair case and the rejected-forms list) and adds a hardware check.

The new fixture. runtime/test/shaders/cs_global_sub_dword.asm seeds two dwords through the output V#, then exercises all four stores (byte, byte_d16_hi, short_d16_hi, short, including one across a dword boundary at byte 3), all six loads (the plain ubyte/sbyte and the d16 forms with their kept halves) and reads the two dwords back. It failed before the fix and passes after it on both AMD devices here:

AMD RX 7600 (gfx1102), RADV Mesa 27.0.0-devel:
  PASS  cs_global_sub_dword  4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294
Ryzen 5 9600X iGPU (gfx1036, RDNA2), same driver:
  PASS  cs_global_sub_dword  4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294

The row in docs/design.md now says GPU-checked and records the old behaviour. Impact on the corpus: none. Of 10,548 global_load instructions in the 26,503-shader corpus there are 0 global_store instructions, so no translated game shader was affected; this is a correctness fix for the supported instruction set, not a released regression.

pytest tests/semantic/test_global_sub_dword.py tests/test_run_gpu_tests.py tests/test_docs_design_rows.py tests/test_design_contract_rows.py tests/test_check_no_game_data.py: 63 passed.

Note: the two registration lines in check.py and run_gpu_tests.py are the same ones #4045 appends to, so the two PRs will textually conflict there; whichever lands second is a one-line rebase.

@MKmajster

Copy link
Copy Markdown

I think the premise is reversed. The encoding says the sub-dword stores have the same operand order as global_store_dword: VADDR first, then VDATA. If so, main is right, and this change would introduce the swap, with the new fixture locking it in.

In a FLAT/GLOBAL instruction the second dword is ADDR[7:0] | DATA[15:8] | SADDR[22:16] | ... | VDST[31:24]. llvm-mc 23.1.6 puts the first operand in ADDR for every store width:

-mcpu=gfx1013 (gfx1030 and gfx1036 encode the same bytes)
global_store_dword       v1, v2, s[4:5]  ; [0x00,0x80,0x70,0xdc, 0x01,0x02,0x04,0x00]
global_store_byte        v1, v2, s[4:5]  ; [0x00,0x80,0x60,0xdc, 0x01,0x02,0x04,0x00]
global_store_short       v1, v2, s[4:5]  ; [0x00,0x80,0x68,0xdc, 0x01,0x02,0x04,0x00]
global_store_byte_d16_hi v1, v2, s[4:5]  ; [0x00,0x80,0x64,0xdc, 0x01,0x02,0x04,0x00]
global_store_dword v[6:7], v2, off       ; [..., 0x06,0x02,0x7d,0x00]
global_store_byte  v[6:7], v2, off       ; [..., 0x06,0x02,0x7d,0x00]
global_load_ubyte  v3, v1, s[4:5]        ; [..., 0x01,0x00,0x04,0x03]   (ADDR = v1, VDST = v3)

-mcpu=gfx1102 (RX 7600 class)
global_store_b32 v1, v2, s[4:5]          ; [0x00,0x00,0x6a,0xdc, 0x01,0x02,0x04,0x00]
global_store_b8  v1, v2, s[4:5]          ; [0x00,0x00,0x62,0xdc, 0x01,0x02,0x04,0x00]
global_store_b16 v1, v2, s[4:5]          ; [0x00,0x00,0x66,0xdc, 0x01,0x02,0x04,0x00]

Disassembling 0x00,0x80,0x60,0xdc,0x01,0x02,0x04,0x00 (ADDR = v1, DATA = v2) also gives back global_store_byte v1, v2, s[4:5].

So for global_store_byte v1, v2 with v1 = 0x5A, v2 = 0x84, the hardware stores byte 0x84 (DATA = v2) at offset 0x5A (ADDR = v1). That is what the description reports the RX 7600 doing before the change. The test body in test_global_sub_dword.py on main (address in v1, data in v2) matches the ISA as LLVM encodes it.

A native hw_oracle kernel built from the raw bytes above (not from re-assembled text) would settle it without relying on any syntax. I don't have an AMD GPU here to run it.

AI-assisted (Claude).

@AC-Storm-YT

Copy link
Copy Markdown
Contributor

Native run from the raw bytes, as @MKmajster suggested: the sub-dword stores take the address from ADDR (the first operand), the same as global_store_dword. So main is right and this change would introduce the swap.

Ryzen 7 9800X3D iGPU (RDNA2, gfx1036), AMD Windows driver 32.0.21045.29003, Windows 11, tools/hw_oracle (HIP launcher) at main aa2988cc, AMD clang from rocm-sdk-core 10.0.0.

Each kernel sets v1 = 0x08 and v2 = 0x34 (both are valid byte offsets into the 64-byte output buffer) and seeds the dwords at bytes 0x08 and 0x34 with 0xFFFFFFFF. It then runs one store, written as two .long words rather than a mnemonic, with SADDR = the output pointer and offset 0. Each opcode runs twice: with ADDR = v1, DATA = v2, and with the fields swapped.

Op Bytes (ADDR = v1, DATA = v2) ADDR = v1 → dword at 0x08 ADDR = v2 → dword at 0x34
global_store_byte 00 80 60 dc 01 02 02 00 0xffffff34 0xffffff08
global_store_byte_d16_hi 00 80 64 dc 01 02 02 00 0xffffff00 0xffffff00
global_store_short 00 80 68 dc 01 02 02 00 0xffff0034 0xffff0008
global_store_short_d16_hi 00 80 6c dc 01 02 02 00 0xffff0000 0xffff0000
global_store_dword (control) 00 80 70 dc 01 02 02 00 0x00000034 0x00000008

In every run, only the dword at the ADDR register's offset changed. The stored value came from DATA (the high halves are 0, so the _d16_hi forms store 0). The other dwords kept the launcher's 0xDEAD0000 fill.

The same assembler encodes global_store_byte v1, v2, s[2:3] for gfx1036 as [0x00,0x80,0x60,0xdc,0x01,0x02,0x02,0x00] (ADDR = v1), and likewise for the short and _d16_hi forms. So in the text syntax the first operand of the sub-dword stores is the address, as it is for global_store_dword, and the test bodies on main (address in v1, data in v2) match the hardware.

The probe script is local; it uses hand-built kernels only, no game files.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 data point. Tested head 7e3905b on Apple M2 (Mac14,3), macOS 26.4, MoltenVK 1.4.2 (direct), subgroup 4..32, llvm-mc 23.1.6, Python 3.13.18, with a clean full build on AppleClang 17 (exit 0).

python -I scripts/run_gpu_tests.py --filter cs_global_sub_dword --build runtime/build/linux: 1 shader translated, 1 pass spirv-val; 1/1 passed — words 4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294, identical to the values in the PR body from AMD. Adds an Apple/MoltenVK result to the cross-vendor evidence. Head fetched unchanged into a separate worktree; no rebase/edit/push. Synthetic shader and counts only; no game files.

AI-assisted.

@controldebils

Copy link
Copy Markdown
Contributor

A second native RDNA2 data point, on a discrete gfx1030, agreeing with @MKmajster and @AC-Storm-YT: the first operand of the sub-dword stores is the address, as for global_store_dword. So main is right and this change would swap them.

AMD Radeon RX 6900 XT (gfx1030), AMD driver 26.11.2, Windows 11, tools/hw_oracle raw kernels, AMD clang from rocm-sdk-core 10.0.0. This time from the text syntax the translator reads (assembled by the same toolchain), not raw bytes: v1 = 0x08, v2 = 0x34, dwords at 0x08 and 0x34 seeded with 0xffffffff, s[2:3] = the output pointer, offset 0. Each op in both operand orders:

Instruction dword 0x08 dword 0x34
global_store_byte v1, v2, s[2:3] 0xffffff34 unchanged
global_store_byte v2, v1, s[2:3] unchanged 0xffffff08
global_store_byte_d16_hi v1, v2, s[2:3] 0xffffff00 unchanged
global_store_byte_d16_hi v2, v1, s[2:3] unchanged 0xffffff00
global_store_short v1, v2, s[2:3] 0xffff0034 unchanged
global_store_short v2, v1, s[2:3] unchanged 0xffff0008
global_store_short_d16_hi v1, v2, s[2:3] 0xffff0000 unchanged
global_store_short_d16_hi v2, v1, s[2:3] unchanged 0xffff0000
global_store_dword v1, v2, s[2:3] (control) 0x00000034 unchanged
global_store_dword v2, v1, s[2:3] (control) unchanged 0x00000008

The store always lands at the first operand's offset with the second operand's value. llvm-mc (gfx1030) encodes global_store_byte/short/dword v1, v2, s[2:3] all with ADDR = v1, DATA = v2 (... 0x01,0x02,0x02,0x00).

cs_global_sub_dword does PASS on this GPU with this branch's translator (4264864580 3203364810 ...), but that run can't tell the two orders apart: the fixture and the translator make the same assumption, so they agree with each other on any GPU. Only a native run of the instruction itself can, and that says first operand = address.

AI-assisted (Claude Code).

@dcastell1

Copy link
Copy Markdown
Contributor

Tested 7e3905b on RTX 5090, 621.24, Windows 11: pass. Programs rebuilt from this tree, LEONIDA_W64_EMULATE=1: 196/211 passed, 15 skipped, 0 failed, and the new case is exact:

PASS  cs_global_sub_dword   4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294

Taking VDATA from the instruction's field for sub-dword global stores is the rule that matters for the narrow formats; the eight words above are the header's derivation, on hardware.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7e3905b on AMD Radeon (gfx1036, RDNA2, driver 26.10.1, Windows 11), natively through tools/hw_oracle raw kernels: the operand order this PR switches to is not the RDNA2 order. On gfx1036 global_store_byte v1, v2, s[2:3] takes v1 as the address (VADDR) and v2 as the data, the same order as global_store_dword, so the current translator (data, vaddr = a[1], a[0]) is right and this change would turn correct code around.

What I ran (one lane, s[2:3] = the output buffer, the target dword zeroed first, then the store, a fence, a global_load_dword back):

  • v1 = 0x20, v2 = 0x5a, global_store_byte v1, v2, s[2:3]: the dword at +0x20 reads 0x0000005a (data 0x5a stored at address 0x20), the dword at +0x58 stays 0.
  • v1 = 0x5a, v2 = 0x20, same store: the dword at +0x20 stays 0, the dword at +0x58 reads 0x00200000 (data 0x20 stored at address 0x5a).

The encoding says the same: llvm-mc -mcpu=gfx1013 -show-encoding gives global_store_byte v1, v2, s[2:3] -> 00 80 60 dc 01 02 02 00 and global_store_dword v1, v2, s[2:3] -> 00 80 70 dc 01 02 02 00, i.e. VADDR = byte 4 = v1, DATA = byte 5 = v2 for both (FLAT/GLOBAL format: VADDR [39:32], DATA [47:40]); the assembler's text order is VADDR then DATA for every global store, byte, short and the _d16_hi forms included. The loads are vdst, vaddr, saddr as before. The PR text says LLVM prints the sub-dword stores VDATA first; for the gfx10 syntax I could not reproduce that (all four spellings assemble to the same field layout as global_store_dword), and the RX 7600 in the description is an RDNA3 part (gfx1102), whose assembler and encoding differ from the RDNA2 target of this translator, so that run does not transfer to RDNA2.

Consequence: with this PR global_store_byte v1, v2, ... as the compiler emits it (address in the first register) would be translated with the roles swapped, and cs_global_sub_dword.asm and test_global_sub_dword.py would encode the swapped order. I would hold this PR until it is re-measured on an RDNA2 GPU with the instruction written exactly as RDNA2 code uses it. The kernel above is 12 lines with tools/hw_oracle (asm.Kernel(..., raw=True)); I can post it.

AI-assisted.

@chpeu

chpeu commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7e3905b6 (main aa2988cc is an ancestor, no rebase needed) on Linux, no game files.

GPU tests (RTX 3080, driver 615.71.09, Vulkan 1.4.351) — your new fixture passes:

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → 196/211 passed, 15 skipped, 0 failed; main aa2988cc on this machine gives 195/210, 15 skipped,
    0 failed
    , so the one extra program is cs_global_sub_dword and it passes.
  • without the variable: 165/211 passed, 46 skipped on your branch vs 164/210 on main.

The build.py / check.py path you also touched (runtime/test/shaders/build.py <out> then
check.py <out> runtime/build/linux), with main measured the same way for comparison:

  • your branch: build.py 235 shaders translated, 235 pass spirv-val; check.py 150 of 185 pass,
    35 skipped
    , and with LEONIDA_W64_EMULATE=1 150 of 185, 33 skipped, 0 failed lines;
  • main aa2988cc: build.py 234/234; check.py 149 of 184 (35 skipped, 33 with the emulation),
    0 failed. Same +1 fixture, no regression.

Other checks: tests/semantic/test_global_sub_dword.py 12 tests OK (python -m unittest discover -s tests/semantic -p test_global_sub_dword.py). Translator side, the 28 pytest-marked files as CI runs them:
1594 passed, 1 failed on your branch vs 1592 passed, 1 failed on main — the single failure is the known
test_u16_high_half::test_d16_buffer_store_no_redundant_extract_after_shr16 (a spirv-val "previous definition"
problem coming from aa2988cc, which #3902 fixes), nothing to do with your change. python -m compileall -q make_game.py tools tests: clean.

The RTX 5080 was not run in this pass: 186 MiB free on that card (it is held by another job on this box), and
I would rather say so than report an allocation failure against your fixture.

Machine: Linux Mint 22 (Ubuntu 24.04 base), llvm-mc 20.1.2, SPIRV-Tools 2025.1~rc1 (Ubuntu packages), GCC 13.3,
Python 3.12.3 with pytest 9.2.0 and numpy 2.5.5, built with cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON. AI-assisted testing, counts read through by hand.

@theswiss24

Copy link
Copy Markdown

Tested 7e3905b6 (contains main aa2988cc) on NVIDIA GeForce RTX 4070 (Ada), driver 621.24, Windows 11 (26300), Vulkan SDK 1.4.363.1, llvm-mc 21.1.8, with test programs built from main aa2988cc (this PR changes no C++): pass.

LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build <main build>: 196/211 passed, 15 skipped, 0 failed. On main it was 195/210 with the same 15 skips, so the new fixture is the only difference:

PASS  cs_global_sub_dword   4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294

The words are identical to the RX 7600 and gfx1036 words in the description. No game files.

AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified on Linux (NVIDIA RTX 5080, driver 580.196.07, fragment/compute subgroups 32..32, Vulkan 1.4): the new cs_global_sub_dword GPU fixture passes on the PR with all eight expected words, and cs_global_neg_offset/cs_store are unchanged. With only tools/rdna2spirv.py rolled back to main, the same fixture fails deterministically — VK_ERROR_DEVICE_LOST (vkQueueWaitIdle -> -4) on one run and a 120 s hang on a second independent rerun — consistent with the store going through the VDATA value as an address, which is the bug the PR fixes; the PR's translator passes again, and the CPU semantic suite (12 tests) is green. AI-assisted: yes. Tested on Linux (RTX 5080 host, GPU run).

@abilliontokens

Copy link
Copy Markdown
Contributor

Tested 7e3905b on NVIDIA GeForce RTX 5060 Ti (driver 621.24, Windows 11) and AMD Radeon RX 9060 XT / RDNA4 (AMDVLK 32.0.32029.2010, Windows 11): the PR's own fixture passes on both, for what that can and cannot show.

  • python -I scripts/run_gpu_tests.py --filter cs_global_sub_dword (loader pinned to one ICD at a time): PASS 1/1 on both GPUs, words 4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294 — identical to the Apple M2/MoltenVK run and the PR body.
  • python -I tests/semantic/test_global_sub_dword.py: 12/12 OK; build.py: 235 shaders, 235 translated, 235 pass spirv-val.
  • python -m unittest discover -s tests/semantic: 7456 tests, 7 failures + 2 errors — the same 9 as main (aa2988c) on this box (test_cmp_class_f64 x2, test_mutations x2, test_loop_ballot_barrier x5; re-ran those three modules on this head to confirm the identical set), none touching the stores this PR changes.
  • Scope of that pass: the fixture's expectations were rewritten with this PR, so a Vulkan run only confirms the translator's own reading end-to-end; it cannot arbitrate the operand-order premise. The native runs above (gfx1036, gfx1030 raw-byte and text-syntax kernels) say the first operand is the address — i.e. main's current reading — and my SPIR-V results are consistent with that dispute being unsettled at the Vulkan level rather than contradicting it.

No game files. AI-assisted (omp orchestrator).

@controldebils

Copy link
Copy Markdown
Contributor

@memorypressure, the device lost with main's translator is expected, and it doesn't show which order is right. This PR also rewrote the fixture into its own order (data in v1, address in v2). main reads the first operand as the address, as gfx1030 and gfx1036 do natively. So with main and the new fixture, global_store_byte_d16_hi v1, v2, s[20:21] offset:2 with v1 = 0xcd0000 addresses 0x40 + 0xcd0000 + 2, far outside the 0x48-byte buffer. That out-of-bounds store is the device lost, and it would happen to any fixture written in the opposite order.

Only the instruction itself can settle the order. Three native runs on two chips (gfx1036 raw bytes and text, gfx1030 text, posted above) show the store lands at the first operand's address, which is what main does.

AI-assisted (Claude Code).

@slopradish slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPU verification: checked out pull/4160/head in an isolated worktree and ran pytest -q tests/semantic/test_global_sub_dword.py: 12 passed, 87 subtests passed.

The operand-order fix is well covered: the test sweeps all sub-dword global store forms with VDATA in the first operand position, and 87 subtests exercise byte/halfword widths and offsets. The change is paired with a GPU fixture, checker word list, runner registration and a contract row, so the semantic model and hardware fixture stay in sync. The fallback for listings that predate this operand order is preserved by the existing decoder path.

I could not run the GPU fixture here; hardware behaviour is the author's measurement. No game files or keys involved.

@radv-tinker

Copy link
Copy Markdown
Contributor

Tested 7e3905b6 on AMD Radeon RX 9070 XT (RDNA4), RADV GFX1201, Linux (Fedora 43), subgroup 32..64: pass. The earlier checks don't include RADV.

python3 -I scripts/run_gpu_tests.py --filter <test> with the Mesa shader cache off, for the 1 test(s) this PR adds. Each passed in all three configurations: Mesa 25.3.6 (Fedora package) with ACO, the same with LLVM (RADV_DEBUG=llvm), and Mesa 26.3.1 (built from source) with ACO. The three give the same words:

  • cs_global_sub_dword: 4264864580 3203364810 3735879851 4294885103 3735944756 3203364744 171 4294967294

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.