Skip to content

Shaders: integer memory atomics and sub-dword buffer accesses - #164

Closed
r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:shader/memory-forms
Closed

r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:shader/memory-forms

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Measured on the title's shaders (by a tester)

EduAzevedo ran victory translate --jobs 8 --list over the retail shaders (v1.0.2.1, 26,148 programs) on main d2a4bae and on this head 3bbc9ba, compared program by program (result): 0 programs gained, 0 programs lost. No refusal reason changes.

So this PR does not change what translates in this title today. It adds coverage and is tested on synthetic programs only; it can wait behind PRs that unblock programs the title uses.

What

The memory instruction families were only partly lowered; a refused form fails its whole shader.

  • LDS (fn ds): inc, dec, rsub, mskor with and without _rtn, and ds_wrap_rtn_b32, as compare-exchange loops after the RDNA 2 ISA's definitions. The 64-bit integer forms (arithmetic, min/max, bitwise, compare-store, exchange, wrxchg2, wrxchg2st64). A shader that uses a 64-bit LDS atomic gets one i64 groupshared allocation that every access width shares, so dword accesses still alias it (they become compare-exchange updates of their half); such shaders declare SM 6.6's 64-bit atomics on groupshared memory.
  • Buffers (fn buffer): signed and unsigned byte and halfword loads, byte and short stores, the unformatted D16 forms; sub, inc, dec, csub (clamps at zero, returns the previous value) and the 13 integer _x2 atomics. Sub-dword stores mask the affected bytes with a compare-exchange per dword, so concurrent writes to neighbouring bytes are not lost.
  • Global and scalar loads: the six global D16 loads, global halves that span two dwords (was a documented shortcut), and s_load_dword{,x2,x4,x8,x16} through traced root SRVs and CBVs.

Still refused, each with its reason: floating-point LDS and buffer atomics (need the console's MODE denormal state), LDS src2 forms, addtid, ds_condxchg32_rtn_b64, global stores and atomics, general FLAT and scratch, scalar stores and atomics, partial or offset typed stores, indexed MTBUF loads.

Not included: byte and halfword LDS reads and writes, which #101 and #116 cover.

Not verified

No game files were used. Semantics are from AMD's RDNA 2 ISA reference (sections 12.13-12.14) and LLVM's DSInstructions.td / BUFInstructions.td. Whole-dword and atomic accesses are taken as naturally aligned; odd-address halves follow the unaligned-access mode, including a half that spans two dwords.

Checked on RDNA2 hardware by a reviewer (Lyrnc, a Radeon integrated GPU, gfx1036, probes assembled by the AMD driver): ds_inc_rtn_u32, ds_dec_rtn_u32, ds_rsub_rtn_u32, ds_mskor_rtn_b32 and ds_wrap_rtn_b32 match the definitions used here on 492 of 492 value triples each, and global_atomic_csub (the same operation as buffer_atomic_csub) on 364 of 364 pairs; every _rtn form returns the old value. Not probed there: the 64-bit forms and the sub-dword buffer accesses. It is desktop RDNA2 silicon, not the console's.

Tested

Windows 11, NVIDIA RTX 5070, synthetic only:

  • GPU tests (hand-assembled compute programs against CPU references, dxil.dll validation): atomic return and stored values, zero limits, wrapping, sign extension, preserved D16 halves, contended byte stores, out-of-bounds accesses, 64-bit atomics sharing the dword allocation. They all stop at unsupported instructions on main.
  • An opt-in probe (memory_opcode_refusals, ignored by default) lists the refusals across the memory opcode tables.
  • cargo test -p victory-shader: 78 passed, 1 ignored. clippy: nothing new. cargo xtask policy: ok.

Stays out of fn image (#48), buffer_format (#25) and wave/lane code.

AI-assisted; no game assets were used.

🤖 Generated with Claude Code

@DaviTostes

Copy link
Copy Markdown

Tested 3bbc9ba on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 56 passed, 0 failed, 1 ignored (2 test binaries), including the 10 tests this PR adds.
  • The ignored test is emit::memory_tests::memory_opcode_refusals (opcode coverage report).
  • The full workspace still needs Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

No game data used.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware check of the ISA definitions this PR implements for the new LDS atomics and csub: all match.

AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. In a HIP kernel (assembled by the AMD driver's amd_comgr), each lane works on its own LDS dword: store tmp, run the _rtn atomic with data (and data2), read the dword back. There are 492 triples: all combinations of 0, 1, 2, 5, 0x7fffffff, 0x80000000, 0xfffffffe, 0xffffffff for tmp and data, with data2 in 0, 3, 0xffffffff, plus 300 random. RDNA2 desktop silicon, not the console's. No game data.

Instruction ISA definition Match Returns the old value
ds_inc_rtn_u32 tmp >= data ? 0 : tmp + 1 492/492 yes
ds_dec_rtn_u32 tmp == 0 || tmp > data ? data : tmp - 1 492/492 yes
ds_rsub_rtn_u32 data - tmp 492/492 yes
ds_mskor_rtn_b32 (tmp & ~data) | data2 492/492 yes
ds_wrap_rtn_b32 tmp >= data ? tmp - data : tmp + data2 492/492 yes
global_atomic_csub (same op as buffer_atomic_csub) tmp >= data ? tmp - data : 0 364/364 yes

csub ran on 364 tmp/data pairs in device memory (in host-coherent memory the atomic does not take effect, so don't be surprised if a test on such memory shows nothing). The 64-bit forms and the sub-dword buffer accesses were not probed.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Lyrnc thank you. That is the check this PR could not do for itself: its GPU tests compare the translation against CPU references written from the same ISA text, so they cannot catch a misreading of the text. Six of the definitions now have 492 (364 for csub) agreeing triples on RDNA2 silicon behind them, including that the _rtn forms return the old value.

I have put the result in the description under "Not verified", with what it leaves open as you state it: the 64-bit forms, the sub-dword buffer accesses, and that it is desktop RDNA2, not the console's.

Your note on host-coherent memory is useful for anyone writing such a probe; nothing in this PR depends on it.

Nothing was run for this note. AI-assisted.

Lower LDS counters, reverse subtraction, masked OR, wrap and separate exchanges. Use one shared i64 allocation when a shader needs native 64-bit atomics so dword accesses still alias it. Translate buffer bytes and halves, including split halfwords and D16, and use native integer atomics or compare-exchange loops according to the RDNA 2 ISA. Support traced root SRV/CBV loads and global D16 reads.

An opt-in opcode probe records refusals. Synthetic compute tests compare both the returned and stored values with CPU references, check contention and out-of-bounds behavior, and validate and dispatch on the host D3D12 device. Floating atomics still require the console MODE state.
@DeerSilver

Copy link
Copy Markdown

Tested head 141fde5 merged onto main ffe310e on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.

Merged onto current main, the test build fails. 6638098 ("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument to resource::track:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
  --> crates/victory-shader/src/emit/memory_tests.rs:113
   |     resource::track(&p, &native.layout, native.stage)
   |     argument #4 of type `Option<&structure::Structured>` is missing

There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break cargo test on main. The call this PR adds needs the fourth argument. main's own tests pass Some(&st); None keeps the old three-argument behaviour.

With , None added locally to that one call, everything passes:

  • The PR's GPU tests ran on the RTX 5060 (DXGI adapter 0 on this laptop; no "no SM 6.6 device" skip) and passed (memory_opcode_refusals is #[ignore] as committed):
    • lds_integer_atomic_updates
    • lds_64_bit_atomics_share_the_dword_allocation
    • lds_paired_exchanges
    • buffer_bytes_and_halves
    • buffer_integer_atomic_updates
    • buffer_64_bit_atomics
    • buffer_concurrent_byte_stores
    • structured_buffer_memory_uses_index_and_member_offset
    • global_and_scalar_loads_through_root_views
  • cargo test --workspace --locked: 351 passed, 0 failed, 7 ignored, against 342 passed / 6 ignored on main.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@DeerSilver, fixed in e469b7c against main at ffe310e. The memory test helper already builds the structured control flow, so it now passes Some(&st) to resource::track, matching the other emitter tests. cargo test --workspace --locked passed locally (351 passed, 7 ignored), and cargo clippy --workspace --all-targets --locked -- -D warnings passed. Thanks for catching the break on the merged tree.

AI-assisted (Codex).

@r4tedx
r4tedx force-pushed the shader/memory-forms branch from e469b7c to 825dbd9 Compare November 24, 2026 13:34
dieharddillon1 pushed a commit to dieharddillon1/VIctory that referenced this pull request Nov 24, 2026
Adapt the subword reader from Marvisak#164 (141fde5) to guarded handles and runtime buffer strides. Exercise byte extraction, sign extension and a split halfword on the host.
jlefebure added a commit to jlefebure/VIctory that referenced this pull request Nov 24, 2026
…fer accesses

Brings byte and half loads/stores on buffers, flat and global memory,
sub-word cbuffer reads, and structured UAVs for 64-bit atomics.

Resolutions against the branch:
- scalar loads through root descriptors keep Marvisak#294's built-source path;
- root view bounds (Marvisak#650) gate the sub-dword and atomic paths too
  (atomics narrow their active lanes by the bound);
- the Marvisak#383 LDS byte path uses lds_ptr and refuses 64-bit LDS atomics;
- memory_tests fill root view sizes in user data, as Marvisak#650 requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/host.rs, crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/memory_tests.rs, crates/victory-shader/src/emit/mod.rs, crates/victory-shader/src/emit/shader.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing this. The change is not wrong; this title does not use the forms it adds, and bringing it up to main (319 commits on, conflicting in five files) would clear no program.

The numbers. Local title data (v1.0.2.1, the main shader archive), offline, CPU only; counts and reasons only.

  • One disassembly pass by main's own binary at 940d32f (shader-info --disasm) over the archive's 26,503 shader containers. All 26,084 programs in it decode, so the pass sees every instruction. None of the forms this PR adds occurs:
    • LDS: no inc, dec, rsub, mskor or wrap_rtn, and no 64-bit atomic (arithmetic, min/max, bitwise, compare-store, exchange);
    • buffers: no byte or halfword load, no byte or short store, no unformatted D16 form, no sub, inc, dec or csub, and no _x2 atomic.
    • global loads: no D16 form. The title's global loads are the dword forms and global_load_ushort (2 containers), and each occurs in programs that validate on main.
  • The neighbouring forms the title does use all occur in programs that validate on main: 16-bit LDS reads and writes (ds_read_u16 in 15 containers, ds_write_b16 in 10, and their d16_hi forms), plain 64-bit LDS writes (ds_write_b64 190, ds_write2_b64 195), the formatted D16 buffer loads and stores (38, 37 and 65 containers), and the 32-bit buffer atomics, signed min/max, swap and compare-swap included.
  • The offline count on main 940d32f (translate --validate --list, 25,889 programs counted): 23,918 validate; the translator refuses 298, under 10 reasons. None names an atomic or a sub-dword access.
  • That agrees with EduAzevedo's measurement on an older main: 0 gained, 0 lost.

Limits. The pass covers that one archive, not shaders anywhere else in the title. It did not look at the scalar s_load_* loads this PR also routes through traced root views; for those I only have the count, where no refusal names them. A refused program reports only its first reason, so for the forms the title does use, a refused operand form could still sit behind another reason in the programs main refuses at emit; by instruction name there is none.

The branch shader/memory-forms stays on the fork at 825dbd9; if a program that needs one of these forms turns up, in this title or another, I will reopen this on current main.

Thank you @Lyrnc for checking the LDS atomics and csub against RDNA2 hardware, @DeerSilver and @DaviTostes for the test runs, and @EduAzevedo for the count that first showed it gains nothing here.

AI-assisted

@r4tedx r4tedx closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants