Skip to content

Shaders: 64-bit image atomics lowering to dx.op.atomicBinOp.i64 and a… - #1127

Closed
ashrafsdrop wants to merge 3 commits into
Marvisak:mainfrom
ashrafsdrop:shader/image-atomic-64
Closed

ashrafsdrop wants to merge 3 commits into
Marvisak:mainfrom
ashrafsdrop:shader/image-atomic-64

Conversation

@ashrafsdrop

Copy link
Copy Markdown

Summary

Implements 64-bit image atomic operations in victory-shader::emit::lower.

  • Detects 64-bit atomic operations from dmask (dmask == 3 for standard atomics, dmask == 0xF for cmpswap).
  • Assembles low and high 32-bit VGPR pairs into i64 integers.
  • Lowers image atomics into DXIL atomicBinOp.i64 (opcode 78) and atomicCompareExchange.i64 (opcode 79).
  • Annotates UAV handles with 64-bit typed resource properties (props1 = 0x107, 1 component of U64).
  • Unpacks 64-bit return values back into vector register pairs when glc is active.
  • Declares the INT64 shader model feature flag in DXIL SFI0 metadata.
  • Adds test image_atomic_64_bit_lowers_with_int64_feature to verify lowering and SFI0 metadata.

Verification

  • cargo test -p victory-shader: 121 tests passed, 0 failed.
  • cargo test --workspace: all workspace crates passing cleanly.
  • cargo run -p xtask -- policy: asset policy passes (154 files ok).

@shortselow-byte

Copy link
Copy Markdown

This PR has no CI run yet, so I ran the CI's checks at head 52a5381 (on main ffe310e): Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT. Workspace tests 343 passed, 0 failed; both clippy runs (default and --all-features) clean.

But the DXIL validator rejects the program from image_atomic_64_bit_lowers_with_int64_feature. With a temporary call to Validator::load(Validator::default_path()) (dxil.dll from SDK 10.0.26100.0) on l.dxil in that test:

error: Flags must match usage.
note: Flags declared=1074790400, actual=5503975424

Declared 0x40100000, required 0x148100000: the missing bits are 1 << 27 and 1 << 32, i.e. ATOMIC_INT64_ON_TYPED_RESOURCE and ATOMIC_INT64_ON_HEAP_RESOURCE. The test passes because it only checks the INT64 bit (0x8000). In the title these programs would still fail validation.

This is also the fourth open PR for 64-bit image atomics, after #305, #539 and #705. On #305, h0useadv measured that those three give the same +520 validated programs (a 64-bit image atomic 653 → 0), and #305 sets exactly the two flags above.

@adamdotpy

Copy link
Copy Markdown

This is the second open PR for 64-bit image atomics (the other is #539), so I reviewed it against that one directly. Windows 11 / Rust 1.98.1, 52a5381 on current main (ffe310e, merges as it stands), isolated target dir. Verified here: cargo test -p victory-shader --lib 121 passed / 1 ignored including the new image_atomic_64_bit_lowers_with_int64_feature; clippy --all-targets --all-features --locked -D warnings clean; cargo build --release -p victory builds.

1. The handle cache collides between the two widths — this is the one real defect. handle's cache key is (cur, load, first, sampler, props.0) (lower.rs:3311) — only props.0. This PR calls it with props.0 = kind | 0x1000 for both paths but props.1 = 0x105 for 32-bit and 0x107 for 64-bit. So a 32-bit and a 64-bit atomic on the same view in the same block produce the same key, and the second call is served the first call's handle, annotated for the other width. #539 does not have this: it keeps 0x105 for both widths and, for Source::Loaded, makes the handle without going through the cache at all. Either the two annotations must be the same value (then 0x107 is wrong) or the key must include props.1; as written, one of the two widths gets a handle the validator sees as the other. Whichever PR lands, this needs deciding first, because both call sites share handle.

2. The SFI0 feature declaration is thinner than #539's, and that is the difference most likely to show up as a validator rejection. This PR sets only INT64 (0x8000) and its test asserts just that. #539 also sets 0x40_0000 for a typed 64-bit atomic and 0x1000_0000 whenever the handle comes from the descriptor heap — which is the common case for UAV tables, i.e. exactly the Source::Loaded path here. If dxil.dll requires those flags for atomicBinOp.i64 / atomicCompareExchange.i64, this branch's containers are rejected where #539's pass. The decisive check neither PR has is validating an emitted 64-bit atomic (not just reading SFI0): Validator::load(...) on a lowered container with a dmask:0x3 atomic, the same way normalized_mtbuf_loads_need_no_int64 validates formats. Until one of the two PRs carries that, the flag set is unverified against the validator, only against each other.

3. What checked out line by line against the 32-bit path and against #539. The dmask mapping ((false,1)|(true,3) → 32-bit, (false,3)|(true,0xF) → 64-bit, anything else refused) is identical to #539's wide, and the new refusal message is the better one. cmpswap packs new = (vals[0], vals[1]), cmp = (vals[2], vals[3]) and passes [cmp, new], matching the 32-bit path's [vals[1], vals[0]]; sub negates the packed value the same way; the guard's restore value follows the 32-bit convention (old from the registers only when glc, else 0), where #539 restores the real pair — both fine, this one is more consistent with its sibling. The handle is made before begin_guarded, as on main, and per-lane waterfall handles come for free through handle() (per_lane is computed inside it), which is simpler than #539's duplicated Loaded arm. The README row dropping "64-bit image atomics" from the missing list is correct.

4. Shared gap with #539: no GPU fixture — the waterfall_tests style synthetic program is what would pin the emitted op and the per-lane handle — and no live-title measurement on this branch; #539 at least has one (zero a 64-bit image atomic is not supported refusals in a 19-minute world run).

Recommendation: these two should be reconciled into one, not merged separately — they touch the same image() branch and would conflict anyway. #539 is ahead on the SFI0 flags and on evidence; this one is ahead on simplicity (no separate image_atomic64 function, handle() used uniformly). The two things to settle before either merges are point 1 (annotation vs cache key) and point 2 (validate an emitted 64-bit atomic with dxil.dll and keep whichever flag set it demands).

@ashrafsdrop

Copy link
Copy Markdown
Author

Thanks for the sharp reviews @shortselow-byte and @adamdotpy. Addressed both points in f89a9d7:

  1. Handle cache key collision:

    • handles cache key now includes props.1 ((self.cur, *load, *first, sampler, props.0, props.1)). 32-bit (0x105) and 64-bit (0x107) operations on the same view in the same block produce distinct cache entries with their respective annotations.
    • Added image_atomic_32_and_64_handles_do_not_collide_in_cache verifying consecutive 32-bit and 64-bit atomics against the same view.
  2. Validator SFI0 / ShaderFlags requirement:

    • Added ATOMIC_INT64_ON_TYPED_RESOURCE (1 << 27) and ATOMIC_INT64_ON_HEAP_RESOURCE (1 << 32) to ShaderFlags and wired their corresponding SFI0 feature bits (0x40_0000 and 0x1000_0000).
    • image_atomic_64_bit_lowers_with_int64_feature now runs Validator::load(...) on the lowered container and asserts all required feature bits (0x8000, 0x40_0000, 0x1000_0000). Passes DXIL validation with zero errors.

All 122 tests in victory-shader and policy checks pass cleanly.

@adamdotpy

Copy link
Copy Markdown

Verified f89a9d7 here (Windows 11 / Rust 1.98.1, isolated target dir): cargo test -p victory-shader --lib 122 passed / 1 ignored, including both atomic tests; under --nocapture there is no "no dxil" line, and dxil.dll from SDK 10.0.26100 is at the validator's default path here, so the Validator::load calls in both tests really ran and the 1.8-style containers with 0x8000 | 0x40_0000 | 0x1000_0000 validate. Clippy --all-targets --all-features --locked -D warnings clean, release build of victory fine. Point by point on my two findings:

1. The cache collision — fixed at the right place, and better than my suggestion. Adding props.1 to the handles key fixes it for every caller, not just the atomics, and image_atomic_32_and_64_handles_do_not_collide_in_cache lowers a 32-bit then a 64-bit atomic on the same view in one block and validates the container. This is also compatible with #539, which keeps 0x105 for both widths — a more precise key changes nothing for it. Nothing else in the tree keyed on the old five-field tuple (the type change compiles clean), so the blast radius is exactly this map.

2. The SFI0 flags — now asserted and validated, not just asserted. ATOMIC_INT64_ON_TYPED_RESOURCE / ATOMIC_INT64_ON_HEAP_RESOURCE map to 0x40_0000 / 0x1000_0000, the same values #539 chose, and the test now validates the emitted container with dxil.dll and asserts all three bits. One deliberate difference from #539 worth knowing: you set the heap bit from self.s.flags & RESOURCE_HEAP != 0 (the shader uses a heap anywhere), where #539 sets it per-handle when the source is not a root view. Yours is the conservative direction — declaring a feature you do not use is accepted, missing one is not — so both are correct, yours just declares more in shaders that mix root and heap handles.

What is still open on both this and #539 is the same and is now cheap to close with the template from #1065: a GPU fixture (waterfall_tests style) that runs a 64-bit atomic, plus the live-title refusal count, which only #539 has so far. With the collision and the flags settled, the remaining difference between the two PRs is shape (your uniform handle() call vs #539's separate image_atomic64), and they still touch the same image() branch, so they should land as one.

@ashrafsdrop

Copy link
Copy Markdown
Author

Appreciate the verification and comparison against #539 @adamdotpy! Glad the uniform handle key and SFI0 flag mappings checked out on the live validator.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed 52a5381 against Windows SDK dxil.dll, dxc -T cs_6_6, and live 32-thread contention dispatches on NVIDIA GeForce RTX 5090, AMD Radeon(TM) Graphics iGPU, and Microsoft D3D12 WARP.

Found three issues that prevent dxil.dll validation or break divergent control flow / mixed queries, with fixes verified on top of 52a5381 in fuzeofficlal/VIctory@e2f58d6 (branch verify/shader-64bit-image-atomics-1127):

  1. Missing SFI0 64-bit typed/heap atomic feature flags (emit/shader.rs, emit/lower.rs):
    • dxil.dll rejects 52a5381 output with Flags must match usage: declared=0x40100000, actual=0x148100000.
    • 64-bit image_atomic_* must set ATOMIC_INT64_TYPED (SFI0 0x40_0000) in addition to INT64 (0x8000), plus ATOMIC_INT64_HEAP (0x1000_0000) when accessed through descriptor heap handles (!matches!(src, Source::Built { .. })).
  2. SSA dominance violation under divergent EXEC masks (emit/lower.rs):
    • Moving let at = self.begin_guarded(guard); ahead of self.vgprs(...), old, and sub negation terminates c (head) and switches self.cur to the guarded then block, while pack64 and Bin::Sub still emit into c (using values materialized in then) and end_guarded references old on the head -> join edge.
    • Materializing operands, 64-bit packing, sub negation, cmpswap compare operands, and self.handle(...) before begin_guarded restores valid SSA dominance for both 32-bit and 64-bit paths, and calling self.end_guarded(at, &[]) when !cache.glc avoids emitting a dead i64 join phi.
  3. annotateHandle element tag & per-block Lower::handles cache key collision (emit/lower.rs):
    • Over DXGI_FORMAT_R32G32_UINT / R32G32_SINT backing views, dxc emits ResourceProperties tag 0x105 (U32) rather than 0x107 (U64) for RWTexture*<uint64_t / int64_t> atomics (signedness is carried by IMin/IMax vs UMin/UMax).
    • Keying Lower::handles on full props: (u32, u32) instead of props.0 alone prevents an earlier image_get_resinfo (0x409 float4) in the same block from returning a mismatched handle to a subsequent image_atomic_* (0x105).

Verification (e2f58d6)

  • dxil.dll validation passed across all 11 64-bit atomic opcodes × glc (false/true), co-located image_get_resinfo, and divergent EXEC masks.
  • 32-thread contention readback test (add glc=false/true with 32-bit carry, sub under lane < 16 mask verifying inactive VGPR preservation, cmpswap dmask=0xF, swap conservation, smin, smax, umax, or, xor over DXGI_FORMAT_R32G32_UINT UAV):
    • NVIDIA GeForce RTX 5090: passed
    • AMD Radeon(TM) Graphics: passed
    • D3D12 WARP: passed

… atomics

Declare ATOMIC_INT64_TYPED (SFI0 0x40_0000) and ATOMIC_INT64_HEAP (SFI0 0x1000_0000) alongside INT64 so dxil.dll accepts 64-bit typed UAV atomics.

Tag 64-bit integer texture UAV handles as U32 (0x105) matching DXC over R32G32 views and key the per-block handle cache on the full (kind, tag) pair so preceding float4 queries (e.g. image_get_resinfo) do not collide.

Materialize atomic operands and handles before begin_guarded to preserve SSA dominance across divergent lane masks, and omit dead join phis when glc is unset. Add dxil.dll validation and multi-adapter D3D12 readback tests.
@ashrafsdrop

Copy link
Copy Markdown
Author

Thanks @fuzeofficlal for the thorough hardware testing across RTX 5090, AMD iGPU, and WARP, and for catching the SSA ordering and element tag details!

Integrated in commit a0b441d:

  1. Guarded SSA Dominance: Operands (vals), 64-bit packing, sub negation, cmpswap compare values, and handle() now all materialize before begin_guarded. Guarded blocks emit only the DXIL intrinsic call; end_guarded avoids dead join phis when !cache.glc.
  2. Resource Properties Tag: Tagged 64-bit integer texture UAV handles as 0x105 (U32) matching DXC conventions over R32G32 backing views.
  3. Multi-Adapter & WARP Contention Tests: Added image_atomic_64_bit_executes_across_threads_and_glc_modes covering all 11 atomic opcodes × glc modes across real adapters and WARP.

cargo test -p victory-shader (122 tests passed including DXIL validator and multi-adapter execution), cargo clippy -p victory-shader --all-targets --all-features -- -D warnings, and cargo run -p xtask -- policy pass with zero warnings.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at a0b441d: cargo test -p victory-shader passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@adamdotpy

Copy link
Copy Markdown

Verified the new head a0b441d here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). The rework since f89a9d7 renames the SFI0 flags (ATOMIC_INT64_TYPED/ATOMIC_INT64_HEAP, bits 0x40_0000/0x1000_0000 unchanged — still validator-tested), reworks the 64-bit image-atomic lowering in lower.rs/host.rs with guarded-SSA handling, and adds 377 lines of tests. Ran here: cargo test -p victory-shader --lib 122 passed / 0 failed / 1 ignored (21 s, GPU tests included), cargo clippy -p victory-shader --all-targets --all-features --locked -D warnings clean, cargo build --release -p victory builds. The handles key including props.1 that fixed the #305-style cache collision is still in place. Nothing blocking from my side.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: +520 programs over main, 0 lost.

Offline. I merged head a0b441d onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 22,244 with this PR.

Failure on main Gained
emit: a 64-bit image atomic 520

All 520 are the same programs that #1467 gains, and the same ones that #1402 gains through #305 (64-bit image atomics). So #305, #539, #1127 and #1467 cover one set. On this data, review can pick between them on correctness and coverage alone.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 122, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

64-bit image atomics. In main since today (lower.rs:7404-7510, test image_atomics_of_64_bits). Same as #539; also edits README.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
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.

7 participants