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
34 changes: 34 additions & 0 deletions docs/experiments/native-direct-cvt-pk16.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 in the direct C++ compute path (#2995)

The direct compute frontend (`src/gfx1013_direct_compute.inl`) now lowers the two saturating 16-bit packs that it
refused with `direct DXIL lowering for <name>`. The semantics are the `gfx2hlsl.py` VOP3-table expressions. Each
saturates S0 and S1 to 16 bits and packs S0 into the low half and S1 into the high half:

- `v_cvt_pk_u16_u32`: min(S, 0xffff) of the unsigned values.
- `v_cvt_pk_i16_i32`: the signed values clamped to [-32768, 32767].

A nonzero unused SRC2 field is refused (`<name> with unused src2 field set`), as in `gfx2hlsl.py`, and so are the
op_sel bits (`<name> with op_sel`). Source and output modifiers stay refused like the other integer VOP3 forms.

As `docs/experiments.md` notes for the Python path, the RDNA 2 ISA text does not define the `u32_to_u16` and
`i32_to_i16` conversions. The saturation follows Mesa and the existing translators, and it is not measured on the
console.

## Fixture

`tests/native_cvt_pk16_check.py controls|hardware`
(`ctest --preset dev --no-tests=error -R '^native_cvt_pk16_' -V`). Eight operand pairs are set by `v_mov` literals and
converted by both instructions; each of 64 lanes stores the sixteen results. The pairs cross both bounds of both forms:
0, 0xffff, 0x10000, 0xffffffff, -32768, -32769, 32767, 32768, 0x7fffffff and 0x80000000 all occur. The VOP3 words
were assembled with LLVM 15.0.7 (`clang -cc1as -triple amdgcn -target-cpu gfx1013 -filetype asm -show-encoding`).
The other words were checked with `tools/disasm_rdna2.py --hex`.

- The controls compile the program, refuse the same program with SRC2 = 3 or an op_sel bit, and check that references that truncate
instead of saturating, or that apply the unsigned bound to the signed form, differ from the expected words. The
reference agrees with the `gfx2hlsl.py` table expressions evaluated in Python for all eight pairs. This was checked
once while writing the fixture.

Measured on 27.11.2026, `cpp-migration` `4ff5e36d` plus this change, Windows 10.0.26100.9711. The controls printed
`compiled; a nonzero src2 field and op_sel refused; the truncating and unsigned-bound references differ`. The readback printed
`requires shader model 6.6 with wave32` and reported a skip (77) on the NVIDIA Quadro M1000M (driver 32.0.15.7371,
debug layer unavailable). The readback is not measured.
15 changes: 15 additions & 0 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,21 @@ struct DirectCompute {
auto narrow = ir.binary(sign ? "ashr" : "lshr", ir.binary("shl", shifted, pad), pad);
z = select(cmp("eq", width, "0"), "0", select(cmp("uge", width, "32"), shifted, narrow));
}
else if (n == "v_cvt_pk_u16_u32" || n == "v_cvt_pk_i16_i32") {
// Saturate S0 and S1 to 16 bits and pack S0 low, S1 high (#2995, gfx2hlsl.py's VOP3 table). The
// instructions have two sources; like gfx2hlsl.py, refuse a nonzero unused SRC2 field.
if (i.src[2] != 0)
refuse(i.pc, string(n) + " with unused src2 field set");
if (i.a & 0x7800)
refuse(i.pc, string(n) + " with op_sel");
auto narrow = [&](const string &x) {
if (n == "v_cvt_pk_u16_u32")
return select(cmp("ugt", x, "65535"), "65535", x);
auto low = select(cmp("slt", x, "-32768"), "-32768", x);
return ir.binary("and", select(cmp("sgt", low, "32767"), "32767", low), "65535");
};
z = ir.binary("or", narrow(a), ir.binary("shl", narrow(b), "16"));
}
else if (n == "v_bfe_u32")
z = ir.binary("and", shift("lshr", a, b), ir.binary("sub", shift("shl", "1", val(2)), "1"));
else if (n == "v_bfe_i32") {
Expand Down
13 changes: 13 additions & 0 deletions tests/cmake/native_cvt_pk16.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Direct C++ compute: v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 (#2995). The controls compile and check the
# reference's mutants; the readback needs shader model 6.6 with wave32 and otherwise reports a skip (77).
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_cvt_pk16_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_cvt_pk16_check.py" controls
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(native_cvt_pk16_controls PROPERTIES TIMEOUT 60)
add_test(NAME native_cvt_pk16_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_cvt_pk16_check.py" hardware
"$<TARGET_FILE:native_store_replay>" "${DXC_BIN_DIR}")
set_tests_properties(native_cvt_pk16_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
endif()
113 changes: 113 additions & 0 deletions tests/native_cvt_pk16_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32 in the direct compute path (#2995).

native_cvt_pk16_check.py controls <compile_dxil> <sdk bin dir>
native_cvt_pk16_check.py hardware <native_store_replay> <sdk bin dir>

Both saturate S0 and S1 to 16 bits and pack S0 into the low and S1 into the high half (gfx2hlsl.py's VOP3 table):
v_cvt_pk_u16_u32 takes min(S, 0xffff) of the unsigned values, v_cvt_pk_i16_i32 clamps the signed values to
[-32768, 32767]. A nonzero unused SRC2 field and op_sel are refused (gfx2hlsl.py refuses the SRC2 field). The VOP3 words were assembled with
LLVM 15.0.7 (clang -cc1as, gfx1013); the v_mov and store words follow the encodings of native_align_bfm_check.py.

Each operand pair below is set by v_mov literals and converted by both instructions; every lane of two groups of
32 stores the sixteen results. The pairs cross both bounds of both forms. References that truncate instead of
saturating and that apply the unsigned bound to the signed form must each differ from the expected words.
"""
from pathlib import Path
import struct
import subprocess
import sys
import tempfile

from native_dpp_quad_check import request

PAIRS = [(0, 0xFFFF), (0x10000, 0xFFFFFFFF), (0xFFFF8000, 0xFFFF7FFF), (0x7FFF, 0x8000),
(0x12345, 0x7FFFFFFF), (0x80000000, 5), (0xFFFFFFFE, 0xFFFE), (1, 0x00010001)]
# s_mov_b32 s20, s4 / s_mov_b32 s21, 0 / s_load_dwordx4 s[32:35], s[20:21], 0x20 / s_lshl_b32 s7, s5, 11 /
# v_lshlrev_b32 v8, 6, v0 / v_add_nc_u32 v8, s7, v8: 64 bytes per lane, 2048 per group of 32
PROLOGUE = [0xBE940304, 0xBE950380, 0xF408080A, 0xFA000020, 0x8F078B05, 0x34100086, 0x4A101007]
CVT_U16, CVT_I16 = (0xD76A0004, 0x00020501), (0xD76B0005, 0x00020501) # v4 / v5 from v1, v2
SENTINEL = 0x69696969
GUARD_WORDS = 8
OUTPUT_WORDS = 2064


def program(src2=0):
code = PROLOGUE + [0xBF8CC07F] # s_waitcnt lgkmcnt(0) before the stores use s[32:35]
for index, (x, y) in enumerate(PAIRS):
code += [0x7E0202FF, x, 0x7E0402FF, y,
CVT_U16[0], CVT_U16[1] | src2 << 18, CVT_I16[0], CVT_I16[1] | src2 << 18]
for slot, vgpr in enumerate((4, 5)):
code += [0xE0701000 | (8 * index + 4 * slot), 0x80080008 | vgpr << 8] # buffer_store_dword v, v8, s[32:35]
return code + [0xBF810000]


def signed(x):
return x - (1 << 32) if x & 0x80000000 else x


def pack(x, y, truncate=False, unsigned_bound=False):
if truncate:
return (x & 0xFFFF) | ((y & 0xFFFF) << 16), (x & 0xFFFF) | ((y & 0xFFFF) << 16)
u = min(x, 0xFFFF) | (min(y, 0xFFFF) << 16)
clamp = (lambda v: min(v, 0x7FFF)) if unsigned_bound else (lambda v: max(-32768, min(32767, signed(v))))
i = (clamp(x) & 0xFFFF) | ((clamp(y) & 0xFFFF) << 16)
return u, i


def reference(**mutation):
expected = [SENTINEL] * OUTPUT_WORDS
for lane in range(64):
base = GUARD_WORDS + lane * 16
for index, (x, y) in enumerate(PAIRS):
expected[base + 2 * index:base + 2 * index + 2] = pack(x, y, **mutation)
return expected


def compile_program(cli, sdk, work, root, code):
marker = request(work, root, code)
return marker, subprocess.run([cli, sdk, work / 'cache', 'gfx1013-direct-cs@0', marker,
marker.with_suffix('.cso')], capture_output=True, text=True)


def controls(cli, sdk, work, root):
marker, result = compile_program(cli, sdk, work, root, program())
assert result.returncode == 0 and marker.with_suffix('.cso').is_file(), (result.stdout, result.stderr)
_, result = compile_program(cli, sdk, work, root, program(src2=3))
assert result.returncode and 'with unused src2 field set' in result.stderr, (result.stdout, result.stderr)
code = program()
code[code.index(CVT_U16[0])] |= 1 << 11 # op_sel bit of src0
_, result = compile_program(cli, sdk, work, root, code)
assert result.returncode and 'with op_sel' in result.stderr, (result.stdout, result.stderr)
expected = reference()
for label, mutant in (('truncated', reference(truncate=True)), ('unsigned bound', reference(unsigned_bound=True))):
assert mutant != expected, label
print('compiled; a nonzero src2 field and op_sel refused; the truncating and unsigned-bound references differ')
return 0


def hardware(replay, sdk, work, root):
marker = request(work, root, program())
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 = list(struct.unpack(f'<{OUTPUT_WORDS}I', output.read_bytes()))
diff = [(i, hex(x), hex(y)) for i, (x, y) in enumerate(zip(actual, reference())) if x != y]
assert not diff, diff[:10]
print('v_cvt_pk_u16_u32 and v_cvt_pk_i16_i32: all 2064 words matched (1024 results, guards and sentinels)')
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:]))
Loading