Recompiler: the HLSL backend's fast-math loses -0 in f16 results and changes v_sin/v_cos on AMD #2168
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#2168
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@alexdeliu on GitHub · 2026-11-25 07:01 UTC · original
The HLSL backend and the direct-DXIL backend give different results for the same instruction on six instructions. The results agree bit for bit on an RX 9070 (RDNA4) and on a gfx1036 iGPU (RDNA2). Compiling the HLSL kernels with DXC's
-Gis(IEEE strictness) removes all six differences. So the cause looks like DXC's default fast-math flags (nsz, etc.) on the HLSL path, which the AMD driver then uses. XCL'sdxc::compilepasses no-Gis.Found with a cross-backend / cross-device VALU fuzzer: one VOP3/VOP3P instruction per wave64 kernel, 256 edge and random inputs. A PR is coming; it is test-only.
v_mul_f160x0001×0x8000(+denorm × -0)0x00000x8000v_pk_mul_f160x8000×0x00010x0000…0x8000…v_ldexp_f16ldexp(0x8000, 1)0x00000x8000v_sin_f16sin(0x8000)0x00000x8000v_sin_f320x7f7fffff(FLT_MAX)0x000000000xffc00000v_cos_f320x47bcfc00(96760.0)0x3f8000000x3f7fb10eldexpof -0,sin(-0)). The direct backend keeps it; the HLSL backend returns +0. This is not the rounding difference thatDIFFERS_ON_1426inrecomp/lower.rslists forV_MUL_F16/V_PK_MUL_F16(RTZ vs RNE). It is the sign of zero, and it shows up on the default (HLSL) path.sin(x * 2π)/cos(x * 2π). Without-Gisthe AMD driver seems to compile the HLSL one differently: it gives 1.0 for cos of a whole number of turns and 0 for sin(FLT_MAX), where the direct backend gives the radian result and NaN. These inputs are outside the [-256, 256] input range the RDNA ISA gives for v_sin/v_cos, so what the console returns there is worth checking separately.v_dot2_f32_f16differs between the two GPUs on both backends: 7 of 256 results, mostly 1-3 ulp, plus a denormal result that RDNA4 keeps (0x00000001) and RDNA2 flushes (0x00000000). The console is RDNA2, so the iGPU's results are presumably the console's.Reproduce: the fuzzer PR adds
valu_fuzz_backends_agree(ignored). Runcargo test -p xcl --lib valu_fuzz -- --ignored --nocapture --test-threads=1, and again withXCL_FUZZ_DXC_GIS=1.Not tested: NVIDIA, the cost of
-Gison real shaders, and a retail title (I have no title here).AI use: found, analysed and written with Claude Code (Claude Opus 5.5). The runs above are real runs on the hardware named, on
mainf5c5bdf.@DarkPanda69 on GitHub · 2026-11-25 07:37 UTC · original
NVIDIA data point (listed as untested here): on an RTX 3060 (driver 32.0.16.1088, Windows, so the split wave64 form),
valu_fuzz_backends_agreefrom #2169 finds none of these six differences. HLSL and direct DXIL agree onv_mul_f16,v_pk_mul_f16,v_ldexp_f16,v_sin_f16,v_sin_f32andv_cos_f32for all 256 inputs. On an RX 9070 XT in the same run the six reproduce with the counts above. So the HLSL fast-math difference shows up through the AMD driver only.The 3060 has one difference of its own,
v_cubetc_f32(58 of 256): the HLSL result is a signed zero where the direct result keeps an f32 denormal, which looks like the same fast-math family (denormal flush instead of the sign of zero). Details and the cross-GPU comparison are in the run on #2169.Head
e792e6bonmain568e7db, debug. AI use: Claude Code (Claude Opus 5.5) ran the fuzzer on this machine and wrote this comment.@luckyteagirl on GitHub · 2026-11-25 07:50 UTC · original
RX 6800M (RDNA2) follow-up on #2169's exact head
e792e6b: I reproduced this issue's six default-HLSL differences with the same 8/19/26/49/4/7 counts (of 256 inputs per instruction).XCL_FUZZ_DXC_GIS=1removed all six, but introduced differences in eight FMA-family instructions that matched without-Gis:V_DIV_FMAS_F32(2),V_FMAC_F32(2),V_FMAC_LEGACY_F32(4),V_FMA_F16(6),V_FMA_F32(1),V_FMA_MIXHI_F16(7),V_FMA_MIXLO_F16(6), andV_PK_FMA_F16(14), each out of 256. A focused rerun of those eight gave 0 differing instructions by default and 8 with-Gis, with identical per-instruction counts.So
-Gisis not a complete cross-backend parity fix on this GPU. The fuzzer compares HLSL with direct DXIL; this run did not check console reference results for the FMA cases. Full setup, logs summary, and limitations are in the #2169 report.AI use: OpenAI Codex ran and repeated the tests and prepared this note.
@YaBulochka on GitHub · 2026-11-25 07:55 UTC · original
Linux status for this one, stated plainly: the cross-backend check cannot run here, so this is "not applicable", not a confirmation.
The comparison is done by executing the generated HLSL and the direct-DXIL form on a device (
runner::runincrates/xcl/src/gpu/recomp/lower_dxil.rs, andrunner::wave32_supported()gating infuzz_tests.rs). On Linux our XCL builds arex86_64-pc-windows-gnuand there is no D3D12 device in the path - Wine has no WARP, and vkd3d-proton would be the "device", which is not what the test measures. Sorunner::wave32_supported()is false and the GPU half of these tests is skipped.What I could do and did not, to be explicit: compile the same kernels with and without
-Gisthrough DXC and diff the DXIL bytes. That would show whether the flag changes the module, but it would not answer your question - the difference you measured is in the executed result (-0in f16, v_sin/v_cos), which needs the card.If a Windows host is available to you, the useful extra data point would be the same fuzzer on RDNA3 natively: your data is RDNA4 (9070) and RDNA2 (gfx1036), and RDNA3 is the generation in between.
@alexdeliu on GitHub · 2026-11-25 10:43 UTC · original
Correction to this issue, with thanks to @luckyteagirl for the full-run data:
-Gisis not the fix. My-Gischeck covered only the six differing instructions. The whole run on the RX 9070, at #2169's heade792e6bon568e7db, shows the same as the RX 6800M:v_cos_f328,v_ldexp_f1619,v_mul_f1626,v_pk_mul_f1649,v_sin_f164,v_sin_f327-Gisv_fma_f321,v_fmac_f322,v_fmac_legacy_f324,v_div_fmas_f322,v_fma_f166,v_pk_fma_f1614,v_fma_mixlo_f166,v_fma_mixhi_f167,v_dot2_f32_f167-Gismakes HLSL'smadan unfused multiply-add, so it trades the sign-of-zero differences for rounding ones in every fused op. A narrower direction would leave fusion alone:preciseonly on the f16 multiply /ldexp/sinresults (or only where a zero can come out), sonszcannot drop their sign. I have not measured that yet. The NVIDIA data above (no default-build differences on an RTX 3060) suggests the sign drop comes from AMD's compiler usingnsz.AI use: Claude Code (Claude Opus 5.5) ran both fuzzer builds on this machine and wrote this comment.
@alexdeliu on GitHub · 2026-11-25 10:55 UTC · original
Measured the narrower direction from my last comment:
preciseonly on the affected HLSL expressions, no-Gis. With it, the HLSL and direct-DXIL backends agree on every instruction on both AMD GPUs, and the FMA-family ops stay fused.-Gisprecisehelpers belowvalu_fuzz_backends_agreefrom #2169 ate792e6bon568e7db: 341 instructions, 333 compared, 8 refused by both.cargo test --workspace -- --test-threads=4with the change (RX 9070): 1152 passed, 2 failed (seeds 722 / 1599, as onmain), 48 ignored.The change: four small helpers whose result is
precise(sonsz/ reassociation cannot touch it), used byv_mul_f16,v_pk_mul_f16,v_ldexp_f16andv_sin/v_cos(f16 and f32):It touches
recomp/emit.rs, so a PR needs a smoke run on a real title, which I cannot do here. Anyone with a title is welcome to take it, or I can open it once someone runsscripts/smoke.ps1on it. Not tested: NVIDIA (the RTX 3060 run above already had no default-build differences), and the shader-size or performance cost ofpreciseon real shaders.AI use: Claude Code (Claude Opus 5.5) wrote the change, ran both fuzzer builds and the suite on this machine, and wrote this comment.