Skip to content
Open
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
13 changes: 11 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,8 @@ 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), EXEC (`exec_lo`/`exec_hi`, both words, in wave32 and wave64), inline integer (`-16..64`, sign-extended when negative), or
- **Source `S0`:** aligned SGPR pair (`s0..s104` even), VCC (`vcc_lo`/`vcc_hi`, both words, in wave32 and wave64),
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 +24,16 @@ 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_hi`/float `S0`; since EXEC became a valid `S0`, the source control uses the odd `exec_hi`); checks 6 mutant
checks 6 invalid destination/source refusals (`vcc`/`exec`/`m0` dst, odd/`m0`/`exec_hi`/float `S0`; since EXEC and VCC
became valid `S0` values, the source controls use `m0` and the odd `exec_hi`); checks 6 mutant
references differ.
- **VCC as `S0`:** `tests/native_salu_bitscan64_vcc_check.py controls|hardware`
(`ctest --preset dev --no-tests=error -R '^native_salu_bitscan64_vcc_' -V`). Runs all 6 ops with `S0 = VCC` in wave32
and wave64, populating VCC both with explicit `s_mov_b32 vcc_lo`/`vcc_hi` pairs (zero, all-ones, low-half-only,
high-half-only, sign-bit, bit 32) and with lane comparisons (`v_cmp_lt_u32` against 16 and `v_cmp_eq_u32` against 0);
verifies exact result words and `SCC` (counts update `SCC = (D != 0)`, scans preserve `SCC`), plus `M0`/odd-SGPR/
invalid-destination refusals. The `vcc`-source refusals this replaces
(`s_bcnt1_i32_b64` in `tests/native_wave64_exec_salu_check.py`, `s_flbit_i32_b64` here) now use `M0`.
- **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.
Expand Down
2 changes: 1 addition & 1 deletion src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,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)) || s0 == 126;
const bool s0_pair = (s0 <= 104 && !(s0 & 1)) || s0 == 106 || 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
11 changes: 11 additions & 0 deletions tests/cmake/native_salu_bitscan64_vcc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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_salu_bitscan64_vcc_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_salu_bitscan64_vcc_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_salu_bitscan64_vcc_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_salu_bitscan64_vcc_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_salu_bitscan64_vcc_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_salu_bitscan64_vcc_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
3 changes: 2 additions & 1 deletion tests/native_salu_bitscan64_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BCNT0_64, BCNT1_64, FF0_64, FF1_64, FLBIT_B64, FLBIT_I64 = 0x0E, 0x10, 0x12, 0x14, 0x16, 0x18
CSELECT32 = 0x0A
ZERO, ONE, NEG_ONE, VCC, EXEC, FLOAT_ONE, LITERAL = 128, 129, 193, 106, 126, 242, 255
M0 = 124
OLD_PAIR = (0xCAFEBABE, 0xDEADBEEF)
LITERAL_VALUE = 0x80000001
MASK32 = (1 << 32) - 1
Expand Down Expand Up @@ -181,7 +182,7 @@ def controls(cli, sdk, work, root):
([sop1(BCNT1_64, VCC, 22), 0xBF810000], 'invalid s_bcnt1_i32_b64 destination'),
([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(FLBIT_B64, 24, M0), 0xBF810000], 'invalid s_flbit_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'),
)
Expand Down
241 changes: 241 additions & 0 deletions tests/native_salu_bitscan64_vcc_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
"""Direct compute 64-bit scalar bit counts and bit scans with VCC source (#3224, #2935)."""
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_salu_bitscan64_check import (
BCNT0_64, BCNT1_64, FF0_64, FF1_64, FLBIT_B64, FLBIT_I64,
eval_bitscan, set_pair, set_scc,
)
from native_scalar_lds_check import scalar, sop1, vmove


CSELECT32 = 0x0A
ZERO, ONE, NEG_ONE, VCC, EXEC, LITERAL = 128, 129, 193, 106, 126, 255
M0 = 124
OLD_PAIR = (0xCAFEBABE, 0xDEADBEEF)
MASK32 = (1 << 32) - 1
MASK64 = (1 << 64) - 1
GUARD_WORDS, OUTPUT_WORDS = 8, 2064
SENTINEL = 0x69696969
VCMP_LT_U32, VCMP_EQ_U32 = 0xC1, 0xC2


def vopc(op, src0, vsrc1):
# GFX10 VOPC: [31:25] == 0b0111110, op in [24:17], VGPR src1 in [16:9], src0 in [8:0].
return 0x7C000000 | op << 17 | vsrc1 << 9 | src0


def set_vcc(lo, hi):
return [sop1(3, VCC, LITERAL), lo, sop1(3, VCC + 1, LITERAL), hi]


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 compute_program(wave_size, instructions, dst):
# Enable every lane before populating VCC: wave64 starts EXEC_HI at zero
# in the narrow model, and v_cmp only sets VCC on active lanes.
if wave_size == 64:
program = [sop1(4, EXEC, NEG_ONE)]
else:
program = [sop1(3, EXEC, NEG_ONE), sop1(3, EXEC + 1, ZERO + 1)]
# Two 32-lane groups in wave32, two 64-lane groups in wave64, with
# 8 bytes per lane and one result word plus one SCC word per lane.
shift = 9 if wave_size == 64 else 8
program += [sop1(3, 20, 4), sop1(3, 21, ZERO),
0xF4080000 | 32 << 6 | 10, 0xFA000020,
scalar(0x1E, 7, 5, 128 + shift), vop2(0x1A, 8, 131, 0), vop2(0x25, 8, 7, 8)]
program += instructions
plane_bytes = 16 * wave_size
program += [vmove(2, dst), vmove(3, dst + 1), 0xE0741000, 0x80080208,
scalar(CSELECT32, 26, ONE, ZERO), vmove(2, 26), vmove(3, ZERO),
0xE0741000 | plane_bytes, 0x80080208, 0xBF810000]
return program


def cases():
ops = (
(BCNT0_64, 'bcnt0'),
(BCNT1_64, 'bcnt1'),
(FF0_64, 'ff0'),
(FF1_64, 'ff1'),
(FLBIT_B64, 'flbit_b64'),
(FLBIT_I64, 'flbit_i64'),
)
vcc_inputs = (
('zero', (0, 0)),
('all_ones', (MASK32, MASK32)),
('low_only', (0x12345678, 0)),
('high_only', (0, 0x89ABCDEF)),
('sign_bit', (0, 0x80000000)),
('bit32', (0, 1)),
)
for wave_size in (32, 64):
for label, (lo, hi) in vcc_inputs:
for op, opname in ops:
primed = 1 if op in (FF0_64, FLBIT_B64) else 0
if op in (BCNT0_64, BCNT1_64):
bits = bin((hi << 32) | lo).count('1')
expected = (64 - bits) if op == BCNT0_64 else bits
primed = 1 - int(expected != 0)
code = (set_pair(24, OLD_PAIR) + set_vcc(lo, hi)
+ [set_scc(primed), sop1(op, 24, VCC)])
yield (f'wave{wave_size} {opname} vcc {label}', wave_size, code, 24,
OLD_PAIR[1], op, lo, hi, primed)
# VCC populated by a lane comparison: v_cmp_lt_u32 vcc, s24, v0 with s24 = 16.
# Lanes 17..wave_size-1 set: wave32 lo = 0xfffe0000, hi = 0;
# wave64 lo = 0xfffe0000, hi = 0xffffffff.
cmp_lo, cmp_hi = 0xFFFE0000, MASK32 if wave_size == 64 else 0
for op, opname in ops:
primed = 1 if op in (FF0_64, FLBIT_B64) else 0
if op in (BCNT0_64, BCNT1_64):
bits = bin((cmp_hi << 32) | cmp_lo).count('1')
expected = (64 - bits) if op == BCNT0_64 else bits
primed = 1 - int(expected != 0)
code = (set_pair(24, OLD_PAIR) + [sop1(3, 24, LITERAL), 16,
vopc(VCMP_LT_U32, 24, 0), set_scc(primed), sop1(op, 24, VCC)])
yield (f'wave{wave_size} {opname} vcc cmp_lt16', wave_size, code, 24,
OLD_PAIR[1], op, cmp_lo, cmp_hi, primed)
# VCC populated by an equality comparison: v_cmp_eq_u32 vcc, s24, v0 with s24 = 0.
# Only lane 0 sets: lo = 1, hi = 0, in both wave sizes.
for op, opname in ops:
primed = 1 if op in (FF0_64, FLBIT_B64) else 0
if op in (BCNT0_64, BCNT1_64):
bits = 1
expected = (64 - bits) if op == BCNT0_64 else bits
primed = 1 - int(expected != 0)
code = (set_pair(24, OLD_PAIR) + [sop1(3, 24, ZERO),
vopc(VCMP_EQ_U32, 24, 0), set_scc(primed), sop1(op, 24, VCC)])
yield (f'wave{wave_size} {opname} vcc cmp_eq0', wave_size, code, 24,
OLD_PAIR[1], op, 1, 0, primed)
# Explicit SCC preservation across count -> scan sequences with a VCC source.
yield (f'wave{wave_size} scc0 preserved by ff1',
wave_size,
set_pair(24, OLD_PAIR) + set_vcc(0, 1)
+ [sop1(BCNT1_64, 24, ZERO), sop1(FF1_64, 24, VCC)],
24, OLD_PAIR[1], FF1_64, 0, 1, 0)
yield (f'wave{wave_size} scc1 preserved by flbit_b64',
wave_size,
set_pair(24, OLD_PAIR) + set_vcc(0, 0)
+ [sop1(BCNT1_64, 24, ONE), sop1(FLBIT_B64, 24, VCC)],
24, OLD_PAIR[1], FLBIT_B64, 0, 0, 1)
# Overlapping destination: s22 = ff1(vcc), s23 stays 0x01234567.
yield (f'wave{wave_size} ff1 overlap s22',
wave_size,
set_pair(22, (0, 0x01234567)) + set_vcc(0, 0x01234567)
+ [set_scc(0), sop1(FF1_64, 22, VCC)],
22, 0x01234567, FF1_64, 0, 0x01234567, 0)


def full_reference(**mutant_flags):
return [eval_bitscan(op, lo, hi, 'pair', primed, **mutant_flags)
for _, _, _, _, _, op, lo, hi, primed in cases()]


def controls(cli, sdk, work, root):
combined = compute_program(64, set_pair(24, OLD_PAIR) + set_vcc(0x80000001, 0x00000010) + [
sop1(BCNT0_64, 24, VCC),
sop1(BCNT1_64, 24, VCC),
sop1(FF0_64, 24, VCC),
sop1(FF1_64, 24, VCC),
sop1(FLBIT_B64, 24, VCC),
sop1(FLBIT_I64, 24, VCC),
], 24)
marker = request(work, root, 64, 64, combined)
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 output.is_file(), (result.stdout, result.stderr)
dump = subprocess.run([Path(sdk) / 'dxc.exe', '-dumpbin', output], capture_output=True, text=True, check=True).stdout
for op_id in (31, 32, 33):
assert f'@dx.op.unaryBits.i32(i32 {op_id}' in dump, (op_id, dump)
assert 'shl i64' not in dump, dump

# A lane-comparison VCC source must compile in both wave sizes.
cmp_prog = compute_program(32, set_pair(24, OLD_PAIR) + [sop1(3, 24, LITERAL), 16,
vopc(VCMP_LT_U32, 24, 0), set_scc(0), sop1(BCNT1_64, 24, VCC)], 24)
cmp_marker = request(work, root, 32, 32, cmp_prog)
cmp_out = cmp_marker.with_suffix('.cmp.cso')
cmp_res = subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', cmp_marker, cmp_out],
capture_output=True, text=True)
assert cmp_res.returncode == 0 and cmp_out.is_file(), (cmp_res.stdout, cmp_res.stderr)

refused = (
([sop1(BCNT1_64, VCC, 22), 0xBF810000], 'invalid s_bcnt1_i32_b64 destination'),
([sop1(FF1_64, M0, 22), 0xBF810000], 'invalid s_ff1_i32_b64 destination'),
([sop1(BCNT0_64, 24, M0), 0xBF810000], 'invalid s_bcnt0_i32_b64 source'),
([sop1(FF0_64, 24, 23), 0xBF810000], 'invalid s_ff0_i32_b64 source'),
([sop1(FLBIT_B64, 24, EXEC + 1), 0xBF810000], 'invalid s_flbit_i32_b64 source'),
([sop1(FLBIT_I64, VCC, 22), 0xBF810000], 'invalid s_flbit_i32_i64 destination'),
)
for n, (words, error) in enumerate(refused):
m = request(work, root, 64, 64, words)
o = m.with_suffix(f'.refused{n}.cso')
r = subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', m, o],
capture_output=True, text=True)
assert r.returncode and error in r.stderr, (error, r.stdout, r.stderr)
assert not o.exists(), o

baseline = full_reference()
for label, flags in (
('missing +32 in high ff0/ff1', {'no_hi_offset_ff': True}),
('missing +32 in low flbit', {'no_lo_offset_flbit': True}),
('unsigned flbit_i64', {'unsigned_flbit': True}),
('scan writes SCC', {'scan_writes_scc': True}),
('zero returns 64 instead of 0xffffffff', {'zero_is_64': True}),
):
assert full_reference(**flags) != baseline, label

print('compiled 6 ops with VCC source (plus lane-compare VCC); '
'6 invalid forms refused; 5 reference mutants differ')
return 0


def hardware(replay, sdk, work, root):
for name, wave_size, code, dst, neighbor, op, lo, hi, primed in cases():
res, scc = eval_bitscan(op, lo, hi, 'pair', primed)
marker = request(work, root, wave_size, wave_size, compute_program(wave_size, code, dst))
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())
group_stride = 2 * wave_size
plane_stride = 4 * wave_size
expected = [SENTINEL] * OUTPUT_WORDS
for group in range(2):
for lane in range(wave_size):
base = GUARD_WORDS + group_stride * group + 2 * lane
expected[base] = res
expected[base + 1] = neighbor
expected[base + plane_stride] = scc
expected[base + plane_stride + 1] = 0
assert list(actual) == expected, (name, res, scc,
[(i, actual[i], expected[i]) for i, (a, b) in
enumerate(zip(actual, expected)) if a != b][:10])
print(f'{name}: result, neighbor, 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:]))
2 changes: 1 addition & 1 deletion tests/native_wave64_exec_salu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def main(cli, replay, sdk):
positives.append((wave_size, threads, op, marker))

for instruction, error in (
(sop1(BCNT1_B64, 24, VCC), COUNT_ERROR),
(sop1(BCNT1_B64, 24, M0), COUNT_ERROR),
(scalar(BFM_B64, M0, ZERO + 16, ZERO), BFM_ERROR),
(scalar(BFM_B64, 23, ZERO + 16, ZERO), BFM_ERROR),
(sop1(BCNT1_B64, 24, 23), COUNT_ERROR),
Expand Down
Loading