Skip to content

shader: lower gfx1013 binary16 v_cmp and v_cmp_class_f16 in pixel_aot - #612

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
Neoplayer:shader/v-cmp-f16
Nov 22, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
Neoplayer:shader/v-cmp-f16

Conversation

@Neoplayer

Copy link
Copy Markdown
Contributor

Change

Lower all 34 gfx1013 binary16 comparison and classification instruction forms (17 VOPC _e32 + 17 VOP3 _e64) in tools/pixel_aot.py, synchronize tools/scan_unsupported_shaders.py, and add binary + DXC/WARP draw tests and negative controls in tests/pixel_aot_check.py and tests/form_coverage_check.py:

  1. tools/pixel_aot.py:
    • Add class16(uint bits) in PREAMBLE (modeling all 10 IEEE binary16 SIInstrFlags::ClassFlags bits on bits [15:0]: signaling NaN, quiet NaN, -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf), and fix the missing opening parenthesis in class32 (if (magnitude != 0u)).
    • Add f16_cmp_operand() supporting binary16 inline constants (INLINE_F16), integer inline patterns, 16-bit literals (rejecting > 0xffff), SGPR/VGPR sources, and VOP3 _e64-only abs/neg modifiers (for _e32, modifiers raise ValueError("Input modifiers require VOP3 encoding")).
    • Add lower() branches for v_cmp_class_f16(_e32|_e64)? and all 16 v_cmp_<COMPARE>_f16(_e32|_e64)? predicates.
  2. tools/scan_unsupported_shaders.py:
    • Accept plain v_cmp_class_f16/v_cmp_<op>_f16 (with optional _e32) and modified v_cmp_class_f16_e64/v_cmp_<op>_f16_e64 (-, |...|, abs(...)) in SUPPORTED_INSTRUCTIONS_REGEX.
  3. tests/pixel_aot_check.py & tests/form_coverage_check.py:
    • Add F16_COMPARE_SETS, f16_compares(), f16_compares_reference(), and check_f16_compare_draws() covering dirty high halfwords, ±0.0, ±denorm (0x0001/0x8001), ±inf, quiet/signaling NaNs, inline constants (-4.0..4.0, 0.15915494/0x3118, integer 1/-1), _e64 input modifiers, and R32G32B32A32_UINT (integer_rt=True) expectations plus wrong-bit and shader-mutation controls (lt -> le swap, dropped - on _e64, dropped - on v_cmp_class_f16_e64).
    • Register (uncompressed_export(f16_compares()), translate_int) in check_binary(), update rejected neighbor controls to v_cmp_class_f64_e32/v_cmp_lt_f64_e64, and update tests/form_coverage_check.py (len(compare_forms) == 90).

Verification

Commit under test: 857b893 on macOS arm64 with Homebrew LLVM 15.0.7 (-mcpu=gfx1013) and Python 3.12.13:

  • FORM_COVERAGE_RECORD=/tmp/x2w_f16_cov.txt python3 -B tests/pixel_aot_check.py binary llvm-mc llvm-objcopy libLLVM-15.dylib: exit 0; recorded all 34 binary16 compare/class forms (17 _e32 + 17 _e64).
  • python3 -B simulation executing emitted HLSL (widen16, class16, main) against f16_compares_reference() across all 14 F16_COMPARE_SETS: All 14 sets match emitted HLSL bit-for-bit! and all 3 shader mutations caught.
  • LLVM_LIB=libLLVM-15.dylib LLVM_MC=llvm-mc LLVM_OBJCOPY=llvm-objcopy python3 -B tests/rdna2_tools_check.py: Ran 50 tests in 0.492s — OK (0 skips).
  • python3 -B tests/form_coverage_check.py: form coverage checks passed.
  • All other pure-Python and binary CI checks runnable without Windows (compute_store_check.py binary, depth_mask_check.py binary, mesh_*_check.py binary, sampled_coverage_check.py binary, ffx_dispatch_aot_check.py, awc_metadata_check.py, bc7_*, bcpack_check.py, bidi_controls_check.py, dependency_paths_check.py, docs_links_check.py, dxcontainer_part_offsets_check.py, dxil_retarget_check.py, gpu_report_format_check.py, layout_chunks_check.py, pso_readiness_check.py, resource_ranges_check.py, resource_root_check.py, script_state_check.py, sgd5_effects_check.py, shader_probe_check.py, xmem_attribute_scan_check.py, intake/paths_check.py): exit 0.
  • git diff --check: exit 0.

MSVC, D3D12, DXC, and WARP runtime execution (pixel_aot_warp / check_f16_compare_draws) were not run on this macOS host and will be exercised by Windows CI.

Evidence and follow-up

All fixtures are source-built synthetic sequences verified with LLVM 15.0.7 -mcpu=gfx1013.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (No docs or build requirements changed.)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

Copy link
Copy Markdown
Contributor

Independent Windows GPU run on 857b89304ce46daea5618c2ca9a2ba1af2996546. Windows build 26300.9974, Windows SDK 10.0.26100.0 DXC 1.8.2502.11. RTX 4060 driver 32.0.16.1088 and WARP 10.0.26100.9818. D3D12 debug layer enabled.

cmake --build out/partial --target offscreen_check
ctest --test-dir out/partial --no-tests=error -R '^pixel_aot_predicate_rounding_(hardware|warp)$' --output-on-failure

WARP passed (7.49 s), including the four wrong-reference cases and three shader mutations. RTX 4060 failed (4.36 s) at the ninth positive FP16 input (F16_COMPARE_SETS[8], batch case 12). The other 13 positive draws and all four wrong-reference checks reported the expected statuses. The fixture raises at the batch status check, before reaching the three shader mutations on hardware.

At pixel (0, 0), channel 1 expected 0x0000ff00 and read 0x0002ff00. The only differing bit is 17, produced by v_cmp_le_f16_e64 vcc_lo, -|s44|, 2.0. Here s44 is 0x00007e00 (quiet NaN). The generated comparison is (-abs(widen16(asuint(params[0].w)))) <= widen16(16384u), and the reference expects false.

I reran that case alone twice per backend: the original shader failed both times on RTX 4060 and passed both times on WARP. In a local HLSL-only diagnostic I guarded just that comparison with (asuint(params[0].w) & 0x7fffu) <= 0x7c00u. Both backends then passed both runs. All isolated runs reported zero debug errors and warnings. This localizes the observed difference to the ordered NaN comparison in this case; I did not establish which compilation stage introduces it or test the guard as a general fix.

AI-assisted.

@MarlomMedeiros MarlomMedeiros left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community review + independent run on a second adapter, since the PR notes MSVC/D3D12/DXC/WARP were not exercised on the authoring macOS host.

Environment

  • Commit under test: 857b893 on base e3946dd
  • Windows 11 build 26300.10075, VS 2026 MSVC 19.52, Ninja, DXC from SDK 10.0.26100
  • ctest --preset dev --no-tests=error -R '^pixel_aot_predicate_rounding_(warp|hardware)$' -V: both passed
  • Hardware: NVIDIA GeForce RTX 5070 (vendor=10de device=2f04), driver 32.0.16.2135, D3D12 debug layer enabled, debug_errors=0 debug_warnings=0 on every draw
  • WARP: Microsoft Basic Render Driver, driver 10.0.26100.10068
  • tests/pixel_aot_check.py controls, tests/form_coverage_check.py, and the LLVM 15.0.7 binary round-trip (binary mode) all pass on this commit

The f16 compare draw runs inside pixel_aot_predicate_rounding_* through check_f16_compare_draws: all 14 F16_COMPARE_SETS cases passed pixel-exact on the integer readback on both adapters, the 4 corrupted-reference controls reported pixel_mismatch, and all 3 shader mutants (e32 predicate flip, e64 dropped neg, class dropped neg) were detected on both backends.

Review notes

  • The lowering mirrors the existing v_cmp_*_f32/v_cmp_class_f32 arms faithfully: same LANE_MASK destination handling, same ("condition", ...) tuple shape, same mask != "entry" and pending-load guards, modifiers gated to _e64 through f16_cmp_operand, widen16/vop3p_f16_word/INLINE_F16 reused rather than duplicated.
  • class16 decomposes the f16 15:0 bits correctly: sNaN/qNaN split on the quiet bit, inf at 0x7c00, normal from 0x0400, denormal and zero below, sign folding the negative classes. The reference test asserts the raw 0x7f800000-emptiness invariant per set.
  • Reject controls look non-vacuous: e32 modifiers, >16-bit literals, clamp/op_sel suffixes, v_cmpx variants, mid-program and pending-load placements, plus a modifier-on-the-class-mask case that correctly routes through int_operand instead.
  • One consistency note: a class mask wider than 10 bits (> 0x3ff) is accepted and emits dead bits, same as the v_cmp_class_f32 arm. Not a defect, just parity.
  • Interaction note for #651 (exec-masked regions, not yet merged): these f16 arms write the same ("condition", ...) tuples s_and_saveexec_b64 consumes, so they would be able to open masked regions automatically once both land. Inside a region the v_cmp deny prefix still rejects them, which is the intended behavior. No conflict, just a free capability.
  • Heads-up on base age: the branch sits on e3946dd, 15+ commits behind current main (8f57366). The cmp arm chain has moved since, so a rebase may hit minor textual conflicts in lower().

No blockers found.

@MarlomMedeiros

Copy link
Copy Markdown
Contributor

AI-assisted: yes. All reported checks ran locally on the adapter and driver listed.

@thepatrickworld

Copy link
Copy Markdown
Contributor

@Neoplayer Independent AMD (RDNA4) result on 857b893. Measurements only, no code change.

Environment

  • Windows 11 25H2, build 26200.9711
  • MSVC 19.44.35230 (toolset 14.44.35207), Windows SDK 10.0.26100.0, DXC 1.9.2602.17
  • AMD Radeon RX 9070 XT, vendor 1002, device 7550, driver 32.0.32029.2010, native wave width 64
  • debug_layer=unavailable (Tools.Graphics.DirectX is NotPresent here)
  • Clean worktree of the PR head 857b893, base 7b55abcd.

Commands

cmake --preset dev
cmake --build --preset dev --parallel 12 --target offscreen_check
set XBOX2WINDOWS_ADAPTER=AMD Radeon RX 9070 XT
ctest --preset dev --no-tests=error -R "^pixel_aot_|^rdna2_tools"

Observed

21 tests, 21 passed, 0 failed, 0 skipped (113.36 s): rdna2_tools_check, ten _hardware suites (pixel_aot, buffer_color, u32_from_f32, attribute, three_way, predicate_rounding, blend_scalar, e64, integer_nc, rndne) and their ten _warp counterparts. This tree has no pixel_aot_bfm entry yet, hence 21 rather than 23.

Main draw suite:

2/21 Test #226: pixel_aot_hardware ... Passed  18.29 sec
12/21 Test #250: pixel_aot_warp ..... Passed   9.55 sec
  • 47 draws on hardware, 46 at pixels=4096 and one at pixels=64, every one max_absolute_error=0 (4 channels per pixel).
  • 47 draws on WARP, same split, max_absolute_error=0.

Why this is worth measuring on this adapter

Two things here are vendor-sensitive by nature:

  1. binary16 comparisons and classification — half-precision flush, rounding and NaN/inf classification can differ per adapter, and the class16 helper models all ten IEEE binary16 class bits. A wrong classification would show as a mismatched pixel, not a compile error. None appeared across 16,384 channels per draw on both backends.
  2. The class32 fix — you add the missing opening parenthesis (if (magnitude != 0u)) to a helper that all existing pixel suites already use. The other nine hardware suites in this run still pass with the corrected preamble, so the fix does not perturb the 32-bit classification path that was already in use.

Limit

The negative controls are in tests/pixel_aot_check.py, and the CTest entries above exercise the draw path. I did not run the standalone binary mode, which needs LLVM with gfx1013 support — not installed on this machine. So this run confirms the draws and the registered tool check, not the assembly-level controls.

AI-assisted; runs executed locally.

Copy link
Copy Markdown
Contributor

Independent RTX 3060 Ti result on 857b89304ce46daea5618c2ca9a2ba1af2996546. I checked whether the RTX 4060’s FP16 NaN comparison mismatch also occurs on Ampere, using the PR’s synthetic fixture.

Windows 10 22H2 build 19045.6466, MSVC 19.44.35230, Windows SDK 10.0.26100.0, DXC 1.8.2502.11. D3D12 debug layer unavailable on both backends. The Debug build used a static CRT (/MTd).

cmake --preset dev "-DINTAKE_PYTHON=$((Get-Command python).Source)" "-DDXC_EXECUTABLE=$dxc" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug
cmake --build --preset dev --parallel 4 --target offscreen_check
$env:XBOX2WINDOWS_ADAPTER = 'RTX 3060 Ti'
ctest --preset dev --no-tests=error -V -R '^pixel_aot_predicate_rounding_(hardware|warp)$'
  • Hardware — failed twice. The renderer identified NVIDIA GeForce RTX 3060 Ti (10de:2414), driver 576.02 (32.0.15.7602 via DXGI). At pixel (0, 0), channel 1 of F16_COMPARE_SETS[8] (batch case 12) expected 0x0000ff00 and read 0x0002ff00. Only bit 17 differed. The other 13 positive FP16 cases and all four wrong-reference controls behaved as expected. Two isolated --check-pixel-case runs reproduced the same mismatch.
  • Software — passed twice. In-box WARP identified Microsoft Basic Render Driver (1414:008c), driver 10.0.19041.5794. Two isolated runs of case 8 also matched all 4,096 pixels exactly.
  • Skipped — zero CTest skips. The hardware batch failure stopped it before the three FP16 shader mutation controls.

The same shader program was rejected during translation at the branch point e3946dd and target base 7b55abc, both at v_cmp_f_f16_e32. The base comparison therefore ended before GPU readback.

AI-assisted. All reported runs were executed locally.

@Neoplayer

Copy link
Copy Markdown
Contributor Author

Thanks for the independent runs. The RTX 4060 and RTX 3060 Ti failure on F16_COMPARE_SETS[8] (-|s44| <= 2.0 with s44 a quiet NaN, bit 17) is real; it is a float NaN comparison folded differently by the NVIDIA compiler, as the diagnostic showed.

Fix in 32dd77b (rebased onto 719552e): every ordering-dependent v_cmp_*_f16 predicate (lt eq le gt lg ge o u and the n* forms) is now guarded by num16(bits), which decides "not a NaN" from the binary16 encoding ((bits & 0x7fff) <= 0x7c00) with abs/neg applied to the bits, instead of relying on float NaN semantics. f and tru are unchanged.

Run on macOS arm64 (LLVM 15.0.7) after the rebase: pixel_aot_check.py binary exit 0, form_coverage_check.py passed, rdna2_tools_check.py 50 tests OK, git diff --check clean. I have no NVIDIA hardware, so the draw suites were not run locally; could someone re-run ^pixel_aot_predicate_rounding_(hardware|warp)$ on the RTX cards?

AI-assisted.

Copy link
Copy Markdown
Contributor

RTX 3060 Ti retest on Windows 10 22H2 build 19045.6466, NVIDIA 621.24 (32.0.16.2124 via DXGI, adapter 10de:2414), MSVC 19.44.35230, SDK 10.0.26100.0, and DXC 1.8.2502.11. The D3D12 debug layer was unavailable on both backends.

cmake --preset dev "-DINTAKE_PYTHON=$((Get-Command python).Source)" "-DDXC_EXECUTABLE=$dxc" -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug
cmake --build --preset dev --parallel 4 --target offscreen_check
$env:XBOX2WINDOWS_ADAPTER = 'RTX 3060 Ti'
ctest --preset dev --no-tests=error -V -R '^pixel_aot_predicate_rounding_(hardware|warp)$'
  • Hardware: Original 857b89304ce46daea5618c2ca9a2ba1af2996546 still failed on 621.24. At pixel (0, 0), channel 1 of F16_COMPARE_SETS[8] expected 0x0000ff00 and read 0x0002ff00. Fixed head 32dd77be451198a5b2337c65ff669c6678d81b37 passed the full test.
  • Isolated control: Holding the renderer and case file fixed, the immediate parent’s translator (6b33fbf) reproduced the mismatch twice. The fixed translator matched all 4,096 pixels twice.
  • Software: In-box WARP (10.0.19041.5794) passed on both full checkouts. Skipped: zero CTest skips.

AI-assisted. All reported runs were executed locally.

@R0Y00
R0Y00 merged commit d575743 into R0Y00:main Nov 22, 2026
5 checks passed
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.

6 participants