Skip to content

Recompiler: 64-bit image atomics keep all 64 bits - #1551

Open
youroot-code wants to merge 1 commit into
xcl-dev:mainfrom
youroot-code:recomp/image-atomics-64-bit
Open

youroot-code wants to merge 1 commit into
xcl-dev:mainfrom
youroot-code:recomp/image-atomics-64-bit

Conversation

@youroot-code

@youroot-code youroot-code commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #1485.

A console image atomic whose DMASK covers two dwords works on 64 bits: VDATA[0:1] against the
texel's two dwords (LLVM's SIISelLowering: DMask = Is64Bit ? 0x3 : 0x1, 0xf : 0x3 for
cmpswap). The mimg atomic arms never read DMASK, so such an atomic ran as a 32-bit one on the
low dword, and the program still counted as translated: an add lost its carry, and a max ordered
by the low dword.

On a PC the operation needs SM 6.6 int64 atomics on a typed resource — a uint64_t view of the
console 32_32_UINT texel's R32G32_UINT format — on a descriptor-heap resource as well, since
the views come from ResourceDescriptorHeap.

What is in it

  • gpu/recomp/emit.rs: a new mimg arm ahead of the 32-bit ones, for dmask == 0x3 (0xf for
    cmpswap). It packs VDATA[0:1] into one uint64_t and emits InterlockedAdd (also for sub,
    as an add of the negation), InterlockedMin/Max/And/Or/Xor/Exchange, a 64-bit
    InterlockedCompareExchange for cmpswap, and the 32-bit form's CAS loop on uint64_t for
    inc / dec (the hardware has neither). GLC returns the old value in VDATA[0:1].
  • gpu/recomp/pso.rs: image_atomic64_for_device, asking D3D12_OPTIONS9::AtomicInt64OnTypedResourceSupported
    and D3D12_OPTIONS11::AtomicInt64OnDescriptorHeapResourceSupported; the result is kept in a
    static (set once from hle/d3d12x/real.rs when the device is created) and read as
    Options::image_atomic64 by the compute / VS / PS, geometry and mesh translation paths.
  • Without those capabilities the program is refused (image_atomic_64, an unsupported entry
    with its reason) instead of being run on the low dword.
  • gpu/recomp/runner.rs: the test GPU reports the two flags, and runner::options carries them,
    so kernel tests translate as the adapter can.
  • gpu/recomp/tests.rs: gpu_image_atomics_64_bit (the program and expectations from the issue):
    image_atomic_add of 0xFFFFFFFF per lane must carry into the high dword, image_atomic_umax
    of (l << 32) | (1000 - l) must pick lane 63. It also asserts that the refusal is only taken
    when the adapter reports no int64 typed atomics, so it cannot pass by refusing on hardware that
    has them.

Not in it: 64-bit smin / smax (signed view) and fmin / fmax / fcmpswap — there are no
64-bit float atomics on a typed resource, and the signed view is a separate change; those are
refused with the same reason for now. The offline xcl shaders path has no device, so it refuses
these programs too (they show up in its unsupported list).

Tested

  • Head 73e7403, rebased onto main 9e3831e (Recompiler: memory windows from the descriptor heap (no per-access switch) #680). The only conflict was
    Options::default, where heap_windows and image_atomic64 now sit side by side.
  • NVIDIA GeForce RTX 4060 8 GB (7 956 MiB), 32-lane waves, driver 32.0.16.2124; Windows 11 Pro
    26200; rustc 1.92.0 (stable-x86_64-pc-windows-msvc), debug; DXC 1.9.2602.17 (SDK 10.0.28000.0).
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4 on
    73e7403: 775 passed, 2 failed, 41 ignored. Both failures are this machine's, and both fail
    on unmodified main here as well:
    • hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory: the test's [u8; 64]
      buffer is misaligned for VirtualQueryEx (ERROR_NOACCESS 998) — Tests: align XMemVirtualQuery buffers and verify CPU flags #650 / Tests: align the XMemVirtualQuery output buffer #656.
    • test_title_draws_with_a_root_signature_under_the_debug_layer: no device is made with the
      debug layer on here (System32\d3d12SDKLayers.dll 10.0.26100.1 against D3D12Core.dll
      10.0.26100.7309), so XCL falls back to the null device.
    • 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);
      set_predication_buffer_state_and_kickoff (no GPU).
  • cargo test -p xcl --lib gpu_image_atomics_64_bit -- --test-threads=1: 1 passed; the
    adapter reports both capabilities, the program is translated and runs on the GPU, and both
    results match.
  • Without the fix: with the new arm disabled (measured on the pre-rebase tree, b7aa9f3), the
    same test fails with the values from the issue — image_atomic_add 64-bit: 0x00000000ffffffc0, want 0x0000003fffffffc0 and image_atomic_umax 64-bit: 0x00000000000003e8, want 0x0000003f000003a9 (0 passed, 1 failed).
  • gpu_images_sample_load_store_atomic (the 32-bit paths) still passes.

Not tested

  • The cmpswap, inc / dec and GLC 64-bit paths on a GPU (they are emitted, not exercised by
    the test above).
  • AMD / native wave64, WARP, release build.
  • Anything else: the runs below are one 60 s window of one title.

Regression run

From main (9e3831e):

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 4060
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1456 (PresentX 1456)
presents per 2 s (last 3): 36 60 56
device removals: 0
crashes: 0
peak commit: 9598 MB

From this PR's head (73e7403):

xcl-smoke v1
commit: 73e7403aa3f1688e1832d8544e8be8fb8844f47f
gpu: NVIDIA GeForce RTX 4060
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1425 (PresentX 1425)
presents per 2 s (last 3): 38 60 56
device removals: 0
crashes: 0
peak commit: 9539 MB

Both are 60 s of the same title, release build, XCL_NETWORK=0.

AI use

Freebuff (Buffy) — the exact model name is not known to me. It wrote the change, ran the tests and
measured the failing case on the machine above; the failing test, the program and its expectations
come from the issue's reporter.

An MIMG atomic with DMASK 0x3 (0xf for cmpswap) works on VDATA[0:1] against the
texel's two dwords, but the mimg atomic arms never read DMASK: such an atomic ran
as a 32-bit one on the low dword and the program still counted as translated. An
add lost its carry and a max ordered by the low dword (xcl-dev#1485).

On a PC the operation needs SM 6.6 int64 atomics on a typed resource, and on a
descriptor-heap resource as well, since the views come from
ResourceDescriptorHeap. Options::image_atomic64 carries that capability, asked once
when the device is created; without it the program is refused rather than run on
32 bits.

Measured on an RTX 4060: gpu_image_atomics_64_bit passes with the change and fails
without it (add 0x00000000ffffffc0 for 0x0000003fffffffc0, umax 0x3e8 for
0x3f000003a9).

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>

@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 bounded compare-exchange loop and post-loop handling in the current diff.

format!("({t}_c == (uint64_t)0 || {t}_c > {t}_v) ? {t}_v : {t}_c - (uint64_t)1")
};
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.

After 4096 failed compare-exchanges this loop exits and still reports _c as the old value, so the 64-bit increment or decrement is silently dropped under sustained contention. Please retry until the compare-exchange succeeds, or report failure on exhaustion. AI-assisted review: I verified there is no success check after the bounded loop.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You are right that the increment is dropped when the loop runs out, and that _old then holds the last observed value rather than the old value of a completed write.

The bound is deliberate and older than this PR. rmw32 (the cas closure, emit.rs) and the 64-bit buffer path (rmw64, same file) both run the same [allow_uav_condition] for (uint i = 0u; i < 4096u; i++) compare-exchange loop, and HANDOFF.md records the design as "a bounded CAS loop (4096)". Retrying without a bound on a typed atomic is a hang risk: a wave spinning on one texel can outlive the TDR timeout and you get a device removal instead — and the regression run measures device removals, so a hang is worse than a lost increment on a path that needs 4096 lost races in a row.

Reporting the failure has no channel in the generated HLSL: the shader cannot return an error, and the only "report" would be a write to a debug buffer, which nothing reads on a title run.

So I kept 64-bit inc / dec byte-for-byte with the existing loops rather than make one path behave differently. If the exhaustion should be handled, it is one change over all three loops (rmw32, rmw64, and this one) and I am happy to open it — but it changes paths this PR does not touch, and the repo asks for one logical change per PR.

On coverage: the test does not exercise 64-bit inc / dec at all (it uses add and umax), which the description lists under "Not tested".

@youroot-code
youroot-code force-pushed the recomp/image-atomics-64-bit branch from 9180292 to 73e7403 Compare November 24, 2026 17:35
@youroot-code
youroot-code marked this pull request as ready for review November 24, 2026 17:44
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Note: #1485 already has #1533 and #1541, both opened earlier. Both pass on an RTX 3070, together with an extended test of mine (64-bit add / umax / smin, cmpswap with GLC, and a 32-bit cmpswap next to them that must leave the high dword alone): #1533 (comment), #1541 (comment). This PR is an implementation of its own, not a copy; I haven't run it. Only one of the three can land in the mimg atomic arms.

AI use: Claude Code (Claude Opus 5.5) compared the diffs and wrote this.

@KainanP

KainanP commented Nov 24, 2026

Copy link
Copy Markdown

Read at 73e7403 while doing the #1607 duplicate check (D9). The 64-bit arm seems to catch 32-bit image_atomic_fcmpswap:

&& dmask == (if &base[7..] == "cmpswap" { 0xFu64 } else { 0x3u64 }) =>
...
if !self.opt.image_atomic64 || matches!(op, "fmin" | "fmax" | "fcmpswap") {
    return Err("image_atomic_64");

fcmpswap is a compare-swap too: its 32-bit form already uses DMASK 0x3 (data + compare), like 32-bit cmpswap. Since only the exact string "cmpswap" gets 0xF, a 32-bit fcmpswap with 0x3 matches this arm and is refused with image_atomic_64, where main translates it in the 32-bit path. Testing base[7..].ends_with("cmpswap") (or matches!(.., "cmpswap" | "fcmpswap")) for the 0xF case would keep it on the 32-bit path; a translation test of a 32-bit fcmpswap would catch it.

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

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 73e7403 (on its base 9e3831e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form: 776 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: gpu_image_atomics_64_bit.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 777 passed, 0 failed, 41 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • heap_window_out_of_memory_falls_back_to_the_switch_form was skipped with --skip. It fails on main 9e3831e too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (XCL_HEAP_WINDOWS unset). Reported as Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests 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.

Thanks!

@ddaikodaiko

Copy link
Copy Markdown

The main fix reads right, but the new arm's guard has one hole around fcmpswap that I think you want to close before merge. The guard is:

_ if base.starts_with("atomic_")
    && dmask == (if &base[7..] == "cmpswap" { 0xFu64 } else { 0x3u64 }) =>

fcmpswap never satisfies the 0xF case because the comparison is against "cmpswap" exactly, so:

  • a 32-bit image_atomic_fcmpswap (DMASK 0x3, VDATA[0]=compare, VDATA[1]=value) takes the else { 0x3 } branch, enters the new 64-bit arm, and is then refused by matches!(op, "fmin" | "fmax" | "fcmpswap") as image_atomic_64. That is a program the current main translates (the generic rmw32 arm below already handles fcmpswap with ends_with("cmpswap") in the dmask guard), so it turns into a new unsupported entry, not a refusal of a 64-bit form.
  • a 64-bit fcmpswap (DMASK 0xF, if such an encoding ever appears) matches neither side of the guard and falls through to the same 32-bit rmw32 arm, i.e. it still runs on the low dword rather than being refused as your description says.

Using ends_with("cmpswap") in both terms fixes both directions:

(dmask == 0x3 && !op.ends_with("cmpswap")) || (dmask == 0xF && op.ends_with("cmpswap"))

which is also what the fork branch I have does; the same expression is in gpu/dxil/lower_image.rs for the direct translator.

Two more things that may be useful:

  • The direct DXIL translator (crates/xcl/src/gpu/dxil/lower_image.rs, the atomic_ arm) has the same original bug and this PR does not touch it, so with XCL_DIRECT_DXIL=1 the issue remains. I have an unverified port for it in ddaikodaiko/xcl contrib/int64-image-atomics ca1b79f (U64/I64 typed heap view, heap_typed_atomic64(), pack64/unpack64, same refusals); the existing test with XCL_DIRECT_DXIL=1 should pin it.
  • 64-bit smin/smax: refusing them is a documented scope choice here, but the signed view is only an int64_t variant of the same uint64_t arm, and the HLSL fix I have runs them that way; worth a line in the "not in it" list saying the console op exists so the program is lost until then.

AI use: written with OpenCode (DeepSeek V4.1 Flash); a person submitting any of this is responsible for running it.

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