Skip to content

rdna2spirv: ds_wrap_rtn_b32 - #4009

Open
Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:claude/ds-wrap
Open

Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:claude/ds-wrap

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-DS-WRAP, #3796 (comment)

What and why

ds_wrap_rtn_b32 raised Unsupported, though it is a valid gfx1013 LDS form. I found it by translating every gfx1013 opcode form on main. Searching the open PR diffs, only #2323's decoder tables name it.

The RDNA ISA text says: tmp = M; M = (tmp >= DATA) ? tmp - DATA : tmp + DATA2; vD = tmp. The compare is unsigned and both results wrap at 32 bits.

Change (tools/rdna2spirv.py, Gen.lds_op):

  • The op is a Workgroup-scope OpAtomicCompareExchange loop on the LDS dword, like the other LDS CAS atomics.
  • Gen.lds_access now takes the OpPhi predecessor from the block the access ended in, not the block it started in. A looping access then stays valid SPIR-V when the LDS bound check (lds_bound) wraps it. For the existing straight-line accesses those two are the same block.

Measured natively on RDNA2 gfx1036 (Radeon iGPU of a Ryzen 9 9950X3D, driver 26.10.1, tools/hw_oracle raw kernel, v0-v15 / s4-s15). There are 16 single-lane cases, and all of them match the ISA text. They cover both branches, M = DATA, wraps below 0 and above 0xffffffff, DATA = 0xffffffff, and DATA2 = 0.

New contract row in docs/design.md.

Tested

Windows 11, LLVM llvm-mc (-mcpu=gfx1013), VK_LOADER_LAYERS_DISABLE=~implicit~, one GPU at a time, python -I scripts/run_gpu_tests.py --filter cs_w32_ds_wrap:

GPU Driver Result
AMD Radeon RDNA2 iGPU, Ryzen 9 9950X3D 26.10.1 PASS 10 155 4 104 4294967280 4294967248 2147483648 1
NVIDIA GeForce RTX 5080 616.92 PASS, same words
  • New fixture: runtime/test/shaders/cs_w32_ds_wrap.asm. In it, 32 lanes run ds_wrap_rtn_b32 on one word with DATA = 10 and DATA2 = 1. Every lane applies the same map, so the final value (10) and the sum of the returned values (155) do not depend on the order. Lane 0 then runs three of the measured single-lane cases. The words are derived in the header. The fixture is registered in check.py, run_gpu_tests.py and regen_fixtures.py.
  • New test: tests/semantic/test_lds_wrap.py checks the 16 measured cases and an offset through spirv_mini_interp, plus a plain-Python reference against the same words.
  • Before the change: on main the test errors with Unsupported: ds_wrap_rtn_b32.
  • Negative control: I used > instead of >= in the compare. The test fails 4 cases, and the Radeon run fails with 155 becoming 177. Reverted.
  • Passing: python -m unittest discover -s tests/semantic (no new failures: the 7 failures and 3 errors it reports fail the same way on main aa2988cc, in test_loop_ballot_barrier (emulate), test_vcmp16_opsel (spirv-val), test_cmp_class_f64 (TEST-853) and test_mutations (v_mul_legacy_f32 anchor)), python tests/semantic/regen_fixtures.py --check (clean), python -I tests/test_shader_checks.py, and pytest tests/test_docs_design_rows.py tests/test_run_gpu_tests.py.

AI-assisted.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

M = M >= DATA ? M - DATA : M + DATA2, the old value returned, as a
Workgroup CAS loop; 16 cases measured natively on gfx1036. lds_access takes
the phi predecessor from the block the access ended in, so a looping access
works under the LDS bound check. GPU fixture cs_w32_ds_wrap, semantic test
test_lds_wrap.py, design.md row.

AI-assisted.
@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Overlap check with #3817 and #3932 (SHADER-715), which change Gen.lds_access too: I merged this PR's head into #3932's head (23f6f465) locally.

So either order works. I'll do the docs text merge on #3932 if this lands first. AI-assisted.

@TheMrBeez

Copy link
Copy Markdown

Tested 8c5d717f: the new fixture natively on a second driver, translated on two GPUs, and under GPU-AV.

  • Native. cs_w32_ds_wrap.asm as a raw tools/hw_oracle kernel (its buffer stores swapped for global_store) on gfx1036, Ryzen 9 9950X3D iGPU, AMD driver 32.0.21030.2001 (the description's run was on 26.10.1).
    • 8/8 words match the fixture, 10 155 4 0x68 0xfffffff0 0xffffffd0 0x80000000 1. That includes the 32-lane contended case on hardware.
    • The same holds in all four MODEs I tried: the default IEEE 1, and IEEE 0 with f32 denormals flushed, f32 kept, or f16/f64 flushed. The op is integer, so this is only a sanity check.
  • Translated. On the RTX 5080 (driver 621.01) and the same gfx1036 through Vulkan: 8/8. spirv-val is clean, subgroup 32.

GPU-AV (Khronos validation 1.4.363, VK_KHRONOS_VALIDATION_GPUAV_ENABLE=1): 6 reports on both GPUs, none from the new CAS loop on the contended word.

  • 5 × SharedMemoryDataRace-RaceOnStore, all naming the setup store ds_write_b32 v1, v2 offset:4. It runs in all 32 lanes, so every invocation does a plain OpStore of 0 to the same word. The ds_write_b32 v1, v2 before it has the same pattern, though no report names it.
    • On hardware that's one instruction and the value is the same either way. Under the Vulkan memory model it's a race.
    • Fix I tried: run the two setup writes on lane 0 only (s_mov_b32 s20, exec_lo, v_cmpx_eq_u32_e32 0, v0, the two writes, s_mov_b32 exec_lo, s20).
    • Result: the five reports go away on both GPUs, and the eight words are unchanged. That would let the fixture run clean under GPU-AV, if you want it to.
  • 1 × SharedMemoryDataRace-RaceOnLoadStoreVsAtomic between the OpAtomicLoad that starts the offset:16 wrap loop and the ds_read_b32 … offset:16 after it.
    • Both run in lane 0 only, in program order, and the layer says "Likely against unknown invocation".
    • main's cs_lds_b64_cmpst.asm, also lane 0 only, gives the same single report on the 5080. So it doesn't come from this PR; I haven't run it down further.

Windows 11, Vulkan SDK 1.4.363, llvm-mc AMD LLVM 23.0.0git. Synthetic data only, no game files. AI-assisted (Claude Code).

@SebSilent

Copy link
Copy Markdown

Tested 8c5d717 on RTX 2070 (Turing, subgroup 32..32), NVIDIA 620.73.04, Vulkan 1.4.357, CachyOS Linux (kernel 7.3.5), llvm-mc 23.1.5. Native Linux build of the test tools (main aa2988c, unchanged by this PR), no game files.

  • New fixture: python -I scripts/run_gpu_tests.py --build <build> --filter cs_w32_ds_wrap PASS, words 10 155 4 104 4294967280 4294967248 2147483648 1, same as your Radeon and RTX 5080 runs.
  • Full suite: 215/215 translated + spirv-val, 165/211 passed, 46 skipped, 0 failed (exit 0). That is main's 164/210 plus the new case, so no regression. The 46 skips are 32 needing a 64-wide subgroup and 14 Windows-only d3d12 tests.
  • python -I tests/semantic/test_lds_wrap.py: 3 OK.

Adds a Linux Turing (32-wide) data point to your Windows AMD + RTX 5080 results. AI-assisted (Tacit).

@tedin7

tedin7 commented Nov 22, 2026

Copy link
Copy Markdown

Tested 8c5d717 (merge base = main aa2988c) on NVIDIA GeForce RTX 5080, driver 580.65.06, Ubuntu 22.04 (Linux): pass

Commands (llvm-mc 21, spirv-val from Vulkan SDK 1.4.363.1, VK_LOADER_LAYERS_DISABLE=~implicit~):

  • python -I scripts/run_gpu_tests.py --filter cs_w32_ds_wrap: PASS 10 155 4 104 4294967280 4294967248 2147483648 1, the same words as in the PR description.
  • python -m unittest tests.semantic.test_lds_wrap: 3 tests OK.
  • Negative control: with tools/rdna2spirv.py taken from main, the same test gives 17 errors (Unsupported: ds_wrap_rtn_b32). With the PR file it passes.
  • Full suite python -I scripts/run_gpu_tests.py: 165/211 passed, 46 skipped, 0 failed (main baseline 164/210; the one extra is the new test).
  • Same with LEONIDA_W64_EMULATE=1: 196/211 passed, 15 skipped, 0 failed (baseline 195/210).
  • python -m unittest discover -s tests/semantic: 7459 tests, failures=7, errors=2 (the PR text says 3 errors on main; I did not diff the names against main, so I only say there is no sign of a new failure).
  • python tests/semantic/regen_fixtures.py --check: clean. python -I tests/test_shader_checks.py: 75 tests OK. pytest tests/test_docs_design_rows.py tests/test_run_gpu_tests.py: 42 passed.

Not tested: the AMD RDNA2 run and the Windows run (no such hardware here). The C++ runtime binaries used were a prebuilt Linux build of another branch; this PR only touches Python, so the GPU test programs are not affected. The compare-operator negative control from the PR text was not repeated.

No game data was used.

AI-assisted run.

@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/4009/head in an isolated worktree.

  • pytest -q tests/test_translate.py: 583 passed, 4 skipped
  • pytest -q tests/semantic/test_lds_wrap.py: 3 passed, 16 subtests

The change is complete and consistent: translator opcode, semantic test, semantic fixture, GPU fixture, checker registration, GPU runner registration and the contract-table row all move together. The semantic test drives the real translator (not a private model) and covers wrap-around, return-value and inactive-lane cases through the fixture.

I could not exercise the GPU fixture on this host, so the hardware-visible part of ds_wrap_rtn_b32 is unverified by me; the author's RDNA2 measurements are cited in the PR. The CPU side is solid.

No game files or keys involved.

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.

6 participants