Skip to content

startup: lower gfx2hlsl f32 SDWA compares with DWORD selections - #2212

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-f32-sdwa-compares
Nov 25, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
MasterDomino:startup/gfx2hlsl-f32-sdwa-compares

Conversation

@MasterDomino

@MasterDomino MasterDomino commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1906

What

Since #1915, compare() in tools/startup/gfx2hlsl.py has refused every v_cmp_*_f32_sdwa and v_cmpx_*_f32_sdwa with <name>: f32 SDWA compares are not lowered. Now that #1930 and #1949 are on main, every f32 predicate in the table is exact on NaN under default-flag FXC and DXC. This PR lowers the DWORD SDWA forms through that table and refuses the byte and word forms with a reason.

Form Result
src0_sel:DWORD src1_sel:DWORD, any neg/abs Lowered: the e64 text with the same modifiers, NaN bit test included (checked for all 720 forms)
A BYTE_n or WORD_n select on either source Refused: <name>: f32 SDWA byte/word selection depends on the binary32 denormal mode
sext() on a float source Refused: <name>: sext() on a float source
clamp, mul:2, div:2, dst_sel, dst_unused; a missing or repeated select Refused, as before
v_cmp_tru_f32_sdwa; class compares Unchanged

Why

The startup cache compiles gfx2hlsl output with FXC and no flags, and since #1930 and #1949 the f32 predicates are exact on NaN under those defaults. The SDWA forms were refused only because no one had checked them against that text. The owner's last comment on #1906 keeps the ordinary f32 predicates on this issue.

SDWA float semantics

  • DWORD selections read the register as binary32, with neg/abs as in the e64 form.
  • Byte and word selections are refused. The selected field is always +0 or a denormal, so the result depends on the denormal mode. gfx2hlsl does not read that mode; tools/pixel_aot.py refuses the same forms.
  • sext() is integer-only and refused.

Sources:

  • LLVM SIPeepholeSDWA models the sub-dword case as zero-extension read as binary32 bits. It folds v_and_b32 0xffff and v_lshrrev_b32 16 into the src_sel of a float compare and keeps the compare's neg/abs (test/CodeGen/AMDGPU/sdwa-peephole-instr.mir, vopc_instructions).
  • RDNA 2 ISA guide: gives only the SDWAB fields (§13.3.8), and says VOPC SDWA has no OMOD, CLMP, DST_U or DST_SEL (§6.8).
  • LLVM AMDGPUModifierSyntax.rst: calls SDWA sext "Valid for integer operands only". LLVM 15's disassembler rejects the sext bit on a float source, which the decode mode checks.

Tests

New file tests/startup_gfx2hlsl_f32_sdwa_check.py, registered as startup_gfx2hlsl_f32_sdwa and startup_gfx2hlsl_f32_sdwa_{warp,hardware} (SKIP_RETURN_CODE 77). The pure-Python check is added to the CI Python job, and binary llvm-mc-15 / decode libLLVM-15.so.1 are added to the LLVM 15 job. The decode mode needs LLVM 15 (libLLVM-15): its sext-bit refusal check depends on LLVM 15's disassembler, so a runner with a newer LLVM can run binary but not decode.

Controls (pure Python, ~1 s). Every f32 SDWA form is built from the VOP_SDWA9Be fields and decoded: 15 table conditions × (v_cmp into vcc_lo, v_cmp into an SGPR, v_cmpx into EXEC) × 7 × 7 selects × 4 × 4 neg/abs = 35,280 forms.

  • DWORD/DWORD (720 forms): each emits the e64 text.
    • Its HLSL is evaluated over the 256 pairs of the lg check: ±0, ±min denormal, ±1, ±max, ±inf, ±quiet, signalling and payload NaN.
    • Each pair runs under the nan check's three models (IEEE, the FXC negation rewrite, the DXC folds): 184,320 evaluations, all equal to the ISA reference on bit patterns.
  • Byte/word (34,560 forms): every form with a byte or word select on src0, src1 or both is refused with the denormal-mode message, for every condition, v_cmp and v_cmpx, and every modifier.
  • Also checked:
    • EXEC masking under every model.
    • An SGPR source (-s3) and an inline constant (neg(1.0)) with DWORD selects; the same with WORD_1 or BYTE_1 is refused.
    • The sext(), clamp, omod and dst_* refusals, and the decoder refusing the sext bit.
    • Class compares unchanged.

Draws (draw). Each pixel builds one source word from x and one from y, so the 16 × 16 pixels cover every pair.

  • cmp_<cond> (15 fixtures) writes four DWORD/DWORD results, (a, b), (-a, |b|), (-|a|, -b) and (|a|, b), into vcc_lo, s6 and s8.
  • cmpx_<cond> (15 fixtures) keeps a pixel through v_cmpx_<cond>_f32_sdwa -v3, |v4| src0_sel:DWORD src1_sel:DWORD.
  • Fixtures are compiled with default-flag FXC ps_5_1 and DXC ps_6_0.
  • Every pixel must match the ISA image with denormals kept or flushed. The choice must be the same across denormal-sensitive fixtures, and the report names it.

Updated, not deleted:

  • startup_gfx2hlsl_vopc_sdwa:
    • The 720-form float refusal now checks that DWORD selections lower as the e64 form.
    • Its 12 sub-dword float select cases now expect the denormal-mode refusal.
    • It also checks the refusal of sext() on a float source and of clamp/mul:2 on a float compare.
  • startup_gfx2hlsl_f32_lg: the 16 lg/nlg SDWA refusals now check the e64 text.

Results

Tested at commit eda692a6 on main ab9a020c (after #2204). Environment: Windows 11 build 26300, Windows SDK 10.0.26100.0 FXC and DXC 1.8.2502.11, D3D12 debug layer on (debug_errors=0 debug_warnings=0).

FXC ps_5_1 (default) DXC ps_6_0 (default)
WARP (Microsoft Basic Render Driver, 10.0.26100.9818) 30/30 fixtures, every pixel 30/30 fixtures, every pixel
AMD Radeon RX 9070 XT (1002:7550), 32.0.31044.16 30/30 fixtures, every pixel 30/30 fixtures, every pixel

The six f, o and u fixtures do not depend on denormals. The other 24 matched the flushed image on both adapters and with both compilers. Each draw run makes 210 checks, all ok. WARP and the RX 9070 XT agree in every cell, including the recorded ones.

cmake --build --preset dev                                                         succeeds (full dev build)
ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_' -j 6
100% tests passed out of 72   (startup_gfx2hlsl_f32_sdwa 1.1 s, _warp 18.8 s, _hardware 84.9 s)
python -B tests/startup_gfx2hlsl_f32_sdwa_check.py                                   PASS (116 checks)
python -B tests/startup_gfx2hlsl_f32_sdwa_check.py binary <llvm-mc 15.0.7>           PASS (35,282 forms incl. refused + 30 fixtures)
python -B tests/startup_gfx2hlsl_f32_sdwa_check.py decode <LLVM-C.dll 15.0.7>        PASS (35,282 forms, sext refused, 30 fixtures)
python -B tests/startup_gfx2hlsl_f32_sdwa_check.py draw <dxc> <offscreen_check> warp <fxc>      PASS (210 checks)
python -B tests/startup_gfx2hlsl_f32_sdwa_check.py draw <dxc> <offscreen_check> hardware <fxc>  PASS (210 checks)
python -B tests/startup_gfx2hlsl_{vopc_sdwa,f32_lg,f32_nan}_check.py [binary|decode]            PASS
python -B tests/docs_links_check.py; python -B tests/bidi_controls_check.py                    pass

Negative controls

Text before #1930/#1949, on the same operands: lg/nlg as !=, the n-forms as !(a OP b), o/u as reflexive compares.

  • Pure Python: it fails all 128 guarded DWORD condition × modifier pairs under the model of the compiler that broke it, and matches that model. lg/nlg already fail under IEEE.

  • Draws, asserted: it fails every guarded fixture against both images on the compiler that broke it (lg/nlg on both compilers; 40 checks per adapter). For example, FXC cmp_nlt on the RX 9070 XT gives pixel mismatch (10,0): expected 3f800000,3f800000,3f800000,3f800000 actual 00000000,00000000,00000000,00000000 / 156 of 256 pixels differ. It also matches that compiler's model image for lg/nlg and the four n-forms (16 checks per adapter).

  • Draws, recorded only (o/u under DXC): the faithful pre-startup: keep gfx2hlsl f32 n-forms, o and u exact under FXC and DXC #1949 text keeps each source's modifiers, and DXC folds x == x only for some operand shapes:

    • cmp_o/cmp_u: folded for (a, b) and (-a, |b|) but not for (-|a|, -b) or (|a|, b).
    • cmpx_o/cmpx_u: the |b| side folded but not the -a side.

    The text still fails against the ISA images (asserted), but it does not match the full-fold model, so that comparison is recorded only. The nan check draws the old o/u text on the plain words, where DXC folds both sides.

  • Unmodified main translator: the new check fails with 10 MISMATCH lines (f32 SDWA compares are not lowered) and FAIL.

Recorded, not asserted:

  • The old text on the compiler that did not break it matched every pixel.
  • /Gis and -Gis: every fixture matched every pixel.

Environment

  • Windows 11 Home 10.0.26300, x64.
  • Windows SDK 10.0.26100.0 FXC and DXC (dxcompiler 1.8.2502.11).
  • LLVM 15.0.7 llvm-mc and LLVM-C.dll.
  • Python 3 from out/pe-env.
  • WARP: Microsoft Basic Render Driver 10.0.26100.9818.
  • Hardware: AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31044.16; D3D12 debug layer on.

Skips

None. No test in the ^startup_gfx2hlsl_ run returned 77.

Not covered

  • Byte and word f32 selections stay refused. Lowering them needs the binary32 denormal mode (MODE[5:4] / FLOAT_MODE) at the compare, which gfx2hlsl does not read. No local cache contains such a form today.
  • Other forms: v_cmp_class_*_sdwa is unchanged (startup: lower binary32 class SDWA compares #1969, startup: add the SDWA forms of v_cmp_class_f16 in gfx2hlsl #2020). v_cmp_tru_f32 and the f16/f64 compares in SDWA form stay refused.
  • Loading path: our local caches contain no f32 SDWA refusals, so there is nothing to recount.
  • Not verified: execution of the title's CPU code, full-game startup and frame rate.

AI-assisted.

🤖 Generated with Claude Code

compare() refused every v_cmp_*_f32_sdwa and v_cmpx_*_f32_sdwa until the
f32 predicates were exact on NaN (R0Y00#1930, R0Y00#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 R0Y00#1930/R0Y00#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 R0Y00#1906

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

@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 eda692a6a23a561915e1a179d94cb6e162664914.

Result: no P1, no P2. Looks correct to me.

What I checked

  • tools/startup/gfx2hlsl.py: DWORD f32 SDWA compares reuse the e64 text and modifiers. BYTE/WORD selections and sext() on a float source are refused with distinct reasons. The earlier blanket refusal is removed. This matches the reasoning in the new comment: a sub-dword field zero-extended as binary32 bits is always +0 or a denormal, so the result depends on the denormal mode.
  • .github/workflows/windows.yml: the new check is added in both places that list its sibling f32_nan check. binary and decode entries are in the Python-check matrix, and the file is in the later script list. Both mirror the existing entries and I found no missing third spot.
  • CMakeLists.txt: a pure-Python CTest plus warp and hardware draw tests, in the same shape as startup_gfx2hlsl_f32_nan_*. The draw tests use offscreen_check readback compared pixel by pixel with the CPU reference, not string-only HLSL checks.
  • tests/startup_gfx2hlsl_f32_lg_check.py and tests/startup_gfx2hlsl_vopc_sdwa_check.py: the edits only move the old "f32 SDWA is refused" expectations. Title and body are neutral, with no clean-room concerns.

Commands executed locally (Linux)

  • python3 -B tests/startup_gfx2hlsl_f32_sdwa_check.py (pure part): PASS
  • python3 -B tests/startup_gfx2hlsl_f32_sdwa_check.py binary llvm-mc-19: PASS. llvm-mc-15 and libLLVM-15 are not installed here, so the decode mode was not run locally.
  • tests/startup_gfx2hlsl_f32_lg_check.py and tests/startup_gfx2hlsl_vopc_sdwa_check.py (binary llvm-mc-19): PASS
  • python3 tests/docs_links_check.py: 18 files, 407 links, OK
  • python3 tests/bidi_controls_check.py: OK
  • git diff --check origin/main...HEAD: clean

Mutations (both caught)

  • Disabling the BYTE/WORD refusal in compare_sdwa_sources: the pure check exits 1.
  • Disabling the sext() refusal: the pure check exits 1.

Windows (real D3D12)

x2w_pipe.py win2025 2212 --regex "startup_gfx2hlsl_(f32_sdwa|f32_nan|vopc_sdwa|f32_lg)"

  • dev: 12/12 passed. This includes startup_gfx2hlsl_f32_sdwa_warp and _hardware.
  • release /O2: 12/12 passed.

CI at review time

Python checks on Linux: pass. The Windows build and the gfx1013 job were still pending.

Nits

  • None blocking. Optionally, say in the PR body that decode mode needs LLVM 15, because Linux runners with a newer LLVM cannot run it.

AI-assisted; commands listed were executed locally.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Ran this head (eda692a6) on three adapters that are not in the PR yet. All pass. Windows 11 build 26100, debug layer unavailable, FXC and DXC from SDK 10.0.26100.0.

Test Adapter Driver Result
startup_gfx2hlsl_f32_sdwa (controls) n/a n/a pass
startup_gfx2hlsl_f32_sdwa_warp Microsoft Basic Render Driver 10.0.26100.8972 pass, 49 s
startup_gfx2hlsl_f32_sdwa_hardware (XBOX2WINDOWS_ADAPTER=Quadro) NVIDIA Quadro M1000M (Maxwell) 32.0.15.7371 pass, 102 s
startup_gfx2hlsl_f32_sdwa_hardware (XBOX2WINDOWS_ADAPTER=Intel) Intel HD Graphics 530 (Gen9) 31.0.101.2111 pass, 108 s

Each run printed the same 211 ok lines and 76 recorded: negative-control mismatches. Those are the old-text draws against the compiler models, for example dxc hardware cmp_o old text against the dxc model: ... 96 of 256 pixels differ, so the guards bite on Maxwell and Gen9 as well. The hardware runs take 100–110 s on these older GPUs, inside the 300 s limit.

AI-assisted.

@R0Y00
R0Y00 merged commit e90774a into R0Y00:main Nov 25, 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.

4 participants