Recompiler: handle overflow, zero, and huge-denominator fallbacks in div_fixup #3312
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3312"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T07:07:49Z.
Problem
Both Direct DXIL (
Lw::div_fixupincrates/xcl/src/gpu/dxil/prelude.rs) and HLSL (div_fixupincrates/xcl/src/gpu/recomp/emit.rs) intentionally skipv_div_scale_f32pre-scaling (2^±32) and pass the unscaledv_div_fmas_f32quotientqdirectly intov_div_fixup_f32/v_div_fixup_f16. Previouslydiv_fixuponly fell back to IEEEn / dwhendornwasNaN,0, or±Inf(and did not checkn == 0at all). That left three real classes of finite RDNA2 divisions broken:1e30 / 1e-20): unscaledv_rcp_f32/v_div_fmas_f32producesq = NaNor wrong-signInf, whichdiv_fixuppassed through unchanged instead of±Inf.|d| > 2^126, e.g.3e38 / 3e38):1.0 / dis subnormal and flushes to0.0under shader FTZ, making bothqand raw shadern / d(n * rcp(d)) evaluate to0.0instead of1.0.-0.0 / 2.0,0.0 / -2.0):n == 0was not in the fallback predicate, soqcould carry the wrong zero sign instead of IEEEn / d.Fix
crates/xcl/src/gpu/dxil/prelude.rs(Lw::div_fixup) andcrates/xcl/src/gpu/recomp/emit.rs(div_fixup):n == 0.0,isnan(q),isinf(q), andq == 0.0, while keeping normal finite non-zeroqpassthrough untouched.nanddby2^-32(0x2F80_0000) when|d| >= 2^96(0x6F80_0000) before dividing ((n * s) / (d * s), usingprecisetemporaries in HLSL so DXC cannot algebraically cancels). Scaling by a power of two is exact on normal mantissas, keeps|d * s| < 2^96sorcp(d * s)never flushes to zero, and preserves all±0,±Inf, andNaNsigns and semantics.crates/xcl/src/gpu/dxil/core_tests.rs:div_fixup_f32_and_f16_special_and_overflow_cases_on_gpu(guarded byrunner::wave32_supported()), running both Direct DXIL and HLSL on the GPU across the full 9-instruction RDNA2v_div_scale_f32 -> v_rcp_f32 -> v_fma_f32 -> v_mul_f32 -> v_fma_f32 -> v_div_fmas_f32 -> v_div_fixup_f32sequence (16 lanes covering normal, signed overflow, huge denominator±3e38 / 3e38, signed underflow, signed zeros, div-by-zero, and0 / 0), plus standalonev_div_fixup_f32(6 lanes) andv_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), DXC1.9.2609.5,rustc 1.99.0.cargo check --workspace --all-targets: passed, 0 errors.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.cargo test --workspace -- --test-threads=4): 2609 passed, 0 failed, 49 ignored (27 runtime-skipped by capability guards).xcl-smoke),main-ancestor baseline vs this branch (0 device removals, 0 crashes, 0 minidumps, 0 System display events):Baseline (
f0e8fdd5):Branch (
cb660cdd):Not tested
v_div_fixup_f16rounding under default RNE mode (currently routes through existingto_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).AI use
Used an AI coding assistant to investigate the
div_fixupfallback edge cases, write the Direct DXIL / HLSL fix and GPU parity test, and verify all tests and smoke runs locally.Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:59:02Z.
Review (head
cb660cdd, diff read, not a merge-check):dxc::compile(...).expect(...)without adxc::location()skip (onlyskip_test("no wave32 GPU")), so on a machine without DXC it panics instead of printing SKIPPED.v_div_fixup_f32NaN/zero handling, another approach), which the description does not mention.Suggested verdict: not yet (DXC skip, model; settle the overlap with #1144).
(Review written with Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:13:24Z.
Re-review (head
0caeca23, diff read, not a merge-check: thePR descriptioncheck fails, so not built or run here yet). Follow-up to my review ofcb660cdd.cb660cddthe fallback code is unchanged: the push is a rebase ontod0832e61plus theskip_test("no DXC")asked for last time (core_tests.rs~1666). The smoke block for the PR is still oncb660cdd, which is whyPR descriptionfails; it needs rerunning on the head.n/d, while RDNA2'sv_div_fixupreturns the sign-adjusted|q|there (NaN, 0, NaN, +Inf). That's right for XCL, whosev_div_scaledoesn't scale, but the comments should say so.3e38/3e38(~1712, ~1730) depends on the driver's fdiv precision (D3D allows 2.5 ulp), and it was only checked on NVIDIA.v_div_fixup_f32/_f16gains about 10 ALU ops on the default path; not measured.Fine: on main the unscaled
qgives NaN for1e30/1e-20, 0 for3e38/3e38and +0 for-0/2, and the new fallbacks fix all three. The 2^-32 scaling for|d| >= 2^96can'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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T16:32:21Z.
Test report (not a merge-check:
PR descriptionred)base:
b2b1f109d2head:
0caeca231bmerge: 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.
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 284isinf(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 legacyf32tof16, 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.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.
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 293For finite non-zero n and d, the new q == 0 / isinf(q) / isnan(q) fallbacks replace q with n/d. Real
v_div_fixup_f32returnssign_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.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 301Not certain about this one: the HLSL backend marks
ns/ds/rasprecise, which this module mirrors withself.s.require(Feature::DisableMathRefactoring)(seemad_nf, ~242), but Direct DXILdiv_fixupdoesn'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.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.
Pull request closed