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
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ jobs:
tests/startup_gfx2hlsl_s_quadmask_check.py \
tests/startup_gfx2hlsl_fma_mix_check.py \
tests/startup_gfx2hlsl_shift64_check.py \
tests/startup_gfx2hlsl_shift64_literal_check.py \
tests/startup_gfx2hlsl_mad64_check.py \
tests/startup_gfx2hlsl_cmp_class_f16_sdwa_check.py \
tests/startup_gfx2hlsl_mul_hi_i32_check.py \
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4264,6 +4264,8 @@ if(EXISTS "${INTAKE_PYTHON}")
# gfx2hlsl 64-bit VOP3 shifts: binary round-trip, overlap, guards and WARP/hardware draws (Refs #1723).
add_test(NAME startup_gfx2hlsl_shift64_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_check.py")
add_test(NAME startup_gfx2hlsl_shift64_literal COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_literal_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_gfx2hlsl_shift64_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_gfx2hlsl_shift64_check.py" draw "${DXC_EXECUTABLE}"
Expand Down
2 changes: 1 addition & 1 deletion docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -8675,7 +8675,7 @@ The draws (`pixel_aot_cmp_f64_warp` and `_hardware`) run eight predicate groups
ctest --preset dev --no-tests=error -R '^pixel_aot_cmp_f64_(warp|hardware)$' -V
```

The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair or an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced). At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware.
The 64-bit `VOP3` shifts `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` (issue #928) follow the [AMD RDNA 2 ISA guide](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) §12.12 (opcodes 767-769): `D = S1 << S0[5:0]`, `S1 >> S0[5:0]` and `signext(S1) >> S0[5:0]`. The opcodes match the LLVM 15 gfx1013 encodings, and LLVM 15 folds an IR shift by `x & 63` into the bare instruction while keeping `x & 31`. The value uses the two-word representation of the 64-bit comparisons above, so the HLSL needs no 64-bit type: both source words are read into temporaries before the destination pair is written, so the in-place form keeps the original value, and the result is selected for amounts below and above 32, so no 32-bit HLSL shift receives a count of 32 or more. Only `v_ashrrev_i64` fills with the sign bit. The amount is a VGPR, an SGPR, an inline integer from -16 to 64 or a literal; the value is a VGPR pair, an SGPR pair, an inline integer, sign-extended to 64 bits as for the comparisons (assumed, not sourced), or a 32-bit hex literal (`0x0` to `0xffffffff`, written in hex form). A literal fills the low word; the high word is zero for `v_lshlrev_b64` and `v_lshrrev_b64` and copies bit 31 for `v_ashrrev_i64`. The same hex literal is now also the 64-bit source of the scalar `s_lshl_b64`, `s_lshr_b64`, `s_ashr_i64`, `s_bfe_u64` and `s_bfe_i64`, which refused it before; the high word is zero for the unsigned operations and copies bit 31 for the signed ones (`s_ashr_i64`, `s_bfe_i64`). Only `s_mov_b64` zero-extended a hex literal before this change. The extension rule is an assumption, not sourced from the ISA text. A decimal literal outside -16 to 64, a hex literal wider than 32 bits and a literal as destination stay refused. `ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_shift64_literal$' -V` checks six lowered vector forms and the scalar `s_ashr_i64`, `s_lshr_b64`, `s_bfe_i64` and `s_bfe_u64` (including `0x80000000` and `0xffffffff`, where the signed forms differ) and these refusals; it is pure Python and needs no adapter. At most one source may be an SGPR or a literal (the ISA's one scalar broadcast constant; LLVM 15 refuses the others with `violates constant bus restrictions`). `_e32`, `clamp`, `op_sel` and source modifiers, a destination that is not a pair in `v0`..`v255` or has an outstanding image sample, float amounts and use inside a masked region are refused. The draw (`tests/pixel_aot_check.py draw-shift64`, tests `pixel_aot_shift64_warp` and `pixel_aot_shift64_hardware`) compiles three programs per instruction (register sources; the in-place pair next to the literal amount `0x41`, which wraps to 1; and an SGPR-pair value next to the inline `-1`) and reads the four result words back through `R32G32B32A32_UINT` for three values (one with a negative high word) and the amounts 0, 1, 31, 32, 33, 63, 64, 65, `0xffffffc1` and `0x80000020`, against a Python reference written from the ISA formulas. Three mutated `v_ashrrev_i64` shaders must each mismatch: the amount masked to five bits, the sign fill replaced with zeros, and the result words swapped. The LLVM 15 round trip covers the nine programs; the scanner accepts the same operand shapes and `tests/rdna2_tools_check.py` pins them. The draw pins the translator's model on a desktop adapter; it is not evidence for gfx1013 hardware.

The non-packed 16-bit integer `VOP3` ops (issue #669) `v_add_nc_u16`, `v_add_nc_i16`, `v_sub_nc_u16`, `v_sub_nc_i16`, `v_min_u16`, `v_max_u16`, `v_min_i16`, `v_max_i16`, `v_mul_lo_u16`, `v_lshlrev_b16`, `v_lshrrev_b16` and `v_ashrrev_i16` compute on the low 16 bits of two sources and reuse the lane arithmetic of the packed forms: results wrap modulo 2^16, signed min, max and shifts sign-extend the 16-bit value, and shift counts are masked to 4 bits (`& 15`, so a count of 16 shifts by 0). Only the low 16 bits of the result are defined and tracked: the lowering records the destination as `("low16", alu)`, a value whose HLSL variable holds the zero-extended result but whose upper half no accepted reader observes. The destination's previous value is not merged, so a destination may hold anything (a tracked value, an untracked register or another low16 result); a destination with an outstanding image sample is refused. The same applies to the unsuffixed and the `_e64` spelling. The readers that see a low16 value are the 12 ops themselves (a chain of two ops is accepted), the 16-bit integer compares `v_cmp_{eq,ne,lt,le,gt,ge}_{i16,u16}` and `v_mov_b32_sdwa` with `src0_sel:WORD_0`, `BYTE_0` or `BYTE_1` (also with `sext()`), which widens the low half into a fully defined word. Every whole-word or high-half reader is refused: 32-bit integer and float arithmetic, moves, `v_cndmask_b32` data operands, DPP, packed `VOP3P` forms, conversions (including `v_cvt_f32_f16`, which is out of scope for integer low16; binary16 arithmetic results use the distinct `half16` tag, below), dot products, class compares, packs, both export forms and image sample coordinates raise `Expected a numeric register`, `Expected a numeric VGPR in DPP`, `Unsupported pack operand`, `Unsupported export` or `Unsupported texture coordinate`; `v_cvt_f16_f32` into a low16 destination raises `Unsupported conversion destination`; and `v_mov_b32_sdwa` with `WORD_1`, `BYTE_2`, `BYTE_3` or `DWORD` on a low16 source, or with `dst_unused:UNUSED_PRESERVE` into a low16 destination, raises `Reads the undefined high half of a 16-bit result`. A source may be a VGPR in 0 to 255 that holds a tracked value, a tracked SGPR in 0 to 105, an integer inline constant from -16 to 64 or a literal up to `0xffff`. `op_sel`, `clamp`, source modifiers, DPP, SDWA, the f16 ops, float tokens, wider literals, inline integers outside -16 to 64 (llvm-mc would encode `-17` as the literal `0xffef`; the translator refuses it by policy, as for the 16-bit compares), an untracked source, a register out of range and a destination with an outstanding image sample are refused. `tests/form_coverage_check.py` asserts that the 12 unsuffixed forms are counted while the f16 neighbours are not. `tools/scan_unsupported_shaders.py` matches instructions syntactically and cannot see the low16 tags, so it can report a shader as supported that the translator then refuses at a whole-word reader. The binary fixtures (four programs; groups 0 to 2 widen all four results and group 3 its two low-half results with `v_mov_b32_sdwa ... src0_sel:WORD_0`) round-trip through LLVM 15.0.7, and the controls refuse every form above with its message. Reproduce with `ctest --preset dev --no-tests=error -R "^pixel_aot_int16_vop3_"`: groups 0 to 2 preload `v24` to `v27` with a `0xa5c3xxxx` sentinel, run four ops on constant words, widen each result into `v12` to `v15` with SDWA `WORD_0` and export through the bit-exact `R32G32B32A32_UINT` target against a Python reference written from the semantics that contains only the zero-extended low halves; group 3 chains `v_add_nc_u16` into `v_mul_lo_u16` and selects between whole words with `v_cmp_lt_u16` and `v_cmp_lt_i16` on low16 results. Six word sets cover `0x7fff` against `0x8000`, `0xffff + 1`, `0 - 1`, shift counts 15, 16, 17 and 31, a negative operand under `ashrrev`, signed and unsigned min and max that disagree, the overflow `0x1234 * 0x0567`, and sources whose high half differs from the low half. Five mutations of the generated shader must each report a pixel mismatch (a source read from bits 31:16, a shift mask of 31 instead of 15, `v_min_i16` replaced by `v_min_u16`, and a `>> 16u` read of a low16 result by the chained op and by the first compare), and a widening that selects `WORD_1` instead of `WORD_0` must be refused at lowering. The sentinel preloads do not detect a high-half leak, because every accepted reader skips the high half; that half is guarded only by the refusal controls. The binary round trip disassembles through libLLVM; `tools/disasm_rdna2.py` decodes the same encodings. [AMD RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture)

Expand Down
4 changes: 2 additions & 2 deletions tests/rdna2_tools_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6158,10 +6158,10 @@ def test_shift64_scanner_support(self):
"""Issue #928: the 64-bit VOP3 shifts are supported with at most one SGPR or literal source."""
for line in ("v_lshlrev_b64 v[12:13], v20, v[18:19]", "v_lshrrev_b64 v[2:3], s46, v[4:5]",
"v_ashrrev_i64 v[2:3], 0x41, v[4:5]", "v_lshlrev_b64 v[2:3], v1, s[4:5]",
"v_ashrrev_i64 v[2:3], 3, -1", "v_lshlrev_b64_e64 v[2:3], v1, v[4:5]"):
"v_ashrrev_i64 v[2:3], 3, -1", "v_lshlrev_b64 v[2:3], v1, 0x41", "v_lshlrev_b64_e64 v[2:3], v1, v[4:5]"):
self.assertTrue(is_instruction_supported(line), line)
for line in ("v_lshlrev_b64 v[2:3], s4, s[4:5]", "v_lshlrev_b64 v[2:3], 0x41, s[4:5]",
"v_lshlrev_b64 v[2:3], v1, 0x41", "v_lshlrev_b64 v[2:3], v1, v[4:5] clamp",
"v_lshlrev_b64 v[2:3], s4, 0x41", "v_lshlrev_b64 v[2:3], v1, v[4:5] clamp",
"v_lshlrev_b64_e32 v[2:3], v1, v[4:5]", "v_lshlrev_b64 v[2:3], -v1, v[4:5]"):
self.assertFalse(is_instruction_supported(line), line)

Expand Down
1 change: 0 additions & 1 deletion tests/startup_gfx2hlsl_shift64_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ def controls():
'v_lshrrev_b64 v[0:1], v2, v[255:256]',
'v_lshrrev_b64 v[0:1], v2, vcc',
'v_lshrrev_b64 v[0:1], v2, exec',
'v_lshrrev_b64 v[0:1], v2, 0x41',
'v_lshrrev_b64 v[0:1], v2, 65',
'v_lshrrev_b64 v[0:1], v2, -17',
'v_lshrrev_b64 v[0:1], v2, 1.0',
Expand Down
60 changes: 60 additions & 0 deletions tests/startup_gfx2hlsl_shift64_literal_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""32-bit hex literal as the 64-bit source of v_lshlrev_b64, v_lshrrev_b64 and v_ashrrev_i64 (Refs #1723).

A literal fills the low word. The high word is zero for the unsigned shifts and copies bit 31 for the signed
shift (the rule of s_mov_b64; the scalar shifts and bitfield extracts take it from this change). Pure Python; run with no arguments.
"""
import struct
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parent / 'tools' / 'startup'))
sys.path.insert(0, str(HERE))
import startup_gfx2hlsl_scalar_m0_check as M # noqa: E402
from startup_gfx2hlsl_bit_alu_check import Probe, check, refused # noqa: E402

# (operation, helper, literal, expected low word, expected high word)
CASES = (
('v_lshlrev_b64', 'shl64', '0x41', '65u', '0u'),
('v_lshrrev_b64', 'shr64u', '0x80000000', '2147483648u', '0u'),
('v_lshrrev_b64', 'shr64u', '0xffffffff', '4294967295u', '0u'),
('v_ashrrev_i64', 'shr64i', '0x7fffffff', '2147483647u', '0u'),
('v_ashrrev_i64', 'shr64i', '0x80000000', '2147483648u', '4294967295u'),
('v_ashrrev_i64', 'shr64i', '0xffffffff', '4294967295u', '4294967295u'),
)


def lowered(op, literal):
probe = Probe()
probe.one(op, '%s v[0:1], v2, %s' % (op, literal))
return probe.lines


def scalar_lowered(opcode):
"""s_<op> s[20:21], 0xffffffff, s9 as encoded (SOP2, source 255 plus a literal dword)."""
stage = M.run_seq(M.encode_sop2(opcode, 20, 255, 9) + struct.pack('<I', 0xffffffff))
return [line for line in stage.lines if 'shr64' in line or 'bfe64' in line]


def main():
check('s_ashr_i64 literal 0xffffffff is sign-extended',
scalar_lowered(0x23)[0].startswith('{ uint2 t = shr64i(4294967295u, 4294967295u, s9);'))
check('s_lshr_b64 literal 0xffffffff is zero-extended',
scalar_lowered(0x21)[0].startswith('{ uint2 t = shr64u(4294967295u, 0u, s9);'))
check('s_bfe_i64 literal 0xffffffff is sign-extended',
'bfe64(4294967295u, 4294967295u, s9, 1u)' in scalar_lowered(0x2a)[0])
check('s_bfe_u64 literal 0xffffffff is zero-extended',
'bfe64(4294967295u, 0u, s9, 0u)' in scalar_lowered(0x29)[0])
for op, helper, literal, low, high in CASES:
want = ['{ uint2 t = %s(%s, %s, v2); v0 = t.x; v1 = t.y; }' % (helper, low, high)]
check('%s with literal %s' % (op, literal), lowered(op, literal) == want)
check('literals wider than 32 bits stay refused',
all(refused('%s v[0:1], v2, 0x100000000' % op) for op in ('v_lshlrev_b64', 'v_ashrrev_i64')))
check('a hex literal stays refused as the addend of v_mad_u64_u32 and v_mad_i64_i32',
all(refused('%s v[0:1], vcc_lo, v2, v3, 0x41' % op) for op in ('v_mad_u64_u32', 'v_mad_i64_i32')))
check('a literal is not accepted as a destination or in the wrong operand count',
refused('v_lshrrev_b64 0x41, v2, v[4:5]') and refused('v_lshrrev_b64 v[0:1], 0x41'))


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions tools/scan_unsupported_shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ def lower_rejection_causes(results: list[dict]) -> dict:
rf"\s+{INT16_SOURCE},\s+{INT16_SOURCE},\s+{INT16_SOURCE}"
)
# 64-bit VOP3 shifts (issue #928): a VGPR-pair destination, a 32-bit amount (VGPR, SGPR, inline integer or
# literal) and a 64-bit value (VGPR pair, SGPR pair or inline integer), with at most one SGPR or literal source.
# literal) and a 64-bit value (VGPR pair, SGPR pair, inline integer or 32-bit hex literal), with at most one SGPR or literal source.
# This matches syntax only; pixel_aot also checks the pair ranges and that the sources are tracked.
SHIFT64_AMOUNT = r"(?:v\d+|-?\d+)"
SHIFT64_VALUE = r"(?:v\[\d+:\d+\]|-?\d+)"
SUPPORTED_INSTRUCTIONS_REGEX.append(
r"v_(?:lshlrev_b64|lshrrev_b64|ashrrev_i64)(?:_e64)?\s+v\[\d+:\d+\],\s+"
rf"(?:{SHIFT64_AMOUNT},\s+{SHIFT64_VALUE}|(?:s\d+|0x[0-9a-f]{{1,8}}),\s+{SHIFT64_VALUE}"
rf"|{SHIFT64_AMOUNT},\s+s\[\d+:\d+\])"
rf"|{SHIFT64_AMOUNT},\s+s\[\d+:\d+\]|{SHIFT64_AMOUNT},\s+0x[0-9a-f]{{1,8}})"
)

for name, (arity, _) in PIXEL_INTEGER.items():
Expand Down
12 changes: 8 additions & 4 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5170,7 +5170,7 @@ def scalar_wide_op(self, name, ops, mods, wide_src, wide_dst, helper, signed=Non
r0 = reg_range(ops[1])
if r0 and r0[0] != 's':
raise Unsupported('%s source %s' % (name, ops[1]))
low, high = self.i64_words(ops[1])
low, high = self.i64_words(ops[1], helper == 'shr64i' or signed is True, True)
known = [self.scalar_const('s%d' % r0[1]), self.scalar_const('s%d' % r0[2])] if r0 else [
int(low[:-1]), int(high[:-1])]
else:
Expand Down Expand Up @@ -7629,8 +7629,9 @@ def op_v_cubetc_f32(self, ops, mods):
def op_v_cubema_f32(self, ops, mods):
self.cube('ma', ops, mods)

def i64_words(self, tok):
"""(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair or an inline int."""
def i64_words(self, tok, signed=False, literal=False):
"""(low, high) uint expressions of a 64-bit integer source: a VGPR/SGPR pair, an inline int or a
32-bit hex literal when `literal` is set (zero-extended, sign-extended when the operation is signed)."""
pair = reg_range(tok)
if pair and pair[2] == pair[1] + 1:
if pair[0] == 'v' and pair[2] <= 255:
Expand All @@ -7641,6 +7642,9 @@ def i64_words(self, tok):
if re.fullmatch(r'0|-?[1-9]\d*', tok) and -16 <= int(tok) <= 64:
value = int(tok) & 0xffffffffffffffff
return '%du' % (value & 0xffffffff), '%du' % (value >> 32)
if literal and re.fullmatch(r'0x[0-9a-f]{1,8}', tok):
word = int(tok, 16)
return '%du' % word, ('4294967295u' if signed and word >> 31 else '0u')
raise Unsupported('64-bit source %s' % tok)

def shift64(self, ops, mods, helper):
Expand All @@ -7656,7 +7660,7 @@ def shift64(self, ops, mods, helper):
or ops[1] in ('vcc', 'exec') or ops[1] in FLOATS):
raise Unsupported('64-bit shift amount %s' % ops[1])
amount = self.u(ops[1])
low, high = self.i64_words(ops[2])
low, high = self.i64_words(ops[2], helper == 'shr64i', True)
body = '{ uint2 t = %s(%s, %s, %s); v%d = t.x; v%d = t.y; }' % (
helper, low, high, amount, dst[1], dst[2])
if hasattr(self, 'uniform_v'):
Expand Down
Loading