diff --git a/docs/experiments/native-dpp-quad-compute.md b/docs/experiments/native-dpp-quad-compute.md index c8f6c61598..dc160a80f9 100644 --- a/docs/experiments/native-dpp-quad-compute.md +++ b/docs/experiments/native-dpp-quad-compute.md @@ -7,6 +7,8 @@ two-bit field in the control byte. It adds that field to the base of the destination lane's quad and reads source 0 from that physical wave32 lane. Quads fit within each physical half of a split guest wave64. +Direct compute also lowers DPP8 selectors within each eight-lane group. FI reads the saved source VGPR and otherwise an inactive source supplies zero. + Without FI, the guest EXEC bit of the selected source decides whether it can be read. BOUND_CTRL supplies a zero source for a disabled lane. With BOUND_CTRL clear, the instruction preserves the old destination. FI reads the diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 11ffc7b2ae..8120f77f71 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -60,6 +60,7 @@ struct Inst { // 0..255 selects within a quad. Row operations and mirrors cross quads. std::optional dpp_control; unsigned dpp_row_mask = 15, dpp_bank_mask = 15; + std::optional dpp8_selectors; bool dpp_fetch_inactive = false; bool dpp_bound_zero = false; unsigned mirror_mask() const { return dpp_control == 0x141 ? 7 : dpp_control == 0x140 ? 15 : 0; } @@ -287,6 +288,9 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "unsupported instruction " + string(encoded.name) + " " + hexword(a)); if (!i.wide && (i.enc == Encoding::Vop1 || i.enc == Encoding::Vop2 || i.enc == Encoding::Vopc)) { const bool half_alu = !graphics && i.enc == Encoding::Vop2 && binary16_alu(encoded.name); + const bool dpp8 = i.src[0] == 233 || i.src[0] == 234; + if (direct && half_alu && dpp8) + refuse(pc, "unsupported " + string(encoded.name) + " DPP form"); const bool sdwa_logic = i.enc == Encoding::Vop2 && i.op == 0x1d; const bool sdwa_class = i.enc == Encoding::Vopc && i.op == 0x88; const bool sdwa_compare = i.enc == Encoding::Vopc && !sdwa_class; @@ -399,7 +403,12 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.src[0]=256+(control&255); i.neg=((control>>20)&1)|((control>>21)&2); i.abs=((control>>21)&1)|((control>>22)&2); - } else if (graphics && (i.src[0] == 233 || i.src[0] == 234) && i.enc != Encoding::Vopc && !lit) { + } else if (direct && !graphics && dpp8 && i.enc != Encoding::Vopc && !lit) { + const Word control = take(); + i.dpp8_selectors = control >> 8; + i.dpp_fetch_inactive = i.src[0] == 234; + i.src[0] = 256 + (control & 255); + } else if (graphics && dpp8 && i.enc != Encoding::Vopc && !lit) { // pixel_aot.parse_dpp: both halves of an octet must select the // same local 2x2 permutation to map to desktop quad operations. const Word control = take(); diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index d10177eb30..4a098a14a4 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -3475,9 +3475,10 @@ struct DirectCompute { const unsigned mask = i.mirror_mask(); const auto row = i.dpp_row(); const bool row_op = row.op != Inst::DppRowOp::None; - const unsigned group_size = row_op ? 16 : mask ? mask + 1 : 4; + const unsigned group_size = i.dpp8_selectors ? 8 : row_op ? 16 : mask ? mask + 1 : 4; if (threads % group_size) - refuse(i.pc, row_op ? "DPP row shift or rotate needs a thread count divisible by 16" + refuse(i.pc, i.dpp8_selectors ? "DPP8 needs a thread count divisible by eight" + : row_op ? "DPP row shift or rotate needs a thread count divisible by 16" : mask ? "DPP mirror needs complete source lane groups" : "DPP quad_perm needs a thread count divisible by four"); const auto rule = alu_rules.find(i.name); @@ -3486,7 +3487,13 @@ struct DirectCompute { if (i.enc == Encoding::Vop1 && ((i.neg | i.abs) & 2)) refuse(i.pc, "unexpected second DPP source modifier"); string source_lane, source_usable; - if (row_op) { + if (i.dpp8_selectors) { + auto octet_base = ir.binary("and", lane, "4294967288"); + auto octet_lane = ir.binary("and", lane, "7"); + auto shift = ir.binary("mul", octet_lane, "3"); + auto fields = ir.binary("lshr", to_string(*i.dpp8_selectors), shift); + source_lane = ir.binary("or", octet_base, ir.binary("and", fields, "7")); + } else if (row_op) { auto row_base = ir.binary("and", lane, "4294967280"); auto row_lane = ir.binary("and", lane, "15"); auto amount = to_string(row.amount); @@ -3515,7 +3522,8 @@ struct DirectCompute { } string write_enabled; if (!source_usable.empty()) { - if (i.dpp_bound_zero) word = select(source_usable, word, "0"); + // DPP8 supplies zero for a disabled source when FI is clear. + if (i.dpp8_selectors || i.dpp_bound_zero) word = select(source_usable, word, "0"); else write_enabled = source_usable; } // Row and bank masks gate destination writes independently of the source and BOUND_CTRL. @@ -3655,7 +3663,7 @@ struct DirectCompute { auto pred = cmp((i.op & 1) ? "ne" : "eq", mask, "0"); ir.body << " br i1 " << pred << ", label %pc" << i.target() << ", label %pc" << i.next << "\n"; } - } else if (i.dpp_control) + } else if (i.dpp_control || i.dpp8_selectors) lower_dpp(i, p.before[k], x*y*z); else if (i.name == "v_permlane16_b32" || i.name == "v_permlanex16_b32") lower_permlane(i, x*y*z); diff --git a/tests/cmake/native_dpp8.cmake b/tests/cmake/native_dpp8.cmake new file mode 100644 index 0000000000..08b341c016 --- /dev/null +++ b/tests/cmake/native_dpp8.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_dpp8_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_dpp8_check.py" + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_dpp8_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_dpp8_check.py b/tests/native_dpp8_check.py new file mode 100644 index 0000000000..d3679ab26c --- /dev/null +++ b/tests/native_dpp8_check.py @@ -0,0 +1,97 @@ +"""Wave32 hardware readback for direct compute DPP8.""" +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 + + +ADD, MIN, MAX = 0x03, 0x0F, 0x10 +SWAP = (4, 5, 6, 7, 0, 1, 2, 3) +ASYMMETRIC = (7, 0, 3, 1, 6, 2, 5, 4) +ALL = 0xFFFFFFFF +SPARSE = 0x55555555 +SENTINEL = 0x69696969 + + +def program(op, selectors, active, fi): + # Each workgroup writes 32 words, starting after eight guard words. + words = [sop1(3, 20, 4), sop1(3, 21, 128), 0xF408080A, 0xFA000020, + scalar(0x1E, 7, 5, 135), vop2(0x1A, 8, 130, 0), vop2(0x25, 8, 7, 8), + vop2(0x25, 9, 145, 0), 0x7E000000 | 9 << 17 | 6 << 9 | 265] + # Seed v9 under full EXEC and alias both inputs with the destination. + # Restore full EXEC before storing so disabled destinations are visible. + src0 = 234 if fi else 233 + instruction = vmove(9, src0) if op is None else vop2(op, 9, src0, 9) + control = 9 | sum(peer << (8 + 3 * lane) for lane, peer in enumerate(selectors)) + return words + [sop1(3, 126, 255), active, instruction, control, + sop1(3, 126, 193), 0xE0701000, 0x80080908, 0xBF810000] + + +def reference(op, selectors, active, fi): + initial = list(range(17, 49)) + result = initial.copy() + for start in range(0, 32, 8): + for offset, selection in enumerate(selectors): + lane, peer = start + offset, start + selection + if not active & (1 << lane): + continue + source = initial[peer] if fi or active & (1 << peer) else 0 + if op == ADD: + result[lane] = source + initial[lane] + elif op == MIN: + result[lane] = min(source, initial[lane]) + elif op == MAX: + result[lane] = max(source, initial[lane]) + else: + result[lane] = source + return struct.unpack("<32I", struct.pack("<32f", *result)) + + +def hardware(replay, sdk, work, root): + # Unique values in all four octets expose a wrong group base. In each + # sparse case lane 0 selects disabled lane 7, exposing ignored FI. + cases = ( + ("min swap", MIN, SWAP, ALL, False), + ("add swap", ADD, SWAP, ALL, False), + ("max swap", MAX, SWAP, ALL, False), + ("mov asymmetric", None, ASYMMETRIC, ALL, False), + ("add sparse without FI", ADD, ASYMMETRIC, SPARSE, False), + # min(0, x) differs from the old destination, so it separates "zero" from "keep". + ("min sparse without FI", MIN, ASYMMETRIC, SPARSE, False), + ("add sparse with FI", ADD, ASYMMETRIC, SPARSE, True), + ) + sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 0x8A) + for name, op, selectors, active, fi in cases: + marker = write_request(work, program(op, selectors, active, fi), root, sa) + output = work / "out.bin" + run = subprocess.run([replay, sdk, marker, work / "cache", "0", output, "--direct"]) + if run.returncode == 77: + return 77 + run.check_returncode() + lanes = reference(op, selectors, active, fi) + expected = (SENTINEL,) * 8 + lanes * 2 + (SENTINEL,) * (2064 - 72) + actual = struct.unpack("<2064I", output.read_bytes()) + for index, (observed, wanted) in enumerate(zip(actual, expected)): + assert observed == wanted, (name, index, hex(observed), hex(wanted)) + print(f"{name}: 64 results and guards matched") + return 0 + + +def main(replay, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + shader = work / "root.hlsl" + shader.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + root = work / "root.bin" + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", + "-Fo", root, shader], check=True) + return hardware(replay, sdk, work, root.read_bytes()) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:])) diff --git a/tests/native_f16_alu_check.py b/tests/native_f16_alu_check.py index e511fc7e02..ab2f0d0d2a 100644 --- a/tests/native_f16_alu_check.py +++ b/tests/native_f16_alu_check.py @@ -306,7 +306,7 @@ def check_model(items): def refusal_programs(): add = vop2(0x32, 3, 257, 2) programs = [] - programs.append((vop2(0x32, 3, 233, 2) + [0xfac68801, 0xbf810000], 'SDWA or DPP vector operands')) + programs.append((vop2(0x32, 3, 233, 2) + [0xfac68801, 0xbf810000], 'unsupported v_add_f16 DPP form')) # DPP8 stays refused on binary16 # Clamp needs a wide encoding. A VOP3 with clamp and the DPP16 source selector is invalid. programs.append((vop3(0x32, 3, 250, 258, clamp=1) + [0xbf810000], 'unsupported v_add_f16 clamp or omod')) programs.append((vop3(0x35, 3, 257, 258, clamp=1) + [0xbf810000], 'unsupported v_mul_f16 clamp or omod'))