From d62f93a77643c86d07da7f41c7d38786da6d0bfe Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sat, 28 Nov 2026 10:17:07 +0100 Subject: [PATCH] compute: lower s_andn2_wrexec_b64 in the narrow wave64 model A guest wave64 request of at most 32 threads runs on the wave32 model, where EXEC_HI starts at zero. s_andn2_wrexec_b64 is now lowered there as in full wave64: D = S0 & ~EXEC and EXEC = D on both words. A V_CMPX that writes EXEC also writes 0 to EXEC_HI in that model, since the guest has no upper lanes. --- src/gfx1013_compute_native.cpp | 14 ++- src/gfx1013_direct_compute.inl | 14 ++- tests/cmake/native_scalar_mask64.cmake | 14 +++ tests/native_scalar_mask64_check.py | 128 +++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 tests/cmake/native_scalar_mask64.cmake create mode 100644 tests/native_scalar_mask64_check.py diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 0e8b4a7af..2e7996903 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -630,7 +630,8 @@ static std::optional byte_descriptor_field(const Inst &i, const State &s return field(a,T::byte_oob); return {}; } -State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_window, bool direct) { +State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_window, bool direct, + bool narrow_wave64) { auto set = [&](unsigned n, Symbol v) { if (n >= s.size()) refuse(i.pc, "invalid SGPR"); @@ -759,7 +760,8 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win i.name == "v_readfirstlane_b32" || i.name == "v_readlane_b32") { set(i.dst, {}); - if ((wave64 && i.enc == Encoding::Vopc) || ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) && + if (((wave64 || (narrow_wave64 && i.dst == 126)) && i.enc == Encoding::Vopc) || + ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) && i.name.size() >= 4 && (i.name.substr(i.name.size() - 4) == "_b64" || i.name.substr(i.name.size() - 4) == "_i64" || i.name.substr(i.name.size() - 4) == "_u64") && @@ -839,7 +841,7 @@ std::map fixed_vertex_loads(const string &code) { return found ? result : std::map{}; } Program parse(const string &code, unsigned entry, const State &initial, bool graph, bool graphics=false, bool wave64=false, bool fixed_vertex=false, - bool heap_window=false) { + bool heap_window=false, bool narrow_wave64=false) { Program p; p.graphics_wave64 = graphics && wave64; if ((entry & 3) || entry >= code.size() || code.size() > 1u << 20) @@ -888,7 +890,7 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra auto k = work.front(); work.pop_front(); auto &i = p.code[k]; - auto after = transfer(i, p.before[k], wave64, graphics, heap_window, graph); + auto after = transfer(i, p.before[k], wave64, graphics, heap_window, graph, narrow_wave64); auto merge = [&](size_t t) { bool changed = !p.reached[t]; auto &in = p.before[t]; @@ -1774,9 +1776,10 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi // has lanes 0..31 only, so EXEC's high word is zero and v_mbcnt_hi adds nothing for its lanes. The model // does not write the zero high word that a wave64 compare or carry-out stores in an SGPR pair destination. const bool wave64 = stage ? !stage->wave32 : direct_dxil && !request.wave32 && threads > 32; + const bool narrow_wave64 = !stage && direct_dxil && !request.wave32 && !wave64; // The byte-address rewrite of a GPU-selected heap descriptor is read only under the raw_buffer contract. const bool heap_window = direct_dxil && heap_view == HeapViewContract::raw_buffer; - auto program = parse(request.code, entry, initial, direct_dxil, false, wave64, false, heap_window); + auto program = parse(request.code, entry, initial, direct_dxil, false, wave64, false, heap_window, narrow_wave64); Emit emit{{}, program,layout->Desc_1_1}; emit.result.constants = constants; if (!lds) @@ -1786,6 +1789,7 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi if (direct_dxil) { DirectCompute compiler{emit}; compiler.wave64 = wave64; + compiler.narrow_wave64 = narrow_wave64; compiler.heap_view = heap_view; compiler.heap_window = heap_window; for (size_t i = 0; i < program.code.size(); ++i) { diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index ca5ee2acb..d3837371c 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -42,6 +42,7 @@ struct DirectCompute { unsigned exchange_words = 0; bool uses_exchange = false; bool wave64 = false; + bool narrow_wave64 = false; // The view class a GPU-selected heap slot is read as; see HeapViewContract. HeapViewContract heap_view = HeapViewContract::none; // heap_view is raw_buffer and the path is direct: the byte-address rewrite of a heap descriptor is tracked. @@ -213,8 +214,13 @@ struct DirectCompute { auto pair = exchange_halves(mask); write(dst, pair[0]); write(dst + 1, pair[1]); - } else + } else { write(dst, mask); + // V_CMPX replaces EXEC. A narrow guest wave64 has no upper + // lanes, even if a preceding scalar write set bits in EXEC_HI. + if (narrow_wave64 && dst == 126) + write(127, "0"); + } } string pair_high(const Inst &i, unsigned src) { if (src <= 104 || src == 106 || src == 126) { @@ -3555,7 +3561,9 @@ 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" || n == "s_andn1_wrexec_b64"); + const bool wrexec = compute && + (((wave64 || narrow_wave64) && n == "s_andn2_wrexec_b64") || + (wave64 && n == "s_andn1_wrexec_b64")); auto op = n.substr(2, n.find('_', 2) - 2); 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) || @@ -3564,6 +3572,8 @@ struct DirectCompute { auto ah = pair_high(i, i.src[0]); const bool updates_exec = save || wrexec; auto bl = updates_exec ? read(126) : b; + // The narrow model starts EXEC_HI at zero. Scalar code can + // explicitly write it, and later scalar instructions can read it. auto bh = updates_exec ? read(127) : pair_high(i, i.src[1]); // SAVEEXEC and WREXEC combine the source with old EXEC in ISA operand order. // WREXEC stores the new mask in the destination pair. diff --git a/tests/cmake/native_scalar_mask64.cmake b/tests/cmake/native_scalar_mask64.cmake new file mode 100644 index 000000000..e75371335 --- /dev/null +++ b/tests/cmake/native_scalar_mask64.cmake @@ -0,0 +1,14 @@ +if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}" AND + EXISTS "${DXC_BIN_DIR}/dxc.exe" AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND + EXISTS "${DXC_BIN_DIR}/dxil.dll") + set(checker "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_scalar_mask64_check.py") + add_test(NAME native_scalar_mask64_controls COMMAND "${INTAKE_PYTHON}" -B + "${checker}" controls "$" "${DXC_BIN_DIR}") + set_tests_properties(native_scalar_mask64_controls PROPERTIES TIMEOUT 120) + if(TARGET native_store_replay) + add_test(NAME native_scalar_mask64_hardware COMMAND "${INTAKE_PYTHON}" -B + "${checker}" hardware "$" "${DXC_BIN_DIR}") + set_tests_properties(native_scalar_mask64_hardware PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77) + endif() + unset(checker) +endif() diff --git a/tests/native_scalar_mask64_check.py b/tests/native_scalar_mask64_check.py new file mode 100644 index 000000000..4b72c7ab4 --- /dev/null +++ b/tests/native_scalar_mask64_check.py @@ -0,0 +1,128 @@ +"""Check s_andn2_wrexec_b64 in a wave64 request of at most 32 threads.""" +from pathlib import Path +import re +import struct +import subprocess +import sys +import tempfile + +from native_scalar_lds_check import sop1 +from native_wrexec64_check import ( + ANDN2_WREXEC_B64, EXEC_HI, LITERAL, MOV_B32, OLD_EXEC, OUTPUT_WORDS, + expected_output, program, request, +) + + +BCNT1_I32_B64, VCC_LO, END = 0x10, 106, 0xBF810000 +SOURCE = (0x96, 0x40000001) +REGISTER = r"%[-\w.]+" + + +def wrexec_program(dst=28, *, high="initial"): + code = program(SOURCE, dst, 0) + code[code.index(sop1(ANDN2_WREXEC_B64, dst, 22))] = sop1(ANDN2_WREXEC_B64, dst, 28) + for src, dst_reg in ((22, 28), (23, 29)): + code[code.index(sop1(MOV_B32, src, LITERAL))] = sop1(MOV_B32, dst_reg, LITERAL) + if high == "initial": + index = code.index(sop1(MOV_B32, EXEC_HI, LITERAL)) + del code[index:index + 2] + elif high == "cmpx": + index = code.index(sop1(ANDN2_WREXEC_B64, dst, 28)) + code.insert(index, 0x7DA40080) # v_cmpx_eq_u32 0, v0 (lane 0 disabled) + return code + + +def count_program(src=VCC_LO): + return [sop1(MOV_B32, VCC_LO, LITERAL), 0x80000003, + sop1(MOV_B32, VCC_LO + 1, LITERAL), 0x40000050, + sop1(BCNT1_I32_B64, 16, src), END] + + +def compile_case(cli, sdk, work, marker, diagnostic=None): + output = marker.with_suffix(".cso") + run = subprocess.run([cli, sdk, work / "cache", "gfx1013-direct-cs@0", marker, output], + capture_output=True, text=True, timeout=30) + if diagnostic: + assert run.returncode and not output.exists(), (run.stdout, run.stderr) + assert any(line.endswith(": " + diagnostic) for line in run.stderr.splitlines()), run.stderr + return "" + assert run.returncode == 0 and output.is_file(), (run.stdout, run.stderr) + return subprocess.run([Path(sdk) / "dxc.exe", "-dumpbin", output], + capture_output=True, text=True, timeout=30, check=True).stdout + + +def definitions(dump): + return {name: rhs.strip() for name, rhs in + re.findall(rf"^\s*({REGISTER}) = ([^\n;]+)", dump, re.MULTILINE)} + + +def stored(dump, reg): + return re.findall(rf"^\s*store i32 (\S+), i32\* %r{reg}, align 4", dump, re.MULTILINE) + + +def check_scc(defs, value, result): + check = re.fullmatch(rf"zext i1 ({REGISTER}) to i32", defs[value]) + assert check and defs[check[1]] == f"icmp ne i32 {result}, 0", defs + + +def check_wrexec(dump, old_high): + defs = definitions(dump) + low, high = (stored(dump, reg)[-1] for reg in (28, 29)) + for result, src, exec_reg in ((low, 28, 126), (high, 29, 127)): + operands = re.fullmatch(rf"and i32 ({REGISTER}), ({REGISTER})", defs[result]) + assert operands and defs[operands[1]] == f"load i32, i32* %r{src}, align 4", dump + inv = re.fullmatch(rf"xor i32 ({REGISTER}), -1", defs[operands[2]]) + assert inv and defs[inv[1]] == f"load i32, i32* %r{exec_reg}, align 4", dump + assert stored(dump, exec_reg)[-2] == result, dump # followed by fixture restore + assert int(stored(dump, EXEC_HI)[-3]) == old_high, dump + scc = stored(dump, 128)[-1] + merged, = [name for name, rhs in defs.items() if rhs == f"or i32 {low}, {high}"] + check_scc(defs, scc, merged) + + +def controls(cli, sdk, work, root): + for high in ("initial", "scalar", "cmpx"): + marker = request(work, root, 32, wrexec_program(high=high)) + check_wrexec(compile_case(cli, sdk, work, marker), OLD_EXEC[1] if high == "scalar" else 0) + for code, threads, diagnostic in ( + (wrexec_program(dst=29), 32, "unsupported wave64 scalar operation"), + (count_program(src=107), 64, "invalid s_bcnt1_i32_b64 source"), + # VCC stays refused as a 64-bit count source. + (count_program(), 64, "invalid s_bcnt1_i32_b64 source"), + (count_program(), 32, "invalid s_bcnt1_i32_b64 source"), + ): + compile_case(cli, sdk, work, request(work, root, threads, code), diagnostic) + print("64-bit scalar mask DXIL and refusal diagnostics matched") + return 0 + + +def hardware(replay, sdk, work, root): + for high in ("initial", "scalar", "cmpx"): + marker = request(work, root, 32, wrexec_program(high=high)) + output = marker.with_suffix(".bin") + run = subprocess.run([replay, sdk, marker, work / "cache", "0", output, "--direct"], timeout=30) + if run.returncode == 77: + return 77 + run.check_returncode() + old_low = 0 if high == "cmpx" else OLD_EXEC[0] + old_high = OLD_EXEC[1] if high == "scalar" else 0 + mask = (SOURCE[0] & ~old_low, SOURCE[1] & ~old_high) + actual = struct.unpack(f"<{OUTPUT_WORDS}I", output.read_bytes()) + expected = expected_output(32, mask, int(any(mask))) + assert list(actual) == expected, (high, actual, expected) + print("32-thread wave64 WREXEC lane stores, destination words and SCC matched") + return 0 + + +def main(mode, tool, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + source, root = work / "root.hlsl", work / "root.bin" + source.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", + "-Fo", root, source], check=True, timeout=30) + return {"controls": controls, "hardware": hardware}[mode](tool, sdk, work, root.read_bytes()) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:]))