Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,9 @@ if(EXISTS "${INTAKE_PYTHON}")
# Scalar SOPK accumulate: s_addk_i32 signed-overflow SCC and s_mulk_i32 against an integer reference.
add_test(NAME startup_gfx2hlsl_sopk_accum COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sopk_accum_check.py")
# s_pack_ll_b32_b16, s_pack_lh_b32_b16 and s_absdiff_i32 against an integer reference (#1723).
add_test(NAME startup_gfx2hlsl_pack_absdiff COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_pack_absdiff_check.py")
# gfx2hlsl run-time sampler heap indexing (#1143): translation controls, then a desktop draw with
# DXC ps_6_0 and, when the Windows SDK's FXC is found, the ps_5_1 the startup tools compile with.
find_program(STARTUP_FXC fxc HINTS "${DXC_BIN_DIR}")
Expand Down
2 changes: 2 additions & 0 deletions tests/startup_gfx2hlsl_bit_alu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def call(name, args):
return v, True
if name in ('asuint', 'uint'):
return v, False
if name == 'abs':
return (-v if v & 0x80000000 else v) & MASK, True
if name == 'min':
assert not s and not rest[0][1]
return min(v, rest[0][0]), False
Expand Down
67 changes: 67 additions & 0 deletions tests/startup_gfx2hlsl_pack_absdiff_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""s_pack_ll_b32_b16, s_pack_lh_b32_b16 and s_absdiff_i32 in tools/startup/gfx2hlsl.py (Part of #1723).

Pure Python. Each form is built as a SOP2 word, decoded, translated, and the emitted lines run on the integer
evaluator of startup_gfx2hlsl_bit_alu_check.py over edge values (the encoder, forms and value set come from
startup_gfx2hlsl_sop2_complement_check.py). The references use plain integer arithmetic from the ISA meaning:

s_pack_ll_b32_b16: D = (S0 & 0xffff) | (S1 << 16) SCC unchanged
s_pack_lh_b32_b16: D = (S0 & 0xffff) | (S1 & 0xffff0000) SCC unchanged
s_pack_hh_b32_b16: D = (S0 >> 16) | (S1 & 0xffff0000) SCC unchanged
s_absdiff_i32: D = |wrap32(S0 - S1)| (INT_MIN stays INT_MIN); SCC = (D != 0)

Lane-mask operands of the pack forms and s_absdiff_i32 are refused (the per-lane projection has no whole word to read).
"""
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import startup_gfx2hlsl_sop2_complement_check as C # noqa: E402

MASK = C.MASK


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


REFERENCES = {
's_pack_ll_b32_b16': (lambda a, b: (a & 0xffff) | ((b << 16) & MASK), False),
's_pack_lh_b32_b16': (lambda a, b: (a & 0xffff) | (b & 0xffff0000), False),
's_pack_hh_b32_b16': (lambda a, b: (a >> 16) | (b & 0xffff0000), False),
's_absdiff_i32': (lambda a, b: abs(signed((a - b) & MASK)) & MASK, True),
}


def main():
for name, (reference, scc) in REFERENCES.items():
C.OPCODE[name] = next(op for op, (n, _) in C.D.SOP2_OPS.items() if n == name)
bad = C.first_mismatch(name, reference, updates_scc=scc)
C.check('%s value and SCC' % name, bad is None, str(bad))
if name.startswith('s_pack') or name == 's_absdiff_i32':
for form in C.MASK_FORMS:
_, stage = C.attempt(C.code(name, form))
C.check('%s refuses lane mask form %r' % (name, form), stage.lines[0].startswith('refused: '))
# the wrapped difference of 0x80000000 and 1 is 0x7fffffff; a compare-first lowering gives 0x80000001
C.check('absdiff overflow reference', REFERENCES['s_absdiff_i32'][0](0x80000000, 1) == 0x7fffffff)
# a symbolic descriptor source must be refused before SCC is left stale
for name in REFERENCES:
stage = C.Probe()
stage.sym[1] = ('ptr', 0, 0)
try:
stage.one(name, '%s s3, s1, s2' % name)
except C.G.Unsupported:
C.check('%s refuses a descriptor source' % name, not stage.lines)
else:
C.check('%s refuses a descriptor source' % name, False, str(stage.lines))
# a wrong reference must be caught: swapped sources
swapped = C.first_mismatch('s_pack_lh_b32_b16', lambda a, b: REFERENCES['s_pack_lh_b32_b16'][0](b, a), False)
C.check('swapped pack_lh reference is caught', swapped is not None)
if C.failures:
print('FAILED: %s' % ', '.join(C.failures))
sys.exit(1)
print('startup_gfx2hlsl_pack_absdiff_check: ok')


if __name__ == '__main__':
main()
13 changes: 10 additions & 3 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,9 @@ def low(tok):
shift_add = base in SHIFT_ADD
word_complement = base in ('s_nand_b32', 's_nor_b32', 's_xnor_b32')
# s_mul_hi_u32 needs the whole 32-bit word of each source, like the halfword pack.
word_pack = base in ('s_pack_hh_b32_b16', 's_sext_i32_i8', 's_sext_i32_i16', 's_mul_hi_u32')
if shift_add or word_complement or word_pack:
word_pack = base in PACK_B16 or base in ('s_sext_i32_i8', 's_sext_i32_i16', 's_mul_hi_u32')
word_absdiff = base == 's_absdiff_i32'
if shift_add or word_complement or word_pack or word_absdiff:
# The per-lane mask cannot supply the full word needed by these operations. s_mul_hi_u32 into an
# SGPR that held a mask replaces it with a plain word (set_s drops the mark), so only vcc/exec count.
mask_dest = (dname in ('exec', 'exec_lo', 'exec_hi', 'vcc', 'vcc_lo', 'vcc_hi') if base == 's_mul_hi_u32'
Expand Down Expand Up @@ -1676,7 +1677,7 @@ def src_sym(t):
self.scc_effect = 'descriptor'
if self.tracks_uniformity():
self.scc_uniform = False
if shift_add or word_complement:
if shift_add or word_complement or word_absdiff:
raise Unsupported('scc from descriptor data')
if word_pack:
raise Unsupported('%s on descriptor data' % base)
Expand Down Expand Up @@ -3601,6 +3602,7 @@ def declarations(self):
SCC_UNMODELLED = frozenset(('s_add_i32', 's_add_u32', 's_sub_i32', 's_sub_u32',
's_min_i32', 's_min_u32', 's_max_i32', 's_max_u32'))

PACK_B16 = frozenset(('s_pack_ll_b32_b16', 's_pack_lh_b32_b16', 's_pack_hh_b32_b16'))
SCA = {
's_mov_b32': ('{0}', None), 's_mov_b64': ('{0}', None), 's_movk_i32': ('{0}', None),
's_and_b32': ('{0} & {1}', '$ != 0'), 's_or_b32': ('{0} | {1}', '$ != 0'), 's_xor_b32': ('{0} ^ {1}', '$ != 0'),
Expand All @@ -3621,7 +3623,12 @@ def declarations(self):
# s_sext_i32_iN: sign-extend the low N bits of S0; SCC unchanged.
's_sext_i32_i8': ('uint(int({0} << 24) >> 24)', None),
's_sext_i32_i16': ('uint(int({0} << 16) >> 16)', None),
# s_pack_*_b32_b16: two halfwords of the sources into one word; SCC unchanged.
's_pack_ll_b32_b16': ('({0} & 0xffffu) | ({1} << 16)', None),
's_pack_lh_b32_b16': ('({0} & 0xffffu) | ({1} & 0xffff0000u)', None),
's_pack_hh_b32_b16': ('({0} >> 16) | ({1} & 0xffff0000u)', None),
# s_absdiff_i32: D = S0 - S1 wrapped to 32 bits, negated if negative (INT_MIN stays); SCC = result != 0.
's_absdiff_i32': ('asuint(abs(asint({0} - {1})))', '$ != 0'),
# s_bfm_b32: a mask of S0[4:0] ones shifted left by S1[4:0]; SCC unchanged.
's_bfm_b32': ('(((1u << ({0} & 31)) - 1) << ({1} & 31))', None),
# s_lshlN_add_u32: low 32 bits of (S0 << N) + S1. SHIFT_ADD also supplies SCC for wide overflow.
Expand Down
Loading