Skip to content

Recompiler: guard and scale direct DXIL v_rcp_f64 and v_rsq_f64 - #2063

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-dxil-rcp-d
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-dxil-rcp-d

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Bring Direct DXIL v_rcp_f64 and v_rsq_f64 into parity with the HLSL path's rcp_d helper (merged in #530).

Part of #1269

  • Problem: In crates/xcl/src/gpu/dxil/lower_wide.rs, Direct DXIL lowering of v_rcp_f64 and v_rsq_f64 still emitted raw unguarded ddiv(1.0, a) / ddiv(1.0, sqrt_d(a)). As documented in Recompiler: v_rcp_f64 and v_rsq_f64 keep NaN, infinities and denormals #530, drivers can return 0 for 1/NaN (for example v_rsq_f64 on negative inputs) or flush subnormal reciprocals such as 1/f64::MAX to 0.
  • Fix: Add Lw::rcp_d in crates/xcl/src/gpu/dxil/lower_wide.rs and wire both v_rcp_f64 (self.rcp_d(a)) and v_rsq_f64 (self.rcp_d(self.sqrt_d(a))) through it:
    • Handle NaN (preserving the input bits), ±0.0 -> ±inf, and ±inf -> ±0.0 explicitly using isnan_d, isinf_d, fcmp(Pred::FOEq, x, 0.0), and mkd with the input sign bit.
    • Substitute 1.0 into the eager division path (safe_x = sel(special, 1.0, x)) so special operands never reach ddiv.
    • Scale finite operands into [0.5, 1) via e = frexp_exp_d(safe_x) and scaled = ldexp_d(safe_x, -e), compute r = ddiv(1.0, scaled), and rescale back with ldexp_d(r, -e) so subnormal reciprocals are preserved.
  • Tests: Add rcp_d_tests at the end of crates/xcl/src/gpu/dxil/lower_wide.rs:
    • v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d: validates both v_rcp_f64 and v_rsq_f64 (wave32 and wave64) with dxil.dll and checks the DXIL disassembly for the zero guard, frexp_exp_d scaling, and post-division ldexp_d / select guards.
    • gpu_direct_rcp_f64_and_rsq_f64_edges: unconditional wave32 Direct DXIL GPU test across 32 edge inputs (±0.0, ±inf, ±qNaN with payload, negative values, ±f64::MAX, ±f64::MIN_POSITIVE, smallest/largest ± denormals, and normal floats), verifying exact ±inf/±0 signs, NaN payload preservation for v_rcp_f64, NaN for v_rsq_f64(-x), <= 1 ULP for v_rcp_f64, and <= 2 ULP for v_rsq_f64.

Tested

  • Hardware & toolchain: NVIDIA GeForce RTX 4060 8 GB (driver 621.14, wave32), Intel Core i5-11400F, 32 GB RAM, Windows 11 Home build 26200, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5 (01b62ad4).
  • Pre-fix failure (unfixed origin/main 5ce558f with new test added, debug): cargo test -p xcl --lib gpu::dxil::lower_wide::rcp_d_tests::v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d -- --nocapture -> 0 passed; 1 failed (panicked: v_rcp_f64 must guard ±0 before dividing because unfixed Direct DXIL emitted bare fdiv double 1.000000e+00, ... with no fcmp oeq double or scaling).
  • Post-fix targeted (release, 4510bf84):
    • cargo test --release -p xcl --lib gpu::dxil::lower_wide -- --nocapture -> 4 passed; 0 failed; 0 ignored (including both v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d and gpu_direct_rcp_f64_and_rsq_f64_edges on RTX 4060).
    • XCL_DIRECT_DXIL=1 cargo test --release -p xcl --lib gpu_f64_roots -- --nocapture -> 1 passed; 0 failed; 0 ignored.
    • cargo test --release -p xcl --lib f64_programs_on_the_gpu -- --nocapture -> 1 passed; 0 failed; 0 ignored (wave32 HLSL vs Direct DXIL bit-for-bit match; wave64 skipped no wave64).
  • Full workspace (release, 4510bf84): cargo build --release --workspace + cargo test --release --workspace -- --test-threads=4 --nocapture -> 1156 passed, 0 failed, 46 ignored across 21 test binaries.
  • SKIPPED lines (27 total, all pre-existing on this wave32 Windows host without Graphics Tools / Developer Mode / heap windows):
    • 20 no wave64 skips (4 in gpu::recomp::lower::tests, 14 in gpu::recomp::lower_dxil::tests, 2 in gpu::recomp::tests).
    • 3 pre-existing wave32 per-lane form: refused skips in gpu::recomp::lower_dxil::tests (f16 arithmetic, f16 packed, f32 conversions).
    • 2 D3D12 debug layer skips (test_title_draws_with_a_root_signature_under_the_debug_layer: no d3d12SDKLayers.dll; uploads_start_from_each_subresources_state: 0x887A002D).
    • 1 shaders::tests::pso_measurement_compiles_an_exact_synthetic_record: cannot disable driver cache without Windows Developer Mode.
    • 1 heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows.

Regression run

Paired 60 s retail title runs with scripts/smoke.ps1 (release build, same title, clean working tree) on origin/main (cbc296ab) and this PR (4510bf84):

main:

xcl-smoke v1
commit: cbc296ab65a2661a61e3e2da109134bcd5319f60
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1608 (PresentX 1608)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14474 MB

this PR:

xcl-smoke v1
commit: 4510bf849fe4850894ef04ba8a227c2c4a6cd523
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1607 (PresentX 1607)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14481 MB

Not tested

  • AMD / RADV / Intel GPUs (where raw 1/NaN returning 0 and 1/DBL_MAX denormal flushing were originally observed in Recompiler: v_rcp_f64 and v_rsq_f64 keep NaN, infinities and denormals #530), wave64 hardware, adapters without double-precision support, D3D12 debug layer, heap windows, and Linux/Wine.
  • Whether the retail smoke title executes v_rcp_f64 or v_rsq_f64 under Direct DXIL; the paired smoke run is a regression check only.

AI use

Google Antigravity (Gemini 4 Neon) wrote the fix, tests, and this PR description; all builds, tests, and paired retail smoke runs were executed locally on the hardware listed above.

Direct DXIL lowering of v_rcp_f64 and v_rsq_f64 emitted raw unguarded double division (1.0 / a and 1.0 / sqrt_d(a)), unlike the HLSL path's rcp_d helper (xcl-dev#530). Drivers may return 0 for 1/NaN (e.g. v_rsq_f64 of negative numbers) or flush subnormal reciprocals such as 1/DBL_MAX to 0.

Add Lw::rcp_d in crates/xcl/src/gpu/dxil/lower_wide.rs handling NaN (preserving payload), ±0 -> ±inf, and ±inf -> ±0 explicitly, substituting 1.0 into the eager divide path for special inputs, and scaling finite operands into [0.5, 1) via frexp_exp_d and ldexp_d around ddiv(1.0, scaled) before rescaling back with ldexp_d. Wire both v_rcp_f64 and v_rsq_f64 through Lw::rcp_d and add DXIL validation/disassembly and wave32 GPU edge regression tests.

Part of xcl-dev#1269

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

Copy link
Copy Markdown

PR #2063 CPU/DXC verification

Tested

PR #2063 was open at 4510bf849fe4850894ef04ba8a227c2c4a6cd523 before and after the run. The public conversation showed no comment or review anchors. The assigned checkout was clean at that SHA.

Windows 11 Enterprise Evaluation 26200.9974, AMD Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175, rustc and Cargo 1.100.0 (x86_64-pc-windows-msvc), unoptimized test profile with debug info disabled. The test logged dxcompiler 1.9.2609.5 (01b62ad4) and dxil 1.9.2609.5 loaded from the explicit DXC directory.

$env:TEMP = 'L:/XclTooling/pr2063'
$env:TMP = 'L:/XclTooling/pr2063'
$env:CARGO_TARGET_DIR = 'L:/XclTooling/pr1947-1831ec4-t3-97236edf/target'
$env:CARGO_BUILD_JOBS = '2'
$env:CARGO_INCREMENTAL = '0'
$env:CARGO_PROFILE_DEV_DEBUG = '0'
$env:CARGO_PROFILE_TEST_DEBUG = '0'
$env:XCL_ADAPTER = 'A770'
$env:XCL_DXC_DIR = Join-Path $env:LOCALAPPDATA 'XclTooling/dxc-1.9.2609/bin/x64'
$env:PATH = (Join-Path $env:USERPROFILE '.cargo/bin') + ';' + $env:PATH
cargo test -p xcl --lib --jobs 2 gpu::dxil::lower_wide::rcp_d_tests::v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d -- --exact --nocapture

Result: 1 passed, 0 failed, 0 ignored, 0 skipped, 0 measured, 1,125 filtered out. Exit code 0. Cargo reported 1m 00s to finish the build and the harness reported 0.03s for the test. Wall time including build and test was 64.691s.

The fixture assembled separate V_RCP_F64 and V_RSQ_F64 kernels and translated, validated, and disassembled both in wave32 and wave64 mode. Split mode was disabled and the fixture used one memory window and sampler base zero. In each wave mode, all five disassembly checks passed.

  • Reciprocal contained fcmp oeq double (the zero-guard marker), @dx.op.unaryBits.i32(i32 33 (the exponent-extraction marker), and fdiv double 1.000000e+00,.
  • Reciprocal square root contained fmul double and select i1 in the text after the first matching reciprocal division (the rescaling and guard-selection markers).

Not tested

GPU dispatch, shader FP64 execution, numerical accuracy, and per-input behavior including signed zero, infinities, NaNs, and subnormals. This Arc has no shader FP64 support. The checked test covers DXIL structure and DXC acceptance. The separate GPU edge-case test, HLSL backend, workspace suite, and title smoke tests were outside this run.

AI use

T3 Code / Codex (gpt-6.2-sol) verified the PR head, ran the checked-in test, and recorded the result.

@JoaoPauluu

Copy link
Copy Markdown

A run with vkd3d-proton as the host D3D12. The DXIL goes through dxil-spirv and then the NVIDIA Vulkan driver, a different compiler from a native-Windows run: this PR's 2 new tests pass.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 4510bf8 as it is (merge base with main cbc296a), no uncommitted changes. The lib test binary runs under Wine, one test per process.
    • gpu_direct_rcp_f64_and_rsq_f64_edges: ok, not skipped
    • v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d: ok, not skipped

Not tested

  • the whole suite on this head, a retail title, native Windows, AMD.

AI use

  • Claude Code (Claude Opus 5.5) built the commit, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@DarkPanda69

Copy link
Copy Markdown

Native-wave64 AMD run on Windows, which this PR lists as not tested (#530's 1/NaN and denormal cases were seen on AMD; the reports above are CPU/DXC and NVIDIA).

Tested: pass, head 4510bf8 applied on its base main cbc296a, debug, Rust 1.100.0, Windows 11 Pro 26200, Ryzen 7 5800X, DXC 1.9.2602.17. cargo test -p xcl --lib -- gpu:: --test-threads=4:

  • AMD RX 9070 XT (RDNA4), driver 32.0.32029.2010, waves 32..64: 586 passed, 2 failed, 22 ignored. gpu_direct_rcp_f64_and_rsq_f64_edges ok on the GPU. The 2 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 and _3, the same as on main cbc296a without this PR on this card.
  • NVIDIA RTX 3060, driver 32.0.16.1088: 588 passed, 0 failed, 22 ignored. Same test ok.

Not tested: RDNA2/3, RADV.

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

@Parakeet-Wheat

Copy link
Copy Markdown

NVIDIA RTX 3070 on native Windows, head 4510bf8 on its base cbc296a (current main): the DXIL-structure test fails without the change and passes with it, the GPU edge-case test passes either way on this card, the whole suite passes, and the smoke pair is within the limits. This adds a native-Windows FP64 GPU run, the fail-before run, the whole-suite counts and the smoke pair to the reports above.

Tested

  • NVIDIA GeForce RTX 3070 8 GB (driver 610.88, 32-lane waves, shader FP64 present), Ryzen 5 5600X, 32 GB, Windows 11 26300, Rust 1.95.0 stable-x86_64-pc-windows-msvc, DXC 1.8.2502.11, retail 1.0.2.1. Head 4510bf8 (one commit on main cbc296a), checked out clean. Tests in the debug profile, smoke in release.
  • cargo test -p xcl --lib rcp_d_tests -- --nocapture --test-threads=1:
    • on cbc296a with the PR's lower_wide.rs but its three Lw hunks above the test module (v_rcp_f64, v_rsq_f64, rcp_d) reverted: 1 failed, 1 passed. v_rcp_f64_and_v_rsq_f64_use_guarded_scaled_rcp_d fails with v_rcp_f64 must guard ±0 before dividing. gpu_direct_rcp_f64_and_rsq_f64_edges passes there too, not skipped: this driver returns the expected values for the edge inputs even from the unguarded division, so on this card only the disassembly test tells the two apart;
    • on 4510bf8: 2 passed, 0 failed, not skipped.
  • .\scripts\test.ps1 (cargo build --workspace, then cargo test --workspace -- --test-threads=4) on 4510bf8: 1156 passed, 0 failed, 46 ignored, exit 0. SKIPPED, 25: 20 × no wave64, 3 × per-lane form refused (V_CVT_F16_F32, V_PK_MUL_F16, V_MUL_F16: "its gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's"), 1 × no heap windows, 1 × cannot disable the driver cache (Windows Developer Mode required).

Regression run (scripts/smoke.ps1 -Game <folder> -Seconds 60, the same title, back to back, menus, no input; unedited). main cbc296a:

xcl-smoke v1
commit: cbc296ab65a2661a61e3e2da109134bcd5319f60
gpu: NVIDIA GeForce RTX 3070
title: 3d7db37d
seconds: 60
exit: 0x0
frames shown: 1490 (PresentX 1490)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17929 MB

PR head 4510bf8:

xcl-smoke v1
commit: 4510bf849fe4850894ef04ba8a227c2c4a6cd523
gpu: NVIDIA GeForce RTX 3070
title: 3d7db37d
seconds: 60
exit: 0x0
frames shown: 1488 (PresentX 1488)
presents per 2 s (last 3): 59 59 17
device removals: 0
crashes: 0
peak commit: 17347 MB

That is 100 % of main's frames and 97 % of its peak commit. One run each; the 59 59 17 tail of the PR's run (against 60 60 60 on main) is not something I repeated.

Not tested

  • Native wave64 (the 20 no wave64 skips above), AMD, a world run (the smoke stays in the menus, so it does not show whether the title reaches a v_rcp_f64 or v_rsq_f64 on an edge operand), the tests on game data (XCL_SAMPLE_* not set).

AI use

  • Claude Code (Claude Sonnet 5.5) ran the tests and wrote this report. The account owner has not reviewed it.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@SlopTart

Copy link
Copy Markdown

Linux/RADV run, the configuration this PR's tester list is missing.

Tested (RX 7900 XT, RDNA3, RADV NAVI31, Mesa 26.2.5, Proton Experimental wine-11.0, DXC 1.9.2609.5, release build, tests under wine, vkd3d-proton.cache removed before each run; branch head 4510bf8 merged with main 629a30b2):

  • gpu_f64 set: 6 passed, 0 failed, default backend
  • gpu_f64 set: 6 passed, 0 failed, XCL_DIRECT_DXIL=1
  • the two new tests of this PR: 2 passed

The before/after on this stack is direct. On main at cbc296ab without this PR, gpu_f64_roots fails deterministically here with v_rsq_f64(-1.5e0) = 0e0, want NaN (same for -1.84e19, -1e-300, -1e300), and gpu_f64_math_random fails its bad.is_empty() assertion. With the Lw::rcp_d guard both pass. So the 1/NaN -> 0 behavior #530 describes does happen under Mesa 26.2.5/RADV as well, not only on the stacks reported there, and this PR's guard fixes it on AMD Linux too.

Not tested: split-wave64 mode, other Mesa versions, native Linux (non-wine) runs.

Counts only, no game data.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@raylitebull
raylitebull merged commit 8c9ad92 into xcl-dev:main Nov 25, 2026
2 checks passed
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.

8 participants