Skip to content

startup: lower gfx2hlsl f32 lg/nlg compares as ordered - #1930

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-f32-lg-ordered
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-f32-lg-ordered

Conversation

@MasterDomino

@MasterDomino MasterDomino commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1920

What / Why

In the gfx10 ISA V_CMP_LG_F32 is "less or greater": ordered and not equal, false when either source is NaN. V_CMP_NLG_F32 is its negation, true when either source is NaN. compare() in tools/startup/gfx2hlsl.py lowered them as a != b and !(a != b). HLSL != is unordered, so both gave the opposite result on every NaN pair, in the plain and _e64 forms of v_cmp and v_cmpx.

Change

  • lg is now (ORDERED && (a < b || a > b)) and nlg is !(ORDERED && (a < b || a > b)). ORDERED is (w & 0x7fffffffu) <= 0x7f800000u for both source words, taken before the neg/abs modifiers (the modifiers never change whether a word is NaN).
  • The bit test is required on the DXC path. DXC without -Gis emits fcmp fast / fcmp one for the plain less-or-greater. On the RX 9070 XT (driver 32.0.31044.16), that build of the cmp fixture returned 1 for both lg and nlg on all 156 NaN pairs. FXC on both backends and DXC on WARP gave the ordered result. This is recorded by the draw (see below). The production path compiles with FXC ps_5_1.
  • Every other condition keeps its text, which the test checks. The f32 SDWA compares stay refused as startup: lower VOPC SDWA compares in gfx2hlsl #1915 left them; only the comment now says why.

Tests

Commit under test: fc9517aa on main b96960b7 (rebased after #1915 merged; the change itself is unchanged from a22cfaa8). After the rebase: startup_gfx2hlsl_* 33/33 including both draws, binary with llvm-mc 15.0.7 PASS.

  • tests/startup_gfx2hlsl_f32_lg_check.py (no arguments, pure Python):
    • Decodes all 30 f32 VOPC opcodes the table lowers (v_cmp and v_cmpx × 15 conditions), in the e32 form and in the e64 form under all 16 neg/abs combinations. That is 510 forms, built as words here.
    • Translates each form and evaluates the emitted HLSL with a small IEEE binary32 evaluator over all 256 pairs of: +-0, +-min denormal, +-1.0, +-max finite, +-inf, +-quiet NaN, +-signalling NaN and +-NaN with a payload.
    • The reference is written from the ISA definitions with integer arithmetic on the bit patterns: ordered relations are false on NaN, the n-forms are their negations, o/u classify, +0 == -0 and denormals are kept.
    • Result: 510 f32 forms x 256 pairs (130560 evaluations) equal the ISA reference: ok.
    • Also checks: EXEC masking when EXEC is reduced, the 13 other conditions' text is unchanged, and all 16 f32 SDWA lg/nlg forms are still refused.
  • binary <llvm-mc> / decode <libLLVM>: LLVM 15 assembles the 510 decoded forms and the 5 pixel fixtures to the same words. The LLVM C API disassembles them to the same text. tools/disasm_rdna2.py prints the unused s0 sdst of a gfx10 v_cmpx_*_e64; this is normalized as a known difference.
  • Draws (draw, registered as startup_gfx2hlsl_f32_lg_{warp,hardware}):
    • A source-built pixel program builds one of the 16 words from x and one from y with integer ops, so the 16 x 16 pixels cover every pair: 88 unequal, 12 equal (±0 included) and 156 NaN.
    • cmp writes v_cmp_lg_f32_e32, v_cmp_nlg_f32_e64 -a, |b|, v_cmp_lg_f32_e64 -|a|, -b and v_cmp_nlg_f32_e32 as 1.0/0.0.
    • cmpx_{lg,nlg}_{e32,e64} keep or discard pixels.
    • Each is compiled with FXC ps_5_1 and DXC ps_6_0 and checked pixel by pixel with offscreen_check --startup-pixel-words. lg is 1 on unequal pairs and 0 on equal and NaN pairs; nlg is the opposite.
    • D3D allows flushing binary32 denormal compare sources to signed zero. The draw accepts the image with denormals kept or flushed (the same choice for every fixture) and reports which one matched. Both backends flushed.
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_f32_lg' -V
Test Result
startup_gfx2hlsl_f32_lg Passed
startup_gfx2hlsl_f32_lg_warp: WARP (Microsoft Basic Render Driver, 10.0.26100.9818), debug layer on, 0 errors Passed. FXC: 5/5 fixtures, all 256 pixels matched (flushed denormals). DXC: 5/5, same.
startup_gfx2hlsl_f32_lg_hardware: AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31044.16, debug layer on, 0 errors Passed. FXC: 5/5, all 256 pixels matched (flushed denormals). DXC: 5/5, same.

Environment: Windows 11 Home 10.0.26300, Windows SDK 10.0.26100.0 fxc/dxc, LLVM 15 (llvm-mc), LLVM C API.

Other runs:

  • ctest --preset dev --no-tests=error -R '^startup_' -j 4: 262 passed, 0 failed, 4 skipped. The skips are capability skips (77) in startup_view_heap_{warp,hardware} and startup_logical_view_{warp,hardware}, unrelated to this change. This includes all 30 startup_gfx2hlsl_* tests.
  • tests/startup_gfx2hlsl_f32_lg_check.py binary <llvm-mc> and decode <LLVM-C.dll>: PASS.
  • tests/docs_links_check.py and tests/bidi_controls_check.py: pass.

Negative controls

  • Registered, in the test: the emitted text with the old != lowering restored.
    • Pure Python: fails 68 of 68 lg/nlg forms. First mismatch: v_cmp_lg_f32_e32 vcc_lo, v1, v2 with F(v1) != F(v2), a=00000000 b=7fc00000 gives 1, the ISA gives 0. It matches its own unordered model.
    • Draws: every fixture fails against both images, on both compilers and both backends, and matches its own image. For example, cmp on the RX 9070 XT with FXC gives pixel mismatch (10,0): expected 00000000,3f800000,00000000,3f800000 actual 3f800000,00000000,3f800000,00000000 / 156 of 256 pixels differ. A cmpx_* fixture gives 166 differing pixels against the denormals-kept image.
  • Manual mutant: with the table in gfx2hlsl.py restored to '{0} != {1}' / '!({0} != {1})', the pure-Python check fails (510 f32 forms ... : MISMATCH, first v_cmp_lg_f32_e32 a=00000000 b=7fc00000 gives 1, ISA 0), and so does the WARP FXC cmp draw.
  • Recorded, not asserted: the same less-or-greater without the bit test.
    • All draws match, except DXC on the RX 9070 XT: cmp fails with pixel mismatch (10,0): expected 00000000,3f800000,00000000,3f800000 actual 3f800000,3f800000,3f800000,3f800000 / 156 of 256 pixels differ.

Not covered

  • tools/pixel_aot.py does not have this bug. Its COMPARE table already lowers lg as ({0} < {1} || {0} > {1}) behind the same bit-level ordered test (f32_comparison), and nlg as ({0} == {1} || isnan({0}) || isnan({1})). No change was made there.
  • f32 SDWA compares stay refused (startup: lower VOPC SDWA compares in gfx2hlsl #1915 review); this PR does not re-enable them.
  • Pre-existing and outside this issue's scope. The emitted HLSL is exact, but the compilers fold it under their default flags (no /Gis / -Gis), observed in compiler output only and not yet in a draw:
    • FXC compiles !(a < b) (nlt) to DXBC ge, which is ordered, so it is false on NaN where nlt is true. The same pattern likely affects nle, ngt and nge.
    • DXC folds u (a != a || b != b) to the constant 0 (and o presumably to true).
    • These need their own issue and draw.
  • Both WARP and the RX 9070 XT flush denormal compare sources. The draws cannot assert the denormals-kept image; the pure-Python check covers the emitted text with denormals kept.

🤖 Generated with Claude Code

V_CMP_LG_F32 is less or greater: ordered and not equal, false when
either source is NaN; V_CMP_NLG_F32 is its negation. compare() lowered
them as a != b and !(a != b), and HLSL != is unordered, so both gave
the opposite result on every NaN pair (plain and e64 forms, v_cmp and
v_cmpx). They now read (ORDERED && (a < b || a > b)) and its negation,
ORDERED testing both source words for NaN on their bits before the
neg/abs modifiers. Without the bit test, the DXC build (fast compares
unless -Gis) of the v_cmp fixture returned 1 for both lg and nlg on all
156 NaN pairs on an RX 9070 XT. The other conditions and the refused
f32 SDWA forms are unchanged.

tests/startup_gfx2hlsl_f32_lg_check.py decodes every f32 condition of
the table (e32 and e64 under every neg/abs, 510 forms), evaluates the
emitted HLSL over all 256 pairs of +-0, +-min denormal, +-1, +-max,
+-inf and quiet, signalling and payload NaNs against a reference built
from the ISA definitions, round-trips the forms through LLVM 15, and
draws 16 x 16 pixels covering every pair on WARP and hardware with FXC
ps_5_1 and DXC ps_6_0, checked pixel by pixel. The old != lowering
fails all 68 lg/nlg forms and every draw.

Fixes R0Y00#1920

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Ran it, broke the ordered lowering four ways

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_f32_lg_check.py
PASS      exit 0

Mutations to tools/startup/gfx2hlsl.py, each reverted:

mutation result
lg back to {0} != {1} (the old unordered text) caught
nlg as the same expression instead of its negation caught
ORDERED bound <= 0x7f800000u → <= 0x7fffffff caught
ORDERED term dropped entirely (({2} && …) → (…)) caught

The third is the one I wanted: 0x7f800000 (infinity) must still count as not NaN, so a <-versus-<= slip is a real
semantic change, and the fixture separates it from the fully-dropped case. Both fail, for different reasons.

What I checked by reading, not by guessing

  • Inline and literal sources are covered: INLINE = {0: 128, …, 0x3f800000: 242} with the value→(inline code, literal)
    helper at line 223. So the ORDERED term is exercised for encodings that are not SGPR/VGPR pairs, which is where a naive
    word(tok) would break.
  • The 16-word class list includes both quiet and signalling NaNs (0x7fc00000, 0x7f800001), so the CPU reference is not just
    testing the quiet case.
  • Every other condition keeps its text — the table diff shows lt, eq, le, gt, ne, nge, o, u unchanged, and the
    test compares them.

What I cannot verify

The DXC -Gis observation — that a driver folds fcmp fast back to unordered on an RX 9070 XT — is a hardware measurement I have
no way to reproduce, and the production path is FXC ps_5_1 anyway. The bit test costs nothing in that path, so "required on
the DXC path, harmless on FXC" is the right conclusion either way; I am only flagging that this part of the description rests on
your measurement, not on mine.

One small note

nlg repeats the whole ordered expression rather than negating a named local:

!((w0 & 0x7fffffffu) <= 0x7f800000u && … && (a < b || a > b))

Correct, and the evaluator in the test covers it. If the emitted text ever gets long enough to matter for fxc, a temporary would
read better — not a request, just an observation while the shape is fresh.

Verdict

Reviewed. The NaN semantics are the whole point of the change and they are pinned on both ends of the ORDERED test, in both
directions (accept-and-verify and refuse-by-mutation). No objections.

Notes

  • AI-assisted review.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Resolved the workflow overlap with #1925, keeping both Python and LLVM registrations. The integrated candidate passes the lg/nlg, scalar complement and class comparison controls. Default FXC/DXC draws passed on WARP and RTX 4060 Laptop with the expected old-lowering mismatches. This keeps the remaining predicate work in #1936 independent.

@R0Y00
R0Y00 merged commit ea0cf33 into R0Y00:main Nov 24, 2026
5 checks passed
R0Y00 pushed a commit that referenced this pull request Nov 25, 2026
compare() refused every v_cmp_*_f32_sdwa and v_cmpx_*_f32_sdwa until the
f32 predicates were exact on NaN (#1930, #1949). DWORD selections now
read the register as binary32 with neg/abs as in the e64 form, so a
src0_sel:DWORD src1_sel:DWORD compare emits the e64 text, NaN bit test
included.

Byte and word selections are refused: the selected field is always +0 or
a denormal, so the result depends on the binary32 denormal mode, which
gfx2hlsl does not read. pixel_aot refuses the same forms. sext() is
integer-only and refused; LLVM 15 does not decode the bit on a float
source. clamp, omod, dst_sel and dst_unused stay refused, and class
compares are unchanged. LLVM's SIPeepholeSDWA models the sub-dword case
as zero-extension read as binary32 bits; the RDNA 2 ISA guide gives only
the SDWAB fields.

tests/startup_gfx2hlsl_f32_sdwa_check.py:
- builds every f32 SDWA form and checks the 720 DWORD/DWORD forms against
  the e64 text and the ISA reference: 256 NaN/denormal pairs under the
  IEEE, FXC and DXC models;
- refuses all 34,560 byte/word forms with the denormal-mode message;
- uses the text before #1930/#1949 as its negative control;
- draws 30 DWORD fixtures with default-flag FXC ps_5_1 and DXC ps_6_0.

The vopc_sdwa and f32_lg checks no longer assert the blanket refusal.
They now check that DWORD selections lower as the e64 form and that
byte/word selections are refused with the new message.

Refs #1906

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

startup: gfx2hlsl lowers f32 lg/nlg compares as unordered (wrong for NaN)

3 participants