From a8a670616c9f2b9f4ce679094d391e3fb0c6cbf2 Mon Sep 17 00:00:00 2001 From: rm-NoobInCoding <35840559+rm-NoobInCoding@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:28:41 +0330 Subject: [PATCH 1/3] shader: lower the binary16 SDWA comparisons v_cmp_*_f16_sdwa (16 predicates) and v_cmp_class_f16_sdwa were refused. WORD_1 now selects the high half of a source and WORD_0 or DWORD the low half the e32 form reads; -, |x| and -|x| apply after the selection, and the existing binary16 comparison and class16 test run on the result. Registers only; byte selections, sext(), constants, a class mask with another selection or a modifier, and v_cmpx stay refused. draw-sdwa-f16-compare exports the 16 predicates under three selection and modifier mixes and four class tests on ten input sets whose halves both carry binary16 values, with selections-ignored and modifiers-ignored references that must mismatch. The shader scanner accepts the same forms. --- CMakeLists.txt | 5 + docs/experiments.md | 2 + tests/pixel_aot_check.py | 172 +++++++++++++++++++++++++++++- tests/rdna2_tools_check.py | 21 ++++ tools/pixel_aot.py | 50 +++++++-- tools/scan_unsupported_shaders.py | 11 ++ 6 files changed, 250 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e327f72bc7..525dc7bd1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6449,6 +6449,11 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(pixel_aot_sad_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sad_${adapter}.txt") + add_test(NAME pixel_aot_sdwa_f16_compare_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-sdwa-f16-compare "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(pixel_aot_sdwa_f16_compare_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_f16_compare_${adapter}.txt") add_test(NAME pixel_aot_rndne_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/pixel_rndne_check.py" "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 469ab31a3c..95125bda0f 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5814,6 +5814,8 @@ 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 binary16 SDWA comparisons `v_cmp_{f,lt,eq,le,gt,lg,ge,o,u,nge,nlg,ngt,nle,neq,nlt,tru}_f16_sdwa` and `v_cmp_class_f16_sdwa` lower with word selections. `WORD_1` selects the high half of a source; `WORD_0` and `DWORD` leave the low half, which is what the e32 form reads, so a comparison with both selections `DWORD` and no modifier emits the same HLSL as its e32 spelling, which a control asserts. `-`, `|x|` and `-|x|` apply after the selection, and the comparison is then the existing binary16 one: the ordered relations test `num16` on the selected, modified encodings and compare the widened values, and the class test reads `class16` of the selected, modified half against the mask. Sources are registers; byte selections, `sext()`, inline constants and literals (LLVM 15 prints them as `neg(2.0)` and similar), a missing selection, `exec` as the destination, a class mask with a selection other than `DWORD` or with a modifier, and the `v_cmpx` SDWA forms are refused. One program exports four words through the `R32G32B32A32_UINT` readback target: three words hold the 16 predicates as bits under (src0, src1, src0_sel, src1_sel) = (A, B, `WORD_1`, `WORD_0`), (-B, |A|, `WORD_1`, `DWORD`) and (-|A|, B, `DWORD`, `WORD_1`), and the fourth holds four class tests of C (`WORD_1`, -C `WORD_0`, |C| `WORD_1`, -|C| `DWORD`) against the mask D. Each of A, B and C carries binary16 values in both halves, so a selection that reads the wrong half changes the answer. Ten input sets cover ordered pairs both ways, equal values, signed zeros, infinities, denormals, quiet and signaling NaNs in either operand, and adjacent values; a static check asserts that every predicate bit other than f and tru, and every class bit, takes both values over the sets. For each wrong model (selections ignored, modifiers ignored) the first set it changes is drawn against that model's words and must report `pixel_mismatch`. That is 12 draws from one compile. They run as `pixel_aot_sdwa_f16_compare_hardware` and `pixel_aot_sdwa_f16_compare_warp` (`tests/pixel_aot_check.py draw-sdwa-f16-compare`), with their own coverage record listing all 17 forms, and passed on the NVIDIA GeForce RTX 4070 (driver 32.0.16.1714) and on WARP, Windows 11 (build 26300). As a check of the fixture, ignoring `WORD_1` in the lowering makes the draw mismatch on WARP. `tools/scan_unsupported_shaders.py` accepts the same forms. The LLVM 15 round trip was not run on that host. They show that the translated shader computes the stated comparisons on desktop GPUs, not that the console does. + 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. The original [#958](https://github.com/R0Y00/Xbox2Windows/issues/958) report at `0f0c3eb` recorded a strict comparison mismatch on an RX 9070 XT. Predicate bit 18 compares binary32 subnormal `0x00000001` with `-0.0` and should be clear. The hardware readback set it while WARP and the separate denormal-preservation control passed. The strict expectation and predicate mutation controls are unchanged. Reproduce with `ctest --preset dev --no-tests=error -R '^pixel_aot_comparisons_(warp|hardware)$' -V`. See the reduced desktop DXIL measurements below and the additional adapter results in #958. diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index 73e550cc6c..cdaa13f6d0 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -16291,6 +16291,7 @@ def _check_controls_inline(directory): "check_sat_pack_u8_lowering", "check_pack_b32_lowering", "check_sad_lowering", + "check_sdwa_f16_compare_lowering", "check_scalar_controls", "check_three_way_templates", "check_sdwa_unary_lowering", @@ -20884,6 +20885,174 @@ def cmp_pred(pred: str, a: float, b: float) -> bool: return (w0, w1, w2, w3) +# Binary16 SDWA comparisons and class tests (v_cmp_*_f16_sdwa, v_cmp_class_f16_sdwa). A (s44 -> v18), B (s45 -> v16), +# C (s46 -> v20) and the class mask D (s47 -> v21) carry binary16 values in both halves, so a selection that reads the +# wrong half changes the answer. Words 0..2 hold the 16 predicates under one (dst, src0, src1, src0_sel, src1_sel) +# each; word 3 holds four class tests as (source, src0_sel). +SDWA_F16_COMPARES = ( + ("vcc_lo", "v18", "v16", "WORD_1", "WORD_0"), + ("s52", "-v16", "|v18|", "WORD_1", "DWORD"), + ("vcc_lo", "-|v18|", "v16", "DWORD", "WORD_1"), +) +SDWA_F16_CLASSES = (("v20", "WORD_1"), ("-v20", "WORD_0"), ("|v20|", "WORD_1"), ("-|v20|", "DWORD")) +SDWA_F16_SETS = ( # (A, B, C, D), each half as high:low + (0x3c004000, 0x40003c00, 0x7e003c00, 0x3ff), # 1:2 against 2:1; qNaN:1 against every class + (0x7e003c00, 0x3c007e00, 0x80000000, 0x060), # qNaN:1 against 1:qNaN; -0:+0 against the zero classes + (0x80000000, 0x00008000, 0x7c00fc00, 0x204), # -0:+0 against +0:-0; +inf:-inf against the infinities + (0x7c00fc00, 0xfc007c00, 0x00018001, 0x090), # +inf:-inf against -inf:+inf; denormals against their classes + (0xc400c400, 0xc4004400, 0x7c013800, 0x101), # -4:-4 against -4:4; sNaN:0.5 against sNaN and +normal + (0x00018001, 0x80010001, 0xbc003c00, 0x108), # denormal pairs; -1:1 against the normal classes + (0x7c013800, 0x38007c01, 0x3c00bc00, 0x002), # sNaN against 0.5; 1:-1 against qNaN only + (0x31183119, 0x31193118, 0xfe007e00, 0x3fc), # adjacent values; -qNaN:qNaN against the non-NaN classes + (0x44003c00, 0xc4003c00, 0x3c007c00, 0x300), # 4:1 against -4:1, so every word compares greater + (0x3c007e00, 0x7e003c00, 0x3c007c00, 0x200), # 1:qNaN against qNaN:1, unordered for words 1 and 2 +) + + +def sdwa_f16_compares() -> list[str]: + body = ["v_mov_b32_e32 v12, 0", "v_mov_b32_e32 v13, 0", "v_mov_b32_e32 v14, 0", "v_mov_b32_e32 v15, 0", + "v_mov_b32_e32 v16, s45", "v_mov_b32_e32 v18, s44", "v_mov_b32_e32 v20, s46", "v_mov_b32_e32 v21, s47"] + + def emit_flag(acc, bit, cond, insn): + body.append(insn) + body.append(f"v_cndmask_b32_e64 v17, 0, 1, {cond}") + if bit: + body.append(f"v_lshlrev_b32_e32 v17, {bit}, v17") + body.append(f"v_or_b32_e32 {acc}, {acc}, v17") + + for word, (dst, src0, src1, sel0, sel1) in enumerate(SDWA_F16_COMPARES): + for bit, pred in enumerate(PREDICATES): + emit_flag(f"v{12 + word}", bit, dst, + f"v_cmp_{pred}_f16_sdwa {dst}, {src0}, {src1} src0_sel:{sel0} src1_sel:{sel1}") + for bit, (source, sel) in enumerate(SDWA_F16_CLASSES): + dst = "s52" if bit % 2 else "vcc_lo" + emit_flag("v15", bit, dst, f"v_cmp_class_f16_sdwa {dst}, {source}, v21 src0_sel:{sel} src1_sel:DWORD") + return body + + +def sdwa_f16_operand(token, words, sel, *, selections=True, modifiers=True): + """The binary16 bits an SDWA source names: its register half, then |x| and -.""" + neg = token.startswith("-") + name = token.lstrip("-") + use_abs = name.startswith("|") + word = words[{"v18": 0, "v16": 1, "v20": 2}[name.strip("|")]] + bits = (word >> 16) & 0xffff if selections and sel == "WORD_1" else word & 0xffff + if modifiers and use_abs: + bits &= 0x7fff + if modifiers and neg: + bits ^= 0x8000 + return bits + + +def sdwa_f16_reference(words, **model): + """The four words sdwa_f16_compares() exports. The keyword arguments describe wrong models.""" + def half(bits): + return struct.unpack("= 0x8000 + if mag > 0x7c00: + return 0x2 if h & 0x0200 else 0x1 + if mag == 0x7c00: + return 0x4 if neg else 0x200 + if mag >= 0x0400: + return 0x8 if neg else 0x100 + if mag: + return 0x10 if neg else 0x80 + return 0x20 if neg else 0x40 + + def holds(pred, a, b): + ordered = not math.isnan(a) and not math.isnan(b) + return {"f": False, "lt": ordered and a < b, "eq": ordered and a == b, "le": ordered and a <= b, + "gt": ordered and a > b, "lg": ordered and a != b, "ge": ordered and a >= b, "o": ordered, + "u": not ordered, "nge": not (ordered and a >= b), "nlg": not (ordered and a != b), + "ngt": not (ordered and a > b), "nle": not (ordered and a <= b), "neq": not (ordered and a == b), + "nlt": not (ordered and a < b), "tru": True}[pred] + + out = [] + for _, src0, src1, sel0, sel1 in SDWA_F16_COMPARES: + a = half(sdwa_f16_operand(src0, words, sel0, **model)) + b = half(sdwa_f16_operand(src1, words, sel1, **model)) + out.append(sum(1 << bit for bit, pred in enumerate(PREDICATES) if holds(pred, a, b))) + out.append(sum(1 << bit for bit, (source, sel) in enumerate(SDWA_F16_CLASSES) + if half_class(sdwa_f16_operand(source, words, sel, **model)) & words[3])) + return tuple(out) + + +SDWA_F16_WRONG_MODELS = (("selections ignored", {"selections": False}), ("modifiers ignored", {"modifiers": False})) + + +def check_sdwa_f16_compare_draws(directory, compiler, renderer, backend): + """Draw the 16 binary16 SDWA predicates and the SDWA class test on SDWA_F16_SETS, and for each wrong model the + first set it changes against that model's words, which must report a mismatch. On a desktop GPU the draws + establish that the translated shader computes the stated comparisons, not what the console does.""" + source = directory / "sdwa-f16-compare.hlsl" + shader = directory / "sdwa-f16-compare.cso" + source.write_text(translate_int(uncompressed_export(sdwa_f16_compares())), encoding="ascii") + compile_shader(compiler, source, shader) + cases = [] + for index, words in enumerate(SDWA_F16_SETS): + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *words) + expected = tuple(as_float(w) for w in sdwa_f16_reference(words)) + cases.append((write_case(directory / f"sdwa-f16-cmp-{index}.case", constants, expected, integer_rt=True), + "pass")) + for label, model in SDWA_F16_WRONG_MODELS: + words = next(w for w in SDWA_F16_SETS if sdwa_f16_reference(w, **model) != sdwa_f16_reference(w)) + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *words) + wrong = tuple(as_float(w) for w in sdwa_f16_reference(words, **model)) + cases.append((write_case(directory / f"sdwa-f16-cmp-{label.replace(' ', '-')}.case", constants, wrong, + integer_rt=True), "mismatch")) + run_pixel_batch(renderer, backend, shader, cases) + return 0 + + +def check_sdwa_f16_compare_lowering(): + """Static checks for the binary16 SDWA comparisons: the 17 forms, the inputs and the refusals.""" + forms = {line.split()[0] for line in sdwa_f16_compares() if "_sdwa" in line} + assert forms == {f"v_cmp_{pred}_f16_sdwa" for pred in PREDICATES} | {"v_cmp_class_f16_sdwa"}, forms + translate_int(uncompressed_export(sdwa_f16_compares())) + # Every predicate bit other than f and tru, and every class bit, takes both values over the sets, and every + # wrong model changes at least one set. + results = [sdwa_f16_reference(words) for words in SDWA_F16_SETS] + for word in range(3): + for bit, pred in enumerate(PREDICATES): + seen = {(result[word] >> bit) & 1 for result in results} + assert seen == ({0} if pred == "f" else {1} if pred == "tru" else {0, 1}), (word, pred, seen) + for bit in range(len(SDWA_F16_CLASSES)): + assert {(result[3] >> bit) & 1 for result in results} == {0, 1}, bit + for label, model in SDWA_F16_WRONG_MODELS: + assert any(sdwa_f16_reference(w, **model) != sdwa_f16_reference(w) for w in SDWA_F16_SETS), label + # With DWORD selections and no modifier the SDWA comparison is the e32 one. + setup = ["v_mov_b32_e32 v16, s45", "v_mov_b32_e32 v18, s44"] + tail = ["v_cndmask_b32_e64 v12, 0, 1, vcc_lo"] + plain = translate_int(uncompressed_export([*setup, "v_cmp_lt_f16_e32 vcc_lo, v18, v16", *tail])) + sdwa = translate_int(uncompressed_export( + [*setup, "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:DWORD src1_sel:DWORD", *tail])) + assert plain == sdwa + # WORD_1 reads the high half, for the value and for the ordered test. + lines = translate_int(uncompressed_export( + [*setup, "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0", *tail])).splitlines() + assert any("num16(((asuint(alu21) >> 16u) & 0xffffu))" in line and "widen16((asuint(alu21) >> 16u))" in line + for line in lines), [line for line in lines if "bool cmp" in line] + good = "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0" + for bad in ( + "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:BYTE_0 src1_sel:WORD_0", + "v_cmp_lt_f16_sdwa vcc_lo, sext(v18), v16 src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_lt_f16_sdwa vcc_lo, 1.0, v16 src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_lt_f16_sdwa vcc_lo, 0x3c00, v16 src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1", + "v_cmp_lt_f16_sdwa exec_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_lt_f16_sdwa vcc_lo, v18, v106 src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_class_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_1", + "v_cmp_class_f16_sdwa vcc_lo, v18, -v16 src0_sel:WORD_1 src1_sel:DWORD", + "v_cmpx_lt_f16_sdwa v18, v16 src0_sel:WORD_1 src1_sel:WORD_0", + ): + translate_int(uncompressed_export([*setup, good, *tail])) + must_reject(translate_int, uncompressed_export([*setup, bad, *tail])) + + def check_f16_compare_draws(directory, compiler, renderer, backend): source = directory / "f16-compare.hlsl" shader = directory / "f16-compare.cso" @@ -22139,7 +22308,7 @@ def main(): "draw-e64-integer", "draw-bfm", "draw-brev", "draw-align", "draw-sdwa-integer", "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-pack-int16", "draw-pack-b32", - "draw-sad", + "draw-sad", "draw-sdwa-f16-compare", "draw-int16-vop3", "draw-int16-ternary", "draw-compressed-export", "draw-uncompressed-export", "draw-uncompressed-integer-export", "draw-title-loads", "draw-ds-swizzle", "draw-exec-regions", "draw-cmpx-regions", "draw-cmpx-e64", "draw-scc-skip", @@ -22172,6 +22341,7 @@ def main(): "draw-pack-int16": check_pack_int16_draws, "draw-pack-b32": check_pack_b32_draw, "draw-sad": check_sad_draws, + "draw-sdwa-f16-compare": check_sdwa_f16_compare_draws, "draw-int16-vop3": check_int16_vop3_draw, "draw-int16-ternary": check_int16_ternary_draw, "draw-f16-arith": check_f16_arith_draw, diff --git a/tests/rdna2_tools_check.py b/tests/rdna2_tools_check.py index f47c7560b9..6bb275c058 100644 --- a/tests/rdna2_tools_check.py +++ b/tests/rdna2_tools_check.py @@ -5373,6 +5373,27 @@ def test_dashboard_report_generation(self): self.assertIn("stages", json_content) self.assertIn("missing_opcodes_ranked", json_content) + def test_sdwa_f16_compare_instructions(self): + """The scanner accepts the binary16 SDWA comparisons pixel_aot lowers and refuses their neighbours.""" + for line in ( + "v_cmp_lt_f16_sdwa vcc_lo, v1, v2 src0_sel:DWORD src1_sel:DWORD", + "v_cmp_nlg_f16_sdwa s6, -v1, |v2| src0_sel:WORD_1 src1_sel:WORD_0", + "v_cmp_tru_f16_sdwa vcc_lo, -|v1|, v2 src0_sel:DWORD src1_sel:WORD_1", + "v_cmp_class_f16_sdwa s6, -v1, v2 src0_sel:WORD_1 src1_sel:DWORD", + ): + self.assertTrue(is_instruction_supported(line), line) + for line in ( + "v_cmp_lt_f16_sdwa vcc_lo, v1, v2 src0_sel:BYTE_0 src1_sel:DWORD", + "v_cmp_lt_f16_sdwa vcc_lo, sext(v1), v2 src0_sel:WORD_1 src1_sel:DWORD", + "v_cmp_lt_f16_sdwa s6, neg(2.0), v2 src0_sel:DWORD src1_sel:DWORD", + "v_cmp_lt_f16_sdwa vcc_lo, v1, v2 src0_sel:WORD_1", + "v_cmp_lt_f16_sdwa exec_lo, v1, v2 src0_sel:WORD_1 src1_sel:DWORD", + "v_cmp_class_f16_sdwa vcc_lo, v1, v2 src0_sel:WORD_1 src1_sel:WORD_1", + "v_cmp_class_f16_sdwa vcc_lo, v1, -v2 src0_sel:WORD_1 src1_sel:DWORD", + "v_cmpx_lt_f16_sdwa v1, v2 src0_sel:WORD_1 src1_sel:DWORD", + ): + self.assertFalse(is_instruction_supported(line), line) + def test_supported_shader_instructions(self): """Verify scanner patterns recognize subrev, mov_b32, waitcnt_expcnt as supported.""" self.assertTrue(is_instruction_supported("s_waitcnt_expcnt null, 0x0")) diff --git a/tools/pixel_aot.py b/tools/pixel_aot.py index 7505af8ac3..7646d53eaf 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -2343,8 +2343,10 @@ def vop3p_f16_word(token, scalar, vector, pending, sample_dest): raise ValueError(f"Unsupported packed operand {token}") -def f16_cmp_operand(token, scalar, vector, pending, sample_dest, *, allow_mods, as_bits): - """Return one binary16 comparison source as low-16-bit uint bits or a widened float.""" +def f16_cmp_operand(token, scalar, vector, pending, sample_dest, *, allow_mods, as_bits, high=False): + """Return one binary16 comparison source as low-16-bit uint bits or a widened float. + + With high (an SDWA WORD_1 selection) the source is the high half of the register; modifiers apply after it.""" neg = False use_abs = False if token not in INLINE_F16 and not re.fullmatch(r"-\d+", token): @@ -2359,6 +2361,8 @@ def f16_cmp_operand(token, scalar, vector, pending, sample_dest, *, allow_mods, if re.fullmatch(r"0x[0-9a-f]{1,8}", token) and int(token, 16) > 0xffff: raise ValueError(f"16-bit literal out of range: {token}") word = vop3p_f16_word(token, scalar, vector, pending, sample_dest) + if high: + word = f"({word} >> 16u)" if as_bits: expr = f"({word} & 0xffffu)" if use_abs: @@ -2374,12 +2378,12 @@ def f16_cmp_operand(token, scalar, vector, pending, sample_dest, *, allow_mods, return expr -def f16_comparison(op, tokens, scalar, vector, pending, sample_dest, *, allow_mods): +def f16_comparison(op, tokens, scalar, vector, pending, sample_dest, *, allow_mods, highs=(False, False)): """The predicate of a binary16 v_cmp or v_cmpx of `tokens`: ordered relations also require num16 of both.""" - values = [f16_cmp_operand(t, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=False) - for t in tokens] - raw = [f16_cmp_operand(t, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=True) - for t in tokens] + values = [f16_cmp_operand(t, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=False, high=h) + for t, h in zip(tokens, highs)] + raw = [f16_cmp_operand(t, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=True, high=h) + for t, h in zip(tokens, highs)] ordered = f"(num16({raw[0]}) && num16({raw[1]}))" direct = {"lt": "lt", "eq": "eq", "le": "le", "gt": "gt", "lg": "lg", "ge": "ge"} negated = {"nge": "ge", "nlg": "lg", "ngt": "gt", "nle": "le", "nlt": "lt", "neq": "eq"} @@ -2394,15 +2398,17 @@ def f16_comparison(op, tokens, scalar, vector, pending, sample_dest, *, allow_mo return COMPARE[op] -def f16_class(value, classes, scalar, vector, pending, sample_dest, *, allow_mods): - """The predicate of a binary16 v_cmp_class or v_cmpx_class: the class of the low 16 bits of `value` in the mask.""" +def f16_class(value, classes, scalar, vector, pending, sample_dest, *, allow_mods, high=False): + """The predicate of a binary16 v_cmp_class or v_cmpx_class: the class of the low 16 bits of `value` in the mask + (of the high 16 bits with high, an SDWA WORD_1 selection).""" if re.fullmatch(r"-?\d+|0x[0-9a-f]{1,8}", classes): bits = f"{int(classes, 0) & 0xffffffff}u" elif re.fullmatch(r"[vs]\d+", classes): bits = int_operand(classes, scalar, vector, pending, sample_dest, signed=False) else: raise ValueError("Unsupported class mask") - operand = f16_cmp_operand(value, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=True) + operand = f16_cmp_operand(value, scalar, vector, pending, sample_dest, allow_mods=allow_mods, as_bits=True, + high=high) return f"(class16({operand}) & {bits}) != 0u" @@ -4970,6 +4976,30 @@ def sample_coord(reg): for j, reg in enumerate(regs): scalar[reg] = ("condition", f"cmp{i}", j, len(regs)) lines.append(f" bool cmp{i} = {expr};") + elif m := match(rf"v_cmp_({'|'.join(COMPARE)}|class)_f16_sdwa {LANE_MASK}, ([^,]+), ([^,]+)" + r" src0_sel:(WORD_[01]|DWORD) src1_sel:(WORD_[01]|DWORD)"): + # Binary16 SDWA comparisons and class tests. WORD_1 selects the high half of a source; WORD_0 and DWORD + # both leave the low half, which is what the e32 form reads. -, |x| and -|x| apply after the selection. + # Sources are registers. Byte selections, sext(), constants, and a class mask with a selection other than + # DWORD are refused. + op, dst, src0, src1, sel0, sel1 = m.groups() + for token in (src0, src1): + if not re.fullmatch(r"-?(?:\|[vs]\d+\||[vs]\d+)", token): + raise ValueError(f"Unsupported binary16 SDWA source {token}") + regs = lane_mask(dst) + if mask != "entry" or pending.intersection(regs): + raise ValueError("Invalid comparison") + if op == "class": + if sel1 != "DWORD" or not re.fullmatch(r"[vs]\d+", src1): + raise ValueError("Unsupported binary16 SDWA class mask") + expr = f16_class(src0, src1, scalar, vector, pending, sample_dest, allow_mods=True, + high=sel0 == "WORD_1") + else: + expr = f16_comparison(op, [src0, src1], scalar, vector, pending, sample_dest, allow_mods=True, + highs=(sel0 == "WORD_1", sel1 == "WORD_1")) + for j, reg in enumerate(regs): + scalar[reg] = ("condition", f"cmp{i}", j, len(regs)) + lines.append(f" bool cmp{i} = {expr};") elif m := match(rf"v_cmp_({'|'.join(COMPARE)})_f16(_e32|_e64)? {LANE_MASK}, ([^,]+), ([^,]+)"): op, enc, dst, *tokens = m.groups() check_vopc_e32_dst(enc, dst) diff --git a/tools/scan_unsupported_shaders.py b/tools/scan_unsupported_shaders.py index c7dd46b11f..98762d2e5e 100644 --- a/tools/scan_unsupported_shaders.py +++ b/tools/scan_unsupported_shaders.py @@ -211,6 +211,17 @@ def lower_rejection_causes(results: list[dict]) -> dict: SUPPORTED_INSTRUCTIONS_REGEX.append( rf"v_cmp_(?:{cmp_ops})_f32_sdwa\s+{PIXEL_LANE_MASK},\s+[^,]+,\s+[^,]+\s+src0_sel:DWORD\s+src1_sel:DWORD" ) + # Binary16 SDWA comparisons and class tests: register sources with -, |x| or -|x|, word or dword selections; + # the class mask is a plain register with a DWORD selection. + _f16_sdwa_source = r"-?(?:\|[vs]\d+\||[vs]\d+)" + SUPPORTED_INSTRUCTIONS_REGEX.append( + rf"v_cmp_(?:{cmp_ops})_f16_sdwa\s+{PIXEL_LANE_MASK},\s+{_f16_sdwa_source},\s+{_f16_sdwa_source}" + r"\s+src0_sel:(?:WORD_[01]|DWORD)\s+src1_sel:(?:WORD_[01]|DWORD)" + ) + SUPPORTED_INSTRUCTIONS_REGEX.append( + rf"v_cmp_class_f16_sdwa\s+{PIXEL_LANE_MASK},\s+{_f16_sdwa_source},\s+[vs]\d+" + r"\s+src0_sel:(?:WORD_[01]|DWORD)\s+src1_sel:DWORD" + ) SUPPORTED_INSTRUCTIONS_REGEX.append( rf"v_cmp_(?:{cmp_ops})_f16{PIXEL_VOPC_E32_DST},\s+{F16_PLAIN},\s+{F16_PLAIN}" ) From e182ad78b5233763e6ced3b0e3c4d4762640caa2 Mon Sep 17 00:00:00 2001 From: rm-NoobInCoding <35840559+rm-NoobInCoding@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:42:13 +0330 Subject: [PATCH 2/3] tests: guard each binary16 SDWA selection at text level The static check asserted WORD_1 only for src0 of v_cmp_lt_f16_sdwa, so a lowering that ignored the src1 selection or the class-test selection was caught only by the GPU draws. Assert the exact operands of a src1-only WORD_1 comparison, and of class tests with WORD_1 and WORD_0 (the modifier applied after the selection, the mask read whole), so each selection is checked without a GPU. --- tests/pixel_aot_check.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index cdaa13f6d0..a918f16940 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -21031,11 +21031,28 @@ def check_sdwa_f16_compare_lowering(): sdwa = translate_int(uncompressed_export( [*setup, "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:DWORD src1_sel:DWORD", *tail])) assert plain == sdwa - # WORD_1 reads the high half, for the value and for the ordered test. - lines = translate_int(uncompressed_export( - [*setup, "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0", *tail])).splitlines() - assert any("num16(((asuint(alu21) >> 16u) & 0xffffu))" in line and "widen16((asuint(alu21) >> 16u))" in line - for line in lines), [line for line in lines if "bool cmp" in line] + # WORD_1 reads the high half, for the value and for the ordered test, and only for the source it names. v18 (A) + # is alu21 and v16 (B) is alu20. These text checks guard each selection without a GPU; the draws check the values. + def comparison(instruction): + lines = translate_int(uncompressed_export([*setup, instruction, *tail])).splitlines() + found = [line for line in lines if line.startswith(" bool cmp")] + assert len(found) == 1, found + return found[0] + + high_a, low_a = "(asuint(alu21) >> 16u)", "asuint(alu21)" + high_b, low_b = "(asuint(alu20) >> 16u)", "asuint(alu20)" + line = comparison("v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0") + assert f"num16(({high_a} & 0xffffu))" in line and f"widen16({high_a})" in line, line + assert f"num16(({low_b} & 0xffffu))" in line and f"widen16({low_b})" in line and high_b not in line, line + line = comparison("v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_0 src1_sel:WORD_1") + assert f"num16(({low_a} & 0xffffu))" in line and f"widen16({low_a})" in line and high_a not in line, line + assert f"num16(({high_b} & 0xffffu))" in line and f"widen16({high_b})" in line, line + # The class test reads the selected half of its value, with the modifier applied after the selection; the mask + # register is read whole. + line = comparison("v_cmp_class_f16_sdwa vcc_lo, -v18, v16 src0_sel:WORD_1 src1_sel:DWORD") + assert line == f" bool cmp22 = (class16((({high_a} & 0xffffu) ^ 0x8000u)) & {low_b}) != 0u;", line + line = comparison("v_cmp_class_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_0 src1_sel:DWORD") + assert line == f" bool cmp22 = (class16(({low_a} & 0xffffu)) & {low_b}) != 0u;", line good = "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:WORD_1 src1_sel:WORD_0" for bad in ( "v_cmp_lt_f16_sdwa vcc_lo, v18, v16 src0_sel:BYTE_0 src1_sel:WORD_0", From 377919df1c8212935299b297f572289892686f97 Mon Sep 17 00:00:00 2001 From: rm-NoobInCoding <35840559+rm-NoobInCoding@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:44:56 +0330 Subject: [PATCH 3/3] tests: put the binary16 SDWA comparisons through the LLVM round trip check_binary assembled no v_cmp_*_f16_sdwa program, so the LLVM 15 job never saw the new forms. Add the sdwa_f16_compares program to its fixtures: it is assembled, disassembled with LLVM 15 and must translate exactly like its source text. --- tests/pixel_aot_check.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index a918f16940..d69f65a60c 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -16886,6 +16886,8 @@ def check_binary(directory, assembler, objcopy, library): fixtures.append((uncompressed_export(pack_b32_group()), translate_int)) fixtures.append((uncompressed_export(pack_b32_inline_group()), translate_int)) fixtures.extend((uncompressed_export(sad_group(group)), translate_int) for group in SAD_BODIES) + # The binary16 SDWA comparisons and class tests, assembled, disassembled and translated like their source text. + fixtures.append((uncompressed_export(sdwa_f16_compares()), translate_int)) fixtures.extend((compressed_export(mask), translate_masked) for mask in (0x3, 0xC, 0xF)) fixtures.extend((uncompressed_export([], mask), translate_masked) for mask in (0x1, 0x8, 0x5, 0xA)) for restore in (MASKED_OR_RESTORE, MASKED_MOV_RESTORE):