diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bd11ced1e..54f38bf43e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6437,6 +6437,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_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 120 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_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 8772c14b56..d71414d140 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -5759,6 +5759,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 integer SDWA comparisons `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` (gfx10 has no f/t forms at 16 bits) lower with byte and word selections. Each source is its selected field, zero-extended or sign-extended under `sext()`, as for the SDWA integer operations; the comparison is then the e32 one on those words: signed or unsigned 32-bit for i32 and u32, and the low 16 bits of the selected word, sign- or zero-extended, for i16 and u16. 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, which a control asserts. Sources are registers; inline constants, literals, float modifiers, unbalanced `sext(`, missing selections, `exec` as the destination, the f and t forms at 16 bits and the `v_cmpx` and 64-bit SDWA forms are refused. Eight programs, two per kind, each compare four channels of A (`s44` copied to `v20`) with B (`s45` copied to `v18`) under five selection configurations that cycle across the channels: `BYTE_1` with `sext` against `BYTE_2`, `WORD_1` against `WORD_0` with `sext`, `BYTE_3` with `sext` against `WORD_1`, `WORD_0` with `sext` against `BYTE_0`, and `DWORD` against `BYTE_3` with `sext`. Each result selects 1.0 or 0 through `v_cndmask_b32`. Every program runs on 13 pairs of normal binary32 words or +0.0, chosen so that every relation except f and t takes both answers. For each program, the first pair on which a wrong model gives other channels is drawn against that 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 negative controls and 132 draws from 8 compiles. They run as `pixel_aot_sdwa_int_compare_hardware` and `pixel_aot_sdwa_int_compare_warp` (`tests/pixel_aot_check.py draw-sdwa-int-compare`), with their own coverage record listing all 28 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 `sext()` in the lowering makes the first program mismatch on WARP. 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..0efb96a57f 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -4251,6 +4251,198 @@ def check_sdwa_predicate_draws(directory, compiler, renderer, backend): raise +# Integer SDWA comparisons (v_cmp_*_{i32,u32,i16,u16}_sdwa). Each channel compares a selected field of A (s44, copied to +# v20) with a selected field of B (s45, copied to v18) and selects 1.0 (s47) or 0 into its channel. The five selection +# configurations are (src0_sel, src0 sext, src1_sel, src1 sext); channels cycle through them so every program mixes byte +# and word fields, sign- and zero-extension, and a whole-dword source. +SDWA_INT_CONFIGS = ( + ("BYTE_1", True, "BYTE_2", False), + ("WORD_1", False, "WORD_0", True), + ("BYTE_3", True, "WORD_1", False), + ("WORD_0", True, "BYTE_0", False), + ("DWORD", False, "BYTE_3", True), +) +SDWA_INT_SEL = {"BYTE_0": (0, 8), "BYTE_1": (8, 8), "BYTE_2": (16, 8), "BYTE_3": (24, 8), + "WORD_0": (0, 16), "WORD_1": (16, 16), "DWORD": (0, 32)} +INT16_PREDICATES = ("lt", "eq", "le", "gt", "ne", "ge") +# Programs by kind: four relations each. gfx10 has no f/t forms at 16 bits, so the second 16-bit program repeats lt +# and gt under other selection configurations. +SDWA_INT_PROGRAMS = { + "i32": (INT_PREDICATES[:4], INT_PREDICATES[4:]), + "u32": (INT_PREDICATES[:4], INT_PREDICATES[4:]), + "i16": (INT16_PREDICATES[:4], (*INT16_PREDICATES[4:], "lt", "gt")), + "u16": (INT16_PREDICATES[:4], (*INT16_PREDICATES[4:], "lt", "gt")), +} +# (A, B) as normal binary32 words or +0.0, so no float move can flush or quiet them. Their byte and halfword fields +# take both signs and equal values under several selections; the last three pairs put the high half of A below and at +# the low half of B and make every field equal, and the last one puts it between B's low half zero-extended and +# sign-extended. In the final pair A's high half equals B's low half as 16 bits but not once that half is +# sign-extended to 32 bits. +SDWA_INT_PAIRS = ( + (0x3f80ff7f, 0xbf8001ff), + (0xc07f8000, 0x40ff7f80), + (0x3fff8001, 0x3fff8001), + (0xbf00807f, 0x3f7f0080), + (0x40017fff, 0xc0018000), + (0x3e80fffe, 0x3e81ffff), + (0xc0ff0040, 0x40400040), + (0x3f7f8080, 0xbf80807f), + (0x3f800000, 0x40007fff), + (0x3f800000, 0x40003f80), + (0x00000000, 0x00000000), + (0xc07f8000, 0x40ff9000), + (0xc07f8000, 0x40ffc07f), +) + + +def sdwa_int_configs(kind, program): + count = len(SDWA_INT_PROGRAMS[kind][program]) + start = 4 * program + return tuple(SDWA_INT_CONFIGS[(start + channel) % len(SDWA_INT_CONFIGS)] for channel in range(count)) + + +def sdwa_int_predicate_group(kind, program): + body = ["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v19, s47", "v_mov_b32_e32 v20, s44"] + ops = SDWA_INT_PROGRAMS[kind][program] + for channel, (op, (sel0, sext0, sel1, sext1)) in enumerate(zip(ops, sdwa_int_configs(kind, program))): + src0 = "sext(v20)" if sext0 else "v20" + src1 = "sext(v18)" if sext1 else "v18" + body.extend([f"v_cmp_{op}_{kind}_sdwa vcc_lo, {src0}, {src1} src0_sel:{sel0} src1_sel:{sel1}", + f"v_cndmask_b32_e32 v{12 + channel}, 0, v19, vcc_lo"]) + return with_constants(body) + + +def sdwa_int_field(word, sel, sext): + offset, width = SDWA_INT_SEL[sel] + value = (word >> offset) & ((1 << width) - 1) + if sext and width < 32 and value >> (width - 1): + value -= 1 << width + return value & 0xffffffff + + +def sdwa_int_compare_value(word, kind): + if kind in ("i16", "u16"): + word &= 0xffff + return word - 0x10000 if kind == "i16" and word & 0x8000 else word + return word - (1 << 32) if kind == "i32" and word & 0x80000000 else word + + +def sdwa_int_holds(op, left, right): + return {"f": False, "lt": left < right, "eq": left == right, "le": left <= right, "gt": left > right, + "ne": left != right, "ge": left >= right, "t": True}[op] + + +def sdwa_int_expected(kind, program, a, b, *, selections=True, sext=True, compare_kind=None): + """The four channels as 0.0 or 1.0. The keyword arguments describe wrong models for the negative controls.""" + compare_kind = compare_kind or kind + result = [] + for op, (sel0, sext0, sel1, sext1) in zip(SDWA_INT_PROGRAMS[kind][program], sdwa_int_configs(kind, program)): + if not selections: + sel0 = sel1 = "DWORD" + left = sdwa_int_compare_value(sdwa_int_field(a, sel0, sext and sext0), compare_kind) + right = sdwa_int_compare_value(sdwa_int_field(b, sel1, sext and sext1), compare_kind) + result.append(1.0 if sdwa_int_holds(op, left, right) else 0.0) + return result + + +SDWA_INT_SWAPPED_KIND = {"i32": "u32", "u32": "i32", "i16": "u16", "u16": "i16"} +SDWA_INT_WIDE_KIND = {"i16": "i32", "u16": "u32"} + + +def sdwa_int_wrong_models(kind): + """(label, keyword arguments for sdwa_int_expected) for the models a wrong lowering would follow.""" + models = [("selections ignored", {"selections": False}), ("sext() ignored", {"sext": False}), + ("signedness swapped", {"compare_kind": SDWA_INT_SWAPPED_KIND[kind]})] + if kind in SDWA_INT_WIDE_KIND: + models.append(("whole selected word compared", {"compare_kind": SDWA_INT_WIDE_KIND[kind]})) + return models + + +def check_sdwa_int_predicate_draws(directory, compiler, renderer, backend): + """Draw the 28 integer SDWA comparisons. Every program runs on each pair of SDWA_INT_PAIRS, and for each wrong + model the first pair on which that model gives other channels is drawn against the wrong reference, 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_int_predicates.hlsl" + shader = directory / "sdwa_int_predicates.cso" + for kind, programs in SDWA_INT_PROGRAMS.items(): + for program in range(len(programs)): + source.write_text(translate(sdwa_int_predicate_group(kind, program)), encoding="ascii") + compile_shader(compiler, source, shader) + cases = [] + + def add_case(a, b, pixel, status, label): + constants = bytearray(256) + struct.pack_into("> 24u) << 16) >> 16) < " + f"(asint((({b} >> 16u) & 0xffu) << 16) >> 16);") + assert expected in lines, [line for line in lines if "cmp23" in line] + lines = translate(sdwa_int_predicate_group("u32", 0)).splitlines() + # BYTE_3 of A sign-extended, WORD_1 of B zero-extended, compared as unsigned words; f is constant false. + assert " bool cmp23 = false;" in lines + expected = f" bool cmp27 = asuint(asint({a} << 0u) >> 24u) == (({b} >> 16u) & 0xffffu);" + assert expected in lines, [line for line in lines if "cmp27" in line] + # The e32 comparison on whole dwords is the SDWA one with DWORD selections. + plain = translate(with_constants(["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v20, s44", + "v_cmp_lt_i32_e32 vcc_lo, v20, v18", "v_cndmask_b32_e32 v12, 0, v18, vcc_lo"])) + sdwa = translate(with_constants(["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v20, s44", + "v_cmp_lt_i32_sdwa vcc_lo, v20, v18 src0_sel:DWORD src1_sel:DWORD", + "v_cndmask_b32_e32 v12, 0, v18, vcc_lo"])) + assert plain == sdwa + setup = ["v_mov_b32_e32 v18, s45", "v_mov_b32_e32 v20, s44"] + for bad in ( + "v_cmp_f_i16_sdwa vcc_lo, v20, v18 src0_sel:WORD_0 src1_sel:WORD_0", + "v_cmp_t_u16_sdwa vcc_lo, v20, v18 src0_sel:WORD_0 src1_sel:WORD_0", + "v_cmp_lt_i32_sdwa vcc_lo, sext(v20, v18 src0_sel:BYTE_0 src1_sel:BYTE_0", + "v_cmp_lt_i32_sdwa vcc_lo, v20, 1 src0_sel:BYTE_0 src1_sel:DWORD", + "v_cmp_lt_i32_sdwa vcc_lo, -v20, v18 src0_sel:BYTE_0 src1_sel:DWORD", + "v_cmp_lt_u32_sdwa vcc_lo, v20, v18 src0_sel:BYTE_0", + "v_cmp_lt_u32_sdwa vcc_lo, v20, v18 src0_sel:BYTE_4 src1_sel:BYTE_0", + "v_cmp_lt_u32_sdwa vcc_lo, v20, v106 src0_sel:BYTE_0 src1_sel:BYTE_0", + "v_cmp_lt_u32_sdwa exec_lo, v20, v18 src0_sel:BYTE_0 src1_sel:BYTE_0", + "v_cmpx_lt_u32_sdwa v20, v18 src0_sel:BYTE_0 src1_sel:BYTE_0", + "v_cmp_lt_i64_sdwa vcc_lo, v20, v18 src0_sel:BYTE_0 src1_sel:BYTE_0", + ): + translate(with_constants([*setup, "v_cmp_lt_u32_sdwa vcc_lo, v20, v18 src0_sel:BYTE_0 src1_sel:BYTE_0"])) + must_reject(translate, with_constants([*setup, bad])) + + def half_rtz(x): bits, = struct.unpack("> 16)") + elif kind == "u16": + values.append(f"({word} & 0xffffu)") + else: + values.append(word) + for j, reg in enumerate(regs): + scalar[reg] = ("condition", f"cmp{i}", j, len(regs)) + lines.append(f" bool cmp{i} = {INT_COMPARE[op].format(*values)};") elif m := match(rf"v_cmp_({'|'.join(COMPARE)})_f32_sdwa {LANE_MASK}, ([^,]+), ([^,]+) src0_sel:DWORD src1_sel:DWORD"): # With whole-dword selections the SDWA comparison is the e32/e64 one; byte and word selections # would compare zero-extended fields as binary32 and are refused. diff --git a/tools/scan_unsupported_shaders.py b/tools/scan_unsupported_shaders.py index c7dd46b11f..30f8b2fef2 100644 --- a/tools/scan_unsupported_shaders.py +++ b/tools/scan_unsupported_shaders.py @@ -222,6 +222,14 @@ def lower_rejection_causes(results: list[dict]) -> dict: SUPPORTED_INSTRUCTIONS_REGEX.append( rf"v_cmp_(?:{icmp_ops})_(?:i32|u32|i16|u16){PIXEL_VOPC_DST},\s+{INTEGER_SOURCE},\s+{INTEGER_SOURCE}" ) + # Integer SDWA comparisons: register sources, optionally under sext(), with both selections printed. gfx10 has + # no f/t forms at 16 bits. + _sdwa_cmp_source = r"(?:sext\([vs]\d+\)|[vs]\d+)" + for _ops, _kinds in ((icmp_ops, "i32|u32"), ("lt|eq|le|gt|ne|ge", "i16|u16")): + SUPPORTED_INSTRUCTIONS_REGEX.append( + rf"v_cmp_(?:{_ops})_(?:{_kinds})_sdwa\s+{PIXEL_LANE_MASK},\s+{_sdwa_cmp_source},\s+{_sdwa_cmp_source}" + rf"\s+src0_sel:{SDWA_SEL_PATTERN}\s+src1_sel:{SDWA_SEL_PATTERN}" + ) # 64-bit comparisons are checked by supports_int64_compare() below: the pair and inline ranges are numeric. INT64_COMPARE_PATTERN = re.compile( rf"v_cmp_(?:{icmp_ops})_(?:i64|u64)(_e32|_e64)?\s+(vcc(?:_lo)?|s\d+|s\[\d+:\d+\]),\s+(\S+),\s+(\S+)"