diff --git a/docs/experiments.md b/docs/experiments.md index b15306833d..1ca84c2866 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -655,7 +655,7 @@ python3 -B tests/startup_buffer_address_check.py controls ## Startup translator: SMEM combined register and immediate offsets -Direct C++ wave32 compute accepts numeric `vcc_lo` and `vcc_hi` as SMEM offsets and scalar load destinations through s107. +Direct C++ compute, in wave32 and wave64, accepts numeric `vcc_lo` and `vcc_hi` as SMEM offsets and scalar load destinations through s107. In RDNA 2 SMEM instructions (`s_load_dword*` and `s_buffer_load_dword*`), a scalar register `SOFFSET` (`s0`..`s105` or `m0`) and a non-zero 21-bit signed immediate `OFFSET` (`w1[20:0]`) can be active at the same time: [`tools/disasm_rdna2.py`](../tools/disasm_rdna2.py) prints the register in `ops[2]` and the signed immediate in `mods` (`offset:0x...` or `offset:-0x...`). Previously, `Stage.op_s_buffer_load_dword()` and `Stage.op_s_load_dwordx4()` in [`tools/startup/gfx2hlsl.py`](../tools/startup/gfx2hlsl.py) ignored `mods`, silently dropping `offset:0x...` on register-addressed buffer and dynamic sampler-heap loads, accepting unknown modifiers, reading negative 21-bit signed encodings (`0x100000`..`0x1fffff`) as `+1`..`2` MiB, and overwriting a runtime SGPR `soffset` mid-load when a multi-dword `s_buffer_load_dwordx*` wrote into that register first. diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index 1f394106b0..2367074ae8 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -90,8 +90,8 @@ struct Inst { unsigned target() const { return next + 4 * static_cast(a & 65535); } }; unsigned memory_count(const Inst &i) { return 1u << (i.op & 7); } -// Wave32 compute can use both VCC words as scalar data. -unsigned smem_sgpr_limit(bool wave32_compute) { return wave32_compute ? 108 : 106; } +// Direct compute stores both VCC words in scalar registers. +unsigned smem_sgpr_limit(bool direct_compute) { return direct_compute ? 108 : 106; } Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=false, bool wave64=false) { const auto encoded=gfx1013::instruction(bytes,pc); @@ -165,7 +165,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.dst = (a >> 6) & 127; i.src[0] = (a & 63) * 2; const unsigned soffset = i.b >> 25; - const unsigned limit = smem_sgpr_limit(direct && !graphics && !wave64); + const unsigned limit = smem_sgpr_limit(direct && !graphics); if ((i.b & 0x01f00000u) || (a & 0x14000) || (i.op & ~8u) > 4 || (soffset != 125 && ((!direct && i.op < 8 && i.op != 2) || soffset >= limit))) refuse(pc, "unsupported scalar memory addressing"); @@ -679,7 +679,7 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win } else if (i.enc == Encoding::Smem) { auto base = s[i.src[0]]; const unsigned offset_reg = i.b >> 25; - const Symbol index = offset_reg < smem_sgpr_limit(!graphics && !wave64) ? s[offset_reg] : Symbol{}; + const Symbol index = offset_reg < smem_sgpr_limit(direct && !graphics) ? s[offset_reg] : Symbol{}; unsigned n = memory_count(i); const bool heap_address = i.op == 2 && i.b >> 25 == 125 && heap_offset_address(s, i.src[0]); for (unsigned j = 0; j < n; ++j) { diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 353e6b1619..2895cd9caf 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -538,9 +538,15 @@ struct DirectCompute { } if (i.enc == Encoding::Smem) { unsigned so = i.b >> 25; - const unsigned limit = smem_sgpr_limit(!graphics_input && !wave64); + const unsigned limit = smem_sgpr_limit(!graphics_input); const auto &low = s.at(i.src[0]); const bool root = low.type == Symbol::Type::root_address; + // Table and heap-offset descriptor lowering track identities and slot indices instead of + // loading payload bits. + if (wave64 && !graphics_input && i.op < 8 && + (low.type == Symbol::Type::table || low.type == Symbol::Type::heap_offset) && + i.dst + memory_count(i) > 106) + refuse(i.pc, "symbolic scalar memory destination overlaps wave64 VCC"); if (low.type == Symbol::Type::heap_offset) { // A resource-heap address formed by s_add_u32 (heap_offset_add): the same slot read as the SGPR-offset // load below, with the byte offset in the low word. diff --git a/tests/cmake/native_smem_vcc_wave64.cmake b/tests/cmake/native_smem_vcc_wave64.cmake new file mode 100644 index 0000000000..35004dffa2 --- /dev/null +++ b/tests/cmake/native_smem_vcc_wave64.cmake @@ -0,0 +1,14 @@ +if(X2W_HAVE_DXCAPI AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_smem_vcc_wave64_controls + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_smem_vcc_wave64_check.py" + controls "$" "${DXC_BIN_DIR}") + set_tests_properties(native_smem_vcc_wave64_controls PROPERTIES TIMEOUT 60) +endif() + +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_smem_vcc_wave64_hardware + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_smem_vcc_wave64_check.py" + hardware "$" "${DXC_BIN_DIR}") + set_tests_properties(native_smem_vcc_wave64_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 97af8e7f33..af6dc525e4 100644 --- a/tests/native_direct_container_check.py +++ b/tests/native_direct_container_check.py @@ -451,13 +451,10 @@ def atomic_program(instructions): root_vs = vertex(constants=(0,2,1), program=[*root_load, 0x7e00020f, 0xf80008cf, 0, end]) check("address-vs", "gfx1013-container-vs", root_vs) check("address-cs", cs, compute(words(*root_load, end), root_descriptor=True)) - for offset in (106, 107): - check(f"wave64-smem-offset-{offset}", cs, - compute(words(0xf4000200, offset << 25, end), threads=64, - root_descriptor=True, wave32=False), "unsupported scalar memory addressing") - check("wave64-smem-vcc-dst", cs, - compute(words(0xf4000000 | 107 << 6, 125 << 25, end), threads=64, - root_descriptor=True, wave32=False), "unsupported scalar memory destination range") + check("wave64-smem-symbolic-vcc-dst", cs, + compute(words(0xbe810380, 0xf4080000 | 104 << 6, 125 << 25, end), + threads=64, table=True, wave32=False), + "symbolic scalar memory destination overlaps wave64 VCC") check("wave32-smem-cross-vcc", cs, compute(words(0xf40c0000 | 104 << 6, 125 << 25, end), root_descriptor=True), "unsupported scalar memory destination range") diff --git a/tests/native_heap_offset_add_check.py b/tests/native_heap_offset_add_check.py index 728b619414..4d5f8978b2 100644 --- a/tests/native_heap_offset_add_check.py +++ b/tests/native_heap_offset_add_check.py @@ -40,6 +40,10 @@ "scc_clobbered": (SELECT + [ADD_LO, 0xBF070808, ADD_HI] + LOAD + END, HIGH_WORD), # s_add_u32 s10, s5, 0x40: a constant offset is not a GPU selection; the heap table is used as a number "constant": (SELECT + [0x800AFF05, 0x40, ADD_HI] + LOAD + END, "unbound numeric SGPR s5"), + # Wave64, s_load_dwordx4 s[104:107], s[10:11], 0x0: the slot index would be written into VCC, which the wave64 + # model keeps as mask data, so the overlap refusal comes before the heap contract. + "wave64_vcc_dst": (SELECT + [ADD_LO, ADD_HI, 0xF4081A05, 0xFA000000] + END, + "symbolic scalar memory destination overlaps wave64 VCC", 64), } @@ -53,9 +57,10 @@ def main(compile_dxil, sdk): subprocess.run([pathlib.Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "RS", "-Fo", binary, source], check=True, capture_output=True) root = binary.read_bytes() - # Group 32x1x1, wave32 (bit 20 of dword 4), 7 user SGPRs. - sa = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 7 << 1) - for name, (program, expected) in PROGRAMS.items(): + for name, (program, expected, *wave) in PROGRAMS.items(): + # Group 32x1x1 in wave32 (bit 20 of dword 4), or 64x1x1 in wave64, 7 user SGPRs. + threads = wave[0] if wave else 32 + sa = struct.pack("<7I", 0, 0, 0, threads | (1 << 16), 1 | (0 if wave else 1 << 20), 0, 7 << 1) folder = base / name folder.mkdir() marker = write_request(folder, program, root, sa) diff --git a/tests/native_smem_vcc_wave64_check.py b/tests/native_smem_vcc_wave64_check.py new file mode 100644 index 0000000000..061db04b09 --- /dev/null +++ b/tests/native_smem_vcc_wave64_check.py @@ -0,0 +1,121 @@ +"""Compile and replay direct wave64 scalar loads that use VCC as scalar data.""" +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 + + +THREADS = 64 +LANE_WORDS = 16 +GUARD_WORDS = 8 +OUTPUT_WORDS = 2064 +SENTINEL = 0x69696969 +INPUT_BASE = 0x51AA0000 +EXEC_LO, EXEC_HI = 0x5A5AA5A5, 0xA5A55A5A + + +def program(first_dst=104): + # Use the same root SRV, synthesized byte descriptor and UAV slot as the wave32 fixture. + words = [ + sop1(4, 126, 193), # s_mov_b64 exec, -1 + sop1(4, 16, 4), sop1(3, 18, 255), 132, sop1(3, 19, 255), 0x31016FAC, + sop1(3, 20, 6), sop1(3, 21, 128), 0xF4080000 | 40 << 6 | 10, 0xFA000020, + 0xF4280008 | first_dst << 6, 0xFA000000, # s_buffer_load_dwordx4 s[104:107], s[16:19], 0 + 0xBF8CC07F, + scalar(0x1E, 12, 7, 140), # group.x * 4096 bytes + vop2(0x1A, 8, 134, 0), vop2(0x25, 8, 12, 8), # group byte base + local.x * 64 + sop1(3, 30, 106), # s_mov_b32 s30, vcc_lo + ] + + def store(reg, slot): + return [vmove(9, reg), 0xE0701000 | slot * 4, 0x800A0908] + + for slot, reg in enumerate((104, 105, 30, 107)): + words += store(reg, slot) + words += [ + 0xF40C0002 | 100 << 6, 0xFA000050, # s_load_dwordx8 s[100:107], s[4:5], 80 + 0xBF8CC07F, sop1(3, 30, 107), # s_mov_b32 s30, vcc_hi + ] + for slot, reg in enumerate((*range(100, 107), 30), start=4): + words += store(reg, slot) + words += [ + sop1(3, 106, 132), sop1(3, 107, 136), # scalar byte offsets 4 and 8 + 0xF4200008 | 30 << 6, 106 << 25 | 24, # s_buffer_load_dword s30, s[16:19], vcc_lo + 24 + 0xF4000002 | 31 << 6, 107 << 25 | 32, # s_load_dword s31, s[4:5], vcc_hi + 32 + 0xBF8CC07F, + *store(30, 12), *store(31, 13), + sop1(3, 126, 255), EXEC_LO, sop1(3, 127, 255), EXEC_HI, + 0xF4040002 | 106 << 6, 0xFA000014, # s_load_dwordx2 vcc, s[4:5], 20 + 0xBF8CC07F, + vmove(1, 129), vop2(1, 9, 128, 1), # v_cndmask_b32 v9, 0, v1, vcc + 0xE0701000 | 14 * 4, 0x800A0908, # EXEC-inactive lanes retain the sentinel + 0xBF810000, + ] + return words + + +def request(work, root, first_dst): + # .sa dword 4 bit 20 is clear, so each 64-thread group is one guest wave64. + sa = struct.pack("<7I", 0, 0, 0, THREADS | 1 << 16, 1, 0, 0x80 | 7 << 1) + return write_request(work, program(first_dst), root, sa) + + +def reference(): + expected = [SENTINEL] * OUTPUT_WORDS + vcc = (INPUT_BASE + 5) | ((INPUT_BASE + 6) << 32) + exec_mask = EXEC_LO | (EXEC_HI << 32) + loaded = [INPUT_BASE + index for index in (*range(4), *range(20, 28), 7, 10)] + for group in range(2): + for lane in range(THREADS): + pos = GUARD_WORDS + (group * THREADS + lane) * LANE_WORDS + expected[pos:pos + len(loaded)] = loaded + if exec_mask & (1 << lane): + expected[pos + 14] = (vcc >> lane) & 1 + return expected + + +def controls(tool, sdk, work, root): + for dst, error in ((104, ""), (105, "unsupported scalar memory destination range")): + marker = request(work, root, dst) + output = marker.with_suffix(".cso") + result = subprocess.run([tool, sdk, work / "cache", "gfx1013-direct-cs@0", marker, output], + capture_output=True, text=True) + if error: + assert result.returncode != 0 and error in result.stderr, (result.stdout, result.stderr) + assert not output.exists(), output + else: + assert result.returncode == 0 and output.exists(), (result.stdout, result.stderr) + return 0 + + +def hardware(tool, sdk, work, root): + marker = request(work, root, 104) + output = work / "out.bin" + result = subprocess.run([tool, sdk, marker, work / "cache", "0", output, "--direct"]) + if result.returncode == 77: + return 77 + result.check_returncode() + actual = struct.unpack(f"<{OUTPUT_WORDS}I", output.read_bytes()) + for word, (got, want) in enumerate(zip(actual, reference())): + assert got == want, f"word {word}: got {got:#010x}, expected {want:#010x}" + print("Wave64 SMEM scalar data, both VCC mask halves, and all guards matched.") + return 0 + + +def main(mode, tool, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + source, root = work / "built.hlsl", work / "built.root" + source.write_text('#define SIG "SRV(t0),DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", + "-Fo", root, source], check=True) + return {"controls": controls, "hardware": hardware}[mode](tool, sdk, work, root.read_bytes()) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:]))