Skip to content

Recompiler: split 64-bit atomics into 32-bit ones where the host drops them - #1501

Open
us3r40456 wants to merge 4 commits into
xcl-dev:mainfrom
us3r40456:recompiler-split-int64-atomics
Open

us3r40456 wants to merge 4 commits into
xcl-dev:mainfrom
us3r40456:recompiler-split-int64-atomics

Conversation

@us3r40456

@us3r40456 us3r40456 commented Nov 24, 2026 •

Copy link
Copy Markdown

On D3DMetal, a program with a 64-bit atomic writes nothing at all (#983, #258), so a title shader with a global / buffer *_x2 atomic computes or draws nothing. This PR emits 32-bit atomics instead where they leave the same memory, on a host where a start-up probe finds 64-bit atomics dropped. Part of #258: it was proposed there, and Ahmet-Dedeler and vakaobr gave it a +1.

What is in it

  • rmw64_split (emit.rs), used when Options::split_int64_atomics is set and the old value is not returned (no GLC):
    • add_x2 / sub_x2 become the low dword's InterlockedAdd, then the high dword's with the carry (or borrow) that the low one's old value shows. Adds commute, so the end state is exact.
    • and_x2 / or_x2 / xor_x2 become one 32-bit op per dword.
    • umin_x2 / umax_x2 become the 64-bit atomic without a returned value, which D3DMetal runs.
    • With GLC every one keeps its 64-bit form: two 32-bit old values are not one 64-bit snapshot, and another lane's update between them can give a value memory never held (gut0to's review below).
    • swap, cmpswap, signed min / max, inc / dec and the float ones keep the 64-bit form.
  • runner::probe_int64_atomics: once, on the title's device, under Wine only, it runs InterlockedAdd64 and a plain store. Where neither lands, it sets the option. XCL_SPLIT_INT64_ATOMICS=1 / =0 overrides it.
  • The option is part of the translated-shader disk cache identity (pso.rs). With it off, the HLSL is unchanged.
  • The window accessors get the non-returning 64-bit min / max overloads that the split forms call.
  • Two tests:
    • split_int64_atomics_emit_32_bit_atomics: the HLSL with and without the option, with GLC (unchanged by the option), with two memory windows and with the windows in the descriptor heap (heap_windows), compiled with DXC.
    • gpu_split_int64_atomics: 64 lanes on one cell per op, covering carries, borrows, one bit per lane through and / or / xor, and the largest of 64 values.

Tested

  • MacBook Pro, Apple M5 Pro (20-core GPU), 24 GB, macOS 27.2.1, Rosetta 2. CrossOver 26.3.0.39832 with D3DMetal 3.0. MSVC debug build, cross-built with cargo-xwin 0.24.1 (Rust 1.100.0, clang-cl / lld 23.1.6). DXC 1.9.2609.

  • lib unit tests, head 26105de against base main b7aa9f3, one module group at a time; a group that hangs or crashes is rerun one test per process:

    • every one of the 743 tests on both gives the same outcome;
    • both new tests pass, and gpu_split_int64_atomics gets every value right on D3DMetal.
  • The 16 integration binaries give the same outcome on both.

  • gpu::recomp with XCL_SPLIT_INT64_ATOMICS=1, every test one per process: the same outcomes as without it. The tests whose programs also hold a cmpswap / inc / dec / float *_x2 still write nothing on D3DMetal (Tests: GPU tests of 64-bit atomics skip where those atomics write nothing #983 skips them).

  • The probe at start-up (tests/e2e.rs, the test title on D3DMetal) logs recompiler: a program with a 64-bit atomic writes nothing on this GPU (probe: [ffffffff, 1, 0]) – add / sub / and / or / xor and unsigned min / max without a returned value become 32-bit atomics; …. The title gets the same 29 [PASS] as on the base.

  • After merging main 9e3831e (its heap_windows option, Recompiler: memory windows from the descriptor heap (no per-access switch) #680) and the GLC change: both tests pass on D3DMetal, and so does gpu::recomp::heap_window_tests.

Regression run
By @mikebuckets171 on a Mac (M5 Pro, CrossOver 26.3 / D3DMetal / Rosetta 2, release x86_64-pc-windows-gnullvm, the same retail title for 60 s each), #1501 (comment). Plain main on a Mac shows no frame of this title, so these show the probe and start-up, not the split atomics in a frame: on this PR's head the probe logs probe: [ffffffff, 1, 0] and turns the split on, on main it does not run. No crash, no device removal in any of the four. Windows does not take this path (the probe runs under Wine only), and the HLSL there is unchanged.

main 9e3831e:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

PR b24436b:

xcl-smoke v1
commit: b24436b3770344f4db3bc5349b85481f96d85523
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

The same on a stack of seven Mac PRs (476c325) and that stack with this PR's diff (7fd3a25): the same blocks, 0 frames, no crash; the probe as above.

Not tested

  • Windows. The probe does not run there (under_wine()), so the option stays off.
  • Linux / Wine with vkd3d-proton. The probe runs there; I expect it to find 64-bit atomics working and change nothing, but I have not checked.
  • A title, and a release build.

AI use

  • Claude Code, Claude Opus 5.5 wrote the change, the tests and this description, and ran the tests on this machine.

…s them

On Apple's D3DMetal (CrossOver), a program with a 64-bit atomic other than
a non-returning unsigned min / max writes nothing at all, its other stores
included, though the device reports 64-bit atomics (xcl-dev#983, xcl-dev#258). A title
shader with a global / buffer `*_x2` atomic then computes or draws nothing.

Where 32-bit atomics leave the same memory, emit those
(`Options::split_int64_atomics`):
- add_x2 / sub_x2: the low dword's InterlockedAdd, then the high dword's
  with the carry (or borrow) its old value shows. Adds commute, so the
  end state is exact.
- and_x2 / or_x2 / xor_x2: one 32-bit op per dword.
- umin_x2 / umax_x2 without GLC: the 64-bit atomic without a returned
  value, which D3DMetal runs.
With GLC the returned old value is the two dwords' old values, which
another lane's update can come between. swap, cmpswap, signed min / max,
inc / dec, the float ones and umin / umax with GLC keep the 64-bit form.

The option is set once per process by a probe on the title's device
(InterlockedAdd64 plus a plain store; under Wine only), or by
XCL_SPLIT_INT64_ATOMICS=1 / =0. It keys the translated-shader disk cache.
Off (Windows drivers), the HLSL is unchanged. The window accessors get
the non-returning 64-bit min / max overloads the split forms call.

Tests: split_int64_atomics_emit_32_bit_atomics (the HLSL, with and without
the option and with two memory windows; DXC-compiled) and
gpu_split_int64_atomics (64 lanes on one cell per op: carries, borrows,
one bit per lane through and / or / xor, the largest of 64 values).
@us3r40456

Copy link
Copy Markdown
Author

@mikebuckets171: this is the split-forms PR from #258. When you have the time, both pairs would help: on plain main (base b7aa9f3, head 26105de), and stacked with the Mac port pieces you used there. As you said, the first shows nothing regressed; the second is where a wrong split or the torn GLC return would show up in the frames and draws. With this PR the start-up log should have recompiler: a program with a 64-bit atomic writes nothing on this GPU on that host.

@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; I verified the split-atomic interleaving described in the inline comment against the generated sequence.

"add" | "sub" => {
let n = if op == "sub" { format!("((uint64_t)0 - {v})") } else { v.to_string() };
format!(
"uint64_t {t}_n = {n}; uint {t}_l = 0u, {t}_h = 0u; mem.InterlockedAdd({a}, (uint){t}_n, {t}_l); mem.InterlockedAdd({a} + 4u, (uint)({t}_n >> 32) + (({t}_l + (uint){t}_n < {t}_l) ? 1u : 0u), {t}_h); {old} = (uint64_t){t}_l | ((uint64_t){t}_h << 32);"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When glc requests the old value, these two 32-bit return values are not one atomic 64-bit snapshot. For example, starting at 0x00000000_FFFFFFFF, interleaving add 1 with add 0x1_00000000 can make the first operation return 0x00000001_FFFFFFFF, although that value never existed. Please disable this split path when the old result is requested (or provide a truly atomic implementation) and cover the interleaving. AI-assisted review; I verified the generated sequence and interleaving.

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.

Right, thanks. 6a1fae4 drops the split for every atomic whose old value is returned: with GLC the 64-bit form stays, so no value memory never held can come back. The split is now only for add / sub / and / or / xor and unsigned min / max without a returned value, where the end state is exact. split_int64_atomics_emit_32_bit_atomics checks that with GLC the option leaves the HLSL unchanged. AI-assisted (Claude Code, Claude Opus 5.5).

With GLC the split forms returned the two dwords' old values, which are
not one 64-bit snapshot: another lane's update can come between them and
give a value memory never held (gut0to's review on xcl-dev#1501: from
0x00000000_FFFFFFFF, `add 1` interleaved with `add 0x1_00000000` can return
0x00000001_FFFFFFFF). A 64-bit atomic whose old value is returned now keeps
its 64-bit form, so the split forms only cover add / sub / and / or / xor
and unsigned min / max without a returned value, whose end state is exact.

split_int64_atomics_emit_32_bit_atomics checks that with GLC the option
leaves the HLSL unchanged.
…4-atomics

# Conflicts:
#	crates/xcl/src/gpu/recomp/mod.rs
… heap

heap_windows (from main) reaches window k through the descriptor heap; the
split forms go through the same accessors there.
@mikebuckets171

Copy link
Copy Markdown

Mac xcl-smoke pairs (M5 Pro, CrossOver 26.3 / D3DMetal / Rosetta 2; release cross-build cargo build --release --locked -p xcl --target x86_64-pc-windows-gnullvm; same retail title, 60 s each). The first pair is current base 9e3831e vs this PR's head b24436b. The second is the seven-PR Mac test stack vs that stack with this PR's current diff applied.

Current main 9e3831e:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

This PR b24436b:

xcl-smoke v1
commit: b24436b3770344f4db3bc5349b85481f96d85523
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Seven-PR stack base 476c325 (#890 76d2ff7, #957 3c43506, #982 3a6f95a, #983 ab7ce43, #996 f242ea8, #1024 699975b, #787 46695ab):

xcl-smoke v1
commit: 476c3257d0f927483e27637b338aaeaaa5a462dd
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Stack plus #1501's current diff, temporary test tree 7fd3a25 (not an upstream commit):

xcl-smoke v1
commit: 7fd3a2586f6c9fd8d114ca7737e7af4ab2d1ecbc
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

On both PR heads the startup probe logged probe: [ffffffff, 1, 0] and enabled the documented split for eligible atomics; neither baseline logged it. All four bounded runs exited cleanly, but showed 0 frames, so this verifies probe detection and startup only—not rendered atomic behavior or gameplay. peak commit: 0 MB was reported by the harness and is unavailable as a memory measurement here.

AI use: OpenCode, GPT-6 Luna ran the builds/smokes and wrote this report.

@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 / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

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.

4 participants