Skip to content
Open
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
34 changes: 26 additions & 8 deletions tests/semantic/spirv_mini_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/semantic/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()