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
7 changes: 5 additions & 2 deletions docs/experiments/native-direct-salu-bitscan64.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using 32-bit DXIL `unaryBits.i32` (`31` `Countbits`, `32` `FirstbitLo`, `33` `Fi
`tools/startup/gfx2hlsl.py`:

- **Destination:** general 32-bit SGPR (`s0..s105`), leaving `s[dst + 1]` untouched.
- **Source `S0`:** aligned SGPR pair (`s0..s104` even), inline integer (`-16..64`, sign-extended when negative), or
- **Source `S0`:** aligned SGPR pair (`s0..s104` even), EXEC (`exec_lo`/`exec_hi`, both words, in wave32 and wave64), inline integer (`-16..64`, sign-extended when negative), or
32-bit literal (sign-extended for `s_flbit_i32_i64`, zero-extended for `_b64`).
- **Semantics & `SCC`:** `s_bcnt0_i32_b64` and `s_bcnt1_i32_b64` sum 32-bit `Countbits` over both halves and write
`SCC = (D != 0)`. `s_ff0_i32_b64`, `s_ff1_i32_b64`, `s_flbit_i32_b64` and `s_flbit_i32_i64` preserve `SCC` and return
Expand All @@ -23,8 +23,11 @@ Encodings were verified with LLVM 15.0.7 (`clang -cc1as -triple amdgcn -target-c

- **Controls:** compiles all 6 ops and checks `dxc.exe -dumpbin` for `@dx.op.unaryBits.i32` opcodes `31`, `32`, `33`
with no `i64` shifts; tests `s_ff1_i32_b64 s31, s[22:23]` right below `s[32:35]` to verify `s32` stays intact;
checks 6 invalid destination/source refusals (`vcc`/`exec` dst, odd/`vcc`/`exec`/float `S0`); checks 6 mutant
checks 6 invalid destination/source refusals (`vcc`/`exec` dst, odd/`vcc`/`exec_hi`/float `S0`; since EXEC became a valid `S0`, the source control uses the odd `exec_hi`); checks 6 mutant
references differ.
- **EXEC as `S0`:** read back with `s_bcnt1_i32_b64` by `tests/native_wave64_exec_salu_check.py`, including masks that
span both host halves of a wave64 (`ctest --preset dev --no-tests=error -R '^native_wave64_exec_salu_hardware$' -V`).
The other five ops accept EXEC through the same source check but are not run with it.
- **Hardware cases:** all 6 ops across `(0, 0)`, `(0xffffffff, 0xffffffff)`, `(0, 1)`, `(0x80000000, 0)`,
`(0xfffffffe, 0xffffffff)`, `(0, 0x80000000)`, `(1, 0)`; inline `-1`; 32-bit literal `0x80000001`; explicit `SCC`
preservation (`0` and `1`) across count-then-scan sequences; overlapping `s22 = ff1(s[22:23])`.
Expand Down
6 changes: 5 additions & 1 deletion docs/experiments/native-salu64-ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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. EXEC destinations are refused.
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.

Direct compute also accepts `s_bitreplicate_b64_b32 exec, imm`, writing both EXEC words and preserving SCC. Wave32 lane activity uses only `exec_lo`.

Expand All @@ -21,10 +22,13 @@ the raw width 64 wrapping to 0. The select cases cover both SCC values,
an SGPR pair, inline -1 and the literal `0x80000001`. The complement
cases include zero output and output with only the high word nonzero.
Hardware without shader model 6.6 and wave32 support returns skip code 77.
`native_wave64_exec_salu_hardware` covers `s_bfm_b64 exec, 16, 0` and `exec, 32, 32` in wave64, 64- and
128-thread groups (the wave32 EXEC destination is accepted but not run), checking which lanes stay active through a per-lane store.

```powershell
cmake --build --preset dev --target compile_dxil native_store_replay
ctest --preset dev --no-tests=error -R '^native_salu64_(controls|hardware)$'
ctest --preset dev --no-tests=error -R '^native_wave64_exec_salu_hardware$'
```

Results: on an NVIDIA GeForce RTX 5080 (Windows 11, DXC from the Windows SDK 10.0.26100) all fifteen cases
Expand Down
4 changes: 2 additions & 2 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2775,7 +2775,7 @@ struct DirectCompute {
return;
}
if (compute && n == "s_bfm_b64") {
if ((i.dst & 1) || i.dst > 104)
if ((i.dst & 1) || (i.dst > 104 && i.dst != 126))
refuse(i.pc, "invalid s_bfm_b64 destination");
if ((i.src[0] > 105 && !constant32(i, i.src[0])) ||
(i.src[1] > 105 && !constant32(i, i.src[1])))
Expand Down Expand Up @@ -2940,7 +2940,7 @@ struct DirectCompute {
if (i.dst > 105)
refuse(i.pc, "invalid " + string(n) + " destination");
const unsigned s0 = i.src[0];
const bool s0_pair = s0 <= 104 && !(s0 & 1);
const bool s0_pair = (s0 <= 104 && !(s0 & 1)) || s0 == 126;
const bool s0_int = s0 >= 128 && s0 <= 208;
if (!(s0_pair || s0_int || s0 == 255))
refuse(i.pc, "invalid " + string(n) + " source");
Expand Down
7 changes: 7 additions & 0 deletions tests/cmake/native_wave64_exec_salu.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(TARGET compile_dxil AND TARGET native_store_replay AND
EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME native_wave64_exec_salu_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_wave64_exec_salu_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_wave64_exec_salu_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
2 changes: 1 addition & 1 deletion tests/native_salu_bitscan64_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def controls(cli, sdk, work, root):
([sop1(FF1_64, EXEC, 22), 0xBF810000], 'invalid s_ff1_i32_b64 destination'),
([sop1(FF0_64, 24, 23), 0xBF810000], 'invalid s_ff0_i32_b64 source'),
([sop1(FLBIT_B64, 24, VCC), 0xBF810000], 'invalid s_flbit_i32_b64 source'),
([sop1(BCNT0_64, 24, EXEC), 0xBF810000], 'invalid s_bcnt0_i32_b64 source'),
([sop1(BCNT0_64, 24, EXEC + 1), 0xBF810000], 'invalid s_bcnt0_i32_b64 source'),
([sop1(FLBIT_I64, 24, FLOAT_ONE), 0xBF810000], 'invalid s_flbit_i32_i64 source'),
)
for n, (words, error) in enumerate(refused):
Expand Down
127 changes: 127 additions & 0 deletions tests/native_wave64_exec_salu_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Compile and replay slot 67 scalar EXEC operands in both wave models."""
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


BCNT1_B64, BFM_B64 = 0x10, 0x25
VCC, EXEC, ZERO, NEG_ONE = 106, 126, 128, 193
COUNT_ERROR = "invalid s_bcnt1_i32_b64 source"
BFM_ERROR = "invalid s_bfm_b64 destination"
GROUP_STRIDE_WORDS = 128
GUARD_WORDS, OUTPUT_WORDS = 8, 2064
SENTINEL = 0x69696969
END = 0xBF810000


def request(work, root, wave_size, threads, words):
z = 1 | (1 << 20 if wave_size == 32 else 0)
sa = struct.pack("<7I", 0, 0, 0, threads | (1 << 16), z, 0, 0x8A)
return write_request(work, words, root, sa)


def program(wave_size, operation):
# s4 is the UAV table and s5 is group.x. v0 is local.x.
# Replay dispatches groups 0 and 1. The byte address is 512 * group.x + 4 * local.x.
if wave_size == 64:
words = [sop1(4, EXEC, NEG_ONE)]
else:
# Explicitly enable all 32 lanes and seed the tracked upper word.
words = [sop1(3, EXEC, NEG_ONE), sop1(3, EXEC + 1, ZERO + 1)]
words += [
sop1(3, 20, 4), sop1(3, 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
]
if operation == "count":
words += [
0x36040083, # v_and_b32 v2, 3, v0
0x7DA40480, # v_cmpx_eq_u32 0, v2
0x7DAA00A0, # v_cmpx_ne_u32 32, v0
sop1(BCNT1_B64, 24, EXEC),
vmove(1, 24),
]
data = 1
else:
width, offset = (16, 0) if operation == "lower" else (32, 32)
words.append(scalar(BFM_B64, EXEC, ZERO + width, ZERO + offset))
data = 0
return words + [0xE0701000, 0x80080008 | data << 8, END]


def expected_words(wave_size, threads, operation):
expected = [SENTINEL] * OUTPUT_WORDS
for group in range(2):
for local in range(threads):
lane = local % wave_size
word = GUARD_WORDS + group * GROUP_STRIDE_WORDS + local
if operation == "count" and local % 4 == 0 and local != 32:
# Wave64 loses lane 32 in the first guest wave only. Wave32
# counts eight lower bits and the explicitly set EXEC_HI bit.
expected[word] = 9 if wave_size == 32 else 15 if local < 64 else 16
elif operation == "lower" and lane < 16:
expected[word] = local
elif operation == "upper" and lane >= 32:
expected[word] = local
return expected


def compile_request(cli, sdk, cache, marker):
output = marker.with_suffix(".cs.cso")
result = subprocess.run([cli, sdk, cache, "gfx1013-direct-cs@0", marker, output],
capture_output=True, text=True)
return output, result


def main(cli, replay, 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)
root = root_file.read_bytes()
cache = work / "cache"
cases = [(64, threads, op) for threads in (64, 128) for op in ("count", "lower", "upper")]
cases.append((32, 32, "count"))
positives = []
for wave_size, threads, op in cases:
marker = request(work, root, wave_size, threads, program(wave_size, op))
output, result = compile_request(cli, sdk, cache, marker)
assert result.returncode == 0 and output.is_file(), (
wave_size, threads, op, result.stdout, result.stderr)
positives.append((wave_size, threads, op, marker))

for instruction, error in (
(sop1(BCNT1_B64, 24, VCC), COUNT_ERROR),
(scalar(BFM_B64, VCC, ZERO + 16, ZERO), BFM_ERROR),
(sop1(BCNT1_B64, 24, 23), COUNT_ERROR),
):
marker = request(work, root, 64, 64, [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

for wave_size, threads, op, marker in positives:
output = work / f"wave{wave_size}_{threads}_{op}.bin"
result = subprocess.run([replay, sdk, marker, cache, "0", output, "--direct"])
if result.returncode == 77:
return 77
result.check_returncode()
actual = struct.unpack(f"<{OUTPUT_WORDS}I", output.read_bytes())
expected = expected_words(wave_size, threads, op)
for index, (got, want) in enumerate(zip(actual, expected)):
assert got == want, (wave_size, threads, op, index, hex(got), hex(want))
print(f"wave{wave_size}, {threads} threads, {op}: both groups and guards matched")
return 0


if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
Loading