diff --git a/docs/experiments.md b/docs/experiments.md index 6c30d84af6..a4094e674e 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -70,6 +70,8 @@ local second operand. A sparse EXEC mask distinguishes fetching an inactive source, supplying zero and retaining the destination. The test checks both workgroups and the surrounding buffer guards. +Wave32 direct compute lowers `s_or_saveexec_b32`. As with `s_mov_b32 exec_lo`, EXEC keeps bits past the group; a lane runs only if its own bit is set. + The pixel fixture opens EXEC to all lanes, seeds a guest register with scalar lane writes and reads back lanes in each half of a guest wave64. The compiler can track those words independently of the desktop rasterizer's choice of diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 11ffc7b2ae..7c2bf2f97e 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -650,6 +650,10 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win set(i.dst, v); if (i.name == "s_mov_b64") set(i.dst + 1, high); + } else if (!graphics && !wave64 && (i.name == "s_and_saveexec_b32" || i.name == "s_or_saveexec_b32")) { + // Both the saved EXEC and the updated EXEC are numeric mask words. + set(i.dst, {}); + set(126, {}); } else if (i.name == "s_and_b32" || i.name == "s_lshl_b32") { const auto a = source(i, i.src[0], s), b = source(i, i.src[1], s); Symbol out{}; diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index d10177eb30..c1eb0ff651 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -2027,7 +2027,7 @@ struct DirectCompute { inst.enc == Encoding::Sopc || n == "s_bfe_u32" || n == "s_and_b32" || n == "s_or_b32" || n == "s_max_u32" || n == "s_add_u32" || n == "s_sub_u32" || n == "s_add_i32" || n == "s_sub_i32" || n == "s_and_saveexec_b32" || - n == "s_andn2_wrexec_b32" || n == "s_andn2_b32" || n == "s_orn2_b32" || + n == "s_or_saveexec_b32" || n == "s_andn2_wrexec_b32" || n == "s_andn2_b32" || n == "s_orn2_b32" || n == "s_nor_b32" || n == "s_nand_b32" || n == "s_xnor_b32" || n == "s_not_b32" || n == "s_lshr_b32" || n == "s_lshl_b32" || n == "s_ashr_i32" || n == "s_lshl_b64" || n == "s_lshr_b64" || n == "s_ashr_i64" || n == "s_xor_b32" || @@ -2980,10 +2980,15 @@ struct DirectCompute { write(128, ir.value("zext i1 " + cmp("ne", ir.binary("or", lo, hi), "0") + " to i32")); return; } - if (n == "s_and_saveexec_b32" || n == "s_andn2_wrexec_b32") { + const bool or_saveexec = n == "s_or_saveexec_b32" && compute && !wave64; + if (or_saveexec || n == "s_and_saveexec_b32" || n == "s_andn2_wrexec_b32") { + const bool save = n != "s_andn2_wrexec_b32"; + if (or_saveexec && i.dst > 107) + refuse(i.pc, "unsupported wave32 SAVEEXEC destination"); auto old = read(126); - auto mask = ir.binary("and", a, n == "s_and_saveexec_b32" ? old : ir.binary("xor", old, "4294967295")); - write(i.dst, n == "s_and_saveexec_b32" ? old : mask); + // Like s_mov_b32 exec_lo, EXEC keeps every bit; a lane runs only if its own bit is set. + auto mask = ir.binary(or_saveexec ? "or" : "and", a, save ? old : ir.binary("xor", old, "4294967295")); + write(i.dst, save ? old : mask); write(126, mask); write(128, ir.value("zext i1 " + cmp("ne", mask, "0") + " to i32")); return; diff --git a/tests/cmake/native_or_saveexec.cmake b/tests/cmake/native_or_saveexec.cmake new file mode 100644 index 0000000000..84ae90a645 --- /dev/null +++ b/tests/cmake/native_or_saveexec.cmake @@ -0,0 +1,6 @@ +if(TARGET native_store_replay AND EXISTS "${INTAKE_PYTHON}" AND + EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_or_saveexec_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_or_saveexec_check.py" "$" "${DXC_BIN_DIR}") + set_tests_properties(native_or_saveexec_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120) +endif() diff --git a/tests/native_direct_container_check.py b/tests/native_direct_container_check.py index 44956501b4..b6c54d4255 100644 --- a/tests/native_direct_container_check.py +++ b/tests/native_direct_container_check.py @@ -344,6 +344,10 @@ def check(name, profile, data, expected=""): check("descriptor-sop1-before-scc-read", cs, compute(words(*address_field, *pair_zero, ff1_pair, select_scc, end), table=True), "numeric SCC from a symbolic descriptor field") + or_saveexec = 0xbe983dc1 # s_or_saveexec_b32 s24, -1 + check("descriptor-or-saveexec-kill", cs, compute(words(*address_field, or_saveexec, read_scc, end), table=True)) + check("or-saveexec-exec-destination", cs, compute(words(0xbefe3dc1, end)), + "unsupported wave32 SAVEEXEC destination") # The previous symbolic SCC is dead at WQM. Reading it before WQM # must still refuse. The nonzero high word supplies the new SCC. numeric_wqm = [0xbe9a0380, 0xbe9b0381, 0xbe980a1a] # s[24:25] = WQM(s[26:27]) diff --git a/tests/native_or_saveexec_check.py b/tests/native_or_saveexec_check.py new file mode 100644 index 0000000000..8f9b4cd133 --- /dev/null +++ b/tests/native_or_saveexec_check.py @@ -0,0 +1,79 @@ +"""Direct wave32 s_or_saveexec_b32: saved EXEC, the OR result, SCC and a restore, in full and partial waves.""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from native_compute_clear_check import write_request +from native_scalar_lds_check import scalar, sop1, vmove + + +def or_saveexec_readback(work, gpu, sdk): + full = 0xffffffff + root = (work / "root.bin").read_bytes() + for width in (8, 32): + entry = (1 << width) - 1 + sa = struct.pack("<7I", 0, 0, 0, width | 1 << 16, 1, 0, 0x80 | 5 << 1) + for name, old, src, mask in ( + ("all", 0x25, 193, full), + ("sgpr", 0x25, 10, 0x80000092), # Source aliases the saved EXEC destination. + ("literal", 0x25, 255, 0x80000018), + ("vcc", 0x25, 106, 0x80000042), + ("empty", 0, 128, 0), + ("past-end", 0, 255, 0x80000000), + ): + updated = old | mask # EXEC is stored as computed, including bits past the group + words = [sop1(3, 20, 4), sop1(3, 21, 128), 0xf408080a, 0xfa000020, + scalar(0x1e, 7, 5, 137), 0x34000000 | 8 << 17 | 132, + 0x4a000000 | 8 << 17 | 8 << 9 | 7, + sop1(0x3c, 24, 255), old] + if src in (10, 106): + words += [sop1(3, src, 255), mask] + if name == "all": + words.append(sop1(3, 10, 32)) # Replace symbolic descriptor provenance with numeric EXEC. + if name == "vcc": + # Derive the same partial EXEC through S_ANDN2_WREXEC_B32. + words += [sop1(3, 126, 255), entry ^ old, sop1(0x47, 25, 24)] + # Set the incoming SCC opposite to the expected new SCC. + words += [0xbf068180 if updated else 0xbf068080, sop1(0x3d, 10, src)] + if src == 255: + words.append(mask) + words += [scalar(0xa, 11, 129, 128), vmove(9, 10), vmove(10, 126), + 0xe0701000, 0x80080908, 0xe0701004, 0x80080a08, + sop1(3, 126, 10), vmove(9, 177), 0xe0701008, 0x80080908, + sop1(3, 126, 24), vmove(9, 11), 0xe070100c, 0x80080908, + 0xbf810000] + expected = [0x69696969] * 2064 + for group in range(2): + for lane in range(width): + pos = 8 + group * 128 + lane * 4 + if updated & (1 << lane): + expected[pos:pos + 2] = [old, updated] + if old & (1 << lane): + expected[pos + 2] = 49 + expected[pos + 3] = int(bool(updated)) + marker = write_request(work, words, root, sa) + dest = work / "or-saveexec.bin" + result = subprocess.run([gpu, sdk, marker, work / "cache", "0", dest, "--direct"]) + if result.returncode == 77: + sys.exit(77) + result.check_returncode() + actual = struct.unpack("<2064I", dest.read_bytes()) + assert list(actual) == expected, (width, name, [ + (i, a, b) for i, (a, b) in enumerate(zip(actual, expected)) if a != b + ][:10]) + + +def main(gpu, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + src, root = work / "root.hlsl", work / "root.bin" + src.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", "-Fo", root, src], check=True) + or_saveexec_readback(work, gpu, sdk) + print("s_or_saveexec_b32 matched six sources in 8- and 32-thread groups, with SCC, restore and guards.") + + +if __name__ == "__main__": + main(*sys.argv[1:])