diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 3fcb4ae72f..62b8149792 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -228,6 +228,7 @@ jobs: tests/startup_stream_fetch_check.py \ tests/compute_entry_layout_check.py \ tests/startup_ngg_slice_check.py \ + tests/startup_gfx2hlsl_handler_arity_check.py \ tests/run_checks_parallel_check.py build-and-test: name: Build and test on Windows diff --git a/AGENTS.md b/AGENTS.md index 9eda0505e0..e26a66fb2a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -182,6 +182,7 @@ python3 -m venv out/pe-env ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_f32_lg_check.py ./out/pe-env/bin/python -B tests/startup_gfx2hlsl_f32_nan_check.py ./out/pe-env/bin/python -B tests/run_checks_parallel_check.py +./out/pe-env/bin/python -B tests/startup_gfx2hlsl_handler_arity_check.py ``` `tests/pixel_aot_check.py binary` additionally needs LLVM with gfx1013 support diff --git a/CMakeLists.txt b/CMakeLists.txt index ebe52ac519..a61793e2fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2216,6 +2216,8 @@ if(EXISTS "${INTAKE_PYTHON}") # gfx2hlsl lowers s_bitcmp1_b32 and s_bitcmp0_b32 (SOPC): CPU evaluation of the translated SCC test. add_test(NAME startup_gfx2hlsl_s_bitcmp COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_s_bitcmp_check.py") + add_test(NAME startup_gfx2hlsl_handler_arity COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_handler_arity_check.py") # VOP1/VOP2 sub-dword addressing (SDWA) in the translator (#1141), checked against the pixel AOT # lowering of the same selections. add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B diff --git a/tests/startup_gfx2hlsl_handler_arity_check.py b/tests/startup_gfx2hlsl_handler_arity_check.py new file mode 100644 index 0000000000..07374227a6 --- /dev/null +++ b/tests/startup_gfx2hlsl_handler_arity_check.py @@ -0,0 +1,47 @@ +"""Verify scalar operand counts through the startup translator's Stage entry point.""" +import sys +from pathlib import Path +from types import SimpleNamespace + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE.parent / 'tools')) +sys.path.insert(0, str(HERE.parent / 'tools' / 'startup')) +import gfx2hlsl as G # noqa: E402 + + +def translate(name, operands): + pipe = SimpleNamespace(root={'params': [], 'flags': 0}) + text = ' '.join((name, ', '.join(operands))).strip() + refusals = [] + stage = G.Stage(pipe, 'ps', b'', refusals=refusals, decoded=[(0, 4, name, text)]) + return refusals, stage.blocks + + +def check(name, operands, expected_output): + refusals, blocks = translate(name, operands) + assert not refusals, (name, operands, refusals) + assert any(expected_output in line for _, lines, _ in blocks for line in lines), (name, blocks) + + for invalid in ((), operands[:-1], (*operands, 's18')): + refusals, blocks = translate(name, invalid) + expected = 'with %d operands, expected %d' % (len(invalid), len(operands)) + assert len(refusals) == 1 and expected in refusals[0][0], (name, invalid, refusals) + assert not blocks, (name, invalid, blocks) + + +def main(): + for condition in ('eq', 'lg', 'gt', 'ge', 'lt', 'le'): + for kind in ('i32', 'u32'): + check(f's_cmp_{condition}_{kind}', ('s12', 's14'), 'scc = ') + check(f's_cmpk_{condition}_{kind}', ('s12', '5'), 'scc = ') + + for width, dst, src in ((32, 's12', 's14'), (64, 's[12:13]', 's[14:15]')): + for operation in ('and_saveexec', 'or_saveexec', 'andn2_wrexec'): + check(f's_{operation}_b{width}', (dst, src), 'exec = ') + check(f's_cselect_b{width}', (dst, src, '1'), 'scc ? ') + check('s_andn2_saveexec_b64', ('s[12:13]', 's[14:15]'), 'exec = ') + print('startup gfx2hlsl scalar arity: 33 accepted instructions and 99 refusals') + + +if __name__ == '__main__': + main() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6491fa63bd..2bb6b35d7a 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -906,6 +906,8 @@ def src_sym(t): return None def op_s_and_saveexec_b64(self, ops, mods, text='exec & {0}'): + if len(ops) != 2: + raise Unsupported('saveexec with %d operands, expected 2' % len(ops)) dst = reg_range(ops[0]) if not dst or dst[0] != 's': raise Unsupported('saveexec destination %s' % ops[0]) @@ -929,6 +931,8 @@ def op_s_andn2_saveexec_b64(self, ops, mods): self.op_s_and_saveexec_b64(ops, mods, '{0} & ~exec') def op_s_andn2_wrexec_b64(self, ops, mods): + if len(ops) != 2: + raise Unsupported('wrexec with %d operands, expected 2' % len(ops)) dst = reg_range(ops[0]) if not dst or dst[0] != 's': raise Unsupported('wrexec destination %s' % ops[0]) @@ -950,6 +954,8 @@ def op_s_cselect_b64(self, ops, mods): self.scalar_select(ops, True) def scalar_select(self, ops, pair): + if len(ops) != 3: + raise Unsupported('scalar select with %d operands, expected 3' % len(ops)) low = [('s%d' % reg_range(t)[1]) if reg_range(t) and reg_range(t)[0] == 's' else t for t in ops] if pair and 'm0' in low: raise Unsupported('64-bit scalar operand m0') @@ -1973,8 +1979,12 @@ def declarations(self): SHIFT_ADD = {'s_lshl1_add_u32': 1, 's_lshl2_add_u32': 2, 's_lshl3_add_u32': 3, 's_lshl4_add_u32': 4} for _name, _op in (('eq', '=='), ('lg', '!='), ('gt', '>'), ('ge', '>='), ('lt', '<'), ('le', '<=')): for _typ, _cast in (('i32', 'int({0}) %s int({1})'), ('u32', '{0} %s {1}')): - def _make(op=_op, cast=_cast): + def _make(op=_op, cast=_cast, name='s_cmp_%s_%s' % (_name, _typ)): def handler(self, ops, mods): + # A scalar compare reads exactly two sources; refusing any other count keeps a malformed + # instruction a refusal rather than an index error in the batch translator. + if len(ops) != 2: + raise Unsupported('%s with %d operands, expected 2' % (name, len(ops))) self.set_scc((cast % op).format(self.u(ops[0]), self.u(ops[1]))) return handler setattr(Stage, 'op_s_cmp_%s_%s' % (_name, _typ), _make())