Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/experiments/native-sdwa-f16.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct Inst {
}
unsigned target() const { return next + 4 * static_cast<int16_t>(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;
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/cmake/native_sdwa_vcc_hi.cmake
Original file line number Diff line number Diff line change
@@ -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" "$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_sdwa_vcc_hi_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
8 changes: 8 additions & 0 deletions tests/native_direct_container_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
52 changes: 52 additions & 0 deletions tests/native_sdwa_vcc_hi_check.py
Original file line number Diff line number Diff line change
@@ -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:])
Loading