Skip to content

shader: lower the integer SDWA comparisons - #1670

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

rm-NoobInCoding wants to merge 3 commits into
R0Y00:mainfrom
rm-NoobInCoding:shader/sdwa-int-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. The integer SDWA comparisons were refused:

  • v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32}_sdwa;
  • v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_sdwa (gfx10 has no f/t forms at 16 bits).

This PR lowers these 28 forms with byte and word selections:

  • Each source is its selected field, zero-extended or sign-extended under sext(). The selection uses the existing sdwa_source(), as for the SDWA integer operations.
  • The comparison is then the e32 one on those words. i32 and u32 compare signed or unsigned 32-bit values. i16 and u16 compare the low 16 bits of the selected word, as their e32 forms read the low half of the register.
  • A whole-dword source is read exactly as the e32 form reads it, so a comparison with both selections DWORD emits the same HLSL as its e32 spelling. A control asserts this.

Refused:

  • inline constants, literals and float modifiers;
  • an unbalanced sext( or a missing selection;
  • exec as the destination;
  • the f and t forms at 16 bits;
  • the v_cmpx and 64-bit SDWA forms.

tools/scan_unsupported_shaders.py keeps its own patterns, so a second commit accepts the same forms there, without f/t at 16 bits, and adds test_sdwa_int_compare_instructions. test_binary_scan_recognizes_vopc_sdwa used the word for v_cmp_gt_u32_sdwa vcc_lo, v1, v2 src0_sel:WORD_1 src1_sel:DWORD as its unlowered example; that word now sits in the supported binary, decoded by tools/disasm_rdna2.py into the syntax the lowering expects, and v_cmpx_eq_f32_sdwa stays the unlowered example.

Coverage: +28 gfx1013 and gfx1030 SDWA forms.

Verification

Base 578d1e1c, patches 389422b9, 85690efe and the round-trip commit. 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_int_compare_' --output-on-failure
python -B tests/pixel_aot_check.py
python -B tests/form_coverage_check.py
python -B tests/rdna2_tools_check.py
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
  • pixel_aot_sdwa_int_compare_hardware and pixel_aot_sdwa_int_compare_warp: 2/2 passed, no skips. Both coverage records list all 28 forms.
  • rdna2_tools_check.py: 146 tests OK (18 skipped without LLVM). All 49 platform-independent Python checks listed in AGENTS.md passed on the patch.
  • The static controls (including the new check_sdwa_int_compare_lowering), form_coverage_check.py, docs links and bidi controls passed.

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

  • Programs. Eight programs, two per kind. Each compares four channels of A (s44 copied to v20) with B (s45 copied to v18) and selects 1.0 or 0 through v_cndmask_b32.
  • Selections. Five configurations cycle across the channels: BYTE_1+sext / BYTE_2, WORD_1 / WORD_0+sext, BYTE_3+sext / WORD_1, WORD_0+sext / BYTE_0, and DWORD / BYTE_3+sext.
  • Inputs. Each program runs on 13 pairs of normal binary32 words or +0.0. A static check asserts that every relation except f and t takes both answers on the pairs.
  • Negative controls. For each program and each wrong model, the first pair on which that model gives other channels is drawn against the model's reference and must report pixel_mismatch. The models are: selections ignored, sext() ignored, signedness swapped, and, for the 16-bit kinds, the whole selected word compared. That is 28 controls; a static check asserts each one has a distinguishing pair. In total, 132 draws from 8 compiles.
  • Lowering negative control. Making the lowering ignore sext() makes pixel_aot_sdwa_int_compare_warp fail on the first program, with the passing rows reported as mismatch.

LLVM 15 round trip: not run locally (no LLVM with gfx1013 on this host). The first two commits did not put the new forms through it: check_binary assembled no v_cmp_*_sdwa integer program (thanks @triggered02). The third commit adds the eight sdwa_int_predicate_group programs to its fixtures, so the Assemble and translate gfx1013 fixtures job now assembles each program, disassembles it with LLVM 15, and requires the disassembly to translate exactly like the source text.

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. The v_cmpx_*_sdwa forms, which also write exec, 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_*_{i32,u32}_sdwa (eight relations each) and v_cmp_*_{i16,u16}_sdwa
(six each; gfx10 has no f/t forms at 16 bits) were refused. Each source
is now its selected byte or word field, zero-extended or sign-extended
under sext() as for the SDWA integer operations, and the comparison is
the e32 one on those words; the 16-bit kinds read the low 16 bits of
the selected word. A whole-dword source is read exactly as the e32 form
reads it, so DWORD/DWORD selections emit the e32 HLSL.

draw-sdwa-int-compare draws the 28 forms in eight programs under five
selection configurations on 13 pairs, with 28 negative controls drawn
against wrong models (selections ignored, sext() ignored, signedness
swapped, whole selected word compared at 16 bits). It runs as
pixel_aot_sdwa_int_compare_{hardware,warp} with its own coverage record.
The shader scanner keeps its own patterns, so it still reported the
integer SDWA comparisons as unsupported after pixel_aot lowered them.
Accept them with register sources, optional sext() and both selections
printed, without f/t at 16 bits, and test the accepted and refused
spellings. test_binary_scan_recognizes_vopc_sdwa used
v_cmp_gt_u32_sdwa as its unlowered example; that word now belongs to
the supported binary, and v_cmpx_eq_f32_sdwa stays the unlowered one.

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

Peer review of #1670 (85690efe5b0b6d95a98a8d9b290bddf372d84647): no defects found (0 P1, 0 P2).

Summary

  • tools/pixel_aot.py: lowers all 28 integer SDWA comparison forms (v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32}_sdwa and v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_sdwa), applying sdwa_source(..., bool(sext)) on sub-dword selections and matching the e32 operand/sign-extension semantics on DWORD and 16-bit forms, while rejecting 16-bit f/t, unbalanced sext(, constants, float modifiers, exec destinations, and v_cmpx/64-bit forms.
  • tests/pixel_aot_check.py, CMakeLists.txt, docs/experiments.md: adds check_sdwa_int_compare_lowering (covering all 28 forms, HLSL bit-extraction, e32 equivalence on DWORD, and 11 rejection cases), check_sdwa_int_predicate_draws (8 programs x 13 pairs + 28 negative-control wrong models), and pixel_aot_sdwa_int_compare_${adapter} with FORM_COVERAGE_RECORD. Title-neutral and git diff --check clean.

Verification

  • Linux:
    • python3 -B tests/pixel_aot_check.py -> PASS
    • git diff --check origin/main...HEAD -> PASS
  • Windows Server 2025 (x2w_pipe.py win2025 1670):
    • Windows Python checks: tests/form_coverage_check.py -> PASS (16.6s)
    • Debug (--preset dev): cmake --build --preset dev --target offscreen_check + ctest --preset dev -R '^(pixel_aot_sdwa_int_compare_warp|pixel_aot_sdwa_integer_warp|pixel_aot_sdwa_unary_warp|pixel_aot_comparisons_warp|pixel_aot_compare_warp|pixel_aot_warp)$' -> 6/6 passed (20.65s: pixel_aot_sdwa_int_compare_warp 3.05s; *_hardware excluded on VM).
    • Release /O2 (--preset release): cmake --build --preset release --target offscreen_check + ctest --preset release -R '^(pixel_aot_sdwa_int_compare_warp|pixel_aot_sdwa_integer_warp|pixel_aot_sdwa_unary_warp|pixel_aot_comparisons_warp|pixel_aot_compare_warp|pixel_aot_warp)$' -> 6/6 passed (20.68s: pixel_aot_sdwa_int_compare_warp 3.06s).
  • Local mutation check:
    • Mutated sdwa_source(..., bool(sext)) -> sdwa_source(..., False) in tools/pixel_aot.py; python3 -B tests/pixel_aot_check.py failed as expected (AssertionError in check_sdwa_int_compare_lowering, exit code 1). Restored via git checkout -- ..

AI-assisted; commands listed were executed locally.

@triggered02

Copy link
Copy Markdown
Contributor

Ran the LLVM job on this head. It passes, but it does not do what the PR body says it does, so the useful part of this comment is the second half.

The run. Independent run of the repository workflow on head 389422b9, in a fork rather than upstream, so it is a fresh execution rather than a re-read of the existing check:

  • repo: triggered02/Xbox2Windows, workflow windows.yml via workflow_dispatch on the PR head
  • run: https://github.com/triggered02/Xbox2Windows/actions/runs/45441959295
  • job: Assemble and translate gfx1013 fixtures — success, 2m41s (job 138615257733)
  • host: ubuntu-22.04, step installs llvm-15 and runs tests/pixel_aot_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 from the COMMON_LLVM list in windows.yml

Upstream CI on the same commit shows the same job green. I could not run it on Windows locally: this host has no LLVM 15 and no 7-Zip to extract a portable copy, and installing one machine-wide needs elevation I do not have. So this is the Ubuntu runner's LLVM, not a second platform.

The gap. The round trip does not assemble any of the 28 forms this PR adds. check_binary() in tests/pixel_aot_check.py extends its fixture list with the SDWA operations (SDWA_ARITH, SDWA_UNARY, SDWA_PRESERVE_BODY, SDWA_PAD_BODY, sdwa_integer_group) but nothing that emits v_cmp_*_{i32,u32,i16,u16}_sdwa; this PR does not touch that list. The only binary-side coverage is in tests/rdna2_tools_check.py, and it is hand-written bytes: f904887d01000506 moves from the unlowered sample to the supported sample, and the count assertion goes 2 → 3. That tests the scanner's recognition, not that an assembler accepts the text LLVM 15 will print back.

So "the Ubuntu job is the first assembler check" is not accurate — no assembler sees these forms. The PR's real coverage is the static lowering check and the 132 draws, which do not depend on LLVM at all.

What would close it. Add the integer comparisons to the round trip, either by extending check_binary() with the eight sdwa_int_predicate_group(kind, program) programs or as a dedicated check_sdwa_int_compare_binary(): assemble each program, re-disassemble it, translate the disassembly, and require the HLSL to equal the text-input translation (the same pattern as the SDWA integer operations). That makes the Ubuntu job genuine coverage for the new forms and would catch a mnemonic or operand-order mistake that the text-input fixtures cannot see.

I am happy to send that as a patch on top of this branch — I can verify it with the same workflow run in the fork, which is the only way I can run LLVM 15 here. Say the word and I will open it as a PR against your branch, or post the diff here if you prefer to apply it yourself.

Not claimed: that the new lowering is correct on hardware. My draws and static checks are not part of this comment; this is only about what the LLVM job does and does not cover.

AI-assisted (Claude Code).

check_binary assembled the SDWA integer operations but none of the
v_cmp_*_{i32,u32,i16,u16}_sdwa programs, so the LLVM 15 job never saw
the new forms. Add the eight sdwa_int_predicate_group programs to its
fixtures: each is assembled, disassembled with LLVM 15 and must
translate exactly like its source text.
@rm-NoobInCoding

Copy link
Copy Markdown
Contributor Author

You're right, and thanks for running it. check_binary never assembled a v_cmp_*_sdwa integer program, so my "first assembler check" line was wrong.

I've applied your suggestion as c641cd39: the eight sdwa_int_predicate_group(kind, program) programs are now in the check_binary fixtures. Each is assembled with LLVM 15, disassembled, and the disassembly must translate exactly like the source text, the same pattern the SDWA integer operations use. No patch needed from your side. I can't run LLVM 15 on this host either, so the Assemble and translate gfx1013 fixtures job on this push is the check; I'll follow up if it finds a spelling mismatch. The PR description is corrected. pixel_aot_check.py static controls pass locally.

#1680 (the binary16 SDWA comparisons) had the same gap, and I'm closing it there the same way.

@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 c641cd3 (comment only, not an approval). My earlier review was at 85690ef.

Verdict: 0 P1 / 0 P2.

Since 85690ef the diff is 3 lines in tests/pixel_aot_check.py: the eight new programs are added to check_binary, which closes the gap that the LLVM round trip assembled none of the new forms. I ran those 8 programs through a copy of that loop with llvm-mc-19 (-mcpu=gfx1013): assemble, extract, disassemble, and translate(decoded) == translate(source) holds for all 8. The full check_binary with LLVM 19 stops earlier on an unrelated f16 fixture (v_cvt_f16_i16_e32 v1, 0xfff0 prints differently than in LLVM 15).

Semantics

  • 28 forms assemble with llvm-mc-19 on gfx1013 and gfx1030 with identical results (f/t at 32 bits, sext() on either source, SGPR sources, s6 destination). v_cmp_f_i16_sdwa and v_cmp_t_u16_sdwa are rejected by the assembler, which matches the 16-bit exclusion in the lowering and the scanner.
  • tools/pixel_aot.py:5049: sub-dword fields go through sdwa_source (zero- or sign-extended under sext()); 16-bit forms take the low 16 bits of the selected word; i32 and u32 compare signed and unsigned; whole-dword selection matches the e32 spelling (a control asserts the same HLSL); a register holding only a low half is refused on sub-dword selections.
  • The scanner regex agrees with the lowering. The count changes in test_binary_scan_recognizes_vopc_sdwa follow from a word moving from unlowered to supported; no assertion is weakened. No new test asserts source text.

Run

  • python3 -B tests/pixel_aot_check.py, tests/rdna2_tools_check.py (146 OK, 18 skipped), tests/docs_links_check.py, tests/bidi_controls_check.py, tests/shader_census_check.py, tests/shader_corpus_report_check.py: all pass.
  • Mutations (restored with git checkout -- <file>, green again): bool(sext) -> False in the sub-dword path fails check_sdwa_int_compare_lowering on the cmp23 line; adding f|t to the scanner's 16-bit list fails rdna2_tools_check on v_cmp_f_i16_sdwa.
  • Two further mutations were NOT caught on Linux: the i32 branch emitting ({word}) instead of asint({word}), and the u16 branch comparing the whole word without & 0xffffu.

Non-blocking

  • check_sdwa_int_compare_lowering pins the HLSL only for i16 program 0 and u32 program 0, so i32 signedness and the u16 mask are guarded only by the Windows draws. One static assertion for an i32 line and one for a u16 line would close that on Linux.
  • Merge order: #1680 (binary16 SDWA comparisons) conflicts textually in tests/pixel_aot_check.py and tests/rdna2_tools_check.py; whichever lands second needs a rebase. Merging into current main is otherwise clean.

Not run on Linux: the 132 draws and 28 negative controls (pixel_aot_sdwa_int_compare_{hardware,warp}), DXC, D3D12, LLVM 15, form_coverage_check.py and shader_probe_check.py (need iced_x86 and pefile). Real-hardware behaviour of the 16-bit selections is not established by any check here.

AI-assisted; commands listed were executed locally.

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

Dual-platform peer review at head_sha: c641cd3951369167dfb22401ac1be8d81e407e14

Findings

No blocking issues (0 P1, 0 P2).

  • tools/pixel_aot.py and tools/scan_unsupported_shaders.py: lower the 28 integer SDWA comparison forms (v_cmp_{f,lt,eq,le,gt,ne,ge,t}_{i32,u32}_sdwa and v_cmp_{lt,eq,le,gt,ne,ge}_{i16,u16}_sdwa) with BYTE_0..3, WORD_0..1, DWORD, optional sext(...), and lane-mask destination (vcc/vcc_lo/SGPR), while rejecting 16-bit f/t, unbalanced sext(, inline constants/literals, float modifiers, exec, v_cmpx, and 64-bit forms. Whole-DWORD selections reuse int_operand/int16_operand so emitted HLSL matches _e32.
  • tests/pixel_aot_check.py, tests/rdna2_tools_check.py, CMakeLists.txt (test-only registration pixel_aot_sdwa_int_compare_${adapter}), docs/experiments.md: include static lowering/refusal controls, LLVM assemble/disassemble round trip in check_binary for all 8 programs, WARP draw checks with 28 negative-model mismatch controls, and scanner parity; title-neutral.

Verification

  • Linux:
    • git diff --check origin/main...HEAD: clean.
    • python3 tests/docs_links_check.py: passed (16 Markdown files, 348 links).
    • python3 tests/bidi_controls_check.py: passed (1276 tracked files).
    • python3 -B tests/rdna2_tools_check.py -k test_binary_scan_recognizes_vopc_sdwa -k test_sdwa_int_compare_instructions: 2 tests ran, OK.
    • python3 -B tests/pixel_aot_check.py: passed.
    • llvm-mc-19 -arch=amdgcn -mcpu=gfx1013 -show-encoding: verified encodings for v_cmp_lt_i32_sdwa, v_cmp_gt_u32_sdwa, v_cmp_ge_u16_sdwa, v_cmp_ne_i16_sdwa.
    • Local mutation checks (each restored via git checkout -- tools/pixel_aot.py):
      1. Replaced if kind in ("i16", "u16") and op in ("f", "t"): with if False: -> python3 -B tests/pixel_aot_check.py failed in check_sdwa_int_compare_lowering (AssertionError: translate accepted invalid input, exit 1).
      2. Ignored sext (sdwa_source(..., False)) -> python3 -B tests/pixel_aot_check.py failed in check_sdwa_int_compare_lowering (HLSL mismatch on cmp23, exit 1).
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • Python checks: tests/form_coverage_check.py ok (16.5s).
    • Debug (--preset dev): ctest --preset dev -R '^(pixel_aot_sdwa_int_compare_warp|rdna2_tools_check)$' --parallel 4 --no-tests=error --output-on-failure: 2/2 passed (5.66s: pixel_aot_sdwa_int_compare_warp 2.47s, rdna2_tools_check 5.59s).
    • Release /O2 (--preset release): ctest --preset release -R '^(pixel_aot_sdwa_int_compare_warp|rdna2_tools_check)$' --parallel 4 --no-tests=error --output-on-failure: 2/2 passed (5.94s: pixel_aot_sdwa_int_compare_warp 2.35s, rdna2_tools_check 5.87s).

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this PR and the earlier fixes at c641cd39; main has moved on and this branch now has a merge conflict.
Could you rebase onto the current main and push so we can re-verify and merge? AI-assisted.

@thepatrickworld

Copy link
Copy Markdown
Contributor

Before you spend the rebase tedin7 asked for: most of this is already in main, so here is what the rebase will actually leave.

main now lowers the SDWA integer comparisons in two branches of tools/pixel_aot.py:

v_cmp_({'|'.join(INT_COMPARE)})_(i32|u32)_sdwa     # 32-bit, the whole INT_COMPARE set
v_cmp_(eq|ne|lt|le|gt|ge)_(i16|u16)_sdwa           # 16-bit, six of the eight

Your branch adds a single regex over (i32|u32|i16|u16) with the full INT_COMPARE set, so after a rebase:

  • the i32/u32 half is already there;
  • the i16/u16 half is already there for eq, ne, lt, le, gt, ge;
  • what remains unique to this PR is v_cmp_f_(i16|u16)_sdwa and v_cmp_t_(i16|u16)_sdwa — the two constant-result forms, which main still refuses because its 16-bit regex lists only the six real comparisons.

So the rebase is worth doing only if you want those two; otherwise this can close without losing anything. INT_COMPARE is f, lt, eq, le, gt, ne, ge, t, so f and t are the whole delta.

Context on the other PR: #1923 hits the same collision and its author is holding it for the same reason.

AI-assisted.

@thepatrickworld

Copy link
Copy Markdown
Contributor

Correction to what I wrote above, after testing instead of reading the regexes.

I said the v_cmp_f_(i16|u16)_sdwa and v_cmp_t_(i16|u16)_sdwa forms would be the remaining delta. They are not:

  • main refuses both — calling pixel_aot.lower(["v_cmp_f_i16_sdwa ..."], 20, 24, [1,2,3,4], constant_descriptor=64) raises Unsupported instruction;
  • this branch refuses them too, and its own comment says why: "gfx10 has no f/t forms at that width". The regex takes them from INT_COMPARE, but the code documents that the hardware does not have them.

So the six 16-bit comparisons this PR adds are exactly the six main already carries (eq, ne, lt, le, gt, ge), and all eight 32-bit ones are there too. After a rebase this PR adds no coverage at all — it can close without losing anything, and the rebase tedin7 asked for is not worth doing for the translation itself.

What is still worth something is the test surface: 198 lines in tests/pixel_aot_check.py against what main carries for the same forms, plus the rdna2_tools_check.py round trip. If you would rather not rebase a branch that adds nothing, that material could be ported into main separately.

Sorry for the wrong first read — the regex made me assume those two forms were covered.

AI-assisted.

@R0Y00

R0Y00 commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Main already lowers the 28 integer SDWA predicates in #1790. #1923 adds per-predicate 32-bit draw and refusal controls. The current 46-form controls and binary scanner check pass. For any extra 16-bit draw cases, please extend the existing fixture while keeping its single-SGPR destination rule. Thanks for the detailed signedness and selection comparisons. Closing this implementation as superseded.

@R0Y00 R0Y00 closed this Nov 26, 2026
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.

5 participants