diff --git a/tests/check_isa_test.py b/tests/check_isa_test.py new file mode 100644 index 00000000..21e6d529 --- /dev/null +++ b/tests/check_isa_test.py @@ -0,0 +1,72 @@ +"""tools/check_isa.py finds instructions above x86-64-v2 in objects and in linked images. + + python tests/check_isa_test.py + +The disassembly below is llvm-objdump -d --no-show-raw-insn output for one AVX2 function, once from an object (short +indented addresses) and once from a linked .exe (9+ hex digits at the start of the line). Standard library only. +""" + +import subprocess +import sys +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools')) +import check_isa # noqa: E402 + +OBJECT = ''' +avx.o:\tfile format coff-x86-64 + +Disassembly of section .text: + +0000000000000000 : + 0: \tvmovdqu\t(%rcx), %ymm0 + 4: \tvpaddd\t%ymm0, %ymm1, %ymm0 + 8: \tvzeroupper + b: \tretq +''' + +LINKED = ''' +avx.exe:\tfile format coff-x86-64 + +Disassembly of section .text: + +00000001400014b0 : +1400014b0: \tvmovdqu\t(%rcx), %ymm0 +1400014b4: \tvpshufd\t$0x55, %ymm0, %ymm1 # ymm1 = ymm0[1,1,1,1,5,5,5,5] +1400014b9: \tvpaddd\t%ymm0, %ymm1, %ymm0 +1400014bd: \tvzeroupper +1400014c0: \tretq + +00000001400014d0
: +1400014d0: \tsubq\t$0x28, %rsp +1400014d4: \tpopcntl\t%eax, %eax +1400014d8: \taddq\t$0x28, %rsp +1400014dc: \tretq +''' + + +def scan(text): + done = subprocess.CompletedProcess([], 0, text, '') + with mock.patch.object(check_isa.subprocess, 'run', return_value=done): + return check_isa.scan_one(Path('x'), 'llvm-objdump') + + +class ScanTest(unittest.TestCase): + def test_an_object_is_scanned(self): + hits = scan(OBJECT) + self.assertEqual(set(hits), {('avx.o', 'sum8')}) + self.assertEqual(hits[('avx.o', 'sum8')]['vpaddd'], 1) + + def test_a_linked_image_is_scanned(self): + hits = scan(LINKED) + self.assertEqual(set(hits), {('avx.exe', 'sum8')}) # main's popcnt is x86-64-v2 + self.assertEqual(sum(hits[('avx.exe', 'sum8')].values()), 4) + + def test_function_headers_and_blank_lines_are_not_instructions(self): + self.assertEqual(scan('\nDisassembly of section .text:\n\n00000001400014b0 :\n'), {}) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/run_all.py b/tests/run_all.py index 178eab4a..7b4ee957 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -48,6 +48,7 @@ TESTS = { # ---- no game files, no build ---- 'flags_check': ('.venv', None, [], ''), 'excl_mt': ('.venv', None, [], ''), + 'check_isa_test': ('.venv', None, [], ''), 'pack_shader_cache_test': ('.venv', None, [], ''), 'spin_waits_test': ('.venv', None, [], ''), 'worktree_test': ('.venv', None, [], ''), diff --git a/tools/check_isa.py b/tools/check_isa.py index e6306fdd..0f53431d 100644 --- a/tools/check_isa.py +++ b/tools/check_isa.py @@ -33,6 +33,7 @@ NON_V2 = {'andn', 'bextr', 'blsi', 'blsmsk', 'blsr', 'bzhi', 'shlx', 'shrx', 'sa 'pclmulqdq', 'rdrand', 'rdseed'} PREFIXES = {'rep', 'repe', 'repz', 'repne', 'repnz', 'lock', 'notrack', 'data16', 'cs', 'ds', 'es', 'ss', 'fs', 'gs'} WIDE = re.compile(r'%[yz]mm\d|%k[0-7]\b') +INSN = re.compile(r'^\s*[0-9a-f]+:') # Function -> the runtime check that keeps it off CPUs without the instructions. DISPATCHED = { @@ -89,7 +90,10 @@ def scan_one(path, objdump): if m: func = m.group(1) continue - if not line[:1].isspace(): + # An instruction line starts with its address: short and indented in an object, 9+ hex digits at the start of + # the line in a linked .exe or .dll. Testing for leading whitespace alone skipped every instruction of a linked + # image and reported it clean. + if not INSN.match(line): continue mn = mnemonic(line) if mn and func not in DISPATCHED and is_non_v2(line, mn):