Recompiler: return +0 from fma_legacy when a zero factor adds -0 (#679) #928
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!928
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/928/head"
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?
@bigiello on GitHub · 2026-11-24 08:31 UTC · original
fix/recomp-fma-legacy-neg-zero→main· +105 −1 · 2 files · 1 commits · closed without mergingDraft pending
scripts/smoke.ps1regression blocks on a retail title (no local game files available; see Not tested).Fixes #679.
In RDNA2,
v_fma_legacy_f32andv_fmac_legacy_f32treat a product with a±0.0multiplicand as+0.0(includinginfandNaN), so adding+0.0to-0.0under default round-to-nearest-even must return+0.0(0x00000000). XCL previously returnedcunchanged fromfma_legacy, preserving-0.0(0x80000000).What is in it
crates/xcl/src/gpu/recomp/emit.rs:fma_legacyinPRELUDEto canonicalizec == 0.0fto0uon integer bits via branchlessasfloat(select(or(a == 0.0f, b == 0.0f), select(c == 0.0f, 0u, asuint(c)), asuint(mad(a, b, c))))so DXC cannot fold a float+0.0/-0.0select back toc.crates/xcl/src/gpu/recomp/tests.rs:fma_legacy_hlsl_clears_neg_zero_in_integer_bits: unit check verifying emitted HLSL and DXC compilation forv_fma_legacy_f32andv_fmac_legacy_f32(ENC_VOP2/ENC_VOP3).gpu_fma_legacy_zero_product_adds_plus_zero: D3D12/DXIL GPU test across wave32 and wave64 verifying exact result bits for±0.0 * {5.0, -5.0, ±inf, qNaN} + (-0.0), VOP3NEGonSRC2, non-zero/NaNaddends, and normal FMA (failed with0x80000000before the fix).Tested
stable-x86_64-pc-windows-msvc(rustc 1.100.0), Windows SDK DXC 1.8.2502.11 (10.0.26100.0\x64).09940bb):cargo test -p xcl --lib -- fma_legacy --nocapturefailed both tests (GPU returned0x80000000for+0 * 5 + (-0)).439f607):cargo test -p xcl --lib -- fma_legacy --nocapture: 2 passed, 0 failed (0.40s).cargo test -p xcl --lib -- --test-threads=1: 677 passed, 0 failed, 21 ignored (49.94s; 2 GPU tests skipped:no wave64—gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware).cargo test --workspace --test "*" -- --test-threads=4: 41 passed, 0 failed, 20 ignored across all integration test binaries (console_dlls_e2e13 passed,console_shaders3 passed / 4 ignored,disasm_e2e1 passed,e2e3 passed,gpu_compute_e2e2 passed,gpu_pso_e2e7 passed,heap_probe0 passed / 4 ignored,pls_quota1 passed,predication_e2e1 passed,rpf_e2e2 passed,sparse_reinit1 passed,texture_states1 passed,xg_oracle6 passed / 12 ignored)..\scripts\test.ps1(--test-threads=4): hit the pre-existing#718timing flake inplaced_title_textures_share_vram_and_preserve_pixels(poisoningtransitions_apply_when_their_list_is_submittedandd32s8_targets_take_their_own_size_of_video_memory: 674 passed, 3 failed, 21 ignored inxcl --lib, 2 skippedno wave64); all 677 tests passed in the--test-threads=1run above.cargo clippy --workspace --all-targets: 0 errors, 0 new warnings on touched lines.rustfmt --check crates\xcl\src\gpu\recomp\emit.rs crates\xcl\src\gpu\recomp\tests.rs: 0 diffs on touched lines (cargo fmt --checkoverall exits 1 due to pre-existing formatting in untouched files).Not tested
gpu_fma_legacy_zero_product_adds_plus_zeroran in wave32 and split-wave64).scripts/smoke.ps1 -Game <folder>(XCL_SAMPLE_*unset; no retail game files available locally).AI use
fma_legacy, writing the unit and GPU regression tests, running verification commands, and drafting this PR description. All diffs and test outputs verified locally.@dcastell1 on GitHub · 2026-11-24 08:40 UTC · original
Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on clean
main@beaa245(713 passed, 0 failed, 41 ignored on this box):Two more passing tests than main, including the
fma_legacyzero-factor-0case; nothing else changes verdict.Tree restored afterwards (md5 and a full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.
@joeypercia on GitHub · 2026-11-24 08:44 UTC · original
AMD run (the PR's tests are NVIDIA only so far), which matters here because AMD is where the other approach fails:
439f607merged ontomain077ce40(clean merge).Tested
gpu_fma_legacy_zero_product_adds_plus_zero: passes on the RDNA2 iGPU and on the RTX 4070 Timain'semit.rsit fails (+0 * 5 + (-0): got 0x80000000)For review: #670 fixes the same thing with
precise float z = c + 0.0f, and that form still gives-0on this AMD driver (measured in #670). This PR's form works on the bits, so it holds on both drivers. Of the two, this is the one that fixes #679 everywhere.Run and report by Claude Code (Claude Opus 5.5).
@KENAJBBY on GitHub · 2026-11-24 09:31 UTC · original
Native RDNA2 (
gfx1036) reference for the legacy FMAsThe description and #670 note that the
+0result follows LLVM's folding and was not measured on hardware. I ranv_fma_legacy_f32andv_fmac_legacy_f32directly on an RDNA2 GPU, on raw bits, under all 1024 values of MODE[9:0]. Each mode was set withs_setreg_b32and read back withs_getreg_b32before the instruction.Zero factor plus −0.
±0 × {5, −5, ±inf, qNaN, sNaN} + (−0)gives+0(0x00000000) with f32 rounding RNE, toward +inf and toward zero. It gives−0(0x80000000) with f32 rounding toward −inf (MODE[1:0] = 2), so the hardware really computesc + (+0)in the current rounding mode. XCL reports a non-default f32 round mode as unsupported, and the pack has none (#91), so with RNE this PR's+0matches the hardware. No other MODE bit changes these results. Every case ofgpu_fma_legacy_zero_product_adds_plus_zerogives the expected bits on the hardware at MODE0x1C0(IEEE 0, DX10_CLAMP 1, RNE):0x000000000x800000000x3F8000000x7FC000000x7F8000110x7F8000110x7FC000110x40E000000x000000000x800000000xFFC00000The legacy FMAs flush f32 denormals in every mode. With MODE[5:4] = 3 (f32 denormals kept),
v_fma_f32andv_mul_legacy_f32keep them. That is my control that the denorm bits really take effect. Both legacy FMAs still read denormal inputs as 0 and flush denormal results, in all 1024 modes:v_fma_f32, MODE[5:4] = 30x000000010x000000000x000000010x00000001+ 00x000000000x000000010x007FFFFF+ 00x000000000x007FFFFF0x00800000+ 00x000000000x004000000x00000001× inf + 10x3F800000(the legacy 0 × inf)When a factor is zero, this PR returns a denormal
cunchanged on the bits, so the result depends on the host. I ran the head439f607with a local scratch test (not proposed for the PR) on an RTX 5070 Ti (621.24, wave32) and on the same RDNA2 iGPU through D3D12 (wave64 and wave32). On both,+0 × 5 + 0x00000001,+0 × 5 + 0x80000001,1 × 0x00000001 + 0,1 × 0x007FFFFF + 0and the VOP2fmacform give0x00000000, and0x00000001 × inf + 1gives0x3F800000. Those are the hardware's answers, because both drivers flush f32 denormals in the DXIL. A sNaNccame back as0x7F800011, the IEEE 0 answer. So I see nothing to change for #679; the flush comes from the host drivers, not from the helper.Tested
gfx1036, RDNA2), driver 32.0.21030.2001, AMD OpenCL3652.0 (PAL,LC), native wave64; Windows 11 Pro 26200.s_setreg_b32 hwreg(HW_REG_MODE, 0, 10), reads it back withs_getreg_b32(checked against the requested value in every run), runs the instruction and restores MODE. Inputs: 30 triples × 2 instructions × 1024 modes, plus 10 denormal triples × 4 instructions × 1024 modes.439f607(on its own base), debug, Rust 1.100.0 MSVC, DXC 1.9.2602.17,XCL_ADAPTER=nvidiaandXCL_ADAPTER=radeon: the scratch test above andgpu_fma_legacy_zero_product_adds_plus_zero, 2 passed, 0 failed on each.Not tested
neg,abs,clamp,omod) on the hardware; the whole suite; a retail title.AI use
@DTsordas on GitHub · 2026-11-24 09:50 UTC · original
Test report for #928 at
439f607: AMD RDNA4 (RX 9060 XT, native wave64), WindowsThe maintainer merges on RDNA4, which the reports so far (NVIDIA, RDNA2 iGPU) don't cover.
Tested
cargo test -p xcl --lib fma_legacy: 2 passed, 0 failed, no skips.emit.rsfrom the base09940bb, both fail, the GPU test withwave64=false lane 0 +0 * 5 + (-0): got 0x80000000. So the integer-bits form also holds on this AMD driver.cargo test --workspace -- --test-threads=4at439f607, two runs:placed_title_textures_share_vram_and_preserve_pixelsfailed at its cubemap reuse check (left: 2555904, right: 17170432), thentransitions_apply_when_their_list_is_submittedandd32s8_targets_take_their_own_size_of_video_memoryfailed on the poisoned lock. That's the #718 pattern, the same three as in the description.--no-fail-fast): 718 passed, 0 failed, 41 ignored, 0 SKIPPED.077ce40on this machine (one run): 716 passed, 0 failed, 41 ignored, 0 SKIPPED.Not tested
xcl-smokeblocks.AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
@SlopTart on GitHub · 2026-11-24 10:37 UTC · original
Linux / AMD RDNA3 datapoint.
Merged state: head
439f607onto main9aec53d, clean merge.Full suite,
cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 715 passed, 11 failed, 41 ignored; failures are the known Wine-lane set from my #42 rows plus this rig's title-textures flake family (see #718). fma_legacy +0-on-zero-factor green.Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.
Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross
x86_64-pc-windows-gnu,XCL_NETWORK=0, release profile,--test-threads=4. My Linux rows: #42 / #718.AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.
@Cecerw on GitHub · 2026-11-24 13:45 UTC · original
Tested on NVIDIA (32-lane waves): the new tests pass; needs a rebase
439f607(lib only, stops at the failures)09940bb, without the PR (lib only)main47efa2e(whole suite)439f607, no local changes:cargo test -p xcl --lib -- --test-threads=4: 674 passed, 3 failed, 21 ignored (same 3 in two runs):placed_title_textures_share_vram_and_preserve_pixels,transitions_apply_when_their_list_is_submitted,d32s8_targets_take_their_own_size_of_video_memory09940bbwithout the PR (672 passed, 3 failed), and pass when run alone. They are not from this change.main47efa2e(no conflicts):.\scripts\test.ps1: 743 passed, 0 failed, 41 ignoredfma_legacy_hlsl_clears_neg_zero_in_integer_bitsandgpu_fma_legacy_zero_product_adds_plus_zero(incl. its wave64 case, run split on this GPU): ok in every runA rebase onto current
mainshould clear the 3 failures.Not tested: AMD, any title (no smoke run).
AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
Pull request closed