Recompiler: handle overflow, zero, and huge-denominator fallbacks in div_fixup #3312

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3312 into main
Kalmarv commented 2026-11-29 07:07:49 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T07:07:49Z.


Problem

Both Direct DXIL (Lw::div_fixup in crates/xcl/src/gpu/dxil/prelude.rs) and HLSL (div_fixup in crates/xcl/src/gpu/recomp/emit.rs) intentionally skip v_div_scale_f32 pre-scaling (2^±32) and pass the unscaled v_div_fmas_f32 quotient q directly into v_div_fixup_f32 / v_div_fixup_f16. Previously div_fixup only fell back to IEEE n / d when d or n was NaN, 0, or ±Inf (and did not check n == 0 at all). That left three real classes of finite RDNA2 divisions broken:

  • Finite overflowing quotients (e.g. 1e30 / 1e-20): unscaled v_rcp_f32 / v_div_fmas_f32 produces q = NaN or wrong-sign Inf, which div_fixup passed through unchanged instead of ±Inf.
  • Huge normal denominators (|d| > 2^126, e.g. 3e38 / 3e38): 1.0 / d is subnormal and flushes to 0.0 under shader FTZ, making both q and raw shader n / d (n * rcp(d)) evaluate to 0.0 instead of 1.0.
  • Zero numerators and wrong-sign zeros (e.g. -0.0 / 2.0, 0.0 / -2.0): n == 0 was not in the fallback predicate, so q could carry the wrong zero sign instead of IEEE n / d.

Fix

  • crates/xcl/src/gpu/dxil/prelude.rs (Lw::div_fixup) and crates/xcl/src/gpu/recomp/emit.rs (div_fixup):
    • Extend the fallback predicate to also trigger on n == 0.0, isnan(q), isinf(q), and q == 0.0, while keeping normal finite non-zero q passthrough untouched.
    • Scale both n and d by 2^-32 (0x2F80_0000) when |d| >= 2^96 (0x6F80_0000) before dividing ((n * s) / (d * s), using precise temporaries in HLSL so DXC cannot algebraically cancel s). Scaling by a power of two is exact on normal mantissas, keeps |d * s| < 2^96 so rcp(d * s) never flushes to zero, and preserves all ±0, ±Inf, and NaN signs and semantics.
  • crates/xcl/src/gpu/dxil/core_tests.rs:
    • Add div_fixup_f32_and_f16_special_and_overflow_cases_on_gpu (guarded by runner::wave32_supported()), running both Direct DXIL and HLSL on the GPU across the full 9-instruction RDNA2 v_div_scale_f32 -> v_rcp_f32 -> v_fma_f32 -> v_mul_f32 -> v_fma_f32 -> v_div_fmas_f32 -> v_div_fixup_f32 sequence (16 lanes covering normal, signed overflow, huge denominator ±3e38 / 3e38, signed underflow, signed zeros, div-by-zero, and 0 / 0), plus standalone v_div_fixup_f32 (6 lanes) and v_div_fixup_f16 (6 lanes).

Tested

Tested on Windows 11 Pro (Build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), DXC 1.9.2609.5, rustc 1.99.0.

  • cargo check --workspace --all-targets: passed, 0 errors.
  • Targeted GPU tests (cargo test --lib -- div_fixup_f32_and_f16_special_and_overflow_cases_on_gpu div_fixup_f16_instruction --test-threads=1): 2 passed, 0 failed across both Direct DXIL and HLSL on RTX 5070 Ti.
  • Full workspace GPU suite (cargo test --workspace -- --test-threads=4): 2609 passed, 0 failed, 49 ignored (27 runtime-skipped by capability guards).
  • Real-title 150 s smoke (xcl-smoke), main-ancestor baseline vs this branch (0 device removals, 0 crashes, 0 minidumps, 0 System display events):

Baseline (f0e8fdd5):

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

Branch (cb660cdd):

xcl-smoke v1
commit: cb660cdd7f5ccac17a369579c3440010d3186e54
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1538 (PresentX 1538)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 34631 MB

Not tested

  • v_div_fixup_f16 rounding under default RNE mode (currently routes through existing to_f16; changing default f16 rounding to RNE is tracked separately).
  • v_div_scale_f64 / v_div_fmas_f64 / v_div_fixup_f64 (out of scope, tracked separately).
  • AMD and Intel GPU hardware.

AI use

Used an AI coding assistant to investigate the div_fixup fallback edge cases, write the Direct DXIL / HLSL fix and GPU parity test, and verify all tests and smoke runs locally.

<!-- greyfinch-sync:ivall/xcl:issue:5548505264 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T07:07:49Z. --- **Problem** Both Direct DXIL (`Lw::div_fixup` in `crates/xcl/src/gpu/dxil/prelude.rs`) and HLSL (`div_fixup` in `crates/xcl/src/gpu/recomp/emit.rs`) intentionally skip `v_div_scale_f32` pre-scaling (`2^±32`) and pass the unscaled `v_div_fmas_f32` quotient `q` directly into `v_div_fixup_f32` / `v_div_fixup_f16`. Previously `div_fixup` only fell back to IEEE `n / d` when `d` or `n` was `NaN`, `0`, or `±Inf` (and did not check `n == 0` at all). That left three real classes of finite RDNA2 divisions broken: - **Finite overflowing quotients** (e.g. `1e30 / 1e-20`): unscaled `v_rcp_f32` / `v_div_fmas_f32` produces `q = NaN` or wrong-sign `Inf`, which `div_fixup` passed through unchanged instead of `±Inf`. - **Huge normal denominators** (`|d| > 2^126`, e.g. `3e38 / 3e38`): `1.0 / d` is subnormal and flushes to `0.0` under shader FTZ, making both `q` and raw shader `n / d` (`n * rcp(d)`) evaluate to `0.0` instead of `1.0`. - **Zero numerators and wrong-sign zeros** (e.g. `-0.0 / 2.0`, `0.0 / -2.0`): `n == 0` was not in the fallback predicate, so `q` could carry the wrong zero sign instead of IEEE `n / d`. **Fix** - `crates/xcl/src/gpu/dxil/prelude.rs` (`Lw::div_fixup`) and `crates/xcl/src/gpu/recomp/emit.rs` (`div_fixup`): - Extend the fallback predicate to also trigger on `n == 0.0`, `isnan(q)`, `isinf(q)`, and `q == 0.0`, while keeping normal finite non-zero `q` passthrough untouched. - Scale both `n` and `d` by `2^-32` (`0x2F80_0000`) when `|d| >= 2^96` (`0x6F80_0000`) before dividing (`(n * s) / (d * s)`, using `precise` temporaries in HLSL so DXC cannot algebraically cancel `s`). Scaling by a power of two is exact on normal mantissas, keeps `|d * s| < 2^96` so `rcp(d * s)` never flushes to zero, and preserves all `±0`, `±Inf`, and `NaN` signs and semantics. - `crates/xcl/src/gpu/dxil/core_tests.rs`: - Add `div_fixup_f32_and_f16_special_and_overflow_cases_on_gpu` (guarded by `runner::wave32_supported()`), running both Direct DXIL and HLSL on the GPU across the full 9-instruction RDNA2 `v_div_scale_f32 -> v_rcp_f32 -> v_fma_f32 -> v_mul_f32 -> v_fma_f32 -> v_div_fmas_f32 -> v_div_fixup_f32` sequence (16 lanes covering normal, signed overflow, huge denominator `±3e38 / 3e38`, signed underflow, signed zeros, div-by-zero, and `0 / 0`), plus standalone `v_div_fixup_f32` (6 lanes) and `v_div_fixup_f16` (6 lanes). **Tested** Tested on Windows 11 Pro (Build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver `32.0.16.1074`), DXC `1.9.2609.5`, `rustc 1.99.0`. - `cargo check --workspace --all-targets`: passed, 0 errors. - Targeted GPU tests (`cargo test --lib -- div_fixup_f32_and_f16_special_and_overflow_cases_on_gpu div_fixup_f16_instruction --test-threads=1`): 2 passed, 0 failed across both Direct DXIL and HLSL on RTX 5070 Ti. - Full workspace GPU suite (`cargo test --workspace -- --test-threads=4`): 2609 passed, 0 failed, 49 ignored (27 runtime-skipped by capability guards). - Real-title 150 s smoke (`xcl-smoke`), `main`-ancestor baseline vs this branch (0 device removals, 0 crashes, 0 minidumps, 0 System display events): Baseline (`f0e8fdd5`): ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` Branch (`cb660cdd`): ``` xcl-smoke v1 commit: cb660cdd7f5ccac17a369579c3440010d3186e54 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1538 (PresentX 1538) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 34631 MB ``` **Not tested** - `v_div_fixup_f16` rounding under default RNE mode (currently routes through existing `to_f16`; changing default f16 rounding to RNE is tracked separately). - `v_div_scale_f64` / `v_div_fmas_f64` / `v_div_fixup_f64` (out of scope, tracked separately). - AMD and Intel GPU hardware. **AI use** Used an AI coding assistant to investigate the `div_fixup` fallback edge cases, write the Direct DXIL / HLSL fix and GPU parity test, and verify all tests and smoke runs locally.
Foniass commented 2026-11-29 14:59:02 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:59:02Z.


Review (head cb660cdd, diff read, not a merge-check):

  • The fallbacks look right, and both backends use the same predicate.
  • The GPU test calls dxc::compile(...).expect(...) without a dxc::location() skip (only skip_test("no wave32 GPU")), so on a machine without DXC it panics instead of printing SKIPPED.
  • It overlaps the open #1144 (same function, v_div_fixup_f32 NaN/zero handling, another approach), which the description does not mention.
  • AI use names no tool or model.

Suggested verdict: not yet (DXC skip, model; settle the overlap with #1144).

(Review written with Claude Code, Claude Opus 5.5.)

<!-- greyfinch-sync:ivall/xcl:comment:7257727360 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7257727360). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T14:59:02Z. --- Review (head cb660cdd, diff read, not a merge-check): - The fallbacks look right, and both backends use the same predicate. - The GPU test calls `dxc::compile(...).expect(...)` without a `dxc::location()` skip (only `skip_test("no wave32 GPU")`), so on a machine without DXC it panics instead of printing SKIPPED. - It overlaps the open #1144 (same function, `v_div_fixup_f32` NaN/zero handling, another approach), which the description does not mention. - AI use names no tool or model. Suggested verdict: not yet (DXC skip, model; settle the overlap with #1144). (Review written with Claude Code, Claude Opus 5.5.)
Foniass commented 2026-12-01 04:13:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:13:24Z.


Re-review (head 0caeca23, diff read, not a merge-check: the PR description check fails, so not built or run here yet). Follow-up to my review of cb660cdd.

  • Since cb660cdd the fallback code is unchanged: the push is a rebase onto d0832e61 plus the skip_test("no DXC") asked for last time (core_tests.rs ~1666). The smoke block for the PR is still on cb660cdd, which is why PR description fails; it needs rerunning on the head.
  • AI use still names no model, and the overlap with the open #1144 (same function, a scaling approach) still isn't mentioned.
  • The test's expectations are XCL's behaviour, not RDNA2's. For lanes ~1727, ~1730 and ~1737-1739 the test expects n/d, while RDNA2's v_div_fixup returns the sign-adjusted |q| there (NaN, 0, NaN, +Inf). That's right for XCL, whose v_div_scale doesn't scale, but the comments should say so.
  • The exact 1.0 expected for 3e38/3e38 (~1712, ~1730) depends on the driver's fdiv precision (D3D allows 2.5 ulp), and it was only checked on NVIDIA.
  • Every v_div_fixup_f32 / _f16 gains about 10 ALU ops on the default path; not measured.

Fine: on main the unscaled q gives NaN for 1e30/1e-20, 0 for 3e38/3e38 and +0 for -0/2, and the new fallbacks fix all three. The 2^-32 scaling for |d| >= 2^96 can't change a result that's still representable, both backends use the same predicate, the f16 path computes in f32 in both, and the expected values are IEEE-correct.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7300506183 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7300506183). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T04:13:24Z. --- Re-review (head 0caeca23, diff read, not a merge-check: the `PR description` check fails, so not built or run here yet). Follow-up to my review of cb660cdd. - **Since cb660cdd the fallback code is unchanged:** the push is a rebase onto d0832e61 plus the `skip_test("no DXC")` asked for last time (`core_tests.rs` ~1666). The smoke block for the PR is still on cb660cdd, which is why `PR description` fails; it needs rerunning on the head. - AI use still names no model, and the overlap with the open #1144 (same function, a scaling approach) still isn't mentioned. - **The test's expectations are XCL's behaviour, not RDNA2's.** For lanes ~1727, ~1730 and ~1737-1739 the test expects `n/d`, while RDNA2's `v_div_fixup` returns the sign-adjusted `|q|` there (NaN, 0, NaN, +Inf). That's right for XCL, whose `v_div_scale` doesn't scale, but the comments should say so. - The exact 1.0 expected for `3e38/3e38` (~1712, ~1730) depends on the driver's fdiv precision (D3D allows 2.5 ulp), and it was only checked on NVIDIA. - Every `v_div_fixup_f32` / `_f16` gains about 10 ALU ops on the default path; not measured. Fine: on main the unscaled `q` gives NaN for `1e30/1e-20`, 0 for `3e38/3e38` and +0 for `-0/2`, and the new fallbacks fix all three. The 2^-32 scaling for `|d| >= 2^96` can't change a result that's still representable, both backends use the same predicate, the f16 path computes in f32 in both, and the expected values are IEEE-correct. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Foniass commented 2026-12-01 16:32:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T16:32:21Z.


Test report (not a merge-check: PR description red)
base: b2b1f109d2
head: 0caeca231b
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2903 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: not repeated here; my review of this same head is at https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7300506183, and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run.
assessment: merges, builds and passes the whole suite on current main (0 failed); the findings in my review stand
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7317773275 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7317773275). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T16:32:21Z. --- Test report (not a merge-check: `PR description` red) base: b2b1f109d27e746a62ec95b04dd1f9924452e682 head: 0caeca231b9da81afb88b3dfe08ac25c532e3348 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2903 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none review: not repeated here; my review of this same head is at https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7300506183, and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main. performance: no smoke run. assessment: merges, builds and passes the whole suite on current main (0 failed); the findings in my review stand hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
bmjask commented 2026-12-01 22:45:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T22:45:56Z.

Code review location: crates/xcl/src/gpu/dxil/prelude.rs; line 284

@@ -268,19 +268,38 @@ impl Lw {
         let y = self.fmin(y, c);
         self.fmax(x, y)
     }
-    /// `div_fixup`
+    /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled
+    /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`).
+    /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush
+    /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`).
     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 nz = self.fcmp(Pred::FOEq, n, z);
+        let qz = self.fcmp(Pred::FOEq, q, 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);
+        let qi = self.isinf(q);

isinf(q) now also triggers the fallback on the f16 path (v_div_fixup_f16, lower_wide.rs:484 / emit.rs:3653). A real f16 overflow is recomputed as a finite f32 n/d and narrowed through legacy f32tof16, which the repo documents as round-toward-zero. With n=60000, d=0.5, hardware gives +Inf (0x7C00), and before this PR q passed through as Inf. Now it's to_f16(120000.0f) = 0x7BFF (65504), so results that should overflow are clamped to f16 max.

<!-- greyfinch-sync:ivall/xcl:review-comment:4762100586 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#discussion_r4762100586). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T22:45:56Z. **Code review location:** `crates/xcl/src/gpu/dxil/prelude.rs`; line 284 ~~~~diff @@ -268,19 +268,38 @@ impl Lw { let y = self.fmin(y, c); self.fmax(x, y) } - /// `div_fixup` + /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled + /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`). + /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush + /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`). 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 nz = self.fcmp(Pred::FOEq, n, z); + let qz = self.fcmp(Pred::FOEq, q, 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); + let qi = self.isinf(q); ~~~~ --- `isinf(q)` now also triggers the fallback on the f16 path (`v_div_fixup_f16`, lower_wide.rs:484 / emit.rs:3653). A real f16 overflow is recomputed as a finite f32 n/d and narrowed through legacy `f32tof16`, which the repo documents as round-toward-zero. With n=60000, d=0.5, hardware gives +Inf (0x7C00), and before this PR q passed through as Inf. Now it's to_f16(120000.0f) = 0x7BFF (65504), so results that should overflow are clamped to f16 max.
bmjask commented 2026-12-01 22:45:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T22:45:56Z.

Review: COMMENTED


Review of 0caeca23: 3 finding(s). 3 are inline.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6141551954 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#pullrequestreview-6141551954). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T22:45:56Z. **Review:** COMMENTED --- Review of `0caeca23`: 3 finding(s). 3 are inline. --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
bmjask commented 2026-12-01 22:45:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T22:45:57Z.

Code review location: crates/xcl/src/gpu/dxil/prelude.rs; line 293

@@ -268,19 +268,38 @@ impl Lw {
         let y = self.fmin(y, c);
         self.fmax(x, y)
     }
-    /// `div_fixup`
+    /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled
+    /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`).
+    /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush
+    /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`).
     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 nz = self.fcmp(Pred::FOEq, n, z);
+        let qz = self.fcmp(Pred::FOEq, q, 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);
+        let qi = self.isinf(q);
+        let qn = self.isnan(q);
+        let c = self.lor(a, b);
+        let c = self.lor(c, dz);
+        let c = self.lor(c, nz);
+        let c = self.lor(c, di);
+        let c = self.lor(c, ni);
+        let c = self.lor(c, qn);
+        let c = self.lor(c, qi);
+        let c = self.lor(c, qz);

For finite non-zero n and d, the new q == 0 / isinf(q) / isnan(q) fallbacks replace q with n/d. Real v_div_fixup_f32 returns sign_out ? -|q| : |q| there, so a standalone fixup now diverges from hardware. q=+0 with d=n=3e38 gives +0.0 on RDNA2 but 1.0 here, and NaN q with d=2, n=7 gives NaN on hardware but 3.5 here. The tests (fix32 lane 4, fix16 lanes 1/2) pin the non-hardware results. Related, in the unchanged passthrough (sel(c, r, q), ~303): q is returned unchanged instead of with sign_out applied, so (q=+3.5, d=2, n=-7) gives +3.5 where RDNA2 gives -3.5.

<!-- greyfinch-sync:ivall/xcl:review-comment:4762100597 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#discussion_r4762100597). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T22:45:57Z. **Code review location:** `crates/xcl/src/gpu/dxil/prelude.rs`; line 293 ~~~~diff @@ -268,19 +268,38 @@ impl Lw { let y = self.fmin(y, c); self.fmax(x, y) } - /// `div_fixup` + /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled + /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`). + /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush + /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`). 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 nz = self.fcmp(Pred::FOEq, n, z); + let qz = self.fcmp(Pred::FOEq, q, 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); + let qi = self.isinf(q); + let qn = self.isnan(q); + let c = self.lor(a, b); + let c = self.lor(c, dz); + let c = self.lor(c, nz); + let c = self.lor(c, di); + let c = self.lor(c, ni); + let c = self.lor(c, qn); + let c = self.lor(c, qi); + let c = self.lor(c, qz); ~~~~ --- For finite non-zero n and d, the new q == 0 / isinf(q) / isnan(q) fallbacks replace q with n/d. Real `v_div_fixup_f32` returns `sign_out ? -|q| : |q|` there, so a standalone fixup now diverges from hardware. q=+0 with d=n=3e38 gives +0.0 on RDNA2 but 1.0 here, and NaN q with d=2, n=7 gives NaN on hardware but 3.5 here. The tests (fix32 lane 4, fix16 lanes 1/2) pin the non-hardware results. Related, in the unchanged passthrough (`sel(c, r, q)`, ~303): q is returned unchanged instead of with sign_out applied, so (q=+3.5, d=2, n=-7) gives +3.5 where RDNA2 gives -3.5.
bmjask commented 2026-12-01 22:45:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T22:45:57Z.

Code review location: crates/xcl/src/gpu/dxil/prelude.rs; line 301

@@ -268,19 +268,38 @@ impl Lw {
         let y = self.fmin(y, c);
         self.fmax(x, y)
     }
-    /// `div_fixup`
+    /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled
+    /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`).
+    /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush
+    /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`).
     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 nz = self.fcmp(Pred::FOEq, n, z);
+        let qz = self.fcmp(Pred::FOEq, q, 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);
+        let qi = self.isinf(q);
+        let qn = self.isnan(q);
+        let c = self.lor(a, b);
+        let c = self.lor(c, dz);
+        let c = self.lor(c, nz);
+        let c = self.lor(c, di);
+        let c = self.lor(c, ni);
+        let c = self.lor(c, qn);
+        let c = self.lor(c, qi);
+        let c = self.lor(c, qz);
+        let ad = self.fabs(d);
+        let big = self.fc(f32::from_bits(0x6F80_0000)); // 2^96
+        let hi_d = self.fcmp(Pred::FOGe, ad, big);
+        let s_dn = self.fc(f32::from_bits(0x2F80_0000)); // 2^-32
+        let one = self.fc(1.0);
+        let s = self.sel(hi_d, s_dn, one);
+        let ns = self.fmul(n, s);
+        let ds = self.fmul(d, s);

Not certain about this one: the HLSL backend marks ns/ds/r as precise, which this module mirrors with self.s.require(Feature::DisableMathRefactoring) (see mad_nf, ~242), but Direct DXIL div_fixup doesn't. Without FMad, the driver may refactor (ns)/(ds) back to n/d, so 3e38/3e38 becomes n*rcp(3e38) = 0 in Direct DXIL while HLSL returns 1.0.

<!-- greyfinch-sync:ivall/xcl:review-comment:4762100603 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#discussion_r4762100603). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T22:45:57Z. **Code review location:** `crates/xcl/src/gpu/dxil/prelude.rs`; line 301 ~~~~diff @@ -268,19 +268,38 @@ impl Lw { let y = self.fmin(y, c); self.fmax(x, y) } - /// `div_fixup` + /// `div_fixup`: fall back to IEEE `n / d` on special operands or when the unscaled + /// `v_div_scale` / `v_div_fmas` quotient overflowed/underflowed (`NaN`, `±Inf`, `±0.0`). + /// Scales `n` and `d` by `2^-32` when `|d| >= 2^96` so `fdiv` (`n * rcp(d)`) cannot flush + /// `rcp(d)` to zero for huge normal denominators (`|d| > 2^126`). 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 nz = self.fcmp(Pred::FOEq, n, z); + let qz = self.fcmp(Pred::FOEq, q, 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); + let qi = self.isinf(q); + let qn = self.isnan(q); + let c = self.lor(a, b); + let c = self.lor(c, dz); + let c = self.lor(c, nz); + let c = self.lor(c, di); + let c = self.lor(c, ni); + let c = self.lor(c, qn); + let c = self.lor(c, qi); + let c = self.lor(c, qz); + let ad = self.fabs(d); + let big = self.fc(f32::from_bits(0x6F80_0000)); // 2^96 + let hi_d = self.fcmp(Pred::FOGe, ad, big); + let s_dn = self.fc(f32::from_bits(0x2F80_0000)); // 2^-32 + let one = self.fc(1.0); + let s = self.sel(hi_d, s_dn, one); + let ns = self.fmul(n, s); + let ds = self.fmul(d, s); ~~~~ --- Not certain about this one: the HLSL backend marks `ns`/`ds`/`r` as `precise`, which this module mirrors with `self.s.require(Feature::DisableMathRefactoring)` (see `mad_nf`, ~242), but Direct DXIL `div_fixup` doesn't. Without FMad, the driver may refactor (n*s)/(d*s) back to n/d, so 3e38/3e38 becomes n*rcp(3e38) = 0 in Direct DXIL while HLSL returns 1.0.
Kalmarv commented 2026-12-02 19:39:08 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:39:08Z.


Closing: duplicate of earlier #1144 (#1607 D208) and review showed the fallback tests diverged from RDNA2 hardware behaviour.

<!-- greyfinch-sync:ivall/xcl:comment:7356510261 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3312#issuecomment-7356510261). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T19:39:08Z. --- Closing: duplicate of earlier #1144 (#1607 D208) and review showed the fallback tests diverged from RDNA2 hardware behaviour.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3312
No description provided.