Recompiler: v_cmp_class_f32 applies abs/neg via float ops (fsrc) instead of bitwise sign-bit ops, flushing denormals and quieting sNaN #1281
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#1281
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?
@hyperland956-lgtm on GitHub · 2026-11-24 14:14 UTC · original
Summary
In
crates/xcl/src/gpu/recomp/emit.rs,v_cmp_class_f32applies VOP3abs/negmodifiers throughSelf::fsrc(abs(asfloat(u))/(-asfloat(u))) and passes afloatintofclass(float f). On RDNA2,v_cmp_class_*modifiers are bitwise sign-bit operations (clearing/flipping bit 31) and must never flush denormals or quiet signaling NaNs.Both sibling paths in the same file already handle this bitwise:
f64v(emit.rs:3075-3086): explicitly masks/flips bit 31 of the high dword with the comment "abs / neg on the sign bit, as the hardware applies them:-xisfsub -0.0, xin DXIL, which leaves the sign of a NaN to the driver".v_cmp_class_f16(emit.rs:3605-3616): applies({h} & 0x7FFFu)/({h} ^ 0x8000u)directly on the rawuintbits and callsfclass16(uint h).By contrast,
v_cmp_class_f32(emit.rs:3633-3638) falls through toSelf::fsrc:where
Self::fsrc(emit.rs:2564-2573) emitsabs(asfloat(u))and(-asfloat(u)).Why this matters
Under D3D12's default float32 FTZ mode and DXC/driver lowering:
abs(asfloat(u))and-asfloat(u)flush f32 subnormals (0x00000001,0x80000001) to±0.0, misclassifying class bits 7/4 (pos/neg denormal) as bits 6/5 (pos/neg zero).abs/negcan quiet an sNaN (0x7f800001) into a qNaN (0x7fc00001) or drop its sign, misclassifying class bit 0 (sNaN) as bit 1 (qNaN).abs/neg, takingfloatinfclass(float f)(emit.rs:5851) forces an unnecessaryuint -> float -> uintround-trip (asuint(asfloat(u))) where a driver may canonicalize NaN payloads or flush denorms. (The same applies tofclass64(double f)at line 5857, which immediately callsasuint(f, l, h).)Suggested fix
fclasstouint fclass_u32(uint u)(operating directly on raw bits, matchingfclass16(uint h)).vopc()(emit.rs:3633), handleop == "class"for"f32"beforeSelf::fsrc, applyingm.abs[0]as({u} & 0x7FFFFFFFu)andm.neg[0]as({u} ^ 0x80000000u)and callingfclass_u32(...).(lo, hi)directly tofclass64_u32(uint l, uint h)forv_cmp_class_f64(emit.rs:3091) to avoid theasdouble/asuintround-trip as well.Synthetic GPU test
Encode VOP3
v_cmp_class_f32withabs=1andneg=1on:0x80000001(negative f32 subnormal) withabs=1, mask1 << 7(positive subnormal) → must returntrue(not flushed to+0.0/ bit 6).0x7f800001(sNaN) withneg=1, mask1 << 0(sNaN) → must returntrue(not quieted to qNaN / bit 1).AI use: source-level audit of
xcl-main(crates/xcl/src/gpu/recomp/emit.rs) with Gemini; not built or run on Windows.github-bot referenced this issue2026-11-26 04:50:12 +00:00
@tiagonogueiraa-bit on GitHub · 2026-11-24 14:31 UTC · original
claiming:
v_cmp_class_f32takes its abs / neg on the sign bit of the raw bits, as the f16 and f64 paths do, andfclassclassifies the bits without a float round trip. It comes with a GPU test of denormals and an sNaN under abs / neg that fails on main. PR within the hour.Written with Claude Code (Claude Opus 5.5).
@Rednaxeh on GitHub · 2026-11-24 14:35 UTC · original
A measurement on RDNA3 (RX 7800 XT, driver 32.0.32029.2010, DXC 1.9.2602.17, Windows 11,
main930e8b4, no local changes): a GPU test next togpu_cmp_class_f16_preserves_snanrunningV_CMP_CLASS_F32(VOP3) withABS/NEGon0x00000001,0x80000001and0x7F800001passes onmainas it is, in wave32 and wave64:abs(0x80000001)and0x00000001match bit 7 (+denormal),neg(0x00000001)matches bit 4 (-denormal);abs(0x00000001)does not match bit 6 (+zero);abs(0x7F800001)andneg(0x7F800001)match bit 0 (sNaN).So on this driver
abs(asfloat(u))/-asfloat(u)neither flush the denormals nor quiet the sNaN beforefclass: the bitwise form would be a tidier match of the f16 / f64 paths, but I could not make it fail here. If someone sees the misclassification on NVIDIA, Intel or WARP, that run is the one to build the test on.Written with Claude Code (Claude Fable 5.1); the run was mine.
@ySKELLETONX on GitHub · 2026-11-24 15:07 UTC · original
Test report for the four open PRs on this issue, on AMD RDNA3. AI-written (Claude Code, Claude Opus 5.5) from real runs; checked by me.
Hardware: AMD Radeon RX 7600 XT (RDNA3, driver 32.0.32015.2008), Ryzen 7 5700X3D, Windows 11 Pro 10.0.26300, rustc 1.100.0, debug build, DXC from Windows SDK 10.0.26100.
scripts/test.ps1(cargo test --workspace -- --test-threads=4) on each PR's head:5b4069cd1fee31ee63ac4aa625c44a6a#1318 failures:
f64_class_compare_vopc_and_vop3(tests.rs:474): the PR changes the f64 class HLSL but not this test's expected strings.v_cmp_class_f32_emits_bitwise_modifiers_without_asfloat(tests.rs:7180):t.hlslincludes the helper prelude, which containsasfloat(, so the assertion fails with the fix in place.Do the new tests catch the bug on AMD? With each PR's
emit.rschange reverted (tests kept), on this RDNA3:gpu_cmp_class_f32_modifiers_keep_the_bits, #1308gpu_cmp_class_f32_preserves_denormals_and_snan, #1309gpu_cmp_class_f32_modifiers_keep_denormals_and_snan: all pass without the fix. AMD's driver keeps denormals / sNaN through FAbs andfsub fast -0.0, x(checked the DXIL with DXC 10.0.26100), so on AMD these GPU checks do not distinguish fixed from unfixed.f64_class_compare_vopc_and_vop3fails without the fix (HLSL check), but covers f64 only.An HLSL-level check on the f32 path (e.g. that the class operand is not passed through
abs(asfloat(/(-asfloat() fails on main and would catch a regression on any GPU.Not tested: NVIDIA, WARP (the GPU tests skip there: no wave64), any retail title (no smoke runs).
@KOLESNiii on GitHub · 2026-11-24 15:08 UTC · original
I compared the four PRs for this issue (#1306, #1308, #1309, #1318) against
main930e8b4, which all four are based on. This is a static review: I read the diffs and the code they touch, but I didn't run them.Which input shows the bug. All four apply ABS and then NEG to bit 31 of the raw bits before classifying, as the f16 path does, so the core change is the same in each. They differ in their tests.
abs()only touches the sign bit, and the RX 7800 XT run above passed every case onmain. The failure comes from NEG:-xisfsub -0.0, xin DXIL, so with fp32 denormals flushed,-(+denormal)comes out as-0. A test that catches this needs a NEG on a denormal. #1309 measured that case failing onmain(RTX 3070).#1309: the one I'd merge
classarm (+12/−1 inemit.rs).-(+denormal)is-denormal, and is not-0), in wave32 and wave64. It fails onmainand passes with the change on an RTX 3070. The PR lists full-suite counts and SKIPPED tests.fclass(asfloat(u))keeps a bitcast round trip. A bitcast isn't a float op, and the NVIDIA run passes, butfclass(uint u)as in #1306 would matchfclass16and remove the question.#1306
"f32" if op == "class"arm,fclass(uint u)without a rename, and f64 left for a separate change. Its test includesneg(-denormal)→+denormal, so it should fail onmainon NVIDIA.cargo checked on macOS: no GPU run, no suite run, no smoke runs. It claimed the issue first, but on the evidence so far #1309 is ahead. I'd suggest folding itsfclass(uint)signature into #1309.#1308
fclass→fclass_u32,fclass64→fclass64_u32).f64v(emit.rs:3074) already applied ABS / NEG to the sign bit. The only thing removed is anasdouble/asuintround trip, and those are bit-exact (makeDouble / splitDouble). No bug is shown there, so this is a second change plus a rename.abs(-denormal)andneg(sNaN)), and the PR doesn't show it failing onmain.m.abs[0.min(2)]/m.neg[0.min(2)]come fromf64v's indexed closure and mean nothing with a literal 0.## Testedheadings instead of**Tested**, and "frontier model" doesn't name the model.#1318
v_cmp_class_f32_emits_bitwise_modifiers_without_asfloatasserts!t.hlsl.contains("asfloat(")over the whole translation. Butfinish()always appendsPRELUDE(emit.rs:4262), which containsasfloat((for example infract_f). So the test fails everywhere, CI included.Suggestion: merge #1309 (optionally with
fclass(uint u)from #1306), and close #1306, #1308 and #1318 in favour of it. If the f64 round trip is worth changing, that can be its own PR.Review written with Claude Code (Claude Opus 5.5); I checked each point against the code myself.
@tiagonogueiraa-bit on GitHub · 2026-11-24 20:14 UTC · original
Thanks @KOLESNiii for comparing the four. One correction about #1306, since the review was written from an earlier state: it is no longer only
cargo checked on macOS. On its head5b4069cit has:SKIPPED), plus lib runs on an RTX 3060 Ti and under Wine;emit.rschange on the RTX 5080 (wave32 abs(-denormal) matches +denormal) and passing without it on AMD, as @ySKELLETONX found; it coversneg(-denormal)andneg(sNaN)too;xcl-smokepair, so itsPR descriptioncheck passes.Either way the fix is the same, and I'm fine with whichever the maintainers prefer. If #1309 is chosen, I'll close #1306; @ySKELLETONX's HLSL-level check (no
abs(asfloat(/(-asfloat(on the class operand) would be a good addition to whichever lands, since it fails on any GPU.Written with Claude Code (Claude Opus 5.5).