Skip to content

rdna2spirv: bitwise neg/abs in v_cmp(x)_class_f32 and add v_cmp(x)_class_f64 - #2572

Closed
timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-cmp-class-f32-f64-hw
Closed

timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-cmp-class-f32-f64-hw

Conversation

@timelery

Copy link
Copy Markdown
Contributor

Addresses the finding reported by @Vonsek in #2300 (#2300 (comment)) and adds hardware-verified v_cmp_class_f64 / v_cmpx_class_f64 translation.

Claimed in: #2300 (comment)

Summary of changes

  1. v_cmp_class_f32 / v_cmpx_class_f32 bitwise source modifiers (tools/rdna2spirv.py Gen.cmp_class)

    • Previously, Gen.cmp_class loaded S0 via src_f(), which routed VOP3 abs/neg through OpExtInst FAbs / OpFNegate before bitcasting back to %uint. In SPIR-V/Vulkan (and hardware FP units), float FAbs/OpFNegate are not guaranteed to preserve signalling NaNs or denormals.
    • Measured natively on RDNA2 (gfx1036, wave32 & wave64, flush 0x1c0 & ieee 0x3c0 via tools/hw_oracle/): -v0, |v0|, and -|v0| on sNaN (0x7f800001 / 0xff800001) all classify as 0x001 (sNaN, never quieted to qNaN) and subnormals stay subnormal.
    • Gen.cmp_class now reads the unmodified uint via src_u() and applies abs (& 0x7FFFFFFF) and neg (^ 0x80000000) bitwise, eliminating the float round-trip while preserving OpFNegate/FAbs elsewhere (e.g. tests/test_rcp_iflag_mods.py).
  2. v_cmp_class_f64 / v_cmpx_class_f64 (tools/rdna2spirv.py Gen.cmp_class_f64)

    • Implements binary64 classification in pure 32-bit unsigned integer ops on (lo, hi) so no OpCapability Float64 is required.
    • Matches every rule measured natively on gfx1036 (wave32 & wave64, flush & ieee):
      • Same 10 class bits (S1[9:0]), mask bits [31:10] ignored.
      • abs/neg applied bitwise on hi[31], keeping binary64 sNaN signalling.
      • Inline integer constants -16..64 sign-extend to 64 bits (1 -> +denormal, -1 -> qNaN, 0 -> +0.0).
      • 32-bit literals occupy the upper 32 bits (lo = 0, hi = lit), compatible both with current parser output and with PR fix(shader): place f64 literal words in the upper half (SHADER-366) #2057's f64_literal flag.
      • Inline float constants (0.5, -1.0, etc.) expand to exact binary64, null reads as +0.0, SGPR/VGPR pairs supported.
      • v_cmpx_class_f64 narrows %active (EXEC) without touching VCC, and inactive lanes write 0 to the destination mask.
  3. Tests & documentation

    • tests/semantic/test_valu_f32_class_rndne.py: added test_snan_source_modifiers_keep_snan verifying -v1, |v1|, -|v1| on sNaN inputs for v_cmp_class_f32_e64 and v_cmpx_class_f32_e64 and asserting no OpFNegate/FAbs is emitted.
    • tests/semantic/test_cmp_class_f64.py: semantic tests covering all 10 binary64 classes (including low-word-only significands), bitwise neg/abs on sNaN/qNaN/zeros/subnormals/infs, inline constants, 32-bit literals, SGPR pairs, null, ignored upper mask bits, wave64, v_cmpx_class_f64 EXEC narrowing & VCC preservation, and absence of OpCapability Float64.
    • docs/design.md: added the hardware-confirmed contract row.

Verification

  • Disclosure: AI-assisted.
  • Hardware / Driver: AMD Radeon(TM) Graphics (gfx1036), Windows driver 32.0.21042.62 (native RDNA2 ISA probes via tools/hw_oracle/).
  • Tests run:
    • python -m unittest discover -s tests/semantic -p test_valu_f32_class_rndne.py -v: 19/19 OK
    • python -m unittest discover -s tests/semantic -p test_cmp_class_f64.py -v: 5/5 OK
    • python -m unittest discover -s tests/semantic -p test_vop3_const_mods.py -v: 10/10 OK
    • python -m unittest discover -s tests/semantic -p test_value*.py -v: 31/31 OK
    • python -I tests/test_rcp_iflag_mods.py -v: 5/5 OK
    • python -I tests/test_f32_fuzz.py -v (with live gfx1036 hardware test): 25/25 OK
    • python -I tools/f32_fuzz.py --oracle translator,hw --ops v_cmp_class_f32 --programs 200 --seed 1: 200/200 translator match (0 mismatch), 212/212 gfx1036 hardware match (0 mismatch, 12 denorm skipped by model)
    • python -m compileall -q make_game.py tools tests + tests/semantic/regen_fixtures.py --check + test_docs_links.py, test_docs_list_items.py, test_docs_repo_paths.py, test_tool_startup.py, test_tool_stdout_encoding.py, test_text_write_line_endings.py, test_shader_checks.py (60/60): all OK

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent Vulkan run at 0599b38 on RTX 5070 / Windows 11 (driver 621.24, physical subgroups 32..32). I reran the same synthetic wave32 and emulated-wave64 probes used to review the other class-compare PRs.

The wave64 results here cover NVIDIA's emulated path.

@timelery

Copy link
Copy Markdown
Contributor Author

Closing in favour of #2542 (SHADER-457, claimed earlier in #2300) and #2535 (\�_cmp_class_f64\ / \�_cmpx_class_f64, also claimed earlier in #2300). Posting our independent native RDNA2 (\gfx1036) hardware verification measurements on #2535 so it has silicon confirmation.

@TurboSosiska304

Copy link
Copy Markdown

Verified on Linux (CachyOS), Python 3.14.10, CPU only. An AMD RX 570 is present but was not used: no Vulkan or hardware-oracle runs were attempted, so the gfx1036 hardware claims (tools/hw_oracle, f32_fuzz with the hw oracle) could not be re-checked here.

Head tested: 0599b38

Commands and results:

  • python3 -m unittest discover -s tests/semantic -p test_cmp_class_f64.py: 5 tests OK
  • ... -p test_valu_f32_class_rndne.py: 19 tests OK
  • ... -p test_vop3_const_mods.py: 10 tests OK
  • ... -p test_value*.py: 31 tests OK
  • python3 -I tests/test_rcp_iflag_mods.py: 5 tests OK
  • full suite, python3 -m unittest discover -s tests/semantic: 2573 tests OK (128 s)

Negative control (tools/rdna2spirv.py replaced with origin/main 4947579): test_cmp_class_f64.py fails with 49 subtest errors, 48 "rdna2spirv.Unsupported: v_cmp_class_f64" and 1 for v_cmpx_class_f64; test_valu_f32_class_rndne.py fails 9 subtests, e.g. test_snan_source_modifiers_keep_snan (-|v1|) reporting "OpFNegate unexpectedly found". Branch file restored afterwards; worktree clean.

GPU-dependent paths (hardware oracle, Vulkan shader checks) were intentionally skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants