From 573c3b7c672c82799cba1a492efbc1f3a65eac6c Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:14:20 +0100 Subject: [PATCH] compute: lower s_andn2_wrexec_b64 for wave64 direct compute s_andn2_wrexec_b64 computes EXEC = S0 & ~EXEC over 64 bits, writes the new mask to EXEC and to the destination pair, and sets SCC to (EXEC != 0). The operand order and the destination receiving the new mask match tools/startup/gfx2hlsl.py op_s_andn2_wrexec_b64, which models it per lane. In the wave64 model each host half holds both EXEC words, so the replicated 64-bit computation reuses the SAVEEXEC path. Aligned general SGPR pairs only; the wave32 model keeps refusing the b64 form, and s_andn1_wrexec_b64 stays refused. New test native_wrexec64_hardware: wave64 64- and 128-thread groups with an explicit 64-bit EXEC, the source pair, the destination pair, SCC and per-lane stores against expected words. --- src/gfx1013_direct_compute.inl | 18 +++-- tests/cmake/native_wrexec64.cmake | 7 ++ tests/native_wrexec64_check.py | 112 ++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 tests/cmake/native_wrexec64.cmake create mode 100644 tests/native_wrexec64_check.py diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 2dcfb7d42f..4ed1243f94 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -2040,7 +2040,8 @@ struct DirectCompute { n == "s_quadmask_b32" || n == "s_quadmask_b64" || n == "s_and_saveexec_b64" || n == "s_or_saveexec_b64" || n == "s_xor_saveexec_b64" || n == "s_andn2_saveexec_b64" || n == "s_orn2_saveexec_b64" || - n == "s_nand_saveexec_b64" || n == "s_nor_saveexec_b64" || n == "s_xnor_saveexec_b64"; + n == "s_nand_saveexec_b64" || n == "s_nor_saveexec_b64" || n == "s_xnor_saveexec_b64" || + n == "s_andn2_wrexec_b64"; const bool overwrites = mask_scc || n == "s_abs_i32" || n == "s_bcnt0_i32_b32" || n == "s_bcnt1_i32_b32" || n == "s_bcnt0_i32_b64" || n == "s_bcnt1_i32_b64" || @@ -3046,12 +3047,16 @@ struct DirectCompute { } if ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) && n.size() > 6 && n.substr(n.size() - 4) == "_b64") { bool save = n.find("_saveexec_") != Name::npos; + const bool wrexec = compute && wave64 && n == "s_andn2_wrexec_b64"; auto op = n.substr(2, n.find('_', 2) - 2); - if ((i.dst & 1) || (i.dst > 104 && i.dst != 106 && i.dst != 126) || - (save && i.enc != Encoding::Sop1) || (!save && i.enc != Encoding::Sop2)) + if (wrexec ? ((i.dst | i.src[0]) & 1) || i.dst > 104 || i.src[0] > 104 + : (i.dst & 1) || (i.dst > 104 && i.dst != 106 && i.dst != 126) || + (save ? i.enc != Encoding::Sop1 : i.enc != Encoding::Sop2)) refuse(i.pc, "unsupported wave64 scalar operation"); auto ah = pair_high(i, i.src[0]); - auto bl = save ? read(126) : b, bh = save ? read(127) : pair_high(i, i.src[1]); + const bool updates_exec = save || wrexec; + auto bl = updates_exec ? read(126) : b; + auto bh = updates_exec ? read(127) : pair_high(i, i.src[1]); auto combine = [&](const string &left, const string &right) -> string { if (op == "and" || op == "or" || op == "xor") return ir.binary(string(op), left, right); if (op == "andn2" || op == "orn2") @@ -3060,11 +3065,12 @@ struct DirectCompute { return ir.binary("xor", ir.binary(op == "nand" ? "and" : op == "nor" ? "or" : "xor", left, right), "4294967295"); refuse(i.pc, "unsupported wave64 scalar mask operation"); }; - // SAVEEXEC combines its source operand with the old EXEC in ISA operand order. + // SAVEEXEC and WREXEC combine the source with old EXEC in ISA operand order. + // WREXEC stores the new mask in the destination pair. auto lo = combine(a, bl), hi = combine(ah, bh); write(i.dst, save ? bl : lo); write(i.dst + 1, save ? bh : hi); - if (save) { write(126, lo); write(127, hi); } + if (updates_exec) { write(126, lo); write(127, hi); } write(128, ir.value("zext i1 " + cmp("ne", ir.binary("or", lo, hi), "0") + " to i32")); return; } diff --git a/tests/cmake/native_wrexec64.cmake b/tests/cmake/native_wrexec64.cmake new file mode 100644 index 0000000000..8f0f147d7a --- /dev/null +++ b/tests/cmake/native_wrexec64.cmake @@ -0,0 +1,7 @@ +if(TARGET compile_dxil AND TARGET native_store_replay AND + EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_wrexec64_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_wrexec64_check.py" + "$" "$" "${DXC_BIN_DIR}") + set_tests_properties(native_wrexec64_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_wrexec64_check.py b/tests/native_wrexec64_check.py new file mode 100644 index 0000000000..c88b11456f --- /dev/null +++ b/tests/native_wrexec64_check.py @@ -0,0 +1,112 @@ +"""Check slot 67 wave64 WREXEC lane stores, destination words and SCC.""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from native_compute_clear_check import write_request +from native_dpp_mirror_check import vop2 +from native_scalar_lds_check import scalar, sop1, vmove + + +MOV_B32, ANDN2_WREXEC_B64 = 3, 0x3A +ZERO, ONE, NEG_ONE, LITERAL = 128, 129, 193, 255 +EXEC_LO, EXEC_HI = 126, 127 +OLD_EXEC = (0x80000012, 0x40000009) +GUARD_WORDS, OUTPUT_WORDS = 8, 2064 +GROUP_WORDS, PLANE_WORDS = 128, 256 +SENTINEL = 0x69696969 +CASES = ( + # X, source words, destination, incoming SCC, expected result, expected SCC + (64, (0x80000084, 0xC0000021), 24, 0, (0x84, 0x80000020), 1), + (128, (0x80000084, 0xC0000021), 24, 0, (0x84, 0x80000020), 1), + (128, (0x80000002, 0xC0000021), 22, 0, (0, 0x80000020), 1), + (64, OLD_EXEC, 24, 1, (0, 0), 0), +) + + +def program(source, dst, incoming_scc, src=22): + # s4 holds the table, s5 holds group.x and v0 holds local.x. + # v8 = 512 * group.x + 4 * local.x, calculated before narrowing EXEC. + words = [ + sop1(MOV_B32, 20, 4), sop1(MOV_B32, 21, ZERO), + 0xF408080A, 0xFA000020, + scalar(0x1E, 7, 5, ZERO + 9), + vop2(0x1A, 8, ZERO + 2, 0), vop2(0x25, 8, 7, 8), + 0xBF8CC07F, + sop1(MOV_B32, EXEC_LO, LITERAL), OLD_EXEC[0], + sop1(MOV_B32, EXEC_HI, LITERAL), OLD_EXEC[1], + sop1(MOV_B32, 22, LITERAL), source[0], + sop1(MOV_B32, 23, LITERAL), source[1], + 0xBF060080 | (ZERO if incoming_scc else ONE) << 8, # s_cmp_eq_u32 + sop1(ANDN2_WREXEC_B64, dst, src), + 0xE0701000, 0x80080008, # store v0 under the new EXEC + scalar(0x0A, 26, ONE, ZERO), # s_cselect_b32 captures SCC + sop1(MOV_B32, EXEC_LO, NEG_ONE), sop1(MOV_B32, EXEC_HI, NEG_ONE), + ] + for plane, reg in enumerate((dst, dst + 1, 26), start=1): + words += [vmove(4, reg), 0xE0701000 | (plane * PLANE_WORDS * 4), 0x80080408] + return words + [0xBF810000] + + +def request(work, root, threads, words): + # .sa dword 4 bit 20 is clear, selecting the wave64 model. + sa = struct.pack("<7I", 0, 0, 0, threads | (1 << 16), 1, 0, 0x8A) + return write_request(work, words, root, sa) + + +def expected_output(threads, result, scc): + expected = [SENTINEL] * OUTPUT_WORDS + for group in range(2): + for local in range(threads): + index = GUARD_WORDS + group * GROUP_WORDS + local + lane = local % 64 + if result[lane // 32] & (1 << (lane % 32)): + expected[index] = local + for plane, value in enumerate((*result, scc), start=1): + expected[index + plane * PLANE_WORDS] = value + return expected + + +def main(cli, replay, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + shader, root_file = work / "root.hlsl", work / "root.bin" + shader.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", + "-Fo", root_file, shader], check=True) + root = root_file.read_bytes() + cache = work / "cache" + compile_cmd = [cli, sdk, cache, "gfx1013-direct-cs@0"] + replay_cases = [] + for threads, source, dst, incoming_scc, result, scc in CASES: + marker = request(work, root, threads, program(source, dst, incoming_scc)) + output = marker.with_suffix(".cso") + compiled = subprocess.run([*compile_cmd, marker, output], capture_output=True, text=True) + assert compiled.returncode == 0 and output.is_file(), (compiled.stdout, compiled.stderr) + replay_cases.append((threads, result, scc, marker)) + + for src, dst in ((106, 24), (23, 24), (22, 25)): + marker = request(work, root, 64, program(CASES[0][1], dst, 0, src)) + output = marker.with_suffix(".cso") + compiled = subprocess.run([*compile_cmd, marker, output], capture_output=True, text=True) + assert compiled.returncode != 0 and "unsupported wave64 scalar operation" in compiled.stderr, ( + src, dst, compiled.stdout, compiled.stderr) + assert not output.exists(), output + + for threads, result, scc, marker in replay_cases: + output = marker.with_suffix(".bin") + replayed = subprocess.run([replay, sdk, marker, cache, "0", output, "--direct"]) + if replayed.returncode == 77: + return 77 + replayed.check_returncode() + actual = struct.unpack(f"<{OUTPUT_WORDS}I", output.read_bytes()) + for index, (got, want) in enumerate(zip(actual, expected_output(threads, result, scc))): + assert got == want, (threads, result, index, hex(got), hex(want)) + print(f"wave64 X={threads}, EXEC={result[1]:08x}:{result[0]:08x}, SCC={scc} matched") + return 0 + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:]))