From 996b3f7715979d22ae0c3e475a7463de78d57a66 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:40:17 +0100 Subject: [PATCH] compute: accept vcc_hi as the wave32 mask of SDWA compares In the direct wave32 compute path a VOPC mask is one SGPR, so an SDWA compare or v_cmp_class may write it to vcc_hi (s107). The decoder capped the destination at vcc_lo. A wave64 container stage, graphics, v_cmpx with an SGPR destination and anything above s107 stay refused. native_sdwa_vcc_hi_hardware writes both masks to vcc_hi in two groups, reads vcc_hi back as the scalar source of a second compare, and checks both per lane against a CPU reference; native_direct_container checks the wave64 container, s108 and v_cmpx refusals. AI-assisted. --- docs/experiments/native-sdwa-f16.md | 2 + src/gfx1013_compute_native.cpp | 9 +++-- tests/cmake/native_sdwa_vcc_hi.cmake | 6 +++ tests/native_direct_container_check.py | 8 ++++ tests/native_sdwa_vcc_hi_check.py | 52 ++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/cmake/native_sdwa_vcc_hi.cmake create mode 100644 tests/native_sdwa_vcc_hi_check.py diff --git a/docs/experiments/native-sdwa-f16.md b/docs/experiments/native-sdwa-f16.md index e04c273a96..29ca0a13b7 100644 --- a/docs/experiments/native-sdwa-f16.md +++ b/docs/experiments/native-sdwa-f16.md @@ -12,6 +12,8 @@ and UNUSED_PRESERVE, which copies it from the old destination. DWORD and byte destinations, UNUSED_SEXT, byte sources, source sext, scalar and constant sources, clamp, and output modifiers are refused. SDWA min and max remain refused. +Direct wave32 compute SDWA VOPC compares can write a 32-bit mask to `vcc_hi` and read it as a scalar source. + ## Fixture The fixture compares the IR and hardware results against the same exact binary16 diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 4a2f8949c7..adf3c5df09 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -87,7 +87,7 @@ struct Inst { } unsigned target() const { return next + 4 * static_cast(a & 65535); } }; -Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=false) { +Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=false, bool wave64=false) { const auto encoded=gfx1013::instruction(bytes,pc); Inst i; i.pc = pc; @@ -270,6 +270,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa 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; + const unsigned sdwa_mask_limit = direct && !graphics && !wave64 ? 107 : 106; if (direct && i.src[0] == 249 && (sdwa_compare || i.enc == Encoding::Vop1 || i.enc == Encoding::Vop2)) { // Operand profiles from disasm_rdna2's GFX10 VOP1/VOP2 SDWA // decoder. Admit only profiles with a direct result placement. @@ -293,7 +294,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa const Word ext = take(); const unsigned sources = i.enc == Encoding::Vop1 ? 1 : 2; if (sdwa_compare) { - if ((ext & 0x8000) && (name.substr(0,7) == "v_cmpx_" || ((ext >> 8) & 127) > 106)) + if ((ext & 0x8000) && (name.substr(0,7) == "v_cmpx_" || ((ext >> 8) & 127) > sdwa_mask_limit)) refuse(pc, "invalid SDWA compare destination"); i.dst = ext & 0x8000 ? (ext >> 8) & 127 : 106; } else { @@ -339,7 +340,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa } else { // VOPC has a scalar mask destination in the same bits that VOP2 uses for placement. i.dst = ext & 0x8000 ? (ext >> 8) & 127 : 106; - if ((ext & 0x70480000u) || (i.dst > 105 && i.dst != 106)) + if ((ext & 0x70480000u) || i.dst > sdwa_mask_limit) refuse(pc, "invalid SDWA class modifiers or mask destination"); i.neg = (ext >> 20) & 1; i.abs = (ext >> 21) & 1; @@ -664,7 +665,7 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra if (pc >= code.size() || (next != instructions.begin() && std::prev(next)->second.next > pc)) refuse(pc, "branch outside code or into an instruction"); const auto fetch = fixed.find(pc); - auto instruction = fetch != fixed.end() ? fetch->second : decode(code, pc,true,graphics); + auto instruction = fetch != fixed.end() ? fetch->second : decode(code, pc,true,graphics,wave64); if (next != instructions.end() && instruction.next > next->first) refuse(pc, "overlapping instruction boundaries"); instructions.emplace_hint(next, pc, instruction); diff --git a/tests/cmake/native_sdwa_vcc_hi.cmake b/tests/cmake/native_sdwa_vcc_hi.cmake new file mode 100644 index 0000000000..d1c10a6fcf --- /dev/null +++ b/tests/cmake/native_sdwa_vcc_hi.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_sdwa_vcc_hi_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_sdwa_vcc_hi_check.py" "$" "${DXC_BIN_DIR}") + set_tests_properties(native_sdwa_vcc_hi_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_direct_container_check.py b/tests/native_direct_container_check.py index 205d647076..b0aeaf4d52 100644 --- a/tests/native_direct_container_check.py +++ b/tests/native_direct_container_check.py @@ -397,6 +397,14 @@ def atomic_program(instructions): check("compare-select-subword", cs, compute(words(*cmp_sdwa, *choose_byte, end))) check("cmpx-explicit-destination", cs, compute(words(cmp_sdwa[0] | (0x10 << 17), cmp_sdwa[1] | 0x8000, end)), "invalid SDWA compare destination") + for name, opcode, ext, error in ( + ("class", 0x7d100af9, 0x0396eb80, "invalid SDWA class modifiers or mask destination"), + ("ne", 0x7d0a0cf9, 0x0086eb6b, "invalid SDWA compare destination"), + ): + check(f"sdwa-{name}-vcc-hi-wave64", cs, compute(words(opcode, ext, end), 64, wave32=False), error) + check(f"sdwa-{name}-sdst-108", cs, compute(words(opcode, (ext & ~0x7f00) | (108 << 8), end)), error) + check("sdwa-cmpx-vcc-hi", cs, compute(words(0x7d2a0cf9, 0x0086eb6b, end)), + "invalid SDWA compare destination") check("atomic-x2", cs, atomic_program((0xe1445000, 0x80020000)), "unsupported buffer atomic width") check("bad-container", cs, compute(ds)[:-1], "invalid DXBC length") diff --git a/tests/native_sdwa_vcc_hi_check.py b/tests/native_sdwa_vcc_hi_check.py new file mode 100644 index 0000000000..4ddb51ce72 --- /dev/null +++ b/tests/native_sdwa_vcc_hi_check.py @@ -0,0 +1,52 @@ +"""Direct wave32 SDWA VOPC compares that write their lane mask to vcc_hi and read it back as a scalar source.""" +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 sdwa_vcc_hi_readback(work, gpu, sdk): + # Each lane owns two dwords. The negated zero class bit is present only in lane zero. + words = [sop1(3, 20, 4), sop1(3, 21, 128), 0xf408080a, 0xfa000020, + scalar(0x1e, 7, 5, 136), # group byte offset = group.x << 8 + 0x1a << 25 | 8 << 17 | 131, # v8 = local.x << 3 + 0x25 << 25 | 8 << 17 | 8 << 9 | 7, # v8 += group byte offset + vmove(5, 255), 0x20000000, + 0x16 << 25 | 5 << 17 | 5 << 9 | 256, # v5 >>= local.x + 0x7d100af9, 0x0396eb80, # class(-0, BYTE_3(v5)) -> vcc_hi + sop1(3, 14, 107), vmove(9, 14), 0xe0701000, 0x80080908, + 0x1c << 25 | 6 << 17 | 255, 0x100, # v6 = local.x | 0x100 + 0x7d0a0cf9, 0x0086eb6b, # ne(vcc_hi, BYTE_0(v6)) -> vcc_hi + sop1(3, 14, 107), vmove(9, 14), 0xe0701004, 0x80080908, + 0xbf810000] + sa = struct.pack("<7I", 0, 0, 0, 32 | 1 << 16, 1 | 1 << 20, 0, 0x80 | 5 << 1) # bit 20: wave32 (#2999) + marker = write_request(work, words, (work / "root.bin").read_bytes(), sa) + dest = work / "sdwa-vcc-hi.bin" + result = subprocess.run([gpu, sdk, marker, work / "cache", "0", dest, "--direct"]) + if result.returncode == 77: + sys.exit(77) + result.check_returncode() + class_mask = sum(1 << lane for lane in range(32) if ((0x20000000 >> lane) >> 24) & (1 << 5)) + ne_mask = sum(1 << lane for lane in range(32) if class_mask != ((lane | 0x100) & 0xff)) + expected = [0x69696969] * 2064 + expected[8:136] = [class_mask, ne_mask] * 64 + actual = struct.unpack("<2064I", dest.read_bytes()) + assert list(actual) == expected, [(i, a, b) for i, (a, b) in enumerate(zip(actual, expected)) if a != b][:10] + print("Both wave32 SDWA vcc_hi masks matched in every lane and all guards held.") + + +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) + sdwa_vcc_hi_readback(work, gpu, sdk) + + +if __name__ == "__main__": + main(*sys.argv[1:])