Recompiler: v_cmp_class_f32 applies abs/neg via float ops (fsrc) instead of bitwise sign-bit ops, flushing denormals and quieting sNaN #1281

Open
opened 2026-11-26 04:49:17 +00:00 by github-bot · 5 comments
Owner

@⁠hyperland956-lgtm on GitHub · 2026-11-24 14:14 UTC · original

Summary

In crates/xcl/src/gpu/recomp/emit.rs, v_cmp_class_f32 applies VOP3 abs/neg modifiers through Self::fsrc (abs(asfloat(u)) / (-asfloat(u))) and passes a float into fclass(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: -x is fsub -0.0, x in 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 raw uint bits and calls fclass16(uint h).

By contrast, v_cmp_class_f32 (emit.rs:3633-3638) falls through to Self::fsrc:

"f32" => {
    let (a, b) = (Self::fsrc(m, 0, &ua), Self::fsrc(m, 1, &ub));
    match op {
        "class" => format!("((({ub}) >> fclass({a})) & 1u) != 0u"),

where Self::fsrc (emit.rs:2564-2573) emits abs(asfloat(u)) and (-asfloat(u)).

Why this matters

Under D3D12's default float32 FTZ mode and DXC/driver lowering:

  1. 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).
  2. Float abs/neg can quiet an sNaN (0x7f800001) into a qNaN (0x7fc00001) or drop its sign, misclassifying class bit 0 (sNaN) as bit 1 (qNaN).
  3. Even without abs/neg, taking float in fclass(float f) (emit.rs:5851) forces an unnecessary uint -> float -> uint round-trip (asuint(asfloat(u))) where a driver may canonicalize NaN payloads or flush denorms. (The same applies to fclass64(double f) at line 5857, which immediately calls asuint(f, l, h).)

Suggested fix

  1. Change fclass to uint fclass_u32(uint u) (operating directly on raw bits, matching fclass16(uint h)).
  2. In vopc() (emit.rs:3633), handle op == "class" for "f32" before Self::fsrc, applying m.abs[0] as ({u} & 0x7FFFFFFFu) and m.neg[0] as ({u} ^ 0x80000000u) and calling fclass_u32(...).
  3. Optionally pass (lo, hi) directly to fclass64_u32(uint l, uint h) for v_cmp_class_f64 (emit.rs:3091) to avoid the asdouble/asuint round-trip as well.

Synthetic GPU test

Encode VOP3 v_cmp_class_f32 with abs=1 and neg=1 on:

  • 0x80000001 (negative f32 subnormal) with abs=1, mask 1 << 7 (positive subnormal) → must return true (not flushed to +0.0 / bit 6).
  • 0x7f800001 (sNaN) with neg=1, mask 1 << 0 (sNaN) → must return true (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.

**@⁠hyperland956-lgtm** on GitHub · 2026-11-24 14:14 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281) ## Summary In `crates/xcl/src/gpu/recomp/emit.rs`, `v_cmp_class_f32` applies VOP3 `abs`/`neg` modifiers through `Self::fsrc` (`abs(asfloat(u))` / `(-asfloat(u))`) and passes a `float` into `fclass(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: `-x` is `fsub -0.0, x` in 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 raw `uint` bits and calls `fclass16(uint h)`. By contrast, `v_cmp_class_f32` (`emit.rs:3633-3638`) falls through to `Self::fsrc`: ```rust "f32" => { let (a, b) = (Self::fsrc(m, 0, &ua), Self::fsrc(m, 1, &ub)); match op { "class" => format!("((({ub}) >> fclass({a})) & 1u) != 0u"), ``` where `Self::fsrc` (`emit.rs:2564-2573`) emits `abs(asfloat(u))` and `(-asfloat(u))`. ## Why this matters Under D3D12's default float32 FTZ mode and DXC/driver lowering: 1. `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). 2. Float `abs`/`neg` can quiet an sNaN (`0x7f800001`) into a qNaN (`0x7fc00001`) or drop its sign, misclassifying class bit 0 (sNaN) as bit 1 (qNaN). 3. Even without `abs`/`neg`, taking `float` in `fclass(float f)` (`emit.rs:5851`) forces an unnecessary `uint -> float -> uint` round-trip (`asuint(asfloat(u))`) where a driver may canonicalize NaN payloads or flush denorms. (The same applies to `fclass64(double f)` at line 5857, which immediately calls `asuint(f, l, h)`.) ## Suggested fix 1. Change `fclass` to `uint fclass_u32(uint u)` (operating directly on raw bits, matching `fclass16(uint h)`). 2. In `vopc()` (`emit.rs:3633`), handle `op == "class"` for `"f32"` before `Self::fsrc`, applying `m.abs[0]` as `({u} & 0x7FFFFFFFu)` and `m.neg[0]` as `({u} ^ 0x80000000u)` and calling `fclass_u32(...)`. 3. Optionally pass `(lo, hi)` directly to `fclass64_u32(uint l, uint h)` for `v_cmp_class_f64` (`emit.rs:3091`) to avoid the `asdouble`/`asuint` round-trip as well. ## Synthetic GPU test Encode VOP3 `v_cmp_class_f32` with `abs=1` and `neg=1` on: * `0x80000001` (negative f32 subnormal) with `abs=1`, mask `1 << 7` (positive subnormal) → must return `true` (not flushed to `+0.0` / bit 6). * `0x7f800001` (sNaN) with `neg=1`, mask `1 << 0` (sNaN) → must return `true` (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 locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:17 +00:00
Author
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-24 14:31 UTC · original

claiming: v_cmp_class_f32 takes its abs / neg on the sign bit of the raw bits, as the f16 and f64 paths do, and fclass classifies 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).

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 14:31 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281#issuecomment-7134514130) claiming: `v_cmp_class_f32` takes its abs / neg on the sign bit of the raw bits, as the f16 and f64 paths do, and `fclass` classifies 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).
Author
Owner

@⁠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, main 930e8b4, no local changes): a GPU test next to gpu_cmp_class_f16_preserves_snan running V_CMP_CLASS_F32 (VOP3) with ABS / NEG on 0x00000001, 0x80000001 and 0x7F800001 passes on main as it is, in wave32 and wave64:

  • abs(0x80000001) and 0x00000001 match bit 7 (+denormal), neg(0x00000001) matches bit 4 (-denormal); abs(0x00000001) does not match bit 6 (+zero);
  • abs(0x7F800001) and neg(0x7F800001) match bit 0 (sNaN).

So on this driver abs(asfloat(u)) / -asfloat(u) neither flush the denormals nor quiet the sNaN before fclass: 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.

**@⁠Rednaxeh** on GitHub · 2026-11-24 14:35 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281#issuecomment-7134620471) A measurement on RDNA3 (RX 7800 XT, driver 32.0.32029.2010, DXC 1.9.2602.17, Windows 11, `main` `930e8b4`, no local changes): a GPU test next to `gpu_cmp_class_f16_preserves_snan` running `V_CMP_CLASS_F32` (VOP3) with `ABS` / `NEG` on `0x00000001`, `0x80000001` and `0x7F800001` passes on `main` as it is, in wave32 and wave64: - `abs(0x80000001)` and `0x00000001` match bit 7 (+denormal), `neg(0x00000001)` matches bit 4 (-denormal); `abs(0x00000001)` does not match bit 6 (+zero); - `abs(0x7F800001)` and `neg(0x7F800001)` match bit 0 (sNaN). So on this driver `abs(asfloat(u))` / `-asfloat(u)` neither flush the denormals nor quiet the sNaN before `fclass`: 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.
Author
Owner

@⁠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:

PR head result
#1306 5b4069cd 752 passed, 0 failed, 41 ignored, 0 SKIPPED
#1308 1fee31ee 752 passed, 0 failed, 41 ignored, 0 SKIPPED
#1309 63ac4aa6 752 passed, 0 failed, 41 ignored, 0 SKIPPED
#1318 25c44a6a lib tests: 707 passed, 2 failed; later test binaries did not run

#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.hlsl includes the helper prelude, which contains asfloat(, so the assertion fails with the fix in place.

Do the new tests catch the bug on AMD? With each PR's emit.rs change reverted (tests kept), on this RDNA3:

  • #1306 gpu_cmp_class_f32_modifiers_keep_the_bits, #1308 gpu_cmp_class_f32_preserves_denormals_and_snan, #1309 gpu_cmp_class_f32_modifiers_keep_denormals_and_snan: all pass without the fix. AMD's driver keeps denormals / sNaN through FAbs and fsub fast -0.0, x (checked the DXIL with DXC 10.0.26100), so on AMD these GPU checks do not distinguish fixed from unfixed.
  • #1308's updated f64_class_compare_vopc_and_vop3 fails 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).

**@⁠ySKELLETONX** on GitHub · 2026-11-24 15:07 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281#issuecomment-7135496005) 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: | PR | head | result | |---|---|---| | #1306 | 5b4069cd | 752 passed, 0 failed, 41 ignored, 0 SKIPPED | | #1308 | 1fee31ee | 752 passed, 0 failed, 41 ignored, 0 SKIPPED | | #1309 | 63ac4aa6 | 752 passed, 0 failed, 41 ignored, 0 SKIPPED | | #1318 | 25c44a6a | lib tests: 707 passed, **2 failed**; later test binaries did not run | #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.hlsl` includes the helper prelude, which contains `asfloat(`, so the assertion fails with the fix in place. **Do the new tests catch the bug on AMD?** With each PR's `emit.rs` change reverted (tests kept), on this RDNA3: - #1306 `gpu_cmp_class_f32_modifiers_keep_the_bits`, #1308 `gpu_cmp_class_f32_preserves_denormals_and_snan`, #1309 `gpu_cmp_class_f32_modifiers_keep_denormals_and_snan`: **all pass without the fix**. AMD's driver keeps denormals / sNaN through FAbs and `fsub fast -0.0, x` (checked the DXIL with DXC 10.0.26100), so on AMD these GPU checks do not distinguish fixed from unfixed. - #1308's updated `f64_class_compare_vopc_and_vop3` fails 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).
Author
Owner

@⁠KOLESNiii on GitHub · 2026-11-24 15:08 UTC · original

I compared the four PRs for this issue (#1306, #1308, #1309, #1318) against main 930e8b4, 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 on main. The failure comes from NEG: -x is fsub -0.0, x in 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 on main (RTX 3070).

#1309: the one I'd merge

  • The change is scoped to the f32 class arm (+12/−1 in emit.rs).
  • The test has 8 cases, each with a positive and a negative check (for example, -(+denormal) is -denormal, and is not -0), in wave32 and wave64. It fails on main and passes with the change on an RTX 3070. The PR lists full-suite counts and SKIPPED tests.
  • Missing: the smoke blocks (the description asks for them).
  • Nit: fclass(asfloat(u)) keeps a bitcast round trip. A bitcast isn't a float op, and the NVIDIA run passes, but fclass(uint u) as in #1306 would match fclass16 and remove the question.

#1306

  • The cleanest code: its own "f32" if op == "class" arm, fclass(uint u) without a rename, and f64 left for a separate change. Its test includes neg(-denormal) → +denormal, so it should fail on main on NVIDIA.
  • But it's a draft that has only been 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 its fclass(uint) signature into #1309.

#1308

  • It also rewrites the f64 path and renames both helpers (fclass → fclass_u32, fclass64 → fclass64_u32). f64v (emit.rs:3074) already applied ABS / NEG to the sign bit. The only thing removed is an asdouble / asuint round trip, and those are bit-exact (makeDouble / splitDouble). No bug is shown there, so this is a second change plus a rename.
  • The test has no NEG on a denormal (only abs(-denormal) and neg(sNaN)), and the PR doesn't show it failing on main.
  • m.abs[0.min(2)] / m.neg[0.min(2)] come from f64v's indexed closure and mean nothing with a literal 0.
  • The test report has a filtered run, a lib run with a failure and a workspace run that aborted, but no full-suite counts. The description check fails because it uses ## Tested headings instead of **Tested**, and "frontier model" doesn't name the model.

#1318

  • v_cmp_class_f32_emits_bitwise_modifiers_without_asfloat asserts !t.hlsl.contains("asfloat(") over the whole translation. But finish() always appends PRELUDE (emit.rs:4262), which contains asfloat( (for example in fract_f). So the test fails everywhere, CI included.
  • It hasn't been built or run (the description says it relies on project CI, and CI hasn't run on it), so "logic verified against new unit tests" isn't accurate.
  • Its GPU test has no NEG on a denormal either, and it makes the same f64 change as #1308.

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.

**@⁠KOLESNiii** on GitHub · 2026-11-24 15:08 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281#issuecomment-7135530868) I compared the four PRs for this issue (#1306, #1308, #1309, #1318) against `main` `930e8b4`, 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 on `main`. The failure comes from NEG: `-x` is `fsub -0.0, x` in 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 on `main` (RTX 3070). **#1309: the one I'd merge** - The change is scoped to the f32 `class` arm (+12/−1 in `emit.rs`). - The test has 8 cases, each with a positive and a negative check (for example, `-(+denormal)` is `-denormal`, and is not `-0`), in wave32 and wave64. It fails on `main` and passes with the change on an RTX 3070. The PR lists full-suite counts and SKIPPED tests. - Missing: the smoke blocks (the description asks for them). - Nit: `fclass(asfloat(u))` keeps a bitcast round trip. A bitcast isn't a float op, and the NVIDIA run passes, but `fclass(uint u)` as in #1306 would match `fclass16` and remove the question. **#1306** - The cleanest code: its own `"f32" if op == "class"` arm, `fclass(uint u)` without a rename, and f64 left for a separate change. Its test includes `neg(-denormal)` → `+denormal`, so it should fail on `main` on NVIDIA. - But it's a draft that has only been `cargo check`ed 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 its `fclass(uint)` signature into #1309. **#1308** - It also rewrites the f64 path and renames both helpers (`fclass` → `fclass_u32`, `fclass64` → `fclass64_u32`). `f64v` (`emit.rs:3074`) already applied ABS / NEG to the sign bit. The only thing removed is an `asdouble` / `asuint` round trip, and those are bit-exact (makeDouble / splitDouble). No bug is shown there, so this is a second change plus a rename. - The test has no NEG on a denormal (only `abs(-denormal)` and `neg(sNaN)`), and the PR doesn't show it failing on `main`. - `m.abs[0.min(2)]` / `m.neg[0.min(2)]` come from `f64v`'s indexed closure and mean nothing with a literal 0. - The test report has a filtered run, a lib run with a failure and a workspace run that aborted, but no full-suite counts. The description check fails because it uses `## Tested` headings instead of `**Tested**`, and "frontier model" doesn't name the model. **#1318** - `v_cmp_class_f32_emits_bitwise_modifiers_without_asfloat` asserts `!t.hlsl.contains("asfloat(")` over the whole translation. But `finish()` always appends `PRELUDE` (`emit.rs:4262`), which contains `asfloat(` (for example in `fract_f`). So the test fails everywhere, CI included. - It hasn't been built or run (the description says it relies on project CI, and CI hasn't run on it), so "logic verified against new unit tests" isn't accurate. - Its GPU test has no NEG on a denormal either, and it makes the same f64 change as #1308. **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.
Author
Owner

@⁠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 head 5b4069c it has:

  • full workspace suites on native Windows: RTX 5080 (752 / 0 / 41) and RX 7600 XT (752 / 0 / 41, no SKIPPED), plus lib runs on an RTX 3060 Ti and under Wine;
  • the new test failing without the emit.rs change on the RTX 5080 (wave32 abs(-denormal) matches +denormal) and passing without it on AMD, as @⁠ySKELLETONX found; it covers neg(-denormal) and neg(sNaN) too;
  • an xcl-smoke pair, so its PR description check 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).

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 20:14 UTC · [original](https://github.com/xcl-dev/xcl/issues/1281#issuecomment-7143248551) 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 check`ed on macOS. On its head `5b4069c` it has: - full workspace suites on native Windows: RTX 5080 (752 / 0 / 41) and RX 7600 XT (752 / 0 / 41, no `SKIPPED`), plus lib runs on an RTX 3060 Ti and under Wine; - the new test failing without the `emit.rs` change on the RTX 5080 (`wave32 abs(-denormal) matches +denormal`) and passing without it on AMD, as @⁠ySKELLETONX found; it covers `neg(-denormal)` and `neg(sNaN)` too; - an `xcl-smoke` pair, so its `PR description` check 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.