diff --git a/docs/experiments/native-salu64-ops.md b/docs/experiments/native-salu64-ops.md index ca5548927f..8f89595cfa 100644 --- a/docs/experiments/native-salu64-ops.md +++ b/docs/experiments/native-salu64-ops.md @@ -7,8 +7,10 @@ offset, writes both words of an aligned SGPR pair, and preserves SCC. `s_not_b64` complements both words and sets SCC when either result word 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. `s_bfm_b64` may also write EXEC, as both words, in wave32 -and wave64. `s_cselect_b64` and `s_not_b64` still refuse EXEC destinations. +write VCC as a pair. `s_bfm_b64` and `s_not_b64` may also write EXEC, as both +words, in wave32 and wave64. `s_cselect_b64` still refuses EXEC destinations. +`s_not_b64` and `s_cselect_b64` also take VCC and EXEC as 64-bit sources +(`tests/native_s_not_b64_exec_check.py`, `ctest --preset dev --no-tests=error -R '^native_s_not_b64_exec_'`). Direct compute also accepts `s_bitreplicate_b64_b32 exec, imm`, writing both EXEC words and preserving SCC. Wave32 lane activity uses only `exec_lo`. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 8fbcfee7a2..3fb10153fb 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -2912,25 +2912,29 @@ struct DirectCompute { return; } if (compute && (n == "s_cselect_b64" || n == "s_not_b64")) { - if ((i.dst & 1) || (i.dst > 104 && i.dst != 106)) + const bool invert = n == "s_not_b64"; + if ((i.dst & 1) || (i.dst > 104 && i.dst != 106 && !(invert && i.dst == 126))) refuse(i.pc, "invalid " + string(n) + " destination"); - auto high = [&](unsigned src) { - if (src == 106 || src == 126) + auto scalar64 = [&](unsigned src) -> std::pair { + if (!((src <= 104 && !(src & 1)) || src == 106 || src == 126 || + (src >= 128 && src <= 208) || src == 255)) refuse(i.pc, "invalid " + string(n) + " source"); - return pair_high(i, src); + return {operand32(i, src), pair_high(i, src)}; }; - auto lo = val(0), hi = high(i.src[0]); - if (n == "s_cselect_b64") { + auto [lo, hi] = scalar64(i.src[0]); + if (!invert) { + auto [other_lo, other_hi] = scalar64(i.src[1]); auto condition = cmp("ne", read(128), "0"); - lo = select(condition, lo, val(1)); - hi = select(condition, hi, high(i.src[1])); + lo = select(condition, lo, other_lo); + hi = select(condition, hi, other_hi); } else { lo = ir.binary("xor", lo, "4294967295"); hi = ir.binary("xor", hi, "4294967295"); - write(128, ir.value("zext i1 " + cmp("ne", ir.binary("or", lo, hi), "0") + " to i32")); } write(i.dst, lo); write(i.dst + 1, hi); + if (invert) + write(128, ir.value("zext i1 " + cmp("ne", ir.binary("or", lo, hi), "0") + " to i32")); return; } if (compute && (n == "s_lshl_b64" || n == "s_lshr_b64" || n == "s_ashr_i64")) { diff --git a/tests/cmake/native_s_not_b64_exec.cmake b/tests/cmake/native_s_not_b64_exec.cmake new file mode 100644 index 0000000000..21a73092cf --- /dev/null +++ b/tests/cmake/native_s_not_b64_exec.cmake @@ -0,0 +1,12 @@ +if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_s_not_b64_exec_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_s_not_b64_exec_check.py" controls + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_s_not_b64_exec_controls PROPERTIES TIMEOUT 60) + if(TARGET native_store_replay) + add_test(NAME native_s_not_b64_exec_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/native_s_not_b64_exec_check.py" hardware + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_s_not_b64_exec_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) + endif() +endif() diff --git a/tests/native_s_not_b64_exec_check.py b/tests/native_s_not_b64_exec_check.py new file mode 100644 index 0000000000..e3763bec27 --- /dev/null +++ b/tests/native_s_not_b64_exec_check.py @@ -0,0 +1,143 @@ +"""Compile and replay 64-bit NOT and CSELECT with the compute mask registers.""" +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from native_scalar_lds_check import scalar, sop1, vmove +from native_wave64_exec_salu_check import ( + END, EXEC, GROUP_STRIDE_WORDS, GUARD_WORDS, NEG_ONE, OUTPUT_WORDS, + SENTINEL, VCC, ZERO, compile_request, request, +) + + +NOT_B64, CSELECT_B64 = 0x08, 0x0B +MOV_B32, MOV_B64, LITERAL = 3, 4, 255 +M0, RESULT = 124, 24 +MASK32, MASK64 = (1 << 32) - 1, (1 << 64) - 1 +EXEC_MASK = 0xA5600FF00F0FF00F +VCC_MASK = 0x3150C4D2FFFFFFFF +PLANE_WORDS = 2 * GROUP_STRIDE_WORDS +CASES = ("not_exec", "not_vcc", "not_vcc_all_ones", "select_scc0", "select_scc1", "not_into_exec") + + +def move_pair(dst, bits): + return [sop1(MOV_B32, dst, LITERAL), bits & MASK32, + sop1(MOV_B32, dst + 1, LITERAL), bits >> 32] + + +def set_scc(value): + # s_cmp_eq_u32 0, 0 (SCC = 1) or s_cmp_eq_u32 0, 1 (SCC = 0). + return 0xBF060000 | (ZERO + (1 - value)) << 8 | ZERO + + +def store(vgpr, plane): + return [0xE0701000 | (4 * PLANE_WORDS * plane), 0x80080008 | vgpr << 8] + + +def program(case): + # Prepare every lane's address before narrowing EXEC. The shader uses + # the same two-group UAV layout as the wave64 EXEC SALU fixture. + words = [ + sop1(MOV_B64, EXEC, NEG_ONE), + sop1(MOV_B32, 20, 4), sop1(MOV_B32, 21, ZERO), + 0xF408080A, 0xFA000020, # s_load_dwordx4 s[32:35], s[20:21], 0x20 + scalar(0x1E, 7, 5, ZERO + 9), # s_lshl_b32 s7, s5, 9 + 0x34100082, # v_lshlrev_b32 v8, 2, v0 + 0x4A101007, # v_add_nc_u32 v8, s7, v8 + ] + words += move_pair(EXEC, EXEC_MASK) + words += move_pair(VCC, MASK64 if case == "not_vcc_all_ones" else VCC_MASK) + if case.startswith("select"): + words += [set_scc(int(case[-1])), scalar(CSELECT_B64, RESULT, EXEC, VCC)] + elif case == "not_into_exec": + words += [set_scc(0), sop1(NOT_B64, EXEC, EXEC), *store(0, 0)] + # Save both new EXEC words before restoring all lanes for readback. + words.append(sop1(MOV_B64, RESULT, EXEC)) + else: + source = EXEC if case == "not_exec" else VCC + words += [set_scc(int(case == "not_vcc_all_ones")), sop1(NOT_B64, RESULT, source)] + + words += [sop1(MOV_B64, EXEC, NEG_ONE), scalar(0x0A, RESULT + 2, ZERO + 1, ZERO)] + for plane, src in enumerate((RESULT, RESULT + 1, RESULT + 2), start=1): + words += [vmove(1, src), *store(1, plane)] + return words + [END] + + +def expected_words(wave_size, case): + if case.startswith("select"): + scc = int(case[-1]) + result = EXEC_MASK if scc else VCC_MASK + else: + source = {"not_exec": EXEC_MASK, "not_vcc": VCC_MASK, + "not_vcc_all_ones": MASK64, "not_into_exec": EXEC_MASK}[case] + result = source ^ MASK64 + scc = int(result != 0) + expected = [SENTINEL] * OUTPUT_WORDS + for group in range(2): + for lane in range(wave_size): + index = GUARD_WORDS + group * GROUP_STRIDE_WORDS + lane + if case == "not_into_exec" and result & (1 << lane): + expected[index] = lane + for plane, value in enumerate((result & MASK32, result >> 32, scc), start=1): + expected[index + plane * PLANE_WORDS] = value + return expected + + +def controls(cli, sdk, work, root): + cache = work / "cache" + failures = [] + for wave_size in (32, 64): + for case in CASES: + marker = request(work, root, wave_size, wave_size, program(case)) + output, result = compile_request(cli, sdk, cache, marker) + if result.returncode or not output.is_file(): + failures.append((wave_size, case, result.stdout, result.stderr)) + assert not failures, failures + + prefix = [sop1(MOV_B32, 23, ZERO), sop1(MOV_B32, M0, ZERO), set_scc(1)] + refusals = [ + (sop1(NOT_B64, RESULT, src), "invalid s_not_b64 source") + for src in (23, M0) + ] + [ + (scalar(CSELECT_B64, RESULT, a, b), "invalid s_cselect_b64 source") + for src in (23, M0) for a, b in ((src, ZERO), (ZERO, src)) + ] + [(scalar(CSELECT_B64, EXEC, ZERO, ZERO), "invalid s_cselect_b64 destination")] + for instruction, error in refusals: + marker = request(work, root, 64, 64, prefix + [instruction, END]) + output, result = compile_request(cli, sdk, cache, marker) + assert result.returncode and error in result.stderr, (error, result.stdout, result.stderr) + assert not output.exists(), output + print("Wave32 and wave64 compile controls and invalid operand refusals passed") + return 0 + + +def hardware(replay, sdk, work, root): + for wave_size in (32, 64): + for case in CASES: + marker = request(work, root, wave_size, wave_size, program(case)) + output = work / f"wave{wave_size}_{case}.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()) + for index, (got, want) in enumerate(zip(actual, expected_words(wave_size, case))): + assert got == want, (wave_size, case, index, hex(got), hex(want)) + print(f"wave{wave_size} {case}: both groups and guards matched") + return 0 + + +def main(mode, tool, sdk): + with tempfile.TemporaryDirectory() as temp: + work = Path(temp) + shader, root_file = work / "root.hlsl", work / "root.bin" + shader.write_text('#define SIG "DescriptorTable(UAV(u0,numDescriptors=8))"') + subprocess.run([Path(sdk) / "dxc.exe", "-T", "rootsig_1_1", "-E", "SIG", + "-Fo", root_file, shader], check=True) + return {"controls": controls, "hardware": hardware}[mode](tool, sdk, work, root_file.read_bytes()) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:])) diff --git a/tests/native_salu64_check.py b/tests/native_salu64_check.py index 25c52f8381..41a94d07c4 100644 --- a/tests/native_salu64_check.py +++ b/tests/native_salu64_check.py @@ -47,7 +47,7 @@ def compute_program(instructions, dst): def controls(cli, sdk, work, root): for words, error in (([scalar(CSELECT, 126, NEG_ONE, ZERO), 0xBF810000], "invalid s_cselect_b64 destination"), - ([sop1(NOT, 24, 106), 0xBF810000], "invalid s_not_b64 source")): + ([sop1(NOT, 24, 127), 0xBF810000], "invalid s_not_b64 source")): marker = request(work, root, words) output = marker.with_suffix(".cso") result = subprocess.run([cli, sdk, work / "cache", "gfx1013-direct-cs@0", marker, output],