From bc03f191030951c765c8d1af26f415b6041fdb7a Mon Sep 17 00:00:00 2001 From: legacy-code-archaeologist <359844974+legacy-code-archaeologist@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:36:04 +0100 Subject: [PATCH] shader: lower the SDWA v_movrel* forms in pixel_aot v_movreld_b32_sdwa, v_movrels_b32_sdwa, v_movrelsd_b32_sdwa and v_movrelsd_2_b32_sdwa (LLVM 15 spells them identically for gfx1013 and gfx1030) now go through the movrel block: the m0 rules, refusals and source-before-destination reads are unchanged. The source selection, with an optional sext(...), applies to the indexed source word and the destination selection to each indexed destination, as v_mov_b32_sdwa applies them; UNUSED_PRESERVE keeps the bits from that destination's own tracked old value, so under a dynamic m0 each reachable destination merges its own word (order read from the RDNA 2 naming, not measured on RDNA hardware). UNUSED_SEXT, UNUSED_PRESERVE into an untracked destination, clamp, 16-bit values, SGPR sources and the movrel refusals are refused. SDWA_MOVREL_BODY runs each move with a dynamic and a static m0 over integer banks with BYTE, WORD and DWORD selections, sext, PAD and PRESERVE, exported through the bit-exact R32G32B32A32_UINT target against an independent SDWA model; five controls must mismatch. The translation controls check nine refusals, the entry mask and a dynamic PRESERVE move inside one bank. Windows 11 26100: pixel_aot_sdwa_movrel_warp (WARP 10.0.26100.8972) and _hardware (Quadro M1000M, 32.0.15.7371) match bit for bit on both sets; every control mismatched. pixel_aot_check.py binary with LLVM 15 passes. Full dev suite without _hardware: 854/854. Refs #1917 Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 5 + docs/experiments.md | 2 + tests/pixel_aot_check.py | 194 ++++++++++++++++++++++++++++++++++++++- tools/pixel_aot.py | 31 ++++++- 4 files changed, 227 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 289f1842b7..f4768df754 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6727,6 +6727,11 @@ if(EXISTS "${INTAKE_PYTHON}") "$" ${adapter}) set_tests_properties(pixel_aot_swaprel_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_swaprel_${adapter}.txt") + add_test(NAME pixel_aot_sdwa_movrel_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-sdwa-movrel "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(pixel_aot_sdwa_movrel_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + ENVIRONMENT "FORM_COVERAGE_RECORD=${CMAKE_BINARY_DIR}/form_coverage/pixel_aot_sdwa_movrel_${adapter}.txt") add_test(NAME pixel_aot_smovrel_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/pixel_aot_check.py" draw-smovrel "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index be66c1b5fa..ddae5b7e66 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6501,6 +6501,8 @@ The plain VOP1 (`_e32`) and VOP3 (`_e64`) encodings of the same four moves (`v_m `v_swaprel_b32` (VOP1 only, `7e18d118` decodes as `v_swaprel_b32 v12, v24` for gfx1013 and gfx1030) exchanges VGPR[vdst + `m0[25:16]`] and VGPR[vsrc + `m0[9:0]`], the same split as `v_movrelsd_2_b32` (#1795). That reading follows the RDNA 2 naming and LLVM's definition; it has not been measured on RDNA hardware. The lowering uses the movrel `m0` rules (static, or dynamic with a proven bit mask; the same refusals). It reads both reachable sides before writing, and then gives each reachable register the source value when the destination offset selects it, the old destination value when the source offset selects it, and its old value otherwise, so a swap of a register with itself changes nothing. `SWAPREL_BODY` runs a dynamic swap between two banks (`m0` = word 3 & `0x30003`, which differs between the two `DPP_MOVREL_SETS`), then, in a second pair of banks, an overlapping swap inside one bank (`m0` = `0x30001`), a swap of a register with itself (`0x20002`) and a static swap between the banks (`0x20001`). The four bank sums (816, 117, 338, 125 and 218, 128, 338, 125) come from an independent list-swap reference. On WARP (driver 10.0.26100.8972) and an NVIDIA Quadro M1000M (driver 32.0.15.7371) on Windows 11 build 26100, with the debug layer unavailable, all 4096 pixels matched with zero error for both sets. Three controls must mismatch, and did on both adapters: a one-way `v_movrelsd_2_b32_e32` in place of the dynamic swap, swapped static `m0` halves, and the dynamic `m0` replaced by 0. The translation controls check five refusals. They also pin the generated selects without a GPU: for a static swap of v25 and v26, and for a dynamic swap with `m0` masked to `0x30001`, the swap lines must match an expected text with the temporaries renamed in order of appearance. So swapped `m0` halves, a source side that reads the wrong old value, source offsets taken from the wrong half, or a missing destination select each fail `python -B tests/pixel_aot_check.py`, not only the draw. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_swaprel_" -V`. +The SDWA encodings of the four moves (`v_movreld_b32_sdwa`, `v_movrels_b32_sdwa`, `v_movrelsd_b32_sdwa`, `v_movrelsd_2_b32_sdwa`; LLVM 15 decodes `7e1886f9 00001518` as `v_movrels_b32_sdwa v12, v24 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_0` for gfx1013 and gfx1030 alike) lower through the same movrel block (#1917). The source selection, with an optional `sext(...)`, applies to the indexed source word, and the destination selection to each indexed destination, as `v_mov_b32_sdwa` applies them (`sdwa_source`, `sdwa_dest`); that order follows the RDNA 2 naming and the other movrel forms and has not been measured on RDNA hardware. `UNUSED_PAD` zeroes the unselected bits and `UNUSED_PRESERVE` keeps them from that destination's own tracked old value, so under a dynamic `m0` every reachable destination merges its own word. Under a dynamic `m0` every reachable destination must hold a tracked numeric word, also for `UNUSED_PAD`, as for the other movrel forms. `UNUSED_SEXT`, `UNUSED_PRESERVE` into an untracked destination, `clamp`, 16-bit (low-half) values as source or kept destination, SGPR sources and the movrel refusals are refused. `SDWA_MOVREL_BODY` runs each move with a dynamic and a static `m0` over integer banks, mixing BYTE, WORD and DWORD selections, `sext`, `UNUSED_PAD` and `UNUSED_PRESERVE`, and exports four words through the bit-exact R32G32B32A32_UINT target. They must equal an independent SDWA model on both `DPP_MOVREL_SETS`; the expected words are (`0x008856aa 0x3600ffbb 0xffffffdd 0x332143bb` and `0x00cc56aa 0x3600ffb9 0x0048ffdd 0x887743bb`), which they did on WARP (driver 10.0.26100.8972) and an NVIDIA Quadro M1000M (driver 32.0.15.7371) on Windows 11 build 26100. Five controls must mismatch, and did on both adapters: a dropped source selection, `UNUSED_PRESERVE` turned into `UNUSED_PAD`, an ignored dynamic `m0`, the wrong source byte and a missing `sext`. The translation controls check nine refusals, the entry mask and a dynamic `UNUSED_PRESERVE` move inside one bank, where each reachable register merges its own old word, and the body is in the LLVM 15 round trip. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_sdwa_movrel_" -V`. + The scalar relative moves `s_movrels_b32`, `s_movrels_b64`, `s_movreld_b32`, `s_movreld_b64` and `s_movrelsd_2_b32` (SOP1; LLVM 15 decodes `be942e28` as `s_movrels_b32 s20, s40` and the others alike for gfx1013 and gfx1030) lower with the same `m0` split as the vector forms (#1882): `s_movrels` reads SGPR[src + m0], `s_movreld` writes SGPR[dst + m0], and `s_movrelsd_2_b32` reads with `m0[9:0]` and writes with `m0[25:16]`; `_b64` moves a pair, with `m0` counted in dwords. That reading follows the RDNA 2 naming and the vector forms; it has not been measured on RDNA hardware. With a static `m0` the move is an `s_mov` from or to the indexed SGPR, so its tracked value (constant, expression, descriptor or pointer word) and bit mask carry over: moving the texture descriptor pointer with `s_movrels_b64 s[72:73], s[16:17]` and `m0` = 4 translates exactly like two `s_mov_b32`. A dynamic `m0` with a proven bit mask becomes a bounded select, and only over plain numeric SGPRs, so no resource depends on a runtime index; for `s_movreld` each reachable destination keeps its old value unless selected. An unbounded or non-numeric `m0`, an index past s105, a write into an outstanding scalar load, a symbolic source or destination under a dynamic `m0` and a `_b64` pair base that could be odd (a static odd offset, or a dynamic mask that allows bit 0) are refused. Every source is read before any destination is written: self-moves, overlapping pairs and a dynamic `s_movreld` whose source lies in its own destination bank are pinned by the exported constants or the generated selects. `SMOVREL_BODY` moves cbuffer-indexed words with each form, static and dynamic, and exports weighted sums (60, 67, 805, 193 and 80, 63, 244, 165 on the two `DPP_MOVREL_SETS`) from an independent reference. On WARP (driver 10.0.26100.8972) and an NVIDIA Quadro M1000M (driver 32.0.15.7371) on Windows 11 build 26100, with the debug layer unavailable, all 4096 pixels matched with zero error for both sets. Four controls must mismatch, and did on both adapters: the dynamic `m0` replaced by 0 for `s_movrels_b32`, `s_movreld_b32` and `s_movreld_b64`, and swapped static halves for `s_movrelsd_2_b32`. The translation controls check the descriptor move, the overlaps and ten refusals. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_smovrel_" -V`. ```powershell diff --git a/tests/pixel_aot_check.py b/tests/pixel_aot_check.py index b5283b93af..50c794f4f3 100644 --- a/tests/pixel_aot_check.py +++ b/tests/pixel_aot_check.py @@ -16476,6 +16476,64 @@ def _check_controls_inline(directory): else: raise AssertionError("Scalar movrel read an outstanding scalar load") + # SDWA v_movrel* (#1917): the selections apply to the indexed words; the refusals of v_mov_b32_sdwa hold. + sdwa_hlsl = translate_int(uncompressed_export(SDWA_MOVREL_BODY)) + assert "dpp_soff" in sdwa_hlsl and "dpp_doff" in sdwa_hlsl and "QuadRead" not in sdwa_hlsl + sdwa_bank = ["v_mov_b32_e32 v24, 0x44332211", "v_mov_b32_e32 v25, 0x88776655"] + for bad_seq, message in ( + (sdwa_bank + ["s_mov_b32 m0, 1", + "v_movrels_b32_sdwa v40, v24 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_0"], + "UNUSED_PRESERVE needs a tracked numeric destination"), + (sdwa_bank + ["s_mov_b32 m0, 1", + "v_movrels_b32_sdwa v12, v24 dst_sel:WORD_1 dst_unused:UNUSED_SEXT src0_sel:BYTE_0"], + "UNUSED_SEXT is not lowered"), + (sdwa_bank + ["v_movrels_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"], + "Unsupported m0 value for v_movrels_b32"), + (sdwa_bank + ["s_mov_b32 m0, s44", + "v_movreld_b32_sdwa v24, v25 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"], + "Dynamic m0 offset is unbounded for v_movreld_b32"), + (sdwa_bank + ["s_mov_b32 m0, 5", + "v_movrels_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"], + "Expected a numeric VGPR at v29"), + (sdwa_bank + ["s_mov_b32 m0, 1", + "v_movrels_b32_sdwa v12, s24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"], + "Unsupported instruction"), + # clamp is not part of the accepted spelling. + (sdwa_bank + ["s_mov_b32 m0, 1", + "v_movrels_b32_sdwa v12, v24 clamp dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"], + "Unsupported instruction"), + # A 16-bit integer result defines only its low half: neither an indexed source nor a PRESERVE destination. + (sdwa_bank + ["v_add_nc_u16 v26, v24, v25", "s_mov_b32 m0, 2", + "v_movrels_b32_sdwa v12, v24 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0"], + "Expected a numeric VGPR at v26"), + (sdwa_bank + ["v_add_nc_u16 v26, v24, v25", "s_mov_b32 m0, 2", + "v_movreld_b32_sdwa v24, v25 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:DWORD"], + "UNUSED_PRESERVE needs a tracked numeric destination"), + ): + try: + translate_int(uncompressed_export(bad_seq)) + except ValueError as error: + assert message in str(error), (bad_seq, error) + else: + raise AssertionError(f"SDWA movrel accepted {bad_seq}") + # A dynamic PRESERVE move inside one bank: every reachable destination merges its own old word, read before + # the write, even where the source bank overlaps it. + overlap = translate_int(uncompressed_export(SDWA_MOVREL_BODY[:4] + [ + "s_mov_b32 s84, s47", "s_mov_b32 s85, 0", "s_and_b64 s[84:85], s[84:85], 0x10001", "s_mov_b32 m0, s84", + "v_movrelsd_2_b32_sdwa v24, v24 dst_sel:BYTE_1 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_0"])) + merges = re.findall(r"precise float alu\d+_(\d) = \(dpp_doff\d+ == \1u\) \? asfloat\(\(\(asuint\((alu\d+)\) & " + r"0xffff00ffu\) \| \(\(dpp_val\d+ & 0xffu\) << 8u\)\)\) : (alu\d+);", overlap) + assert len(merges) == 2 and all(old == kept for _, old, kept in merges), merges + quad = assembly(False) + at = quad.index("s_wqm_b64 exec, exec") + 1 + try: + translate(quad[:at] + ["s_mov_b32 m0, 0", + "v_movrels_b32_sdwa v20, v8 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:DWORD"] + quad[at:]) + except ValueError as error: + assert "Invalid v_movrels_b32_sdwa outside entry mask" in str(error), error + else: + raise AssertionError("SDWA movrel accepted outside the entry mask") + # v_swaprel_b32 (#1795): bounded selects on both sides, and the movrel refusals. assert "swap_doff" in translate(with_constants(SWAPREL_BODY)) check_swaprel_exact() @@ -17803,6 +17861,7 @@ def check_binary(directory, assembler, objcopy, library): fixtures.append((with_constants(PLAIN_MOVREL_BODY), translate)) fixtures.append((with_constants(swap_movrel_encodings(PLAIN_MOVREL_BODY)), translate)) fixtures.append((with_constants(SWAPREL_BODY), translate)) + fixtures.append((uncompressed_export(SDWA_MOVREL_BODY), translate_int)) fixtures.append((with_constants(SMOVREL_BODY), translate)) fixtures.append((uncompressed_export(DPP_F16_CVT_LANES), translate_f16)) fixtures.append((uncompressed_export(DPP_F16_CVT_WORDS), translate_f16)) @@ -21249,6 +21308,138 @@ def check_swaprel_exact(): assert dynamic == SWAPREL_DYNAMIC_LINES, dynamic +# SDWA v_movrel* (#1917), exported through the bit-exact integer target. Each channel runs one move with a dynamic +# and then a static m0 over integer banks, mixing BYTE, WORD and DWORD selections, sext, PAD and PRESERVE. +SDWA_MOVREL_BANK = (0x44332211, 0x88776655, 0xCCBBAA99, 0x00FFEEDD) +SDWA_MOVREL_BODY = [ + *(f"v_mov_b32_e32 v{24 + k}, {word:#x}" for k, word in enumerate(SDWA_MOVREL_BANK)), + # R (v12): v_movrels_b32_sdwa. + "v_mov_b32_e32 v12, 0x12345678", + "s_bfe_u32 s56, s44, 0x20000", + "s_mov_b32 m0, s56", + "v_movrels_b32_sdwa v12, v24 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_3", + "s_mov_b32 m0, 2", + "v_movrels_b32_sdwa v12, sext(v24) dst_sel:BYTE_0 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_1", + # G (v13): v_movreld_b32_sdwa into v28..v31. + "v_mov_b32_e32 v28, 0x10000001", + "v_mov_b32_e32 v29, 0x20000002", + "v_mov_b32_e32 v30, 0x30000003", + "v_mov_b32_e32 v31, 0x40000004", + "v_mov_b32_e32 v40, 0xa5b6c7d8", + "s_bfe_u32 s57, s45, 0x20000", + "s_mov_b32 m0, s57", + "v_movreld_b32_sdwa v28, sext(v40) dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_2", + "s_mov_b32 m0, 1", + "v_movreld_b32_sdwa v28, v40 dst_sel:BYTE_3 dst_unused:UNUSED_PAD src0_sel:WORD_1", + "v_add_nc_u32_e32 v13, v28, v29", + "v_xor_b32_e32 v13, v13, v30", + "v_add_nc_u32_e32 v13, v13, v31", + # B (v14): v_movrelsd_b32_sdwa into v36..v39. + "v_mov_b32_e32 v36, 0x0f0f0f0f", + "v_mov_b32_e32 v37, 0x1e1e1e1e", + "v_mov_b32_e32 v38, 0x2d2d2d2d", + "v_mov_b32_e32 v39, 0x3c3c3c3c", + "s_bfe_u32 s58, s46, 0x20000", + "s_mov_b32 m0, s58", + "v_movrelsd_b32_sdwa v36, v24 dst_sel:BYTE_2 dst_unused:UNUSED_PRESERVE src0_sel:WORD_0", + "s_mov_b32 m0, 3", + "v_movrelsd_b32_sdwa v36, sext(v24) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0", + "v_add_nc_u32_e32 v14, v36, v37", + "v_xor_b32_e32 v14, v14, v38", + "v_add_nc_u32_e32 v14, v14, v39", + # A (v15): v_movrelsd_2_b32_sdwa into v44..v47 (source m0[9:0], destination m0[25:16]). + "v_mov_b32_e32 v44, 0xaaaa5555", + "v_mov_b32_e32 v45, 0xbbbb6666", + "v_mov_b32_e32 v46, 0xcccc7777", + "v_mov_b32_e32 v47, 0xdddd8888", + "s_mov_b32 s84, s47", + "s_mov_b32 s85, 0", + "s_and_b64 s[84:85], s[84:85], 0x30003", + "s_mov_b32 m0, s84", + "v_movrelsd_2_b32_sdwa v44, v24 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:WORD_0", + "s_mov_b32 m0, 0x20001", + "v_movrelsd_2_b32_sdwa v44, v24 dst_sel:BYTE_0 dst_unused:UNUSED_PAD src0_sel:BYTE_3", + "v_add_nc_u32_e32 v15, v44, v45", + "v_xor_b32_e32 v15, v15, v46", + "v_add_nc_u32_e32 v15, v15, v47", +] + + +def sdwa_movrel_reference(w0, w1, w2, w3): + """Independent model of SDWA_MOVREL_BODY: SDWA source and destination selections on 32-bit words.""" + 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)} + + def source(word, name, sext): + offset, width = sel[name] + value = (word >> offset) & ((1 << width) - 1) + if sext and value >> (width - 1): + value -= 1 << width + return value & 0xffffffff + + def dest(value, name, preserve, old): + offset, width = sel[name] + field = ((value & ((1 << width) - 1)) << offset) & 0xffffffff + keep = ~(((1 << width) - 1) << offset) & 0xffffffff + return (old & keep) | field if preserve else field + + def mix(bank): + return ((((bank[0] + bank[1]) & 0xffffffff) ^ bank[2]) + bank[3]) & 0xffffffff + + src = list(SDWA_MOVREL_BANK) + r = dest(source(src[w0 & 3], "BYTE_3", False), "WORD_1", True, 0x12345678) + r = dest(source(src[2], "BYTE_1", True), "BYTE_0", True, r) + g = [0x10000001, 0x20000002, 0x30000003, 0x40000004] + g[w1 & 3] = dest(source(0xA5B6C7D8, "BYTE_2", True), "WORD_0", True, g[w1 & 3]) + g[1] = dest(source(0xA5B6C7D8, "WORD_1", False), "BYTE_3", False, g[1]) + b = [0x0F0F0F0F, 0x1E1E1E1E, 0x2D2D2D2D, 0x3C3C3C3C] + b[w2 & 3] = dest(source(src[w2 & 3], "WORD_0", False), "BYTE_2", True, b[w2 & 3]) + b[3] = dest(source(src[3], "BYTE_0", True), "DWORD", False, b[3]) + a = [0xAAAA5555, 0xBBBB6666, 0xCCCC7777, 0xDDDD8888] + a[(w3 >> 16) & 3] = dest(source(src[w3 & 3], "WORD_0", False), "WORD_1", True, a[(w3 >> 16) & 3]) + a[2] = dest(source(src[1], "BYTE_3", False), "BYTE_0", False, a[2]) + return (r, mix(g), mix(b), mix(a)) + + +def check_sdwa_movrel_draw(directory, compiler, renderer, backend): + program = uncompressed_export(SDWA_MOVREL_BODY) + source = directory / "sdwa_movrel.hlsl" + shader = directory / "sdwa_movrel.cso" + references = [sdwa_movrel_reference(*words) for words in DPP_MOVREL_SETS] + if references[0] == references[1]: + raise AssertionError("The two constant sets do not tell the dynamic m0 offsets apart") + + def case(name, words, expected): + constants = bytearray(256) + struct.pack_into("<4I", constants, 12, *words) + return write_case(directory / name, constants, tuple(as_float(w) for w in expected), integer_rt=True) + + source.write_text(translate_int(program), encoding="ascii") + compile_shader(compiler, source, shader) + run_pixel_batch(renderer, backend, shader, + [(case(f"sdwa-movrel-{n}.case", words, expected), "pass") + for n, (words, expected) in enumerate(zip(DPP_MOVREL_SETS, references))]) + print("sdwa movrel: " + ", ".join(f"({', '.join(f'{w:#010x}' for w in ref)})" for ref in references)) + good = case("sdwa-movrel-control.case", DPP_MOVREL_SETS[0], references[0]) + for old, new, label in ( + ("dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:BYTE_3", "dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:DWORD", + "v_movrels_b32_sdwa with the source selection dropped"), + ("dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE", "dst_sel:WORD_0 dst_unused:UNUSED_PAD", + "v_movreld_b32_sdwa with PRESERVE turned into PAD"), + ("s_mov_b32 m0, s58", "s_mov_b32 m0, 0", "v_movrelsd_b32_sdwa with the dynamic m0 ignored"), + ("dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0", "dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1", + "v_movrelsd_b32_sdwa reading the wrong byte"), + ("v_movrelsd_b32_sdwa v36, sext(v24)", "v_movrelsd_b32_sdwa v36, v24", "v_movrelsd_b32_sdwa without sext"), + ): + mutated = [line.replace(old, new) for line in program] + assert sum(a != b for a, b in zip(mutated, program)) == 1, label + source.write_text(translate_int(mutated), encoding="ascii") + compile_shader(compiler, source, shader) + run_pixel_batch(renderer, backend, shader, [(good, "mismatch")]) + print(f"control {label}: pixel mismatch, as required") + return 0 + + def check_swaprel_draw(directory, compiler, renderer, backend): program = with_constants(SWAPREL_BODY) source = directory / "swaprel.hlsl" @@ -23817,13 +24008,14 @@ def main(): "draw-region-body", "draw-region-waits", "draw-region-scalar-alu", "draw-region-scalar-load", "draw-div-fmas", "draw-cmp64", "draw-cmp-f64", "draw-shift64", "draw-f16-arith", "draw-f16-round", "draw-f16-fmac-ldexp", "draw-f16-cvt-int16", "draw-f16-to-int16", "draw-flat-scratch", "draw-smem-pointers", "draw-sopk-hwreg", - "draw-dpp-cvt", "draw-dpp-movrel", "draw-plain-movrel", "draw-swaprel", "draw-smovrel", "draw-dpp-f16", "draw-dpp-extra", "draw-pk-fmac"): + "draw-dpp-cvt", "draw-dpp-movrel", "draw-plain-movrel", "draw-swaprel", "draw-sdwa-movrel", "draw-smovrel", "draw-dpp-f16", "draw-dpp-extra", "draw-pk-fmac"): try: draw = {"draw-blend-scalar": check_blend_and_scalar_draws, "draw-dpp-cvt": check_dpp_cvt_draw, "draw-dpp-movrel": check_dpp_movrel_draw, "draw-plain-movrel": check_plain_movrel_draw, "draw-swaprel": check_swaprel_draw, + "draw-sdwa-movrel": check_sdwa_movrel_draw, "draw-smovrel": check_smovrel_draw, "draw-dpp-extra": check_dpp_extra_draws, "draw-predicate-rounding": check_predicate_and_rounding_draws, diff --git a/tools/pixel_aot.py b/tools/pixel_aot.py index 623730dfb8..1aa9c8e480 100644 --- a/tools/pixel_aot.py +++ b/tools/pixel_aot.py @@ -1403,6 +1403,11 @@ def check_vopc_e32_dst(enc, dst): # The plain VOP1 (e32) and VOP3 (e64) encodings of the same four moves, as LLVM 15 prints them for gfx1013 and # gfx1030 (two VGPR operands; the VOP3 form takes no clamp, omod or source modifier in that spelling). PLAIN_MOVREL_PATTERN = re.compile(r"(v_movreld_b32|v_movrels_b32|v_movrelsd_b32|v_movrelsd_2_b32)_e(?:32|64) (v\d+), (v\d+)") +# Their SDWA encodings (#1917): the source selection applies to the indexed source word and the destination +# selection to each indexed destination, as v_mov_b32_sdwa applies them. +SDWA_MOVREL_PATTERN = re.compile(r"(v_movreld_b32|v_movrels_b32|v_movrelsd_b32|v_movrelsd_2_b32)_sdwa (v\d+), " + r"(sext\()?(v\d+)(\))? dst_sel:(BYTE_[0-3]|WORD_[01]|DWORD) " + r"dst_unused:(UNUSED_PAD|UNUSED_SEXT|UNUSED_PRESERVE) src0_sel:(BYTE_[0-3]|WORD_[01]|DWORD)") def scalar_const_u32(token, scalar, pending): @@ -5333,8 +5338,10 @@ def sample_coord(reg): expression = f"saturate({expression})" vector[dst] = f"alu{i}" lines.append(f" precise float alu{i} = {expression};") - elif (m := DPP_MOVREL_PATTERN.fullmatch(line)) or (plain := PLAIN_MOVREL_PATTERN.fullmatch(line)): - # The plain forms index exactly as the DPP forms do, each lane reading its own VGPRs (no swizzle). + elif ((m := DPP_MOVREL_PATTERN.fullmatch(line)) or (plain := PLAIN_MOVREL_PATTERN.fullmatch(line)) + or (sdwa := SDWA_MOVREL_PATTERN.fullmatch(line))): + # The plain and SDWA forms index exactly as the DPP forms do, each lane reading its own VGPRs (no swizzle). + sdwa_sel = None if m: op, rest = m.groups() if mask != "entry": @@ -5342,11 +5349,21 @@ def sample_coord(reg): ops, perm, _ = parse_dpp(rest, 2) if not all(re.fullmatch(r"v\d+", t) for t in ops): raise ValueError(f"Expected two VGPR operands without modifiers for {op}") - else: + elif plain: op, *ops = plain.groups() perm = None if mask != "entry": raise ValueError(f"Invalid {op} outside entry mask") + else: + op, dst_tok, sext, src_tok, close, dst_sel, unused, src_sel = sdwa.groups() + ops, perm = [dst_tok, src_tok], None + if mask != "entry": + raise ValueError(f"Invalid {op}_sdwa outside entry mask") + if bool(sext) != bool(close): + raise ValueError(f"Unbalanced sext in {op}_sdwa") + if unused == "UNUSED_SEXT": + raise ValueError("UNUSED_SEXT is not lowered") + sdwa_sel = (src_sel, bool(sext), dst_sel, unused) kind = op if op.endswith("_dpp") else f"{op}_dpp" # the indexing rule is the same for both encodings dst_base, src_base = int(ops[0][1:]), int(ops[1][1:]) if not (0 <= dst_base <= 255 and 0 <= src_base <= 255): @@ -5404,8 +5421,12 @@ def sample_coord(reg): lines.append(f" precise float dpp_src{i} = asfloat({sel});") swizzled = (f"asuint(dpp_src{i})" if perm is None else emit_quad_swizzle_u32(lines, i, f"dpp_src{i}", perm)) + if sdwa_sel: + swizzled = sdwa_source(swizzled, sdwa_sel[0], sdwa_sel[1]) if len(dst_offsets) == 1: dst_reg = f"v{dst_base + dst_offsets[0]}" + if sdwa_sel: + swizzled = sdwa_dest(swizzled, sdwa_sel[2], sdwa_sel[3], vector.get(dst_reg)) vector[dst_reg] = f"alu{i}" lines.append(f" precise float alu{i} = asfloat({swizzled});") else: @@ -5414,8 +5435,10 @@ def sample_coord(reg): lines.append(f" uint dpp_doff{i} = {doff_expr};") for k, prev in prev_dsts: dst_reg = f"v{dst_base + k}" + written = (sdwa_dest(f"dpp_val{i}", sdwa_sel[2], sdwa_sel[3], prev) if sdwa_sel + else f"dpp_val{i}") vector[dst_reg] = f"alu{i}_{k}" - lines.append(f" precise float alu{i}_{k} = (dpp_doff{i} == {k}u) ? asfloat(dpp_val{i}) : {prev};") + lines.append(f" precise float alu{i}_{k} = (dpp_doff{i} == {k}u) ? asfloat({written}) : {prev};") elif m := match(r"v_swaprel_b32 (v\d+), (v\d+)"): # VGPR[vdst + m0[25:16]] and VGPR[vsrc + m0[9:0]] exchange their values (#1795), with the m0 rules of # the movrel forms: both sides are read before either is written, so a pair naming one register is