From db10c4aca4cb7e5b75f2c8c90d2fa2325826e665 Mon Sep 17 00:00:00 2001 From: KomaGuy <265821007+KomaGuy@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:30:00 +0300 Subject: [PATCH] tests: add supplemental SDWA 32-bit integer compare draws and refusal controls Rebase #1923 onto current main (after #1790 landed all 46 non-cmpx SDWA comparisons in tools/pixel_aot.py and tools/scan_unsupported_shaders.py) and keep the supplemental per-predicate 32-bit integer test coverage: - Four 4-channel draw programs (i32_lo, i32_hi, u32_lo, u32_hi) covering all 16 v_cmp_*_{i32,u32}_sdwa forms with 41 targeted single-channel negative controls (signed vs unsigned, sext vs zero-extend, byte/word selectors, predicate swaps) registered as pixel_aot_sdwa_int_compare_{warp,hardware} and included in check_binary. - Additional static lowering and refusal checks: whole-dword equivalence with e32, single-register destination enforcement (refusing SGPR pairs and out-of-range s106), low16 VGPR source refusal, pending sample and pending scalar source/destination refusal, non-entry mask refusal, float modifiers, clamp, dst_sel, and remaining unlowered SDWA compare neighbours (i64, v_cmpx). --- CMakeLists.txt | 5 + docs/experiments.md | 2 +- tests/pixel_aot_check.py | 289 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 294 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fabaeed539..ffe5b15ce9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8561,6 +8561,11 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(pixel_aot_sdwa_cmp_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_cmp_${adapter}.txt") + add_test(NAME pixel_aot_sdwa_int_compare_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-sdwa-int-compare "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(pixel_aot_sdwa_int_compare_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_int_compare_${adapter}.txt") add_test(NAME pixel_aot_get_lod_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/pixel_get_lod_check.py" draw "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 5fce219d99..2f4dc1a8e0 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -7698,7 +7698,7 @@ Scalar ALU support also covers the 64-bit SOP1/SOP2 family (`s_cmov_b64`, `s_not The SDWA forms of the 16 `v_cmp_*_f32` predicates are lowered when both `src0_sel` and `src1_sel` are `DWORD`. With whole-dword selections they compare the operands exactly as the e32/e64 forms do; the translator emits identical HLSL for both, and a control asserts it. A byte or word selection would compare a zero-extended field as binary32 and is refused. The predicate draws are repeated with the SDWA compares for all four groups and the same five input pairs, including NaN on either side. A reference that inverts each group's unordered result must report a mismatch. -The 46 non-`v_cmpx` VOPC SDWA comparisons left by lot 1a are lowered: the 16 binary16 predicates and `v_cmp_class_f16_sdwa`, `v_cmp_class_f32_sdwa`, the 8 signed and 8 unsigned 32-bit predicates and the 6 signed and 6 unsigned 16-bit predicates. VOPC SDWA encodes only `src0_sel` and `src1_sel`; `llvm-mc` rejects `dst_sel`/`dst_unused` on these instructions, so the destination stays `vcc`/`vcc_lo` or one SGPR and the translator refuses an SGPR pair and the destination fields. Sources accept `BYTE_0..3`, `WORD_0..1` and `DWORD`, with `sext()` where LLVM prints it. The selection is zero-extended, or sign-extended under `sext()`, before the existing table applies: binary16 uses the low 16 bits through `widen16`/`num16`, the 16-bit integer forms take the low 16 bits, the 32-bit integer forms compare the selected word as signed or unsigned, and the class forms feed the selected bits to `class16` or `class32`. The comparison result is tracked in the same VCC/SGPR lane-mask state as the e32 and e64 forms, so `v_cndmask_b32` consumes it unchanged. The source-built fixture `tests/pixel_aot_check.py draw-sdwa-cmp` puts all 46 forms in one program, folds each predicate into two exported `R32G32B32A32_UINT` words, and repeats the rows with selections on every field. Its wrong references must each report `pixel_mismatch`: selections ignored (whole dwords), every field reversed (`BYTE_0`/`BYTE_3`, `BYTE_1`/`BYTE_2`, `WORD_0`/`WORD_1`), signedness inverted on the rows containing `0x8000`/`0xffff`, and zero extension in place of `sext()`. A second program writes two comparisons to `s52` and `vcc_lo` before consuming both; a reference that uses the second mask for both channels must mismatch. `tests/form_coverage_check.py` isolates this fixture's accepted words and asserts the 46 forms count in both `gfx1013.tsv` and `rdna2.tsv`, while the non-SDWA `v_cmp_*` neighbours and the `v_cmpx_*_sdwa` forms stay out. The same program is part of `check_binary`, so the LLVM 15 `Assemble and translate gfx1013 fixtures` job assembles, disassembles and re-translates it. On base `0aa6522c` the measurement moved from 1255/1968 to 1301/1968 on gfx1013 and from 1246/1863 to 1292/1863 on gfx1030, exactly 46 forms in each reference. The new tests passed on WARP and on `pixel_aot_sdwa_cmp_hardware` on an AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Windows 11 build 10.0.26300.10075, in 1.10 s; the debug layer was unavailable. The separate `pixel_aot_comparisons_hardware` entry still fails on this adapter in its pre-existing strict binary32 subnormal comparison ([issue #958](https://github.com/R0Y00/Xbox2Windows/issues/958)); that failure is outside this lot and does not affect the SDWA draw. This measures translated fixtures, not console behavior or full title execution. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_sdwa_cmp_' -V`; the full measurement is `ctest --preset dev --no-tests=error -R '^(pixel_aot_|compute_store_aot_)' -E '_hardware$' -j 1 --output-junit out/build/dev/out/wave1-sdwa-after.xml` followed by `python -B tools/form_coverage.py out/build/dev/out/wave1-sdwa-after.xml --records out/build/dev/form_coverage --json out/wave1-sdwa-after.json`. +The 46 non-`v_cmpx` VOPC SDWA comparisons left by lot 1a are lowered: the 16 binary16 predicates and `v_cmp_class_f16_sdwa`, `v_cmp_class_f32_sdwa`, the 8 signed and 8 unsigned 32-bit predicates and the 6 signed and 6 unsigned 16-bit predicates. VOPC SDWA encodes only `src0_sel` and `src1_sel`; `llvm-mc` rejects `dst_sel`/`dst_unused` on these instructions, so the destination stays `vcc`/`vcc_lo` or one SGPR and the translator refuses an SGPR pair and the destination fields. Sources accept `BYTE_0..3`, `WORD_0..1` and `DWORD`, with `sext()` where LLVM prints it. The selection is zero-extended, or sign-extended under `sext()`, before the existing table applies: binary16 uses the low 16 bits through `widen16`/`num16`, the 16-bit integer forms take the low 16 bits, the 32-bit integer forms compare the selected word as signed or unsigned, and the class forms feed the selected bits to `class16` or `class32`. The comparison result is tracked in the same VCC/SGPR lane-mask state as the e32 and e64 forms, so `v_cndmask_b32` consumes it unchanged. The source-built fixture `tests/pixel_aot_check.py draw-sdwa-cmp` puts all 46 forms in one program, folds each predicate into two exported `R32G32B32A32_UINT` words, and repeats the rows with selections on every field. Its wrong references must each report `pixel_mismatch`: selections ignored (whole dwords), every field reversed (`BYTE_0`/`BYTE_3`, `BYTE_1`/`BYTE_2`, `WORD_0`/`WORD_1`), signedness inverted on the rows containing `0x8000`/`0xffff`, and zero extension in place of `sext()`. A second program writes two comparisons to `s52` and `vcc_lo` before consuming both; a reference that uses the second mask for both channels must mismatch. `tests/form_coverage_check.py` isolates this fixture's accepted words and asserts the 46 forms count in both `gfx1013.tsv` and `rdna2.tsv`, while the non-SDWA `v_cmp_*` neighbours and the `v_cmpx_*_sdwa` forms stay out. The same program is part of `check_binary`, so the LLVM 15 `Assemble and translate gfx1013 fixtures` job assembles, disassembles and re-translates it. On base `0aa6522c` the measurement moved from 1255/1968 to 1301/1968 on gfx1013 and from 1246/1863 to 1292/1863 on gfx1030, exactly 46 forms in each reference. The new tests passed on WARP and on `pixel_aot_sdwa_cmp_hardware` on an AMD Radeon RX 9060 XT (driver 32.0.32029.2010), Windows 11 build 10.0.26300.10075, in 1.10 s; the debug layer was unavailable. The separate `pixel_aot_comparisons_hardware` entry still fails on this adapter in its pre-existing strict binary32 subnormal comparison ([issue #958](https://github.com/R0Y00/Xbox2Windows/issues/958)); that failure is outside this lot and does not affect the SDWA draw. This measures translated fixtures, not console behavior or full title execution. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_sdwa_cmp_' -V`; the full measurement is `ctest --preset dev --no-tests=error -R '^(pixel_aot_|compute_store_aot_)' -E '_hardware$' -j 1 --output-junit out/build/dev/out/wave1-sdwa-after.xml` followed by `python -B tools/form_coverage.py out/build/dev/out/wave1-sdwa-after.xml --records out/build/dev/form_coverage --json out/wave1-sdwa-after.json`. A supplemental per-predicate fixture (`tests/pixel_aot_check.py draw-sdwa-int-compare`, tests `pixel_aot_sdwa_int_compare_{warp,hardware}`) exercises all 16 32-bit integer SDWA comparisons across four 4-channel groups (`i32_lo`, `i32_hi`, `u32_lo`, `u32_hi`) with 41 targeted single-channel negative controls plus static refusal checks for `low16` sources, pending loads, masked regions, float modifiers, `clamp` and SGPR pair destinations. Binary32 floating-point vector comparisons (`v_cmp_{f,lt,eq,le,gt,lg,ge,o,u,nge,nlg,ngt,nle,neq,nlt,tru}_f32` in e32, e64, and whole-dword SDWA forms) evaluate the 16 IEEE-754 relations against scalar and vector operands, including subnormals, signed zeroes, infinities, and quiet/signaling NaNs. The e32 (VOPC) encoding requires destination `vcc` or `vcc_lo`; SGPR destinations (`s52`, `s[52:53]`) are accepted on suffix-less and explicit `_e64` (VOP3) encodings. Source modifiers (`neg`, `abs`) are accepted on e64 operands (`-s44`, `|v18|`, `-|s44|`), while `clamp` and `op_sel` remain rejected on comparisons. Rejection controls verify that untracked source registers, SGPR destinations on explicit e32, and sub-dword SDWA selections (`src0_sel:WORD_0`, `src1_sel:BYTE_2`) raise `ValueError`. The synthetic fixture `f32_compares()` verifies all 16 predicates across e32, e64, and whole-dword SDWA, validating them against an exact reference oracle (`f32_compares_reference()`). Mutating predicates or modifiers produces a mismatch. diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index e44288b234..b0e12e5d9c 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -11346,6 +11346,167 @@ def sdwa_integer_cases(group): return cases +# Supplemental per-predicate SDWA 32-bit integer comparison draws (all 16 forms across four 4-channel groups), +# with A, B, C and D in s44 to s47, B in v18, C in v19 and 1.0 (0x3f800000) in v20. Each channel writes 0 or +# 0x3f800000 via v_cndmask_b32 from vcc_lo or s52. Together the four groups cover all 16 opcodes, all 7 +# selectors on both src0_sel and src1_sel, zero- and sign-extension on either or both sources, and all +# SGPR/VGPR source pairs. +SDWA_INT_CMP_ONE = 0x3f800000 +SDWA_INT_CMP_BODIES = { + "i32_lo": [ + "v_cmp_f_i32_sdwa vcc_lo, s44, v18 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e32 v12, 0, v20, vcc_lo", + "v_cmp_lt_i32_sdwa s52, sext(s44), v18 src0_sel:BYTE_0 src1_sel:BYTE_1", + "v_cndmask_b32_e64 v13, 0, v20, s52", + "v_cmp_eq_i32_sdwa vcc_lo, sext(v18), sext(s44) src0_sel:WORD_0 src1_sel:WORD_1", + "v_cndmask_b32_e32 v14, 0, v20, vcc_lo", + "v_cmp_le_i32_sdwa s52, s44, sext(v19) src0_sel:WORD_0 src1_sel:BYTE_2", + "v_cndmask_b32_e64 v15, 0, v20, s52", + ], + "i32_hi": [ + "v_cmp_gt_i32_sdwa vcc_lo, s44, sext(s45) src0_sel:BYTE_3 src1_sel:BYTE_2", + "v_cndmask_b32_e32 v12, 0, v20, vcc_lo", + "v_cmp_ne_i32_sdwa s52, v18, v19 src0_sel:BYTE_1 src1_sel:BYTE_0", + "v_cndmask_b32_e64 v13, 0, v20, s52", + "v_cmp_ge_i32_sdwa vcc_lo, sext(v18), sext(s44) src0_sel:BYTE_3 src1_sel:WORD_1", + "v_cndmask_b32_e32 v14, 0, v20, vcc_lo", + "v_cmp_t_i32_sdwa s52, s44, v18 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e64 v15, 0, v20, s52", + ], + "u32_lo": [ + "v_cmp_f_u32_sdwa vcc_lo, v18, s44 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e32 v12, 0, v20, vcc_lo", + "v_cmp_lt_u32_sdwa s52, s44, sext(v18) src0_sel:WORD_0 src1_sel:BYTE_0", + "v_cndmask_b32_e64 v13, 0, v20, s52", + "v_cmp_eq_u32_sdwa vcc_lo, s44, s45 src0_sel:BYTE_2 src1_sel:BYTE_3", + "v_cndmask_b32_e32 v14, 0, v20, vcc_lo", + "v_cmp_le_u32_sdwa s52, v18, v19 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e64 v15, 0, v20, s52", + ], + "u32_hi": [ + "v_cmp_gt_u32_sdwa vcc_lo, sext(s44), v18 src0_sel:WORD_1 src1_sel:DWORD", + "v_cndmask_b32_e32 v12, 0, v20, vcc_lo", + "v_cmp_ne_u32_sdwa s52, sext(v18), sext(s44) src0_sel:BYTE_1 src1_sel:BYTE_1", + "v_cndmask_b32_e64 v13, 0, v20, s52", + "v_cmp_ge_u32_sdwa vcc_lo, s44, v19 src0_sel:WORD_0 src1_sel:WORD_0", + "v_cndmask_b32_e32 v14, 0, v20, vcc_lo", + "v_cmp_t_u32_sdwa s52, v18, s44 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e64 v15, 0, v20, s52", + ], +} +SDWA_INT_CMP_OPS = tuple(sorted({line.split()[0] for body in SDWA_INT_CMP_BODIES.values() for line in body + if line.split()[0].endswith("_sdwa")})) + + +def _u32_sext(value, width): + return _sext(value, width) & 0xffffffff + + +SDWA_INT_CMP_RESULTS = { + "i32_lo": lambda a, b, c, d: ( + False, + _sext(_byte(a, 0), 8) < _byte(b, 1), + _sext(_word(b, 0), 16) == _sext(_word(a, 1), 16), + _word(a, 0) <= _sext(_byte(c, 2), 8), + ), + "i32_hi": lambda a, b, c, d: ( + _byte(a, 3) > _sext(_byte(b, 2), 8), + _byte(b, 1) != _byte(c, 0), + _sext(_byte(b, 3), 8) >= _sext(_word(a, 1), 16), + True, + ), + "u32_lo": lambda a, b, c, d: ( + False, + _word(a, 0) < _u32_sext(_byte(b, 0), 8), + _byte(a, 2) == _byte(b, 3), + b <= c, + ), + "u32_hi": lambda a, b, c, d: ( + _u32_sext(_word(a, 1), 16) > b, + _u32_sext(_byte(b, 1), 8) != _u32_sext(_byte(a, 1), 8), + _word(a, 0) >= _word(c, 0), + True, + ), +} +SDWA_INT_CMP_WRONG = { + "i32_lo": ( + (0, 0, lambda a, b, c, d: True, "t for f"), + (0, 1, lambda a, b, c, d: _byte(a, 0) < _byte(b, 1), "sign extension dropped on src0"), + (0, 1, lambda a, b, c, d: _u32_sext(_byte(a, 0), 8) < _byte(b, 1), "unsigned for signed"), + (1, 1, lambda a, b, c, d: _sext(_byte(a, 1), 8) < _byte(b, 1), "BYTE_1 for BYTE_0"), + (0, 2, lambda a, b, c, d: _word(b, 0) == _sext(_word(a, 1), 16), "sign extension dropped on src0"), + (0, 2, lambda a, b, c, d: _s32(b) == _s32(a), "whole DWORD for WORD_0/WORD_1"), + (0, 2, lambda a, b, c, d: _sext(_word(b, 0), 16) != _sext(_word(a, 1), 16), "ne for eq"), + (0, 3, lambda a, b, c, d: _word(a, 0) <= _byte(c, 2), "sign extension dropped on src1"), + (0, 3, lambda a, b, c, d: _word(a, 0) <= _u32_sext(_byte(c, 2), 8), "unsigned for signed"), + (1, 3, lambda a, b, c, d: _word(a, 0) < _sext(_byte(c, 2), 8), "lt for le"), + (1, 3, lambda a, b, c, d: _word(a, 1) <= _sext(_byte(c, 2), 8), "WORD_1 for WORD_0"), + ), + "i32_hi": ( + (0, 0, lambda a, b, c, d: _byte(a, 3) > _byte(b, 2), "sign extension dropped on src1"), + (0, 0, lambda a, b, c, d: _byte(a, 3) > _u32_sext(_byte(b, 2), 8), "unsigned for signed"), + (1, 0, lambda a, b, c, d: _byte(a, 3) >= _sext(_byte(b, 2), 8), "ge for gt"), + (0, 1, lambda a, b, c, d: b != c, "whole DWORD for BYTE_1/BYTE_0"), + (0, 1, lambda a, b, c, d: _byte(b, 0) != _byte(c, 0), "BYTE_0 for BYTE_1"), + (0, 1, lambda a, b, c, d: _byte(b, 1) == _byte(c, 0), "eq for ne"), + (0, 2, lambda a, b, c, d: _sext(_byte(b, 3), 8) >= _word(a, 1), "sign extension dropped on src1"), + (0, 2, lambda a, b, c, d: _u32_sext(_byte(b, 3), 8) >= _u32_sext(_word(a, 1), 16), "unsigned for signed"), + (1, 2, lambda a, b, c, d: _byte(b, 3) >= _sext(_word(a, 1), 16), "sign extension dropped on src0"), + (0, 3, lambda a, b, c, d: False, "f for t"), + ), + "u32_lo": ( + (0, 0, lambda a, b, c, d: True, "t for f"), + (0, 1, lambda a, b, c, d: _word(a, 0) < _byte(b, 0), "sign extension dropped on src1"), + (0, 1, lambda a, b, c, d: _word(a, 0) < _sext(_byte(b, 0), 8), "signed for unsigned"), + (0, 1, lambda a, b, c, d: _word(a, 0) > _u32_sext(_byte(b, 0), 8), "gt for lt"), + (0, 2, lambda a, b, c, d: _byte(a, 3) == _byte(b, 3), "BYTE_3 for BYTE_2"), + (0, 2, lambda a, b, c, d: _byte(a, 2) == _byte(b, 2), "BYTE_2 for BYTE_3"), + (0, 2, lambda a, b, c, d: a == b, "whole DWORD for BYTE_2/BYTE_3"), + (0, 2, lambda a, b, c, d: _byte(a, 2) != _byte(b, 3), "ne for eq"), + (0, 3, lambda a, b, c, d: _s32(b) <= _s32(c), "signed for unsigned"), + (1, 3, lambda a, b, c, d: b < c, "lt for le"), + ), + "u32_hi": ( + (0, 0, lambda a, b, c, d: _word(a, 1) > b, "sign extension dropped on src0"), + (0, 0, lambda a, b, c, d: _sext(_word(a, 1), 16) > _s32(b), "signed for unsigned"), + (0, 0, lambda a, b, c, d: _u32_sext(_word(a, 0), 16) > b, "WORD_0 for WORD_1"), + (0, 1, lambda a, b, c, d: b != a, "whole DWORD for BYTE_1"), + (0, 1, lambda a, b, c, d: _u32_sext(_byte(b, 0), 8) != _u32_sext(_byte(a, 1), 8), "BYTE_0 for BYTE_1"), + (0, 1, lambda a, b, c, d: _u32_sext(_byte(b, 1), 8) == _u32_sext(_byte(a, 1), 8), "eq for ne"), + (0, 2, lambda a, b, c, d: _word(a, 0) >= _word(c, 1), "WORD_1 for WORD_0 on src1"), + (0, 2, lambda a, b, c, d: _word(c, 0) >= _word(a, 0), "swapped operands"), + (1, 2, lambda a, b, c, d: _word(a, 0) > _word(c, 0), "gt for ge"), + (0, 3, lambda a, b, c, d: False, "f for t"), + ), +} +SDWA_INT_CMP_ROWS = { + "i32_lo": ((0x81250090, 0x40d18125, 0x3ef01234, 0), (0x40250040, 0x3f802001, 0x3e401234, 0)), + "i32_hi": ((0xc0251234, 0x40f05511, 0x3f002255, 0), (0x00801234, 0xc0005511, 0x3f002266, 0)), + "u32_lo": ((0x3f400100, 0x40123480, 0xbf800000, 0), (0x3f410100, 0x4012347f, 0x4012347f, 0)), + "u32_hi": ((0xc0257000, 0x3f807011, 0xbf803f80, 0), (0x3f803f80, 0x3f809111, 0xbf803f80, 0)), +} + + +def sdwa_int_compare_group(group): + return ["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v19, s46", "v_mov_b32_e32 v20, 1.0", + *SDWA_INT_CMP_BODIES[group]] + + +def sdwa_int_compare_cases(group): + """(row, expected words, status, reason) for every draw of a group: the right rows, then the controls.""" + rows = SDWA_INT_CMP_ROWS[group] + right = [tuple(SDWA_INT_CMP_ONE if flag else 0 for flag in SDWA_INT_CMP_RESULTS[group](*row)) for row in rows] + cases = [(row, words, "pass", "") for row, words in zip(rows, right)] + for index, channel, wrong, reason in SDWA_INT_CMP_WRONG[group]: + words = list(right[index]) + words[channel] = SDWA_INT_CMP_ONE if wrong(*rows[index]) else 0 + assert words[channel] != right[index][channel], (group, reason) + cases.append((rows[index], tuple(words), "mismatch", reason)) + for row, words in zip(rows, right): + assert all(binary32_normal_or_zero(word) for word in (*row, *words)), (group, [hex(w) for w in (*row, *words)]) + return cases + + # Saturating 16-bit packs (v_cvt_pk_u16_u32, v_cvt_pk_i16_i32). Bodies for uncompressed_export(): s44 = A and # s45 = B from the constants, v18 = B, v19 = B >> 16 (the high half, always inside both 16-bit ranges when # read as unsigned or sign-extended below), v20 = B >> 12 and v21 = B >> 16 arithmetic. A normal binary32 @@ -12614,6 +12775,99 @@ def statement(*body): must_reject(translate_int, uncompressed_export(["v_mov_b32_e32 v18, s45", bad])) +def check_sdwa_int_compare_lowering(): + """Supplemental static lowering and refusal checks for the 16 32-bit integer SDWA comparisons.""" + assert (SDWA_INT_CMP_BODIES.keys() == SDWA_INT_CMP_RESULTS.keys() + == SDWA_INT_CMP_WRONG.keys() == SDWA_INT_CMP_ROWS.keys()) + src0_sels, src1_sels = set(), set() + for group, body in SDWA_INT_CMP_BODIES.items(): + translate_int(uncompressed_export(sdwa_int_compare_group(group))) + sdwa_int_compare_cases(group) + for line in body: + if m := re.search(r"src0_sel:(\S+) src1_sel:(\S+)", line): + src0_sels.add(m.group(1)) + src1_sels.add(m.group(2)) + assert src0_sels == set(pixel_aot.SDWA_SEL), src0_sels + assert src1_sels == set(pixel_aot.SDWA_SEL), src1_sels + expected_ops = {f"v_cmp_{op}_{kind}_sdwa" for op in INT_PREDICATES for kind in ("i32", "u32")} + assert len(SDWA_INT_CMP_OPS) == 16 and set(SDWA_INT_CMP_OPS) == expected_ops + assert expected_ops <= ACCEPTED, expected_ops - ACCEPTED + + def cmp_statement(compare_line, mask_reg="vcc_lo"): + cnd = f"v_cndmask_b32_{'e32' if mask_reg in ('vcc', 'vcc_lo') else 'e64'} v12, 0, v19, {mask_reg}" + program = uncompressed_export(["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v19, 1.0", compare_line, cnd]) + index = program.index(compare_line) + lines = [line for line in translate_int(program).splitlines() if line.startswith(f" bool cmp{index} = ")] + assert len(lines) == 1, (compare_line, lines) + return lines[0].replace(f"cmp{index} ", "c ", 1) + + a, v = "params[0].w", "asuint(alu20)" + # With whole-dword selections every form matches its e32 comparison, and vcc/vcc_lo/s52 emit identical HLSL. + for kind in ("i32", "u32"): + for op in INT_PREDICATES: + whole_vcc = cmp_statement(f"v_cmp_{op}_{kind}_sdwa vcc_lo, s44, v18 src0_sel:DWORD src1_sel:DWORD") + whole_vcc64 = cmp_statement(f"v_cmp_{op}_{kind}_sdwa vcc, s44, v18 src0_sel:DWORD src1_sel:DWORD", "vcc") + whole_s52 = cmp_statement(f"v_cmp_{op}_{kind}_sdwa s52, s44, v18 src0_sel:DWORD src1_sel:DWORD", "s52") + assert whole_vcc == whole_vcc64 == whole_s52, (kind, op, whole_vcc, whole_vcc64, whole_s52) + plain = cmp_statement(f"v_cmp_{op}_{kind}_e32 vcc_lo, s44, v18") + if kind == "u32": + assert whole_vcc == plain, (kind, op, whole_vcc, plain) + else: + assert whole_vcc == plain.replace("asint(alu20)", f"asint({v})"), (kind, op, whole_vcc, plain) + assert cmp_statement("v_cmp_lt_i32_sdwa s52, sext(s44), v18 src0_sel:BYTE_0 src1_sel:BYTE_1", "s52") == ( + f" bool c = asint(asuint(asint({a} << 24u) >> 24u)) < asint((({v} >> 8u) & 0xffu));") + assert cmp_statement("v_cmp_lt_u32_sdwa s52, s44, sext(v18) src0_sel:WORD_0 src1_sel:BYTE_0", "s52") == ( + f" bool c = (({a} >> 0u) & 0xffffu) < asuint(asint({v} << 24u) >> 24u);") + + good = "v_cmp_lt_i32_sdwa vcc_lo, s44, v18 src0_sel:DWORD src1_sel:DWORD" + for bad, message in ( + (good.replace("s44,", "sext(s44,"), "Unsupported SDWA comparison operands"), + (good.replace("s44,", "s44),"), "Unsupported SDWA comparison operands"), + (good.replace("v18 ", "sext(v18 "), "Unsupported SDWA comparison operands"), + (good + " clamp", "Unsupported SDWA comparison operands"), + ("v_cmp_lt_i32_sdwa vcc_lo, s44, v18 src0_sel:DWORD", "Unsupported SDWA comparison operands"), + (good.replace("src0_sel:DWORD", "src0_sel:WORD_2"), "Unsupported SDWA comparison operands"), + (good.replace("vcc_lo", "s106"), "Scalar register out of range"), + (good.replace("vcc_lo", "s[52:53]"), "VOPC SDWA destination cannot be an SGPR pair"), + (good.replace("vcc_lo", "s[51:52]"), "VOPC SDWA destination cannot be an SGPR pair"), + (good.replace("vcc_lo", "s[105:106]"), "VOPC SDWA destination cannot be an SGPR pair"), + (good.replace("v18", "v0"), "Expected a numeric register"), + ): + expect_value_error(translate_int, uncompressed_export(["v_mov_b32_e32 v18, s45", bad]), message) + for bad in ( + "v_cmp_lt_i32_sdwa vcc_lo, s44, v18 dst_sel:DWORD src0_sel:DWORD src1_sel:DWORD", + good.replace("s44,", "-s44,"), + good.replace("v18 ", "|v18| "), + good.replace("v18 ", "abs(v18) "), + good.replace("s44,", "sext(-s44),"), + good.replace("v18", "v106"), + ): + must_reject(translate_int, uncompressed_export(["v_mov_b32_e32 v18, s45", bad])) + # A low16 VGPR source (undefined high half) is rejected as non-numeric, matching SDWA_INT. + expect_value_error( + translate_int, + uncompressed_export(["v_mov_b32_e32 v18, s45", "v_add_nc_u16 v24, s44, v18", + "v_cmp_lt_i32_sdwa vcc_lo, v24, v18 src0_sel:WORD_0 src1_sel:WORD_0"]), + "Expected a numeric register", + ) + # Pending sample source, pending scalar source/destination and non-entry mask are rejected. + base_prog = uncompressed_export(sdwa_int_compare_group("i32_lo")) + pos = base_prog.index("s_waitcnt vmcnt(0)") + must_reject(translate_int, base_prog[:pos] + ["v_cmp_eq_u32_sdwa vcc_lo, v12, v13 src0_sel:DWORD src1_sel:DWORD"] + base_prog[pos:]) + load = next(i for i, line in enumerate(base_prog) if line.startswith("s_buffer_load_dwordx4")) + pos = base_prog.index("s_waitcnt lgkmcnt(0)", load) + must_reject(translate_int, base_prog[:pos] + ["v_cmp_eq_i32_sdwa vcc_lo, s44, s45 src0_sel:DWORD src1_sel:DWORD"] + base_prog[pos:]) + must_reject(translate_int, base_prog[:pos] + ["v_cmp_eq_i32_sdwa s44, s46, s47 src0_sel:DWORD src1_sel:DWORD"] + base_prog[pos:]) + pos = base_prog.index("s_wqm_b64 exec, exec") + 1 + must_reject(translate_int, base_prog[:pos] + [good] + base_prog[pos:]) + # Remaining unlowered SDWA compare neighbours stay refused. + for bad in ( + "v_cmp_lt_i64_sdwa vcc_lo, s[44:45], v[18:19] src0_sel:DWORD src1_sel:DWORD", + "v_cmpx_lt_i32_sdwa s44, v18 src0_sel:DWORD src1_sel:DWORD", + ): + must_reject(translate_int, uncompressed_export(["v_mov_b32_e32 v18, s45", bad])) + + def check_pack_int16_lowering(): """Static checks for v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 in the programs of pack_group().""" a = "params[0].w" @@ -18508,6 +18762,7 @@ def _check_controls_inline(directory): "check_cvt_int_f64_lowering", "check_cvt_f32_f64_lowering", "check_sdwa_integer_lowering", + "check_sdwa_int_compare_lowering", "check_sdwa_cmp_controls", "check_pack_int16_lowering", "check_sat_pack_u8_lowering", @@ -19158,6 +19413,7 @@ def check_binary(directory, assembler, objcopy, library): fixtures.extend((cvt_int_f64_static_binary_program(kind), translate_int) for kind in CVT_INT_F64_STATIC_ROWS) fixtures.append((cvt_f32_f64_binary_program(), translate_int)) fixtures.extend((uncompressed_export(sdwa_integer_group(group)), translate_int) for group in SDWA_INT_BODIES) + fixtures.extend((uncompressed_export(sdwa_int_compare_group(group)), translate_int) for group in SDWA_INT_CMP_BODIES) fixtures.extend((uncompressed_export(pack_group(group)), translate_int) for group in PACK_BODIES) fixtures.append((sat_pack_u8_program(), translate_int)) fixtures.append((uncompressed_export(pack_b32_group()), translate_int)) @@ -19839,6 +20095,36 @@ def check_sdwa_integer_draws(directory, compiler, renderer, backend): return 0 +def check_sdwa_int_compare_draws(directory, compiler, renderer, backend): + """Draw the supplemental per-predicate SDWA 32-bit integer comparison groups through integer readback. + + Each group of SDWA_INT_CMP_BODIES is compiled once and drawn in one renderer batch: every row of + SDWA_INT_CMP_ROWS with the right words, then one draw per SDWA_INT_CMP_WRONG entry with one channel + replaced by the word a wrong signedness, extension, selection or predicate would give, which must + report a mismatch. On a desktop GPU the draws establish that the translated shader computes the stated + words, not what the console does. + """ + source = directory / "sdwa_int_compare.hlsl" + shader = directory / "sdwa_int_compare.cso" + for group in SDWA_INT_CMP_BODIES: + source.write_text(translate_int(uncompressed_export(sdwa_int_compare_group(group))), encoding="ascii") + compile_shader(compiler, source, shader) + cases = [] + for row, words, status, _ in sdwa_int_compare_cases(group): + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *row) + case = directory / f"sdwa_int_compare-{len(cases)}.case" + case.write_bytes(bytes(constants) + struct.pack("<4I", *words) * 64 + b"\x01") + cases.append((case, status)) + try: + run_pixel_batch(renderer, backend, shader, cases) + except subprocess.CalledProcessError as error: + if error.returncode == 77: + return 77 + raise + return 0 + + def check_pack_int16_draws(directory, compiler, renderer, backend): """Draw v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 through the integer readback target and compare the words. @@ -25347,7 +25633,7 @@ def main(): directory = Path(tmp) if mode in ("draw-blend-scalar", "draw-predicate-rounding", "draw-integer-nc", "draw-e64-integer", "draw-bfm", "draw-brev", "draw-align", - "draw-sdwa-integer", "draw-plain-bitscan", "draw-vop3-ternary-int", "draw-swap", + "draw-sdwa-integer", "draw-sdwa-int-compare", "draw-plain-bitscan", "draw-vop3-ternary-int", "draw-swap", "draw-mac-legacy", "draw-carry-out", "draw-carry-in", "draw-mad64", "draw-cvt-f64-int", "draw-cvt-f64-f32", "draw-cvt-int-f64", "draw-cvt-f32-f64", "draw-pack-int16", "draw-pack-b32", "draw-sad", "draw-int16-vop3", "draw-int16-ternary", "draw-compressed-export", "draw-uncompressed-export", @@ -25377,6 +25663,7 @@ def main(): "draw-cvt-int-f64": check_cvt_int_f64_draws, "draw-cvt-f32-f64": check_cvt_f32_f64_draws, "draw-sdwa-integer": check_sdwa_integer_draws, + "draw-sdwa-int-compare": check_sdwa_int_compare_draws, "draw-bfm": check_bfm_draw, "draw-brev": check_brev_draw, "draw-align": check_align_draws,