Skip to content

rdna2spirv: read v_cmp(x)_class_f16 mask as f16 constant via src_h - #3894

Open
Wasler wants to merge 1 commit into
ItsNotRexed:mainfrom
Wasler:fix/cmp-class-f16-mask
Open

Wasler wants to merge 1 commit into
ItsNotRexed:mainfrom
Wasler:fix/cmp-class-f16-mask

Conversation

@Wasler

@Wasler Wasler commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in #3796: #3796 (comment)

What and why

Gen.cmp_class (tools/rdna2spirv.py:3266) read the class-mask operand (a[-1]) through self.src_u(a[-1]) for all three float widths (f32, f16, f64). On RDNA2 (gfx1036, previously measured via tools/sdwa_diff.py --oracle hw and pinned in tests/test_sdwa_diff.py:58), v_cmp(x)_class_f16 encodes float inline constants in the mask operand as 16-bit binary16 values in the low half over a zero high half (0.5 -> 0x00003800, 1.0 -> 0x00003C00, -2.0 -> 0x0000C000, 0.15915494 -> 0x00003118) before sdwa_src selection.

Reading a[-1] via self.src_u encoded those constants as binary32 (0x3F000000, 0x3F800000, 0xC0000000, 0x3E22F983), which broke:

  • _sdwa sub-dword mask selects (e.g., src1_sel:WORD_1 of 1.0 read 0x3F80 instead of 0, and src1_sel:BYTE_3 of -2.0 read 0xC0 instead of 0; sdwa_diff.py --family f16 --ops v_cmp_class_f16 --programs 500 failed 12/500 on main), and
  • _e64 masks using fractional inline float constants such as 0.15915494 (1/(2*pi)).

Changing line 3266 to self.src_h(a[-1]) if ty == "f16" else self.src_u(a[-1]) makes v_cmp(x)_class_f16 handle float inline constants consistently for both operands (src_h falls back to src_u for VGPR/SGPR/integer immediates while preserving sdwa_sel). Also updated docs/design.md (v_cmp_class_f16 contract row) and removed the stale on main that is v_cmp_class_f16 skip note from tools/sdwa_diff.py.

Tested

  • python -I tools/sdwa_diff.py --family f16 --ops v_cmp_class_f16 --programs 500: 12 mismatches on main (4c52ce27) -> 500 pass, 0 mismatch, 0 skipped after fix
  • python -I tools/sdwa_diff.py --programs 500: 492 pass, 0 mismatch, 8 skipped (8 f16 NaN reference skips)
  • python -m unittest -v tests/semantic/test_alu_cmp_class_f16_f64.py: 5 tests OK (covers _sdwa BYTE_3/BYTE_1/WORD_1 and _e64 / v_cmpx_class_f16_e64 with 0.15915494, validated with spirv-val + spirv_mini_interp)
  • python -m unittest -v tests/test_sdwa_diff.py: 20 tests OK (added 0.5 src1_sel:BYTE_1, 1.0 src1_sel:WORD_1, and -2.0 src1_sel:BYTE_3 assertions)
  • pytest -q tests/test_text_write_line_endings.py tests/test_semantic_translator_use.py clean

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md).
  • Contract table in docs/design.md updated with verification basis.

AI-assisted.

@Atlonsky

Copy link
Copy Markdown

Windows / NVIDIA check at ce72421, before anyone else has looked at this one.

Environment: Windows 11 Pro 26200, Python 3.15.2, NVIDIA GeForce RTX 3080 (Ampere, 32-wide subgroup), driver 616.92 (Vulkan 1.4.351), llvm-mc 23.1.6 (LLVM 23, AMDGPU target).

  • python -I -m unittest discover -s tests\semantic -p test_alu_cmp_class_f16_f64.py → Ran 5 tests ... OK
  • python -I tests\test_sdwa_diff.py → Ran 20 tests ... OK (18 on main, so the two new ones are the ones this PR adds)
  • python -m compileall -q make_game.py tools tests → clean

I did not run the full semantic suite on this head, only the two files the diff touches.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested ce7242107b7db32415c2a884fb3132d033b8386b on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent GPU check passed at ce724210 on Linux, NVIDIA RTX 5090, driver 595.99.02, subgroup 32. I built the head’s Vulkan harness, translated two synthetic pixel shaders, and assembled and validated their SPIR-V for Vulkan 1.2. Each shader took a binary16 word at draw time. I drew one representative of each of the ten classes, including both zeros and both NaN classes. Every RGBA32F pixel matched in all 20 draws (4,096 pixels per draw).

  • For v_cmp_class_f16_sdwa, BYTE_1 of inline 0.5 acted as mask 0x38: negative normal, negative subnormal and negative zero matched. WORD_1 of 1.0 and BYTE_3 of −2.0 matched none.
  • For e64 compare and compare-with-EXEC-update, inline 0.15915494 acted as class mask 0x118: negative normal, negative subnormal and positive normal matched. The signalling NaN 0x7c01 did not. In the EXEC test the shader restored EXEC before export, so both outcomes were observable.

This covers translated shader execution for uniform inputs on a 32-lane NVIDIA subgroup. AI-assisted GPU check (Codex).

AI-assisted verification.

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested ce72421 (on its base main 4c52ce2) on Windows 11, Python 3.11.9: pass for the CPU side.

  • python -I tests/semantic/test_alu_cmp_class_f16_f64.py: 5 tests, OK.
  • python -I tests/test_sdwa_diff.py: 20 tests, OK.
  • Not run: GPU shader checks and tests/test_translate.py. There's no AMDGPU llvm-mc on this machine, so they skip.

AI-assisted (Claude Code).

@chpeu

chpeu commented Nov 22, 2026

Copy link
Copy Markdown

Tested ce724210 on Linux, no game files. main aa2988cc is not in this branch's history (it predates it), so
the totals here are the branch's own 210 programs, which happen to match current main's count.

RTX 3080 (driver 615.71.09, Vulkan 1.4.351), identical to what current main gives on this machine:

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → 195/210 passed, 15 skipped, 0 failed
  • without the variable → 164/210 passed, 46 skipped, 0 failed
  • main aa2988cc, same commands, same machine: 195/210 and 164/210, 0 failed. No movement.

RTX 5080: not run this time. The card is holding ~15.6 GB for another job on this box, so I skipped it rather
than report an out-of-memory failure as if your change caused it. I'll re-run it on this head when the card is free.

Tests you changed: tests/semantic/test_alu_cmp_class_f16_f64.py 5 tests OK, tests/test_sdwa_diff.py
20 tests OK. Translator side, the 28 pytest-marked files as CI runs them: 1593 passed, 41 skipped,
97 subtests passed
, fully green.

One thing to know so my numbers are not read as yours: current main gives 1592 passed, 1 failed with that
same pytest command — the failure is tests/test_u16_high_half.py::test_d16_buffer_store_no_redundant_extract_after_shr16,
where spirv-val says line 153: Operand '135[%135]' requires a previous definition on
%_arr_uint_135 = OpTypeArray %uint %135. It appeared at aa2988cc (the format-size table); the thirteen commits
before it are green on this file here. #3902 is on it, and your branch predates aa2988cc, so nothing of this is
yours.

Machine: Linux Mint 22 (Ubuntu 24.04 base), llvm-mc 20.1.2, SPIRV-Tools 2025.1~rc1 (Ubuntu packages), GCC 13.3,
Python 3.12.3 with pytest 9.2.0 and numpy 2.5.5, built with cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON. python -m compileall -q make_game.py tools tests: clean. AI-assisted
testing, counts read through by hand.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested ce724210 (base 4c52ce27) on Linux (Ubuntu 26.04 container, Python 3.14), NVIDIA RTX 3060 Ti, driver 580.173.02: pass, and the new tests are red on the base.

  • tests/semantic/test_alu_cmp_class_f16_f64.py + tests/test_sdwa_diff.py: 25 passed, 48 subtests passed. With tools/ from 4c52ce27, 5 failed. Among them is CmpClassF16Tests.test_f16_float_const_mask_uses_src_h (1073741824 != 1065353216, so the f16 constant mask was read as f32).
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped. That matches main 4c52ce27 test for test on this card. ctest: 47/47 passed.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@traeb0n

traeb0n commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested ce72421 on RTX 5070, driver 621.01, Windows 11 Pro 26300, MSVC 14.44 (build_runtime.ps1), Vulkan SDK 1.4.363, Python 3.11, llvm-mc 23.1.6 (conda-forge llvm-tools; it assembles the v_lshlrev_b16_e64 … op_sel:[0,1,1] line from the docs): pass.

  • pytest tests/semantic/test_alu_cmp_class_f16_f64.py: 5 passed, 32 subtests passed
  • pytest tests/test_sdwa_diff.py: 20 passed, 16 subtests passed
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, the same per-test results as main, including cs_cmpx_class_f32, ps_vcmp_f16, ps_vcmp_f16_e64, ps_vcmpx_f16 and ps_vcmpx_f16_e64. All 214 shaders pass spirv-val.

Baseline on main aa2988c, same machine: full ctest 149/149 (llvm-mc build), pytest tests/test_translate.py 581 passed, 4 skipped, scripts/run_gpu_tests.py 164/210 passed, 46 skipped, 0 failed. Of the 46 skips, 31 need a 64-wide subgroup (this card is 32-only), 1 needs a required 64-wide fragment subgroup, and 14 are run by their own ctest target.

No game files. AI-assisted (Claude Code).

@ramenbellic

Copy link
Copy Markdown

Tested ce72421 on NVIDIA GeForce RTX 3070, driver 620.70.06, Linux (Bazzite, Fedora 45 base, kernel 7.3.3): pass

  • GPU suite (scripts/run_gpu_tests.py): 164/210 passed, 46 skipped (wave64 tests on 32-wide subgroups), 0 failed native; 195/210 passed, 15 skipped, 0 failed with LEONIDA_W64_EMULATE=1
  • The PR's tests: tests/semantic/test_alu_cmp_class_f16_f64.py: OK; tests/test_sdwa_diff.py: OK. tests/semantic as a whole: 7 failures, 2 errors, the same as main aa2988c alone on this machine (test_cmp_class_f64, test_loop_ballot_barrier, test_mutations), so not from this PR
  • Whole shader corpus (26,503 game shaders, --wave64-emulate), this PR merged onto main f847748: 26,503/26,503 translated, 26,503 pass spirv-val (main alone: the same). No per-shader diff count: the PR is based on a main 1,306 commits newer than that baseline

AI-assisted run (Claude Code); counts only, no game data.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested ce724210 on NVIDIA GeForce RTX 3060 Ti (subgroup 32..32), driver 580.173.02, Linux (kernel 6.18), Python 3.13: the fix is right, and current main still has the bug. I found the same bug independently with sdwa_diff.py and then saw this PR, so these are only results, not a second fix.

Merged with main d0835dd (clean merge):

  • tools/sdwa_diff.py --known --all-failures --family f16 --programs 8000, seeds 775141, 775142 and 775143 (24,000 programs): main 17 mismatches (5, 6, 6), all VCC of v_cmp_class_f16 with SDWA src1_sel. This PR: 0. Default family, seed 775104, 8,000 programs: main 1 mismatch, this PR 0.
  • A standalone check of 5 SDWA src1_sel cases (BYTE_1, BYTE_3 and WORD_1 over an inline float mask, for example v_cmp_class_f16_sdwa vcc, v1, -1.0 src1_sel:BYTE_3 with v1.lo = 0x0001): main 0/5 right, this PR 5/5.
  • pytest tests/semantic/test_alu_cmp_class_f16_f64.py tests/test_sdwa_diff.py: 28 passed, 48 subtests passed.
  • The whole semantic suite (unittest discover -s tests/semantic, 7,457 tests): the same 10 failures and errors as main alone, all from my environment, none new.
  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed, with ps_sdwa, ps_sdwa_dword_hi and ps_sdwa_f16_const 4096/4096 pixels exact.

Current main 7b7d73f: the 5-case check is still 0/5 there, so the bug is not fixed by round 43. GitHub reports a conflict now. It's only in docs/design.md: tools/rdna2spirv.py, tools/sdwa_diff.py and the tests merge cleanly. With main's side of docs/design.md taken, the 5 cases are 5/5, and test_alu_cmp_class_f16_f64.py, test_sdwa_diff.py and test_fmisc_diff.py pass (45 passed, 91 subtests passed).

No game files. AI-assisted (Claude Code).

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.

7 participants