Skip to content

shader: lower the binary16 SDWA comparisons - #1680

Closed
rm-NoobInCoding wants to merge 3 commits into
R0Y00:mainfrom
rm-NoobInCoding:shader/sdwa-f16-compare
Closed

rm-NoobInCoding wants to merge 3 commits into
R0Y00:mainfrom
rm-NoobInCoding:shader/sdwa-f16-compare

Conversation

@rm-NoobInCoding

@rm-NoobInCoding rm-NoobInCoding commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Change

tools/pixel_aot.py lowered the SDWA comparisons only for binary32, with whole-dword selections. This PR lowers the 16 binary16 predicates v_cmp_{f,lt,eq,le,gt,lg,ge,o,u,nge,nlg,ngt,nle,neq,nlt,tru}_f16_sdwa and v_cmp_class_f16_sdwa:

  • Selections: WORD_1 selects the high half of a source. WORD_0 and DWORD leave the low half, which is what the e32 form reads. So a comparison with both selections DWORD and no modifier emits the same HLSL as its e32 spelling, and a control asserts this.
  • Modifiers: -, |x| and -|x| apply after the selection.
  • Comparison: the existing binary16 comparison then runs on the result. The ordered relations test num16 on the selected, modified encodings and compare the widened values. The class test reads class16 of the selected, modified half against the mask register. f16_cmp_operand, f16_comparison and f16_class gain an optional high-half selection; existing callers are unchanged.

Refused:

  • byte selections and sext();
  • inline constants and literals (LLVM 15 prints them as neg(2.0) and similar);
  • a missing selection, and exec as the destination;
  • a class mask with a selection other than DWORD, or with a modifier;
  • the v_cmpx SDWA forms.

tools/scan_unsupported_shaders.py keeps its own patterns, so it accepts the same forms. test_sdwa_f16_compare_instructions checks the accepted and refused spellings.

Coverage: +17 gfx1013 and gfx1030 SDWA forms.

Verification

Base c864f138, patch a8a67061. Windows 11 build 26300, dev preset, DXC 10.0.26100.0, NVIDIA GeForce RTX 4070 (driver 32.0.16.1714) and WARP.

cmake --preset dev
ctest --preset dev --no-tests=error -R '^pixel_aot_sdwa_f16_compare_' --output-on-failure
python -B tests/pixel_aot_check.py
python -B tests/rdna2_tools_check.py

plus every platform-independent Python check listed in AGENTS.md.

  • pixel_aot_sdwa_f16_compare_hardware and pixel_aot_sdwa_f16_compare_warp: 2/2 passed, no skips. Both coverage records list all 17 forms.
  • pixel_aot_check.py static controls, including the new check_sdwa_f16_compare_lowering: passed.
  • rdna2_tools_check.py: OK, 18 skipped without LLVM.
  • All 49 platform-independent Python checks listed in AGENTS.md: passed.

The fixture (tests/pixel_aot_check.py draw-sdwa-f16-compare):

  • Output. One program exports four words through the R32G32B32A32_UINT readback target. Three words hold the 16 predicates as bits under (src0, src1, src0_sel, src1_sel) = (A, B, WORD_1, WORD_0), (-B, |A|, WORD_1, DWORD) and (-|A|, B, DWORD, WORD_1). The fourth word holds four class tests of C against the mask D.
  • Inputs. A, B and C carry binary16 values in both halves, so a selection that reads the wrong half changes the answer. Ten sets cover ordered pairs both ways, equal values, signed zeros, infinities, denormals, quiet and signaling NaNs in either operand, and adjacent values. A static check asserts that every predicate bit other than f and tru, and every class bit, takes both values over the sets.
  • Negative controls. For each wrong model (selections ignored, modifiers ignored), the first set it changes is drawn against that model's words and must report pixel_mismatch. That is 12 draws from one compile.
  • Lowering negative control. Making the lowering ignore WORD_1 makes pixel_aot_sdwa_f16_compare_warp fail.

LLVM 15 round trip: not run locally (no LLVM with gfx1013 on this host). The first commit did not put the new forms through it: check_binary assembled no v_cmp_*_f16_sdwa program. A later commit adds the sdwa_f16_compares program to its fixtures, so the Assemble and translate gfx1013 fixtures job assembles it, disassembles it with LLVM 15, and requires the disassembly to translate exactly like the source text. After review, check_sdwa_f16_compare_lowering also pins each selection (src0, src1 and the class test) at text level, so a lowering that ignores any one of them fails without a GPU.

Evidence and follow-up

All inputs are synthetic. The draws show that the translated shader computes the stated comparisons on desktop GPUs, not that the console does. Related: #1670 lowers the integer SDWA comparisons. The two PRs touch neighbouring code but are independent. The v_cmpx_*_sdwa forms remain uncovered.

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.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

v_cmp_*_f16_sdwa (16 predicates) and v_cmp_class_f16_sdwa were
refused. WORD_1 now selects the high half of a source and WORD_0 or
DWORD the low half the e32 form reads; -, |x| and -|x| apply after the
selection, and the existing binary16 comparison and class16 test run on
the result. Registers only; byte selections, sext(), constants, a class
mask with another selection or a modifier, and v_cmpx stay refused.

draw-sdwa-f16-compare exports the 16 predicates under three selection
and modifier mixes and four class tests on ten input sets whose halves
both carry binary16 values, with selections-ignored and
modifiers-ignored references that must mismatch. The shader scanner
accepts the same forms.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of head a8a670616c9f2b9f4ce679094d391e3fb0c6cbf2.

Linux limits. Linux x86_64, llvm-mc-19 (CI pins 15), Python 3.14. No Windows, no DXC, no GPU, so I could NOT run the new draw-sdwa-f16-compare / pixel_aot_sdwa_f16_compare_<adapter> tests. The NaN, -0/+0 and sign-handling claims are only checked by those draws; I checked the translation text and the machine encodings.

What I ran

  • New static check check_sdwa_f16_compare_lowering() from tests/pixel_aot_check.py, called directly: passes in 0.1 s.
  • Binary: assembled the SDWA forms with llvm-mc-19 -mcpu=gfx1010 (WORD_1/WORD_0/DWORD, -v, |v|, -|v|, SGPR destination, v_cmp_class_f16_sdwa with WORD_1 and DWORD) and decoded each back with the in-tree disasm_rdna2.decode_instruction: all round-trip to the same text. sext(v18) is not accepted by llvm-mc-19 for these float compares, matching the PR's refusal.
  • Mutation 1 (src0 high ignored: highs=(False, sel1 == "WORD_1")): the static check FAILED, as intended.
  • Mutation 2 (src1 high ignored: highs=(sel0 == "WORD_1", False)): the static check STILL PASSED.
  • Mutation 3 (class test ignores WORD_1: high=False): the static check STILL PASSED.
  • Everything restored with git checkout -- ..

Findings

  • The only text-level WORD_1 assertion covers src0 of v_cmp_lt. A wrong src1 selection and a wrong class-test selection are caught only by the GPU draws (SDWA_F16_SETS and SDWA_F16_WRONG_MODELS cover src1 and class), which run on Windows. On a Linux or no-GPU run they are unguarded. A one-line text assertion for src0_sel:WORD_0 src1_sel:WORD_1 and one for v_cmp_class_f16_sdwa ... src0_sel:WORD_1 would close that.
  • -, |x|, -|x| are applied after the high-half selection (f16_cmp_operand, (word >> 16u) first, then abs/neg on the 16 bits). That is the SDWA order I expect; confirm with the draws.
  • NaN handling: the ordered relations require num16 of both, the negated forms are !ordered || !rel, so a NaN in either half is unordered. -0 vs +0 compares equal through the widened float. Both rely on the existing f16_comparison, whose behavior the new draws exercise; I did not re-derive them on a GPU.
  • Refusals (byte selections, sext, constants, v_cmpx, class mask with WORD selection, exec as destination, v106) are covered by must_reject. tools/scan_unsupported_shaders.py has two new regexes that match the same shapes.

Verdict: the lowering looks right, and the binary forms are valid. The Linux-runnable guard is weaker than the PR text suggests. Not approving.

AI-assisted; commands listed were executed locally.

The static check asserted WORD_1 only for src0 of v_cmp_lt_f16_sdwa, so
a lowering that ignored the src1 selection or the class-test selection
was caught only by the GPU draws. Assert the exact operands of a
src1-only WORD_1 comparison, and of class tests with WORD_1 and WORD_0
(the modifier applied after the selection, the mask read whole), so
each selection is checked without a GPU.
@rm-NoobInCoding

Copy link
Copy Markdown
Contributor Author

Thanks, agreed: the text-level guard only covered src0. Fixed in e182ad78 (tests only, no lowering change).

check_sdwa_f16_compare_lowering now pins the exact operands of each selection, so a GPU is no longer needed to catch them:

  • src0_sel:WORD_1 src1_sel:WORD_0: num16/widen16 of A's high half, and B's low half with no shift;
  • src0_sel:WORD_0 src1_sel:WORD_1: A's low half with no shift, and num16/widen16 of B's high half;
  • v_cmp_class_f16_sdwa vcc_lo, -v18, v16 src0_sel:WORD_1 src1_sel:DWORD: the whole line, class16(((A >> 16) & 0xffff) ^ 0x8000) & B, so the negation is applied after the selection and the mask is read whole;
  • the same class test with WORD_0: the low half, no shift.

Your three mutations, rerun against e182ad78 on Windows (Python 3.14):

Mutation Static check
highs=(False, sel1 == "WORD_1") fails
highs=(sel0 == "WORD_1", False) fails (passed before)
class high=False fails (passed before)

With the lowering restored: pixel_aot_check.py static controls and form_coverage_check.py pass, and pixel_aot_sdwa_f16_compare_hardware (RTX 4070) and _warp pass 2/2.

check_binary assembled no v_cmp_*_f16_sdwa program, so the LLVM 15 job
never saw the new forms. Add the sdwa_f16_compares program to its
fixtures: it is assembled, disassembled with LLVM 15 and must translate
exactly like its source text.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of head 377919df1c8212935299b297f572289892686f97 (3 commits on main: the lowering a8a67061 I reviewed before, plus e182ad78 and 377919df, both tests only; the delta from a8a6706 is +24/-5 in tests/pixel_aot_check.py).

Linux limits. Linux x86_64, llvm-mc-19 and llvm-objcopy-19 (CI pins 15), Python 3.14. No CMake configure and no ctest (the two new pixel_aot_sdwa_f16_compare_<adapter> registrations are unexercised), no Windows, no DXC, no GPU. The draw-sdwa-f16-compare draws, the "12 draws from one compile" and the RTX 4070 and WARP results are not reproduced by me. The NaN, signed-zero and denormal behaviour is only checked by those draws.

Earlier findings, now addressed. My review of a8a6706 showed that ignoring WORD_1 on src1, or in the class test, still passed the text-level check. e182ad78 pins the exact operands for both selections and for the class test with and without the modifier; 377919df adds the SDWA program to the binary fixtures.

What I ran on this head

  • check_sdwa_f16_compare_lowering() called directly: PASS.
  • The new fixture round trip, run by itself: sdwa_f16_compares() (234 instructions, 52 SDWA lines) assembled with llvm-mc-19 -mcpu=gfx1013, objcopy'd, disassembled with the in-tree disassemble() against libLLVM 19, then translate_int of the decoded text equals translate_int of the source text. The full binary mode with llvm-mc-19 stops earlier in check_f16_cvt_int16_binary (v_cvt_f16_i16_e32 v1, 0xfff0, an unrelated check; likely LLVM 19 against 15 formatting, which I did not confirm), so I did not get a full binary pass.
  • Four local mutations of tools/pixel_aot.py, each run against the static check and the round trip, then restored (git status clean):
    • src1 WORD_1 ignored: FAIL, the assertion prints the offending bool cmp22 line (this passed on a8a6706).
    • class test ignores WORD_1: FAIL (also passed on a8a6706).
    • src0 WORD_1 ignored: FAIL.
    • WORD_1 shift 16 -> 8: FAIL.
  • git diff --check origin/main...HEAD: clean; no non-ASCII bytes added. tests/rdna2_tools_check.py (OK, 18 skipped), tests/bidi_controls_check.py and tests/docs_links_check.py: pass. git merge-tree against current main (68969ed8): no conflicts.

Findings

  • None blocking. The text-level guard now distinguishes src0, src1 and class selections, and modifiers applied after the selection. The lowering matches the stated semantics (WORD_1 is the high half, WORD_0 and DWORD the low half; class mask read whole).
  • Minor. The docs say the LLVM 15 round trip was not run on the author's host; I could not run 15 either, so the CI llvm-mc-15 path for the new fixture is still unconfirmed. The draw results above stay the author's measurements.

AI-assisted review; commands listed were executed locally.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

This PR currently has merge conflicts with main. Could you please rebase it onto the latest main so it can be checked for merge?

AI-assisted.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

I tried to bring this branch up to date with main so it could go through the full cycle. It no longer merges cleanly, and the conflict is not a mechanical one: #1790 (merged 2026-11-24) already lowers the same slice, the 16 binary16 v_cmp_*_f16_sdwa predicates and v_cmp_class_f16_sdwa, in tools/pixel_aot.py (f16_comparison_bits, f16_class_bits), and its lot also covers the f32 and 16/32-bit integer SDWA compares.

Resolving it would mean choosing between two implementations of the same lowering, so I did not push anything to this branch. Leaving the decision to @R0Y00 and @rm-NoobInCoding. If this PR still adds something that main lacks (for example the WORD_1 high-half selection tests or the scanner entries in tools/scan_unsupported_shaders.py), a rebuild of the diff on top of current main containing only that would be the smallest path.

Commands executed: git merge origin/main in a scratch worktree (conflicts in tools/pixel_aot.py, tests/pixel_aot_check.py, tests/rdna2_tools_check.py, docs/experiments.md), then git merge --abort; git log -S f16_comparison_bits origin/main.

AI-assisted; commands listed were executed locally.

@R0Y00

R0Y00 commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Main already covers the 16 binary16 SDWA predicates and the binary16 class form in #1790, with source-field selection, distinct mask destinations, and a binary round trip. The current comparison controls pass. Thanks for the NaN, signed-zero, and half-selection investigations. Any extra discriminating rows can extend the existing fixture directly. Closing this implementation as superseded.

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