Skip to content

Recompiler: v_div_fmas_f32 scales by VCC and v_div_fixup_f32 handles NaN/zero/sign - #1144

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:recomp/div-fmas-and-fixup
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:recomp/div-fmas-and-fixup

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown

v_div_fmas_f32 emitted mad(s0, s1, s2) and ignored its implicit VCC operand, so lanes with VCC == 1 never got the 2^32 scale (ldexp_f(..., 32)).

div_fixup (v_div_fixup_f32) returned q unchanged when q was NaN (overflow in the division sequence), when n == ±0.0 with finite d, when exp(n) - exp(d) < -150 (underflow), and without setting the result sign to sign(d) ^ sign(n). This replaces div_fixup with an IEEE-754 bit-level implementation covering NaN (numerator before denominator), 0/0, Inf/Inf, x/0, Inf/x, x/Inf, 0/x, exp(n) - exp(d) < -150 underflow, q = NaN overflow, and copysign(q, sign(d) ^ sign(n)).

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: scale v_div_fmas_f32 by 2^32 via ldexp_f for lanes with VCC == 1; update div_fixup for NaN priority (numerator before denominator), zero/inf, exp(n) - exp(d) < -150 underflow, q = NaN overflow, and sign.
  • crates/xcl/src/gpu/recomp/tests.rs: add GPU tests gpu_div_fmas_f32_scales_by_vcc and gpu_div_fixup_f32_special_cases_and_sign (17 cases including numerator-over-denominator NaN priority and the -151 / -150 exponent difference boundary) across wave32 and split wave64 (verified failing before the fix).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 753 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib gpu_div_fmas_f32_scales_by_vcc and gpu_div_fixup_f32_special_cases_and_sign: both pass across wave32 and split wave64 (both failed on the GPU before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: eac652c3d11927169ea46739249ebcc188ad800e
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14620 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited emit.rs, wrote the fix and GPU tests, addressed the PortBridge hardware-probe feedback (n NaN priority and exp(n) - exp(d) < -150), ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug; DXC 1.8.2502.11.

  • cargo test -p xcl --lib div_fmas: 1 passed, 0 failed — gpu_div_fmas_f32_scales_by_vcc covers VCC scaling and the NaN/zero/sign fixup cases on real hardware.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@TobiasFornell

Copy link
Copy Markdown

AMD / Linux data point – the PR was tested on NVIDIA only, where native wave64 is not available.

Tested: RX 9070 XT (RDNA4, native wave64), Mesa RADV 26.3.1, CachyOS (kernel 7.3), Proton Experimental 11.0-20261112 Wine with vkd3d-proton v3.1 (scripts/linux build), DXC 1.9.2609, x86_64-pc-windows-msvc cross-build, release. Head 2aa8211 merged onto main 47efa2e (clean), together with #1158.

  • gpu_div_fmas_f32_scales_by_vcc and gpu_div_fixup_f32_special_cases_and_sign: pass, including the native wave64 path.
  • Same two tests with the emit.rs change reverted (tests kept): both fail, e.g. wave64 lane 1 (vcc=true) v_div_fmas_f32 – left: 1088421888, right: 1356857344. So the fix is confirmed on AMD hardware too.

Testing and write-up assisted by Claude Code (Claude Opus 5.5); runs on my own hardware, checked by me.

@akimfomin

akimfomin commented Nov 24, 2026 •

Copy link
Copy Markdown

PortBridge linked this to Lyrnc’s gfx1036 probe in VIctory #200 and Continued #762’s translator tests. I compiled the exact div_fixup helper from 2aa8211 in an isolated dispatch and read its output on WARP (DXC 1.9.2602.17).

  • With (q,d,n)=(0x40000000,0x5d800000,0x12000000) the exponent difference is −151. The helper returned 0x40000000; the AMD probe and Continued returned 0x00000000.
  • Replacing n with 0x12800000 gives −150. All three preserved 0x40000000.

Across 14 vectors, including 10 from the hardware probe, the Continued translator plus CPU interpreter matched 14/14. The exact XCL helper matched 9/14. A local HLSL control that prioritizes numerator NaN and applies exp(n)-exp(d)<-150 before handling quotient NaN matched all 14 on WARP. This was a helper-level check. PortBridge case. Synthetic inputs, AI-assisted with Codex.

I independently reran the same WARP probe on the published fix at eac652c. It matched 14/14 on the original vectors, up from 9/14 on 2aa8211. I also replayed the 17 rows in the updated gpu_div_fixup_f32_special_cases_and_sign fixture through the isolated helper. The updated helper matched 17/17, including the NaN payload checks and both signed zero cases, up from 13/17 on the earlier helper. Thanks for adding the regression cases.

…NaN/zero/sign

v_div_fmas_f32 emitted mad(s0, s1, s2) and ignored its implicit VCC
operand instead of scaling by 2^32 (ldexp_f(..., 32)) for lanes with
VCC == 1.

div_fixup returned q unchanged when q was NaN (overflow in the quotient
sequence), when n == +-0.0 with finite d, when exp(n) - exp(d) < -150
(underflow), and without forcing the result sign to sign(d) ^ sign(n).
Handle NaN (numerator before denominator), 0/0, Inf/Inf, x/0, Inf/x,
x/Inf, 0/x, exp(n) - exp(d) < -150 underflow, quotient NaN overflow,
and sign on the IEEE-754 bits directly, with GPU tests for both
instructions across wave32 and split wave64.

Assisted-by: OpenCode (Gemini 4 Neon)
@b3IKK
b3IKK force-pushed the recomp/div-fmas-and-fixup branch from 2aa8211 to eac652c Compare November 24, 2026 13:54
@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks @akimfomin! Updated \eac652c\ to check numerator NaN before denominator NaN and apply \exp(n) - exp(d) < -150\ underflow before quotient NaN, added GPU test cases for both (including the -151\ vs -150\ boundary), re-ran the full test suite (753 passed, 0 failed, 41 ignored) and refreshed the 60 s smoke block.

@JoaoPauluu

Copy link
Copy Markdown

A run on Linux with vkd3d-proton, where the shaders go through dxil-spirv to Vulkan instead of a Windows driver's DXIL compiler.

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 (DXIL → SPIR-V through dxil-spirv), DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. eac652c as it is (based on 930e8b4), no uncommitted changes. The lib test binary runs under Wine, one test per process (--exact).
  • gpu::recomp::tests::gpu_div_fmas_f32_scales_by_vcc: ok, no SKIPPED.
  • gpu::recomp::tests::gpu_div_fixup_f32_special_cases_and_sign: ok, no SKIPPED.

Not tested

  • whether the new tests fail without the fix on this stack; the rest of the suite on this head; native Windows; AMD / native wave64.

AI use

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

@TobiasFornell

Copy link
Copy Markdown

Re-tested the updated head eac652c (merged onto main 47efa2e, clean) on the same AMD setup: RX 9070 XT (RDNA4, native wave64), RADV 26.3.1, Proton Experimental 11.0-20261112 Wine, vkd3d-proton v3.1 (scripts/linux build), release.

  • gpu_div_fixup_f32_special_cases_and_sign (incl. the new −151/−150 underflow and NaN-order cases), gpu_div_fmas_f32_scales_by_vcc, gpu_divergence_through_exec: 3 passed, 0 failed, native wave64 path included.

Testing and write-up assisted by Claude Code (Claude Opus 5.5); runs on my own hardware, checked by me.

@regiumlepidi

Copy link
Copy Markdown

RTX 5070 Ti: eac652c merged into main 20cf130 passes its two tests on the default path; both still fail with XCL_DIRECT_DXIL=1

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (32-lane waves), driver 617.42; Ryzen 7 9800X3D; Windows 11 Pro 26H2, build 26300.9974; Rust 1.100.0 MSVC; Windows SDK 10.0.26100.0, DXC 1.8.2502.11; debug; no XCL_SAMPLE_*
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --nocapture, one run per cell; PR head eac652c3d11927169ea46739249ebcc188ad800e merged locally into 20cf13022f764208384c610c2ae6a3f4eed201a2, no conflicts (passed / failed / ignored):
tree default XCL_DIRECT_DXIL=1
main 20cf130 972 / 4 / 45 971 / 5 / 45
merge 974 / 4 / 45 971 / 7 / 45
  • Default path: gpu_div_fmas_f32_scales_by_vcc and gpu_div_fixup_f32_special_cases_and_sign pass. The 4 failures are fuzz_validate_and_gpu_0 to _3, which fail the same way on main with this driver (DXIL GPU fuzz tests reproducibly fail with E_OUTOFMEMORY on RTX 5060 #1905)
  • XCL_DIRECT_DXIL=1: those 4, offline_compute_pipeline_is_reused_by_a_second_process (fails on main too), and both new tests:
    • gpu_div_fmas_f32_scales_by_vcc: wave32 lane 1 (vcc=true), left 1088421888, right 1356857344
    • gpu_div_fixup_f32_special_cases_and_sign: wave32 lane 0 v_div_fixup_f32(q=0x7fc00000, d=0x1e3ce508, n=0x7e967699) = 0x7fc00000, want 0x7f800000
  • Negative control, only the PR's tests.rs changes applied to main 20cf130: both new tests fail on both paths with the values above
  • So with XCL_DIRECT_DXIL=1 the merged tree returns the same values for these two cases as main does (the PR's diff touches recomp/emit.rs and recomp/tests.rs)
  • 25 SKIPPED lines in the default run, 18 of them no wave64; none names the new tests

Not tested

  • the wave64 half of the new tests (gpu_waves() runs wave32 only on this adapter), release build, any retail title, other GPUs or drivers

AI use

  • Claude Code with Claude Sonnet 5.5: read the diff, ran the tests and wrote this report.

@regiumlepidi

Copy link
Copy Markdown

@b3IKK as in my report above, this PR's two tests still fail with XCL_DIRECT_DXIL=1: the direct translator has the same two bugs in dxil/lower_valu.rs and dxil/prelude.rs. I wrote the matching change for it (below): v_div_fmas_f32 scales by 2^32 where VCC is set (also in programs that direct f32 rounding), and div_fixup works on the bits in the same order as your HLSL helper.

Measured on an RTX 5070 Ti, driver 617.42, debug, with this PR's commit and the patch on main 62265d3: both new tests pass with and without XCL_DIRECT_DXIL=1; cargo test --workspace --locked --no-fail-fast -- --test-threads=4 gives 984 / 4 / 45 (default) and 983 / 5 / 45 (direct), the failures being the four fuzz_validate_and_gpu_* of #1905 and, on direct, offline_compute_pipeline_is_reused_by_a_second_process, which fail on main here too. Not tested: wave64, a v_div_fmas_f32 in a directed-rounding program, any retail title.

Take it into this PR if you like, or say if you would rather it go in a separate PR stacked on this one.

Patch for the direct translator (applies with git apply on this PR merged into main 8e87fc7)
diff --git a/crates/xcl/src/gpu/dxil/lower_valu.rs b/crates/xcl/src/gpu/dxil/lower_valu.rs
index beafdcd..10898fe 100644
--- a/crates/xcl/src/gpu/dxil/lower_valu.rs
+++ b/crates/xcl/src/gpu/dxil/lower_valu.rs
@@ -627,10 +627,16 @@ impl Lw {
                     _ => self.fma_legacy(x, y, cf),
                 })
             }
-            "v_fma_f32" | "v_mad_f32" | "v_fma_legacy_f32" | "v_div_fmas_f32" => {
+            "v_div_fmas_f32" => {
+                let (x, y, w) = (fs!(0), fs!(1), fs!(2));
+                let e = self.fmad(x, y, w);
+                let e = self.div_fmas_scale(&u, e)?;
+                fr!(e)
+            }
+            "v_fma_f32" | "v_mad_f32" | "v_fma_legacy_f32" => {
                 let (x, y, w) = (fs!(0), fs!(1), fs!(2));
                 fr!(match name {
-                    "v_fma_f32" | "v_div_fmas_f32" => self.fmad(x, y, w),
+                    "v_fma_f32" => self.fmad(x, y, w),
                     "v_mad_f32" => self.mad_nf(x, y, w),
                     _ => self.fma_legacy(x, y, w),
                 })
@@ -664,8 +670,10 @@ impl Lw {
                 fr!(self.cube(name, x, y, w))
             }
             "v_div_fixup_f32" => {
-                let (q, dd, nn) = (fs!(0), fs!(1), fs!(2));
-                fr!(self.div_fixup(q, dd, nn))
+                // on the bits: NaN payloads and denormal operands reach the checks unchanged
+                let (q, dd, nn) = (self.fsrc_bits(&m, 0, a), self.fsrc_bits(&m, 1, b), self.fsrc_bits(&m, 2, c));
+                let r = self.div_fixup(q, dd, nn);
+                fr!(self.f(r))
             }
             // clamp: unsigned saturation
             "v_add_nc_u32" | "v_add3_u32" if m.clamp => {
@@ -1061,6 +1069,15 @@ impl Lw {
         Ok(())
     }
 
+    /// `v_div_fmas_f32`: the fma result scaled by 2^32 in lanes whose VCC bit (source 3) is set.
+    fn div_fmas_scale(&mut self, u: &[Src], e: Value) -> LResult<Value> {
+        let Some(Src::Pair(l, h)) = u.get(3).copied() else { return Err("v_div_fmas_f32: VCC".into()) };
+        let bit = self.lane_of(l, h);
+        let k = self.c(32);
+        let scaled = self.ldexp_f(e, k);
+        Ok(self.sel(bit, scaled, e))
+    }
+
     /// Source bits with abs / neg applied on the bits (no float operation: denormals stay).
     fn fsrc_bits(&mut self, m: &Mods, i: usize, u: Value) -> Value {
         let mut s = u;
@@ -1114,10 +1131,15 @@ impl Lw {
                 let zz = self.fc(0.0);
                 self.sel(lz, zz, r)
             }
-            "v_fma_f32" | "v_div_fmas_f32" => {
+            "v_fma_f32" => {
                 let n = self.fmad(fa, fb, fc);
                 self.rm_fma(ba, bb, bc, n)
             }
+            "v_div_fmas_f32" => {
+                let n = self.fmad(fa, fb, fc);
+                let r = self.rm_fma(ba, bb, bc, n);
+                self.div_fmas_scale(u, r)?
+            }
             "v_fma_legacy_f32" => {
                 let n = self.fmad(fa, fb, fc);
                 let r = self.rm_fma(ba, bb, bc, n);
diff --git a/crates/xcl/src/gpu/dxil/prelude.rs b/crates/xcl/src/gpu/dxil/prelude.rs
index a171fd0..08a9d1a 100644
--- a/crates/xcl/src/gpu/dxil/prelude.rs
+++ b/crates/xcl/src/gpu/dxil/prelude.rs
@@ -192,20 +192,44 @@ impl Lw {
         let y = self.fmin(y, c);
         self.fmax(x, y)
     }
-    /// `div_fixup`
+    /// `div_fixup` on f32 bits (quotient, denominator, numerator), as the HLSL path's helper:
+    /// NaN (numerator first), 0/0 and Inf/Inf, x/0 and Inf/x, x/Inf and 0/x, underflow when
+    /// exp(n) - exp(d) < -150, a NaN quotient as overflow, and the sign sign(d) ^ sign(n).
     pub fn div_fixup(&mut self, q: Value, d: Value, n: Value) -> Value {
-        let a = self.isnan(d);
-        let b = self.isnan(n);
-        let z = self.fc(0.0);
-        let dz = self.fcmp(Pred::FOEq, d, z);
-        let di = self.isinf(d);
-        let ni = self.isinf(n);
-        let x = self.lor(a, b);
-        let y = self.lor(di, ni);
-        let y = self.lor(dz, y);
-        let c = self.lor(x, y);
-        let r = self.fdiv(n, d);
-        self.sel(c, r, q)
+        let sign = self.xor(d, n);
+        let sign = self.andc(sign, 0x8000_0000);
+        let (qa, da, na) = (self.andc(q, 0x7FFF_FFFF), self.andc(d, 0x7FFF_FFFF), self.andc(n, 0x7FFF_FFFF));
+        let inf = self.orc(sign, 0x7F80_0000);
+        // lowest priority first: each select overrides the ones before it
+        let r = self.or(sign, qa);
+        let qnan = self.cmpc(Pred::UGt, qa, 0x7F80_0000);
+        let r = self.sel(qnan, inf, r);
+        let en = self.lshrc(n, 23);
+        let en = self.andc(en, 0xFF);
+        let ed = self.lshrc(d, 23);
+        let ed = self.andc(ed, 0xFF);
+        let diff = self.sub(en, ed);
+        let under = self.cmpc(Pred::SLt, diff, -150i32 as u32);
+        let r = self.sel(under, sign, r);
+        let dinf = self.cmpc(Pred::Eq, da, 0x7F80_0000);
+        let ninf = self.cmpc(Pred::Eq, na, 0x7F80_0000);
+        let dz = self.cmpc(Pred::Eq, da, 0);
+        let nz = self.cmpc(Pred::Eq, na, 0);
+        let to_zero = self.lor(dinf, nz);
+        let r = self.sel(to_zero, sign, r);
+        let to_inf = self.lor(dz, ninf);
+        let r = self.sel(to_inf, inf, r);
+        let zz = self.land(dz, nz);
+        let ii = self.land(dinf, ninf);
+        let invalid = self.lor(zz, ii);
+        let k = self.c(0xFFC0_0000);
+        let r = self.sel(invalid, k, r);
+        let dnan = self.cmpc(Pred::UGt, da, 0x7F80_0000);
+        let dq = self.orc(d, 0x0040_0000);
+        let r = self.sel(dnan, dq, r);
+        let nnan = self.cmpc(Pred::UGt, na, 0x7F80_0000);
+        let nq = self.orc(n, 0x0040_0000);
+        self.sel(nnan, nq, r)
     }
     /// cube_id / cube_sc / cube_tc / cube_ma
     pub fn cube(&mut self, name: &str, x: Value, y: Value, z: Value) -> Value {

Claude Code with Claude Opus 5.5 wrote the patch, ran the tests and wrote this comment.

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.

6 participants