Skip to content

Recompiler: implement 64-bit MIMG image atomics (DMASK 0x3 / 0xF) with cap gating - #1541

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:recomp/image-atomic-64bit
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:recomp/image-atomic-64bit

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1485.

Em::mimg in crates/xcl/src/gpu/recomp/emit.rs ignored DMASK on IMAGE_ATOMIC_* instructions and always emitted 32-bit RWTexture<uint>/RWTexture<int> atomics on VDATA[0]. On RDNA2 (per LLVM SIISelLowering), DMASK == 0x3 (or 0xF for cmpswap/fcmpswap) selects 64-bit image atomics operating on VDATA[0:1] (and VDATA[2:3] as compare value for cmpswap), so 64-bit carries, upper-dword comparisons, and GLC 64-bit return values were silently truncated to 32 bits.

This adds 64-bit MIMG image atomic translation using SM 6.6 RWTexture<uint64_t> / RWTexture<int64_t> typed 64-bit atomics (InterlockedAdd, InterlockedAnd, InterlockedOr, InterlockedXor, InterlockedExchange, InterlockedMin, InterlockedMax, InterlockedCompareExchange, plus bounded CAS loops matching rmw64 for inc/dec), gated on D3D12_FEATURE_DATA_D3D12_OPTIONS9::AtomicInt64OnTypedResourceSupported && D3D12_FEATURE_DATA_D3D12_OPTIONS11::AtomicInt64OnDescriptorHeapResourceSupported. Adapters without those capabilities cleanly refuse 64-bit image atomics instead of failing at pipeline creation or silently truncating to 32 bits, and invalid DMASK values are also refused.

Since the review (@KainanP): a device without the capability no longer bypasses the shader caches. The capability is part of their keys instead — a field of the in-memory ShaderKey, and an identity of its own for the translations on disk (translated::identity_for_caps). A capable device's identity is unchanged, so its existing entries stay valid. The device is asked once, not for every translation. The mesh-shader path's own cache key, added on main since the first version, carries the same field.

#1533 and #1551 fix the same issue (#1607, group D9). The difference that is left: this PR asks the device first and refuses a program that uses 64-bit image atomics where the capabilities are missing; the other two always emit the 64-bit form. Merging any one of the three makes the others conflict in the same mimg arms.

What is in it

  • crates/xcl/src/gpu/recomp/mod.rs: add int64_typed_atomic: bool to Options (default true for standalone translation; adapter-probed in runtime and GPU test runner).
  • crates/xcl/src/gpu/recomp/emit.rs: distinguish 32-bit vs 64-bit IMAGE_ATOMIC_* by DMASK (0x1/0x3 vs 0x3/0xF), refuse invalid DMASK or missing int64_typed_atomic capability, and emit SM 6.6 64-bit typed UAV atomics (RWTexture<int64_t> direct InterlockedMin/InterlockedMax for smin/smax, RWTexture<uint64_t> for the rest) with GLC 64-bit writeback.
  • crates/xcl/src/gpu/recomp/pso.rs: probe OPTIONS9/OPTIONS11 on the runtime device (once) and pass int64_typed_atomic into the recompiler's Options; the capability in ShaderKey and in the disk identity (identity_for_caps), for the compute and the mesh path; unit test a_device_without_int64_typed_atomics_has_its_own_disk_entries, and the changed-key case in shader_cache_checks_context_and_blob_bytes.
  • crates/xcl/src/gpu/recomp/runner.rs: probe the test adapter and expose int64_typed_atomic_supported().
  • crates/xcl/src/gpu/recomp/tests.rs: add gpu_image_atomics_64_bit testing capability refusal, invalid DMASK refusal, and GPU execution of 64-bit add (with GLC), umax, smin, cmpswap (with GLC, DMASK 0xF), and inc across a 32-bit carry.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 995 passed, 0 failed, 45 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required))
  • cargo test -p xcl --lib -- gpu::recomp::pso image_atomic: 16 passed, 0 failed, 1 ignored (gpu_image_atomics_64_bit ran on the GPU; this adapter has both capabilities)

Regression run

main (80f4819, clean tree, no local changes):

xcl-smoke v1
commit: 80f4819098e921f2e1ab371144d4832f68ffedd6
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1637 (PresentX 1637)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17769 MB

PR (9e1e7d3, clean tree, no local changes):

xcl-smoke v1
commit: 9e1e7d30e9a58de28a932675c5a5b1fc8e6e3a58
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1638 (PresentX 1638)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17843 MB

(Two earlier runs of this head, straight after the suite, came out at 1373 and 1284 frames with the last windows at 1–2 presents and 24–26 GB of commit. The same main binary before and after them ran at 60 frames per window, so I put that down to the machine rather than the change, and the pair above is from the same state one after the other.)

Not tested

  • an adapter lacking AtomicInt64OnTypedResourceSupported / AtomicInt64OnDescriptorHeapResourceSupported: the refusal is tested at translation level and the cache keys in unit tests, but no such device ran the title or the suite
  • the direct DXIL path (XCL_DIRECT_DXIL=1) for these instructions: this PR changes the HLSL path only
  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

@gut0to gut0to 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.

AI-assisted review (GPT-6): I verified the failure path in the generated 64-bit compare-exchange loop.

let u = format!("{t}_U[{icoord}]");
let cas64 = |new: String| {
format!(
"uint64_t {t}_c = {u}; [allow_uav_condition] for (uint {t}_i = 0u; {t}_i < 4096u; {t}_i++) {{ uint64_t {t}_n = {new}; uint64_t {t}_p = 0; InterlockedCompareExchange({u}, {t}_c, {t}_n, {t}_p); if ({t}_p == {t}_c) break; {t}_c = {t}_p; }} {t}_old = {t}_c;"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this retry until the compare-exchange succeeds? If all 4096 attempts lose under contention, the loop exits without applying the atomic operation but still reports _c as the old value. That silently drops updates for 64-bit smin, smax, inc, or dec. Please handle exhaustion as a failure or keep retrying. AI-assisted review: I checked the generated loop and its failure path.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Checked on NVIDIA at head ec4c343: pass. Also a note that #1533 fixes the same issue.

Tested

  • RTX 3070, driver 616.56 (32-lane waves, so the split form), Windows 11 Pro 26100, Rust 1.100.0 MSVC, DXC 1.8.2502.11, debug.
  • cargo test -p xcl --lib -- image_atomic --test-threads=1: gpu_image_atomics_64_bit passes.
  • An extra test of my own, added locally (not part of this PR), also passes. It has a 64-bit add with GLC, umax, smin over negative values, cmpswap with GLC (exactly one winner), and a 32-bit cmpswap (DMASK 0x3) in the same program that must leave the high dword alone. Test: https://github.com/upipibi/xcl/blob/fix/image-atomic-64/crates/xcl/src/gpu/recomp/tests.rs (gpu_image_atomics_64_bit there).

Compared with #1533 (same issue, opened earlier; both pass the test above here):

Not tested: the whole suite, AMD, a device without the caps, a retail run.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this comment.

@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @gut0to and @upipibi — updated in \d9138ac\ to use native \RWTexture<int64_t>\ \InterlockedMin/\InterlockedMax\ directly for 64-bit \smin\ and \smax\ (no CAS loop needed at all for those two). For \inc\ and \dec\ (which have no HLSL wrap-at-limit intrinsic), the 4096-iteration bounded CAS loop matches the existing
mw32\ and
mw64\ helpers in \emit.rs\ so shaders cannot hang in an unbounded GPU loop and trigger TDR. Re-ran \gpu_image_atomics_64_bit, \scripts/test.ps1, and the 60 s \scripts/smoke.ps1\ run.

@Belox17

Belox17 commented Nov 24, 2026

Copy link
Copy Markdown

Test report for d9138ac2 on NVIDIA (RTX 3060 Ti, 32-lane waves): passes, and the GPU check catches the bug without the fix

Tested

  • NVIDIA GeForce RTX 3060 Ti 8 GB, driver 32.0.16.2124; Ryzen 5 5600X; Windows 11 build 26300; rustc 1.98.1 MSVC; DXC 1.8.2502.11 (Windows SDK 10.0.26100); debug build. Head d9138ac2 as is (base 9e3831e), clean tree; the previous head ec4c343f gave the same results.
  • cargo test -p xcl --lib gpu_image_atomics_64_bit: 1 passed. The GPU part ran (no SKIPPED, so the adapter has 64-bit typed atomics through the descriptor heap).
  • cargo test --workspace -- --test-threads=4: 777 passed, 0 failed, 41 ignored. SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows). main 9e3831e on the same machine: 776 passed, 0 failed, 41 ignored, same three skips.
  • With emit.rs from 9e3831e (the rest of the PR kept), the test fails at its CPU check (64-bit image atomic must be refused when adapter lacks int64_typed_atomic). With the CPU checks also removed, its GPU part fails on this adapter: image_atomic_add 64-bit, left 4294967232 (0xFFFFFFC0), right 274877906880 (0x3FFFFFFFC0): the carry into the high dword is lost.

Not tested

  • AMD / wave64 hardware; an adapter without 64-bit typed atomics (the refusal only ran as the CPU check); any retail title (no smoke run).

AI use: Claude Code (Claude Opus 5.5) ran the commands and wrote this report; I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

kacper-serewis pushed a commit to kacper-serewis/xcl that referenced this pull request Nov 24, 2026
Adapt xcl-dev#1541 and the signed views and contention fixtures from xcl-dev#1533. Key translation caches on the device capability. Five independent operations failed on the baseline and passed on the RTX 5070.

Co-authored-by: enderrr9 <178652333+enderrr9@users.noreply.github.com>
Assisted-by: OpenCode (Gemini 4 Neon)
Assisted-by: T3 Code (gpt-6.2-astra)
@KainanP

KainanP commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for the 64-bit atomics work. I read the diff at ec4c343 and rechecked d9138ac (not run); the line below is unchanged.

In console_to_dxil_in (pso.rs:146), || !int64_typed_atomic() sends every shader past ShaderCache and the translated disk cache on GPUs without 64-bit typed atomics, not only shaders that use them. On those machines, each boot re-translates the whole corpus (the module comment cites 0.1-0.3 s per program over ~2 800 programs on a retail title) and pipelines no longer share translations within a run. The DXIL cache still saves the DXC step.

Suggestion: keep the caches and add the capability bit to the keys instead, i.e. a field in ShaderKey and a component of the translated::file path/identity. Memoising int64_typed_atomic() in a OnceLock once the device exists would also avoid two CheckFeatureSupport calls per request.

AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting.

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #1533 #1551 solves the same problem (for #1485) in a different way: #1607, group D9. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

b3IKK added a commit to b3IKK/xcl that referenced this pull request Nov 24, 2026
…h cap gating

The MIMG image atomic emitter ignored DMASK, silently lowering 64-bit
image atomics (DMASK 0x3 for add/sub/min/max/and/or/xor/swap/inc/dec,
DMASK 0xF for cmpswap/fcmpswap) as 32-bit atomics on the low dword only,
losing carries, ordering u64 values by their low 32 bits, and ignoring
VDATA[1] / VDATA[2:3].

Decode 32-bit vs 64-bit width from DMASK (rejecting invalid masks),
gate 64-bit typed image atomics on `Options::int64_typed_atomic`
(queried from `D3D12_OPTIONS9::AtomicInt64OnTypedResourceSupported` and
`D3D12_OPTIONS11::AtomicInt64OnDescriptorHeapResourceSupported`), and
emit SM 6.6 `RWTexture*<uint64_t>` Interlocked* / 64-bit CAS loops with
full 64-bit GLC return unpacking into VDATA[0:1]. When the adapter lacks
these capabilities, 64-bit image atomics are cleanly refused rather than
mistranslated as 32-bit.

The capability is part of the caches' keys instead of a reason to bypass
them: a field of the in-memory `ShaderKey`, and an identity of its own
for the translations on disk of a device without it (a capable device's
identity, and so its existing entries, is unchanged). Without that, a
device lacking the capability translated every program again at each
start (review of xcl-dev#1541). The device is asked once, not per translation.

Fixes xcl-dev#1485.

Assisted-by: OpenCode (Claude Fable 5.2)
@b3IKK
b3IKK force-pushed the recomp/image-atomic-64bit branch from d9138ac to a4f7834 Compare November 24, 2026 23:15
@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@KainanP you are right: || !int64_typed_atomic() took every shader past both caches on a device without the capability, not only the programs that use 64-bit image atomics. Pushed a4f7834 (rebased onto 689f2db):

  • The bypass is gone. The capability is a field of ShaderKey, and the translations on disk of a device without it live under an identity of their own (translated::identity_for_caps). With the capability the identity is the one main has, so existing entries stay valid.
  • int64_typed_atomic() asks the device once it exists and keeps the answer, instead of two CheckFeatureSupport calls per translation.
  • New unit test a_device_without_int64_typed_atomics_has_its_own_disk_entries: an entry stored for a capable device is a miss for one without, each reuses its own, and a disabled cache (identity 0) stays disabled.

Suite on this head: 977 passed, 0 failed, 45 ignored. Smoke for 689f2db and a4f7834: 1643 and 1642 frames, 0 crashes, 0 device removals.

Not tested: a device that lacks the capability. I have none here, so the cache behaviour on one is covered by the unit tests only.

On the #1607 hint (group D9, #1533 and #1551): the description now says what differs. This PR refuses a program that uses 64-bit image atomics where the device lacks them; the other two always emit the 64-bit form. I have not run either of them, so I am not claiming more than that.

Also corrected in the description: it listed a change in hle/d3d12x/real.rs that this PR does not have. The probe is in pso.rs.

AI use: OpenCode, Claude Fable 5.2 made the change, ran the tests and wrote this comment.

…h cap gating

The MIMG image atomic emitter ignored DMASK, silently lowering 64-bit
image atomics (DMASK 0x3 for add/sub/min/max/and/or/xor/swap/inc/dec,
DMASK 0xF for cmpswap/fcmpswap) as 32-bit atomics on the low dword only,
losing carries, ordering u64 values by their low 32 bits, and ignoring
VDATA[1] / VDATA[2:3].

Decode 32-bit vs 64-bit width from DMASK (rejecting invalid masks),
gate 64-bit typed image atomics on `Options::int64_typed_atomic`
(queried from `D3D12_OPTIONS9::AtomicInt64OnTypedResourceSupported` and
`D3D12_OPTIONS11::AtomicInt64OnDescriptorHeapResourceSupported`), and
emit SM 6.6 `RWTexture*<uint64_t>` Interlocked* / 64-bit CAS loops with
full 64-bit GLC return unpacking into VDATA[0:1]. When the adapter lacks
these capabilities, 64-bit image atomics are cleanly refused rather than
mistranslated as 32-bit.

The capability is part of the caches' keys instead of a reason to bypass
them: a field of the in-memory `ShaderKey`, and an identity of its own
for the translations on disk of a device without it (a capable device's
identity, and so its existing entries, is unchanged). Without that, a
device lacking the capability translated every program again at each
start (review of xcl-dev#1541). The device is asked once, not per translation.

Fixes xcl-dev#1485.

Assisted-by: OpenCode (Claude Fable 5.2)

(The mesh-shader path's own cache key, added on main since the last rebase, carries the same capability.)
@b3IKK

b3IKK commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 80f4819 (main had moved the cache to shader_cache() and added a cache key to the mesh path, which now carries the capability too). Head 9e1e7d3: 995 passed, 0 failed, 45 ignored, and the two smoke blocks in the description are from 80f4819 and this head, run one after the other. AI use: OpenCode, Claude Fable 5.2.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked the rebased head 9e1e7d3 on the RTX 3070 (driver 616.56, Windows 11, debug). With XCL_DIRECT_DXIL=0, my extended 64-bit test passes, and so does the existing image_atomic GPU test. The extended test covers add with a carry between the halves, umax, smin of negative values, and cmpswap with GLC: one winner, and the other 63 lanes return the final value. Same result as at ec4c343.

AI-assisted (Claude Code).

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.

Recompiler: 64-bit image atomics (DMASK 0x3) run as 32-bit atomics on the low dword

7 participants