Skip to content

Recompiler: use IEEE-754 equality for float compare-and-swap atomics - #2011

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
Decodere:fix-1489-fcmpswap-ieee-eq
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
Decodere:fix-1489-fcmpswap-ieee-eq

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

Changes RDNA2 float compare-and-swap atomics (ds_cmpst[_rtn]_f32, {buffer,flat,global}_atomic_fcmpswap[_x2], and image_atomic_fcmpswap) in both the HLSL emitter and Direct DXIL translator from raw bitwise compare-exchange to bounded CAS loops using IEEE-754 float equality (-0.0 == +0.0 swaps and returns the pre-swap sign bits; qNaN == qNaN and sNaN == sNaN keep without swapping). Part of #1489 (XCL-BT-042).

Matches AC-Storm-YT's RDNA2 (gfx1036) silicon verification on #1489 (#issuecomment-7142546620). Comparisons are expressed with pure uint / uint64_t bit operations (!is_nan(cur) && (cur == cmp || ((cur | cmp) & !SIGN) == 0)) so DXC fast-math cannot miscompile NaN or signed-zero checks, 64-bit fcmpswap needs no DoubleExtensions, and PRELUDE_MEM stays untouched. Bitwise integer cmpswap / cmpst_b32 operations remain single compare-exchange instructions.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: split "fcmpswap" / "cmpst_f32" from bitwise "cmpswap" / "cmpst_b32" in rmw32 and "fcmpswap" from "cmpswap" in rmw64, emitting bounded 4096-iteration IEEE-754 CAS loops starting from an atomic Or 0 read (InterlockedOr/InterlockedOr64 + InterlockedCompareExchange/InterlockedCompareExchange64, preventing 64-bit load tearing or non-atomic mismatch early-exits, compatible with multi-window accessors).
  • crates/xcl/src/gpu/dxil/lower_mem.rs: add Lw::feq_bits32, Lw::feq_bits64, Lw::fcas32, and Lw::fcas64 (initializing init via atomic Or 0), and use them for "fcmpswap" / "cmpst_f32" in rmw32 and "fcmpswap" in atomic64.
  • crates/xcl/src/gpu/dxil/lower_image.rs: add Lw::texel_fcas (reusing Lw::feq_bits32 and atomic Or 0 initial read) for image_atomic_fcmpswap while keeping image_atomic_cmpswap as a single atomic_cmpxchg.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • Update atomic_fcmpswap_x2_uses_ieee_64_bit_cas (verifying atomic InterlockedOr64 initial load, 1/2-window HLSL DXC compile, and 1/2/heap-window Direct DXIL validation) and gpu_atomic_fcmpswap_x2 (±0.0 swaps, qNaN keeps, contended shared cells).
    • Add gpu_float_cas_uses_ieee_equality verifying LDS (ds_cmpst_rtn_f32), 32-bit/64-bit buffer/global/flat (*_atomic_fcmpswap[_x2]), image UAV (image_atomic_fcmpswap), and bitwise integer controls across 8 edge cases (-0/+0, +0/-0, qNaN, sNaN, denormal vs +0, finite match/mismatch, -inf) on both HLSL and Direct DXIL.

Tested

  • Hardware / toolchain: NVIDIA GeForce RTX 4060 (8 GB, 32-lane waves, driver 621.14), Intel Core i5-11400F, 32 GB RAM, Windows 11 Home build 26200, Rust 1.100.0 (0ef1bc386), DXC 1.9.2609.5 (01b62ad4), release build.
  • Pre-fix reproduction: confirmed atomic_fcmpswap_x2_uses_ieee_64_bit_cas (CPU) and gpu_float_cas_uses_ieee_equality (RTX 4060 GPU: hlsl lane 0 case 0: ds_cmpst_rtn_f32 kept 0x80000000 instead of swapping -0.0 vs +0.0 to 1.0f) fail before the fix.
  • Targeted post-fix runs (0 failures, 0 skipped):
    • cargo test --release -p xcl --lib gpu::recomp::tests::atomic_fcmpswap_x2_uses_ieee_64_bit_cas -- --nocapture: 1 passed, 0 failed, 0 ignored.
    • cargo test --release -p xcl --lib gpu_float_cas_uses_ieee_equality -- --nocapture: 1 passed, 0 failed, 0 ignored (both HLSL and Direct DXIL executed on RTX 4060).
    • gpu_atomic_fcmpswap_x2 (default HLSL and XCL_DIRECT_DXIL=1) and gpu_images_sample_load_store_atomic (XCL_DIRECT_DXIL=1): all passed, 0 failed.
  • cargo build --release --workspace + cargo test --release --workspace -- --test-threads=4: 991 passed, 0 failed, 45 ignored across 20 test binaries.
  • SKIPPED GPU/runtime tests on this adapter (24 lines):
    • 19 no wave64 subcases across gpu::recomp::lower::tests (4), gpu::recomp::lower_dxil::tests (13), and gpu::recomp::tests (2)
    • 3 intentional per-lane #1426 refusals (V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32) in gpu::recomp::lower_dxil::tests
    • shaders::tests::pso_measurement_compiles_an_exact_synthetic_record: cannot disable the driver cache (Windows Developer Mode is required)
    • heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows

Regression run
scripts/smoke.ps1 on main (8e87fc7b62a87ea589ccb9ea6182f514016cf7db) and on this PR's head commit (990b9d4795ca4707ca5c71f2fd93aca5071e4a35):

xcl-smoke v1
commit: 8e87fc7b62a87ea589ccb9ea6182f514016cf7db
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1573 (PresentX 1573)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14705 MB
xcl-smoke v1
commit: 990b9d4795ca4707ca5c71f2fd93aca5071e4a35
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1576 (PresentX 1576)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14714 MB

Not tested

  • AMD and Intel GPUs, native wave64 hardware, and heap-window GPU execution (skipped above; heap-window Direct DXIL translation and validation passed on CPU).
  • MODE.FP_DENORM=0 (f32 denormal-flushed mode, where RDNA2 flushes denormals to ±0.0 before comparing): XCL currently preserves denormals and does not track s_denorm_mode for float atomics.
  • ds_cmpst[_rtn]_f64 (64-bit LDS atomics are not currently implemented in XCL).
  • Debug-profile full workspace test run (the full suite ran in --release).

AI use

  • Google Antigravity (Gemini 4 Neon) — audited float CAS lowering across emit.rs, lower_mem.rs, and lower_image.rs, implemented the fix, CPU/GPU regression tests, and PR description; every line of the diff, test run, and smoke check was verified on local RTX 4060 hardware.

RDNA2 float compare-and-swap atomics (`ds_cmpst[_rtn]_f32`,
`{buffer,flat,global}_atomic_fcmpswap[_x2]`, `image_atomic_fcmpswap`)
compare using IEEE-754 float equality where `-0.0 == +0.0` swaps
(returning the pre-swap sign bits) and `NaN == NaN` (both qNaN and sNaN)
does not swap, as verified on RDNA2 silicon (`XCL-BT-042`, xcl-dev#1489).

Previously both the HLSL emitter (`recomp/emit.rs`) and Direct DXIL
translator (`dxil/lower_mem.rs`, `dxil/lower_image.rs`) lowered all
float CAS atomics to raw bitwise compare-exchange. Replace those with
bounded 4096-iteration CAS loops starting from an atomic `Or 0` read
(preventing 64-bit load tearing or non-atomic early-exit reads on
mismatch) and checking bitwise IEEE-754 ordered equality
(`!is_nan(cur) && (cur == cmp || ((cur | cmp) & !SIGN) == 0)`) in pure
`u32` / `u64` integer ops so DXC fast-math cannot miscompile NaN/zero
checks, 64-bit `fcmpswap` needs no `DoubleExtensions`, and `PRELUDE_MEM`
stays untouched. Integer `cmpswap` / `cmpst_b32` ops remain single
bitwise compare-exchanges.

Update `atomic_fcmpswap_x2_uses_ieee_64_bit_cas` and
`gpu_atomic_fcmpswap_x2`, and add `gpu_float_cas_uses_ieee_equality`
covering all 32-bit/64-bit float CAS families plus integer controls on
both HLSL and Direct DXIL.

Part of xcl-dev#1489

Assisted-by: Google Antigravity (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 25, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 990b9d4 (on its base 8e87fc7).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, 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 --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::const_and_null_mask_operands_agree_with_hlsl_on_the_gpu --skip gpu::dxil::fuzz_tests: 980 passed, 0 failed, 43 ignored. SKIPPED (no wave64): a_2d_or_3d_group_agrees_with_the_hlsl_path_on_the_gpu, a_per_lane_2d_or_3d_group_runs_on_the_gpu, coalesced_guarded_runs_agree_with_the_hlsl_path_on_the_gpu, cross_lane_reads_agree_with_the_hlsl_path_on_the_gpu, gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, the_wave64_lowering_agrees_with_the_hlsl_path_on_the_gpu.
  • The new tests ran and passed: gpu_float_cas_uses_ieee_equality.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4 --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 990 passed, 0 failed, 45 ignored. SKIPPED: f16_programs_on_the_gpu (f16 arithmetic, wave32, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 arithmetic, wave64, per-lane form: refused (unsupported V_MUL_F16 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave32, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f16_programs_on_the_gpu (f16 packed, wave64, per-lane form: refused (unsupported V_PK_MUL_F16: VOP3 modifiers (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave32, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), f32_programs_on_the_gpu (f32 conversions, wave64, per-lane form: refused (unsupported V_CVT_F16_F32 (its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's))), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • --skip leaves out tests that fail on main 7994cd1 too, not because of this PR. On NVIDIA: heap_window_out_of_memory_falls_back_to_the_switch_form (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), the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests and const_and_null_mask_operands_agree_with_hlsl_on_the_gpu (follow-up on #1651) fail because runner::skip_test turns their skips into failures under XCL_REQUIRE_GPU=mesh, and gpu::dxil::fuzz_tests seeds 70, 545, 1019 and 1507 fail CreateComputePipelineState with E_OUTOFMEMORY on this card (drivers 616.64 and 621.40). On AMD: fuzz_validate_and_gpu_1 fails on seed 540 (res[833] 0x7 vs CPU 0x6). The AMD run does not set XCL_REQUIRE_GPU.

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.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

AMD RDNA3 (RX 7800 XT, native wave64) test report for #2011 at head 990b9d47 merged with main 2e1c9562 (clean merge).

Tested

  • AMD Radeon RX 7800 XT 16 GB (RDNA3, native wave64, driver 32.0.31035.1003), Ryzen 7 5700X3D, 64 GB RAM, Windows 11 Pro 26200, Rust 1.100.0 MSVC, DXC 1.8.2502.11 (Windows Kits\10\bin\10.0.26100.0\x64), debug build, XCL_ADAPTER="RX 7800 XT".
  • Targeted runs (all passed, 0 failed, 0 skipped):
    • cargo test -p xcl --lib gpu::recomp::tests::atomic_fcmpswap_x2_uses_ieee_64_bit_cas -- --nocapture: 1 passed, 0 failed (0.09s)
    • cargo test -p xcl --lib gpu_float_cas_uses_ieee_equality -- --nocapture: 1 passed, 0 failed (0.37s; both HLSL and Direct DXIL on RX 7800 XT)
    • cargo test -p xcl --lib gpu_atomic_fcmpswap_x2 -- --nocapture: 1 passed, 0 failed (0.27s)
    • XCL_DIRECT_DXIL=1 cargo test -p xcl --lib gpu_atomic_fcmpswap_x2 -- --nocapture: 1 passed, 0 failed (0.22s)
    • XCL_DIRECT_DXIL=1 cargo test -p xcl --lib gpu_images_sample_load_store_atomic -- --nocapture: 1 passed, 0 failed (0.24s)
  • Full workspace (cargo build --workspace + cargo test --workspace -- --test-threads=4 --nocapture, with e2e verified after cargo build --workspace): 1001 passed, 1 failed, 45 ignored across 20 test binaries.
    • The 1 failure is gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 (seed 722: res[30] = 0x1, CPU 0x7fffffff, sdiv i64 inside a switch). Verified directly on origin/main 2e1c9562 on the same RX 7800 XT: it fails identically there (pre-existing on main, unrelated to this PR).
    • SKIPPED lines (7): the 6 intentional #1426 per-lane refusals in gpu::recomp::lower_dxil::tests (V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32, wave32 and wave64) plus heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows. Zero no wave64 skips — all native wave64 GPU tests ran and passed.

Not tested

  • Release build, scripts/smoke.ps1 title run, or other AMD generations.

AI use

  • T3 Code (Antigravity), Gemini 4 Neon: checked out the PR in a temporary worktree, ran the targeted and workspace tests on @oni-swr's machine, and wrote this report.

@raylitebull
raylitebull merged commit 0cdc896 into xcl-dev:main Nov 25, 2026
2 checks passed
@JJAutoCompare

Copy link
Copy Markdown

Independent Intel Arc A770 verification of clean PR head 990b9d4795ca4707ca5c71f2fd93aca5071e4a35. The PR merged during the run. The tested commit is included in merge 0cdc896fa3b63ab781a5ba0dc0c278b240e02976.

Tested

  • Intel Arc A770, driver 32.0.101.9175, Ryzen 7 3800X (6 cores / 12 logical processors exposed), Windows 11 Enterprise Evaluation 25H2 x64 (26200.9974). Rust/Cargo 1.100.0 MSVC, debug test profile. Both logs confirmed DXC and DXIL 1.9.2609.5 via XCL_DXC_DIR.
  • Used an isolated Cargo target and TEMP/TMP on L:, with CARGO_BUILD_JOBS=2 and CARGO_INCREMENTAL=0. Precompiled with cargo test -p xcl --lib --no-run outside the shared Arc lock. Set XCL_ADAPTER=A770 for GPU execution.
cargo test -p xcl --lib gpu::recomp::tests::atomic_fcmpswap_x2_uses_ieee_64_bit_cas -- --exact --nocapture
& 'L:/XclTooling/Invoke-ArcTest.ps1' -Test {
    cargo test -p xcl --lib gpu::recomp::tests::gpu_float_cas_uses_ieee_equality -- --exact --nocapture
}
  • CPU, 1 passed, 0 failed, 0 ignored, 0 skipped. Checked 64-bit buffer, flat, and global CAS code generation, compiled one- and two-window HLSL, and validated Direct DXIL for one, two, and heap windows.
  • GPU, 1 passed, 0 failed, 0 ignored, 0 capability skips. The same test dispatched both HLSL-generated and Direct DXIL kernels on Arc. All 64 logical lanes checked original values and final contents across eight cases, including both signed-zero orders, quiet and signaling NaNs, a denormal against zero, finite matches and mismatches, and negative infinity. Coverage included LDS, image UAVs, 32- and 64-bit buffer, flat, and global float CAS, plus bitwise integer controls.

Not tested

Full workspace suite, release build, retail-title smoke, heap-window GPU dispatch, or other GPU/driver combinations in this run.

AI use

Codex (gpt-6.2-sol) ran the focused tests, checked the logs, and wrote this report.

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.

5 participants