diff --git a/CMakeLists.txt b/CMakeLists.txt index 17b5b29286..b64b8cba74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2157,6 +2157,10 @@ if(EXISTS "${INTAKE_PYTHON}") # The 64-bit vector shifts (#1815). add_test(NAME startup_gfx2hlsl_b64_shift COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_b64_shift_check.py") + # The SOPK arithmetic forms s_mulk_i32 and s_addk_i32 (#1723). + add_test(NAME startup_gfx2hlsl_s_mulk_addk COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_mulk_addk_check.py") + set_tests_properties(startup_gfx2hlsl_s_mulk_addk PROPERTIES TIMEOUT 60) # SDWA forms of the float-result conversions (#1727). add_test(NAME startup_gfx2hlsl_sdwa_cvt COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_cvt_check.py") diff --git a/tests/startup_gfx2hlsl_s_mulk_addk_check.py b/tests/startup_gfx2hlsl_s_mulk_addk_check.py new file mode 100644 index 0000000000..0dca604047 --- /dev/null +++ b/tests/startup_gfx2hlsl_s_mulk_addk_check.py @@ -0,0 +1,121 @@ +"""s_mulk_i32 and s_addk_i32 in tools/startup/gfx2hlsl.py (issue #1723, Run 20 census #5). + +usage: startup_gfx2hlsl_s_mulk_addk_check.py + +The two SOPK arithmetic forms `s_mulk_i32 sN, simm16` and `s_addk_i32 sN, simm16` were refused. Both read and write +SDST and take a 16-bit immediate that is sign-extended; s_addk_i32 also sets SCC to the signed overflow of the add. + +The instruction words are constructed here from the layout the checked-in decoder reads (`tools/disasm_rdna2.py`, +SOPK branch): 0xb0000000, opcode in bits 27:23 (0x0f add, 0x10 mul), SDST in bits 22:16, SIMM16 in bits 15:0. +The emitted statements are executed on 32-bit values and compared with the architectural result. +""" +import os +import re +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +SOPK_ADDK, SOPK_MULK = 0x0F, 0x10 +MASK = 0xFFFFFFFF + + +class Pipeline: + def __init__(self): + self.resources, self.notes, self.modified = {}, set(), set() + + +class Stage(G.Stage): + """A Stage with no pipeline: enough state for a decoded instruction to be translated.""" + + def __init__(self): + self.lines, self.sym, self.masks = [], {}, set() + self.used_s, self.used_v, self.uniform_v = set(), set(), set() + self.exec_known = True + self.exports = {} + self.pipe = Pipeline() + + +def encode(op, sdst, simm): + return struct.pack('= 0x8000 else simm + asm, stage = translate(encode(SOPK_MULK, 4, simm)) + check('decodes s_mulk_i32 0x%x' % simm, asm == 's_mulk_i32 s4, 0x%x' % simm, asm) + ok = all(run(stage.lines, a)[0] == (signed(a) * imm) & MASK + for a in (0, 1, 3, 0x7FFFFFFF, 0x80000000, 0xFFFFFFFF, 0x12345678)) + check('s_mulk_i32 0x%x multiplies by the sign-extended immediate' % simm, ok, ' '.join(stage.lines)) + check('s_mulk_i32 0x%x leaves SCC alone' % simm, all('scc' not in line for line in stage.lines), ' '.join(stage.lines)) + + asm, stage = translate(encode(SOPK_ADDK, 4, simm)) + check('decodes s_addk_i32 0x%x' % simm, asm == 's_addk_i32 s4, 0x%x' % simm, asm) + ok = all(run(stage.lines, a) == expected_add(a, imm) + for a in (0, 1, 3, 0x7FFF, 0x7FFFFFFF, 0x7FFFFFF0, 0x80000000, 0x80000001, 0xFFFFFFFF, 0x12345678)) + check('s_addk_i32 0x%x adds and sets SCC to the signed overflow' % simm, ok, ' '.join(stage.lines)) + +# SCC is written from the old value, so the result write must come after it. +_asm, stage = translate(encode(SOPK_ADDK, 4, 1)) +check('s_addk_i32 reads the old destination for SCC', stage.lines[0].startswith('scc =') and stage.lines[1].startswith('s4 ='), + ' '.join(stage.lines)) + +# A destination that is not a plain SGPR is refused rather than lowered wrongly. +refused = True +try: + translate(encode(SOPK_MULK, 106, 2)) +except G.Unsupported: + pass +else: + refused = False +check('a non-SGPR destination is refused', refused) + +if failures: + print('FAIL') + for text in failures: + print(' ' + text) + sys.exit(1) +print('PASS') diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 4b0bd4fb48..495ac96fbb 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -1750,6 +1750,38 @@ def handler(self, ops, mods): setattr(Stage, 'op_s_cmpk_%s_%s' % (_name, _typ), _make()) +def _sopk_arith(self, base, scalar_op, ops, mods): + """s_mulk_i32 / s_addk_i32: SDST = SDST op sign_extend(SIMM16), through the two-source scalar lowering. + + The decoder prints the 16-bit immediate as unsigned hex; it is sign-extended here. s_addk_i32 also sets SCC + to the signed overflow of the add, which the plain add lowering does not model.""" + if mods or len(ops) != 2: + raise Unsupported('%s with %d operands' % (base, len(ops))) + reg = reg_range(ops[0]) + if not reg or reg[0] != 's' or reg[1] != reg[2] or reg[1] > 105: + raise Unsupported('%s destination %s' % (base, ops[0])) + try: + imm = int(ops[1], 0) + except ValueError: + raise Unsupported('%s immediate %s' % (base, ops[1])) + if not -0x8000 <= imm <= 0xffff: + raise Unsupported('%s immediate %s outside 16 bits' % (base, ops[1])) + if imm >= 0x8000: + imm -= 0x10000 + word = imm & 0xffffffff + if base == 's_addk_i32': + if self.sym.get(reg[1]): + raise Unsupported('scc from descriptor data') + old = self.u(ops[0]) + self.emit('scc = ((%s ^ 0x%08xu) & 0x80000000u) == 0u && ((%s ^ (%s + 0x%08xu)) & 0x80000000u) != 0u;' + % (old, word, old, old, word)) + return self.scalar(scalar_op, [ops[0], ops[0], '0x%08x' % word]) + + +Stage.op_s_mulk_i32 = lambda self, ops, mods: _sopk_arith(self, 's_mulk_i32', 's_mul_i32', ops, mods) +Stage.op_s_addk_i32 = lambda self, ops, mods: _sopk_arith(self, 's_addk_i32', 's_add_i32', ops, mods) + + def parse_root(blob): if not blob or blob[:4] != b'DXBC': raise Unsupported('root signature blob missing')