Skip to content
Draft
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
13 changes: 13 additions & 0 deletions tests/semantic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ not a regression test. `tests/test_semantic_translator_use.py` enforces this; th
files whose subject is hand-written SPIR-V are listed there with a reason. The guard checks that a file reaches the
translator, not that every assertion depends on it.

On CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 `support.py` loads the modules of `tools/` through
`piecewise_parse.py`. There the tokenizer copies the rest of a source string at every f-string field (fixed in
3.13.16, 3.14.8 and 3.15.0, gh-155525; CI runs 3.12, which keeps it), so a process that finds no valid `.pyc` of
`tools/rdna2spirv.py` spends 7 s parsing it: after every edit of the translator (most files here set
`sys.dont_write_bytecode`, so they do not leave a new one), for each mutant of `test_mutations.py`, and always where
`PYTHONDONTWRITEBYTECODE` is set. `piecewise_parse.py` parses a source in pieces of 300 lines and joins them into the
tree a whole-file parse gives: under 1 s for the translator, and the same code object.
`tests/test_piecewise_parse.py` compares the trees for every Python file of the repository and the code objects for
some. A file that imports `rdna2spirv` before `support`, or without it, still pays the 7 s. The tests under `tests/`
that read the translator's syntax tree (`test_runtime_paths.py`, `test_isa_coverage.py`,
`test_shader_stage_resume.py`, `test_cmake_translator_deps.py`, `test_resource_hygiene.py`) call
`piecewise_parse.parse` where they would call `ast.parse`, for the same reason: no `.pyc` helps a parse.

What they check (`tools/rdna2spirv.py` on pixel, vertex, compute and other stages, wave32 and wave64, mostly one
instruction family per file; the list below describes some of them, not every file):

Expand Down
201 changes: 201 additions & 0 deletions tests/semantic/piecewise_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
"""Parse a Python source in pieces and join them into the tree a whole-file parse gives (standard library only).

tree = piecewise_parse.parse(source, filename) # equal to ast.parse(source, filename), positions included

Why: from CPython 3.12.0 (PEP 701) the tokenizer copies the rest of its input at every replacement field of an
f-string: `update_fstring_expr` in Parser/tokenizer.c (in Parser/lexer/lexer.c from 3.13, as
`_PyLexer_update_fstring_expr` and then `_PyLexer_update_ftstring_expr`) does `strlen(tok->cur)`, `PyMem_Malloc`
and `strncpy`. `import`, `compile` and `ast.parse` hand the module over as one string, so "the rest" is the rest of
the file and the time grows with the square of its size: tools/rdna2spirv.py (1.2 MB, 4,759 f-strings with about
12,700 fields) takes 7 s to parse and 0.1 s to turn into code. A file run as a script is read line by line and is
not affected. CPython 3.13.16, 3.14.8 and 3.15.0 fixed it (gh-155525); 3.12, which CI runs, only gets security fixes
now and keeps it (3.12.16 has it). `needed()` says whether the running interpreter is one that still copies.

What a field costs depends on how much source follows it, so the same statements are cheap in a short string:

* `tokenize` reads line by line (linear). It gives the depth of every logical line (INDENT/DEDENT) and its first
token. A statement starts at a logical line that does not begin with `elif`, `else`, `except` or `finally` and
does not follow a decorator line.
* Consecutive top-level statements are gathered into pieces of about PIECE_LINES lines. Each piece is parsed with
one "\\n" in front for every line before it, so every node keeps its line and column.
* A longer top-level `class` is cut between the statements of its body. The first piece, with the header, gives the
real ClassDef. Each later piece is parsed under `class _:` written on the line before it, its statements are
appended to the real body, and the class ends where the last piece's `class _:` ends (both end at the last token
of the body).

A piece starts where the whole-file tokenizer starts a logical line with the same indentation stack and ends where
it has closed every bracket and string, so the tokens are the same, and no rule of the grammar looks past a
statement except for the clauses and decorators kept together above. Whatever does not fit is not guessed at:
`parse_pieces` returns None for a source it cannot split, a piece that does not parse, or a piece in which a row
`tokenize` gave is not where a statement starts, and the caller compiles the whole source the usual way, with the
usual exception. A SyntaxWarning the parser gives for a line of a piece (an invalid escape, say) is given once,
with the file's name and line, as in a whole-file parse; only a source that then fails in a later piece gets it a
second time, from the whole-file parse that reports the error.

tests/test_piecewise_parse.py compares the trees, every field and position, for every Python file of the
repository (`ast.dump(..., include_attributes=True)` for the hand-written cases and the translator), and the code
objects for some.
"""
import ast
import importlib.util
import io
import sys
import tokenize

# tools/rdna2spirv.py on Python 3.14.7: 0.89 s in pieces of 50 or 100 lines, 0.90 s at 300, 1.10 s at 1,000,
# 1.85 s at 5,000 and 7.0 s whole. Below 300 the copying is already gone and smaller pieces only add calls.
PIECE_LINES = 300
DUMMY = "class _:\n"

_CONTINUES = frozenset(("elif", "else", "except", "finally"))
_BLANK = frozenset((tokenize.NL, tokenize.COMMENT, tokenize.ENDMARKER))


class _Whole(Exception):
"""This source is to be parsed whole."""


# What tokenizing or parsing a piece can raise for a source that a whole-file parse may still accept, or reports
# in its own words: all of them mean "parse it whole".
_GIVE_UP = (_Whole, SyntaxError, ValueError, RecursionError, MemoryError, OverflowError, tokenize.TokenError)


def needed(version=None, implementation=None):
"""True on the interpreters whose tokenizer copies the rest of the source at every f-string field: CPython
3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7 (see the module docstring). Elsewhere a whole-file parse is as
fast and simpler."""
version = tuple((sys.version_info if version is None else version)[:3])
implementation = sys.implementation.name if implementation is None else implementation
return implementation == "cpython" and ((3, 12) <= version < (3, 13, 16) or (3, 14) <= version < (3, 14, 8))


def parse(source, filename="<unknown>", piece_lines=None):
"""The module tree of `source` (str, or the bytes of a file): what `ast.parse(source, filename)` returns, and
the same exception for a source with an error. It is built from pieces where `needed()` says a whole-file parse
is the slow way, or where the caller names `piece_lines`; elsewhere the whole-file parse is the faster one."""
tree = None
if piece_lines is not None or needed():
tree = parse_pieces(source, filename, piece_lines or PIECE_LINES)
if tree is None:
tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True)
return tree


def parse_pieces(source, filename="<unknown>", piece_lines=PIECE_LINES):
"""The same tree built from pieces of about `piece_lines` lines, or None when the source has to be parsed whole
(see the module docstring). Nothing is raised for a source with an error: the whole-file parse reports it."""
if sys.version_info < (3, 12):
# Before 3.12 `tokenize` is not the parser's tokenizer (it does not apply the tab rule, for one), so the
# statements it finds prove nothing about what the parser accepts; and there is nothing to gain there.
return None
try:
if not isinstance(source, str):
source = importlib.util.decode_source(source) # as import does: coding line, BOM, newlines
if "\r" in source: # the tokenizer reads "\r\n" and "\r" as "\n"
source = source.replace("\r\n", "\n").replace("\r", "\n")
# A NUL is an error. `from __future__ import barry_as_FLUFL` changes what the parser accepts after it.
if "\0" in source or "barry_as_FLUFL" in source:
return None
rows = io.StringIO(source).readlines() # split at "\n" only, as tokenize numbers them
if len(rows) <= piece_lines: # one piece: nothing to look for
return compile(source, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True)

def statements(first, end, header=""):
"""What rows first..end-1 parse to, `header` standing on the row before them."""
text = "\n" * (first - 1 - bool(header)) + header + "".join(rows[first - 1:end - 1])
return compile(text, filename, "exec", ast.PyCF_ONLY_AST, dont_inherit=True).body

body = []
for first, end, cuts, starts in _pieces(_statements(source), len(rows), piece_lines):
found = statements(first, cuts[0] if cuts else end)
body += found
if cuts:
if len(found) != 1 or type(found[0]) is not ast.ClassDef:
raise _Whole
for at, stop in zip(cuts, cuts[1:] + [end]):
rest = statements(at, stop, DUMMY)
if len(rest) != 1 or type(rest[0]) is not ast.ClassDef:
raise _Whole
found[0].body += rest[0].body
found[0].end_lineno, found[0].end_col_offset = rest[0].end_lineno, rest[0].end_col_offset
found = found[0].body
if not _rows(found).issuperset(starts): # a row `tokenize` gave is not where a statement starts
raise _Whole
return ast.Module(body=body, type_ignores=[])
except _GIVE_UP:
return None


def _rows(nodes):
"""The rows where these statements start, decorators included. A statement after a `;` can start on a row of
its own (`b` when `a = (` is closed by `); b = 2` on the next row), so there can be more rows than logical
lines."""
return {min([node.lineno] + [d.lineno for d in getattr(node, "decorator_list", ())]) for node in nodes}


def _statements(source):
"""[row, word, rows] for every top-level statement: the row it starts on, the word its own line starts with
(the one after its decorators; None for a statement that must not be cut) and the rows where the statements
one level down start."""
tops = []
depth = 0
fresh = True # the next token that is not blank starts a logical line
before = None # (depth, first token) of the logical line before
indent = None # what the statement one level down before this one is indented by
for kind, word, (row, column), _, line in tokenize.generate_tokens(io.StringIO(source).readline):
if kind == tokenize.INDENT:
depth += 1
elif kind == tokenize.DEDENT:
depth -= 1
elif kind == tokenize.NEWLINE:
fresh = True
elif fresh and kind not in _BLANK:
fresh = False
if depth < 2:
decorated = before == (depth, "@")
if not tops and (depth or decorated or word in _CONTINUES):
raise _Whole # indented, or a clause, before any statement
if depth:
if not decorated and word not in _CONTINUES:
tops[-1][2].append(row)
if indent not in (None, line[:column]):
# Each piece of a class starts a new indentation stack, so the tokenizer's tab rule
# between two statements of its body would go unchecked: such a body is not cut.
tops[-1][1] = None
indent = line[:column]
elif decorated:
tops[-1][1] = word
elif word in _CONTINUES:
tops[-1][1] = None # several clauses: not a class
else:
tops.append([row, word, []])
indent = None
before = (depth, word)
return tops


def _pieces(tops, last_row, piece_lines):
"""(first row, row after the last, cuts, starts) of every piece, in order. For a run of whole statements `cuts`
is empty and `starts` are the rows they start on; for a class too long for one piece `cuts` are the rows where
its later pieces start and `starts` the rows where the statements of its body start."""
start, starts = 1, [] # the first piece takes the lines above the first statement
ends = [top[0] for top in tops[1:]] + [last_row + 1]
for (row, word, inner), end in zip(tops, ends):
cuts, at = [], row
if word == "class" and end - row > piece_lines:
for below in inner[1:]: # the first statement of the body stays with the header
if below - at >= piece_lines:
cuts.append(below)
at = below
if cuts:
if row > start:
yield start, row, [], starts
yield row, end, cuts, inner
start, starts = end, []
else:
starts.append(row)
if end - start >= piece_lines:
yield start, end, [], starts
start, starts = end, []
if start <= last_row:
yield start, last_row + 1, [], starts
43 changes: 43 additions & 0 deletions tests/semantic/support.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
"""Shared helpers for the translator tests (standard library only)."""
import gc
import importlib.machinery
import os
import struct
import sys
import tempfile
import warnings

import piecewise_parse

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(os.path.dirname(HERE))
# The mutation tests point this at a sabotaged copy of tools/; normally it is the repository's own.
TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools")
FIXTURE_DIR = os.path.join(HERE, "fixtures", "disasm")


class PiecewiseLoader(importlib.machinery.SourceFileLoader):
"""Compiles a module from the tree piecewise_parse.py builds: the same code object as the usual loader's,
without the 6 s CPython 3.12 to 3.14.7 spends on rdna2spirv.py's f-strings when it parses the file as one
string. A valid .pyc is still used first, and a source the pieces cannot give goes to the usual compile."""

def source_to_code(self, data, path, *args, **kwargs):
try:
tree = piecewise_parse.parse_pieces(data, path)
if tree is not None:
return super().source_to_code(tree, path, *args, **kwargs)
except Exception: # nothing to report from here: the usual compile says what is wrong, in its words
pass
return super().source_to_code(data, path, *args, **kwargs)


class ToolsFinder:
"""Hands the modules of TOOLS_DIR to PiecewiseLoader. It asks PathFinder, so sys.path decides which file a
name means exactly as it does without this finder; a name found anywhere else, or one that is not a plain
source file, is left to the finders after it."""

@staticmethod
def find_spec(name, path=None, target=None):
if path is not None or not os.path.isfile(os.path.join(TOOLS_DIR, name + ".py")):
return None
spec = importlib.machinery.PathFinder.find_spec(name, path, target)
loader = getattr(spec, "loader", None)
if type(loader) is not importlib.machinery.SourceFileLoader or not _same_folder(loader.path, TOOLS_DIR):
return None
spec.loader = PiecewiseLoader(loader.name, loader.path)
return spec


def _same_folder(path, folder):
return os.path.normcase(os.path.dirname(os.path.abspath(path))) == os.path.normcase(os.path.abspath(folder))


sys.dont_write_bytecode = True # never drop __pycache__ into the tree under test
sys.path.insert(0, TOOLS_DIR)
# Only where the tokenizer has that cost; just before PathFinder, which would otherwise load these modules.
if piecewise_parse.needed() and importlib.machinery.PathFinder in sys.meta_path:
sys.meta_path.insert(sys.meta_path.index(importlib.machinery.PathFinder), ToolsFinder)
import rdna2spirv # noqa: E402 (path set above)
from spirv_mini_interp import run_fragment # noqa: E402

Expand Down
4 changes: 3 additions & 1 deletion tests/test_cmake_translator_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
sys.path.append(str(ROOT / "tests" / "semantic"))
import make_game # noqa: E402
import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12)

TOOLS = ROOT / "tools"
SHADERS = ROOT / "runtime" / "test" / "shaders"
Expand Down Expand Up @@ -46,7 +48,7 @@ def reached():
if path in seen:
continue
seen.add(path)
for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))): # function-level imports too
for node in ast.walk(piecewise_parse.parse(path.read_text(encoding="utf-8"))): # function-level imports too
if isinstance(node, ast.Import):
mods = [a.name for a in node.names]
elif isinstance(node, ast.ImportFrom) and node.module and not node.level:
Expand Down
19 changes: 15 additions & 4 deletions tests/test_isa_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
TOOLS_DIR = os.path.join(ROOT, "tools")
if TOOLS_DIR not in sys.path:
sys.path.insert(0, TOOLS_DIR)
sys.path.append(os.path.join(ROOT, "tests", "semantic"))

import isa_coverage as ic # noqa: E402
import piecewise_parse # noqa: E402

TRANSLATOR = os.path.join(TOOLS_DIR, "rdna2spirv.py")
REAL_XML = os.environ.get(ic.ENV_XML)


def in_pieces():
"""While this is entered, the tool's `ast.parse` of the real translator is tests/semantic/piecewise_parse.py's:
the same tree in 1 s, where parsing the 1.2 MB file whole takes 7 s on CPython 3.12 (twice per scan pair)."""
return patch.object(ic.ast, "parse", lambda source, filename="<unknown>": piecewise_parse.parse(source, filename))


def _ins(name, *encodings):
encs = "".join(f"<InstructionEncoding><EncodingName>{e}</EncodingName></InstructionEncoding>" for e in encodings)
return (f"<Instruction><InstructionName>{name}</InstructionName>"
Expand Down Expand Up @@ -279,7 +287,8 @@ class TranslatorScanTests(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.scan = ic.scan_translator(TRANSLATOR)
with in_pieces():
cls.scan = ic.scan_translator(TRANSLATOR)

def test_finds_well_known_rules(self):
for name in ("v_mov_b32", "s_mov_b32", "v_cndmask_b32", "v_add_f32", "s_endpgm", "exp"):
Expand All @@ -292,7 +301,8 @@ def test_dispatch_size_sanity(self):
self.assertGreater(len(self.scan["exact"]), 300)

def test_accepted_names_matches_non_refused_exact(self):
accepted = ic.accepted_names(TRANSLATOR)
with in_pieces():
accepted = ic.accepted_names(TRANSLATOR)
self.assertEqual(set(self.scan["exact"]) - accepted, set(self.scan["refused"]))


Expand All @@ -319,8 +329,9 @@ class RealIsaTests(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.rep = ic.build_report(ic.read_isa(REAL_XML), ic.scan_translator(TRANSLATOR),
ic.accepted_names(TRANSLATOR))
with in_pieces():
cls.rep = ic.build_report(ic.read_isa(REAL_XML), ic.scan_translator(TRANSLATOR),
ic.accepted_names(TRANSLATOR))

def test_translator_names_exist_in_rdna2(self):
stray = sorted(e["name"] for e in self.rep["not_in_rdna2"] if e["name"] not in self.KNOWN_NOT_IN_XML)
Expand Down
Loading