Repository navigation
Recompiler: 64-bit image atomics keep all 64 bits - #1551
youroot-code wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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;" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
9180292 to
73e7403
Compare
|
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 AI use: Claude Code (Claude Opus 5.5) compared the diffs and wrote this. |
|
Read at && dmask == (if &base[7..] == "cmpswap" { 0xFu64 } else { 0x3u64 }) =>
...
if !self.opt.image_atomic64 || matches!(op, "fmin" | "fmax" | "fcmpswap") {
return Err("image_atomic_64");
AI use: Claude Code (Claude Opus 5.5) read the diff; not run. Checked by the user before posting. |
|
Full-suite run, since this PR has no test report yet. Tested exact head Tested
Not tested
AI use
|
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
|
The main fix reads right, but the new arm's guard has one hole around _ if base.starts_with("atomic_")
&& dmask == (if &base[7..] == "cmpswap" { 0xFu64 } else { 0x3u64 }) =>
Using (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 Two more things that may be useful:
AI use: written with OpenCode (DeepSeek V4.1 Flash); a person submitting any of this is responsible for running it. |
|
The new arm in
Linux, Wine 10.0 + vkd3d-proton, NVIDIA RTX 3050 Laptop (driver 580.178.04), DXC 1.9.2609.5, Rust 1.98.1 ( |
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 : 0x3forcmpswap). Themimgatomic arms never read DMASK, so such an atomic ran as a 32-bit one on thelow 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_tview of theconsole
32_32_UINTtexel'sR32G32_UINTformat — on a descriptor-heap resource as well, sincethe views come from
ResourceDescriptorHeap.What is in it
gpu/recomp/emit.rs: a newmimgarm ahead of the 32-bit ones, fordmask == 0x3(0xfforcmpswap). It packs VDATA[0:1] into oneuint64_tand emitsInterlockedAdd(also forsub,as an add of the negation),
InterlockedMin/Max/And/Or/Xor/Exchange, a 64-bitInterlockedCompareExchangeforcmpswap, and the 32-bit form's CAS loop onuint64_tforinc/dec(the hardware has neither). GLC returns the old value in VDATA[0:1].gpu/recomp/pso.rs:image_atomic64_for_device, askingD3D12_OPTIONS9::AtomicInt64OnTypedResourceSupportedand
D3D12_OPTIONS11::AtomicInt64OnDescriptorHeapResourceSupported; the result is kept in astatic (set once from
hle/d3d12x/real.rswhen the device is created) and read asOptions::image_atomic64by the compute / VS / PS, geometry and mesh translation paths.image_atomic_64, an unsupported entrywith its reason) instead of being run on the low dword.
gpu/recomp/runner.rs: the test GPU reports the two flags, andrunner::optionscarries 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_addof0xFFFFFFFFper lane must carry into the high dword,image_atomic_umaxof
(l << 32) | (1000 - l)must pick lane 63. It also asserts that the refusal is only takenwhen 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) andfmin/fmax/fcmpswap— there are no64-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 shaderspath has no device, so it refusesthese programs too (they show up in its unsupported list).
Tested
73e7403, rebased ontomain9e3831e(Recompiler: memory windows from the descriptor heap (no per-access switch) #680). The only conflict wasOptions::default, whereheap_windowsandimage_atomic64now sit side by side.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=4on73e7403: 775 passed, 2 failed, 41 ignored. Both failures are this machine's, and both failon unmodified
mainhere as well:hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory: the test's[u8; 64]buffer is misaligned for
VirtualQueryEx(ERROR_NOACCESS998) — 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 thedebug layer on here (
System32\d3d12SDKLayers.dll10.0.26100.1 againstD3D12Core.dll10.0.26100.7309), so XCL falls back to the null device.
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; theadapter reports both capabilities, the program is translated and runs on the GPU, and both
results match.
b7aa9f3), thesame test fails with the values from the issue —
image_atomic_add 64-bit: 0x00000000ffffffc0, want 0x0000003fffffffc0andimage_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
cmpswap,inc/decand GLC 64-bit paths on a GPU (they are emitted, not exercised bythe test above).
Regression run
From
main(9e3831e):From this PR's head (
73e7403):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.