From aa8a986867f6d5b47d278ad9030e29081510cbd1 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Mon, 23 Nov 2026 02:57:11 +0100 Subject: [PATCH] tests/semantic: the mini interpreter parses a module once, not once per invocation run_workgroup runs one generator per invocation over the same SPIR-V text, and each one split and regex-matched every line again. The parsed lines are now cached by text (a line that does not parse still raises where it is reached). test_saveexec_b64 40.9 s -> 24.5 s, test_wave64_scmp_masks 38.2 s -> 19.9 s; the 366 semantic test files give the same results. --- tests/semantic/spirv_mini_interp.py | 34 ++++++++++++++++++++++------- tests/semantic/test_interp.py | 11 ++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/semantic/spirv_mini_interp.py b/tests/semantic/spirv_mini_interp.py index 044e8cf80..b7201b80d 100644 --- a/tests/semantic/spirv_mini_interp.py +++ b/tests/semantic/spirv_mini_interp.py @@ -428,6 +428,30 @@ def advance(i, value): _TICK = ("tick", None, None, None) +_PARSED = {} + + +def _parsed_lines(text): + """The (result, opcode, operands, line) of each non-blank line of `text`, parsed once: run_workgroup runs one + generator per invocation over the same module, and parsing it for every one of them dominated the run time. + A line that does not parse has opcode None; the caller raises where the line is reached, as before.""" + parsed = _PARSED.get(text) + if parsed is None: + parsed = [] + for raw in text.splitlines(): + line = raw.strip() + if not line: + continue + m = _LINE.match(line) + if m: + res, op, rest = m.groups() + parsed.append((res, op, rest.split(), line)) + else: + parsed.append((None, None, None, line)) + if len(_PARSED) >= 64: + _PARSED.clear() + _PARSED[text] = parsed + return parsed def _invocation(text, inputs, push_constants, memory, step=False): @@ -472,15 +496,9 @@ def walk(type_id, indices): raise UnsupportedConstruct(f"OpAccessChain into {k[0]}") return off, cur - for raw in text.splitlines(): - line = raw.strip() - if not line: - continue - m = _LINE.match(line) - if not m: + for res, op, a, line in _parsed_lines(text): + if op is None: raise UnsupportedConstruct(f"cannot parse line: {line!r}") - res, op, rest = m.groups() - a = rest.split() if in_body: body.append((res, op, a, line)) continue diff --git a/tests/semantic/test_interp.py b/tests/semantic/test_interp.py index 1cc9ed6bf..ca8bb12f8 100644 --- a/tests/semantic/test_interp.py +++ b/tests/semantic/test_interp.py @@ -543,6 +543,17 @@ def test_push_constant_reads_zero(self): text = HEADER + "%p = OpAccessChain %_ptr_PC_uint %pc %c0\n%res = OpLoad %uint %p\nOpStore %out %res\n" + FOOTER self.assertEqual(si.run_fragment(text)["%out"][0], 0) + def test_a_module_run_twice_gives_the_same_words_and_the_same_parse_error(self): + # run_workgroup runs one generator per invocation over one text: each must see the same module. + text = HEADER + "%res = OpIAdd %uint %c1 %c2\nOpStore %out %res\n" + FOOTER + self.assertEqual(si.run_fragment(text)["%out"][0], 3) + self.assertEqual(si.run_fragment(text)["%out"][0], 3) + broken = HEADER + "%res = OpIAdd %uint %c1 %c2\nthis is not an instruction\n" + FOOTER + for _ in range(2): + with self.assertRaises(si.UnsupportedConstruct) as ctx: + si.run_fragment(broken) + self.assertIn("this is not an instruction", str(ctx.exception)) + if __name__ == "__main__": unittest.main()