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

Merged
MasterDomino merged 2 commits from upstream-pr/1930 into main 2026-11-24 22:07:48 +00:00
MasterDomino commented 2026-11-24 21:37:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by MasterDomino on 2026-11-24T21:37:31Z.


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 #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 (#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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591914708 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1930). Originally posted by [MasterDomino](https://github.com/MasterDomino) on 2026-11-24T21:37:31Z. --- 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 #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 (#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](https://claude.com/claude-code)
ChazY11 commented 2026-11-24 22:01:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T21:53:31Z.


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145525673 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1930#issuecomment-7145525673). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T21:53:31Z. --- ## Ran it, broke the ordered lowering four ways Windows 11 build 26300.9974, device-free: ```powershell 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: ```hlsl !((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 commented 2026-11-24 22:01:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T22:00:52Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145686544 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1930#issuecomment-7145686544). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T22:00:52Z. --- 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.
upstream-sync closed this pull request 2026-11-24 23:03:05 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1923
No description provided.