diff --git a/docs/experiments/native-salu64-ops.md b/docs/experiments/native-salu64-ops.md index a48b40ad1d..af2388ef64 100644 --- a/docs/experiments/native-salu64-ops.md +++ b/docs/experiments/native-salu64-ops.md @@ -9,6 +9,8 @@ is nonzero. Both take aligned SGPR pairs or constants, with sign extension for inline negatives and zero extension for other integers. They can write VCC as a pair. EXEC destinations are refused. +Direct compute also accepts `s_bitreplicate_b64_b32 exec, imm`, writing both EXEC words and preserving SCC. Wave32 lane activity uses only `exec_lo`. + ## Verification `native_salu64_controls` checks that compiling `s_cselect_b64 exec, -1, 0` diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index fb6e462aea..a57267dfcf 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -2383,8 +2383,8 @@ struct DirectCompute { return; } if (n == "s_bitreplicate_b64_b32") { - if ((i.dst > 104 && (graphics_input || i.dst != 106)) || (i.dst & 1)) - refuse(i.pc, "bitreplicate requires an aligned general scalar pair or compute VCC"); + if ((i.dst > 104 && (graphics_input || (i.dst != 106 && i.dst != 126))) || (i.dst & 1)) + refuse(i.pc, "bitreplicate requires an aligned general scalar pair or compute VCC/EXEC"); // pixel_aot expands each 16-bit half into the even positions, // then duplicates each of the original bits into its neighbour. auto replicate = [&](string half) { diff --git a/tests/cmake/native_bitreplicate_exec.cmake b/tests/cmake/native_bitreplicate_exec.cmake new file mode 100644 index 0000000000..371e7a3483 --- /dev/null +++ b/tests/cmake/native_bitreplicate_exec.cmake @@ -0,0 +1,10 @@ +if(X2W_HAVE_DXCAPI AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_bitreplicate_exec_controls + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_bitreplicate_exec_check.py" + controls "$" "${DXC_BIN_DIR}") + set_tests_properties(native_bitreplicate_exec_controls PROPERTIES TIMEOUT 60) + add_test(NAME native_bitreplicate_exec_hardware + COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_bitreplicate_exec_check.py" + hardware "$" "${DXC_BIN_DIR}") + set_tests_properties(native_bitreplicate_exec_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_bitreplicate_exec_check.py b/tests/native_bitreplicate_exec_check.py new file mode 100644 index 0000000000..2adc28ee62 --- /dev/null +++ b/tests/native_bitreplicate_exec_check.py @@ -0,0 +1,109 @@ +"""Direct compute wave32 EXEC bitreplicate readback.""" +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 + + +BITREPLICATE = 0x3B +MOV_B32 = 3 +EXEC_LO, EXEC_HI = 126, 127 +LITERAL = 255 +THREADS = 32 +LANES = 2 * THREADS +GUARD_WORDS = 8 +OUTPUT_WORDS = 2064 +SENTINEL = 0x69696969 +CASES = ( + (0xFF00FF00, LITERAL), + (0x0000FFFF, LITERAL), + (5, 128 + 5), + (0xF0F0F0F0, LITERAL), +) + + +def program(value, source, dst=EXEC_LO): + # Table pointer s4, group.x s5. v8 addresses one dword per dispatch lane. + words = [ + sop1(MOV_B32, 20, 4), sop1(MOV_B32, 21, 128), + 0xF408080A, 0xFA000020, # s_load_dwordx4 s[8:11], s[20:21], 32 + scalar(0x1E, 7, 5, 135), # group.x * 128 + vop2(0x1A, 8, 130, 0), vop2(0x25, 8, 7, 8), # v8 = group base + local.x * 4 + 0xBF8CC07F, + 0xBF068180, # s_cmp_eq_u32 0, 1 (SCC = 0) + sop1(BITREPLICATE, dst, source), + ] + if source == LITERAL: + words.append(value) + words += [ + 0xE0701000, 0x80080008, # store local.x from v0 under replicated EXEC + sop1(MOV_B32, 6, EXEC_LO), + sop1(MOV_B32, 12, EXEC_HI), + sop1(MOV_B32, EXEC_LO, 193), # restore all 32 lanes + ] + for plane, reg in enumerate((6, 12, 253), start=1): + # Store the saved low word, saved high word and incoming SCC on every lane. + offset = plane * LANES * 4 + words += [vmove(4, reg), 0xE0701000 | offset, 0x80080408] + return words + [0xBF810000] + + +def request(work, root, words): + sa = struct.pack("<7I", 0, 0, 0, THREADS | (1 << 16), 1 | (1 << 20), 0, 0x8A) + return write_request(work, words, root, sa) + + +def reference(value): + lo, hi = (sum(3 << (2 * bit) for bit in range(16) if value & (1 << (bit + start))) + for start in (0, 16)) + masked = tuple(lane if lo & (1 << lane) else SENTINEL for lane in range(THREADS)) + return ((SENTINEL,) * GUARD_WORDS + masked * 2 + (lo,) * LANES + (hi,) * LANES + + (0,) * LANES + (SENTINEL,) * (OUTPUT_WORDS - GUARD_WORDS - 4 * LANES)) + + +def controls(cli, sdk, work, root): + value, source = CASES[0] + marker = request(work, root, program(value, source, dst=9)) + output = marker.with_suffix(".cso") + result = subprocess.run([cli, sdk, work / "cache", "gfx1013-direct-cs@0", marker, output], + capture_output=True, text=True) + assert result.returncode != 0 and "bitreplicate requires an aligned general scalar pair" in result.stderr, ( + result.stdout, result.stderr) + assert not output.exists(), output + return 0 + + +def hardware(replay, sdk, work, root): + for value, source in CASES: + marker = request(work, root, program(value, source)) + output = work / "out.bin" + result = subprocess.run([replay, 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()) + expected = reference(value) + for word, (got, want) in enumerate(zip(actual, expected)): + assert got == want, f"{value:#010x}, word {word}: got {got:#010x}, expected {want:#010x}" + print(f"{value:#010x}: lane stores, saved EXEC, SCC and guards matched") + return 0 + + +def main(mode, tool, 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 {"controls": controls, "hardware": hardware}[mode](tool, sdk, work, root.read_bytes()) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:]))