You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2026/09/24 13:00:37 UTC
(tvm) branch script/canonical-parser-df updated: [REFACTOR][TVMSCRIPT] Simplify builder output and organize language coverage
This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch script/canonical-parser-df
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/script/canonical-parser-df by this push:
new b2f9790e25 [REFACTOR][TVMSCRIPT] Simplify builder output and organize language coverage
b2f9790e25 is described below
commit b2f9790e2568a56ed8de9ebf4be7959b20036ba3
Author: Tianqi Chen <tq...@users.noreply.github.com>
AuthorDate: Thu Sep 24 12:59:35 2026 +0000
[REFACTOR][TVMSCRIPT] Simplify builder output and organize language coverage
Introduce a modular, customizable parser infrastructure for TVMScript through transpilation. The key idea is to deterministically transpile language syntax into a corresponding sequence of builder calls while preserving source locations and providing span context.
Organize shared script coverage around real language examples and distinct failures.
Key design elements include:
- Acquire source and its definition scope, collect syntax facts once, rewrite the owned AST, and execute the resulting builder program with the original lexical bindings.
- Each language variant customizes behavior through a `parser_protocol.py` file, which defines special handling for syntax such as `for` and `if`.
- Create reusable span entries during rewriting. Attach locations to meaningful expressions and pass spans directly to builder operations that own them.
- Reuse existing builder and IR APIs, so new types and functions can be added without changing the parser, provided they have well-defined builder behavior.
- Generate specialization handling only when specialization is requested.
For example, this kernel:
```python
@T.prim_func
def add_one(A: T.Buffer((128,), "float32"), B: T.Buffer((16, 8), "float32")):
for i in range(128):
B[i // 8, i % 8] = A[i] + 1
```
is lowered to the following builder calls. This presentation wraps the emitted program in `build_add_one` and gives its local scaffolding readable aliases. `I` supplies shared builders, `T` is the dialect namespace, `_S` holds source-span entries, and `_host0` and `_host1` are captured annotation helpers.
```python
def build_add_one():
with I.IRBuilder() as builder:
with T.function_(span=_S[6]) as fn:
T.func_name('add_one')
_parameter0 = T.arg('A', _host0(_S[1](T.Buffer(_S[0]((128,)), 'float32'))), span=_S[2])
_parameter1 = T.arg('B', _host1(_S[4](T.Buffer(_S[3]((16, 8)), 'float32'))), span=_S[5])
def build_body():
global T
A, B = fn.params
with T.for_(T.range_(128), span=_S[7], names='i') as i:
_value0 = _S[9](_S[8](A[i]) + 1)
T.setitem_(B, (_S[10](i // 8), _S[11](i % 8)), _value0, span=_S[12])
build_body()
result = fn.function
result.__name__ = 'add_one'
T.check_well_formed_(result)
return fn.function
```
---
python/tvm/relax/script/builder/__init__.py | 4 +-
python/tvm/relax/script/builder/parser_protocol.py | 4 +-
python/tvm/script/ir_builder/ir/parser_protocol.py | 4 +-
python/tvm/script/parser/entry.py | 3 +
python/tvm/script/parser/protocol_registry.py | 8 +-
python/tvm/script/parser/transpile.py | 85 +-
python/tvm/tirx/script/builder/__init__.py | 4 +-
python/tvm/tirx/script/builder/parser_protocol.py | 4 +-
tests/python/script/conftest.py | 25 +-
.../script/{dummy_builder.py => minilang.py} | 66 +-
tests/python/script/test_basic_usage.py | 1044 ++++++++++++++++++++
.../python/script/test_capture_definition_scope.py | 420 --------
tests/python/script/test_capture_frontend_scope.py | 232 -----
tests/python/script/test_capture_gallery_source.py | 134 ---
tests/python/script/test_capture_nonlocal.py | 66 --
tests/python/script/test_cross_function_call.py | 83 ++
tests/python/script/test_error_handling.py | 165 ++++
tests/python/script/test_jit_specialization.py | 187 ++++
tests/python/script/test_meta_programming.py | 478 +++++++++
tests/python/script/test_parser.py | 781 ---------------
.../python/script/test_parser_alias_restoration.py | 84 --
.../script/test_parser_bypass_source_attachment.py | 34 -
tests/python/script/test_parser_call_scopes.py | 126 ---
.../python/script/test_parser_capture_lifetime.py | 340 -------
.../script/test_parser_comparison_semantics.py | 388 --------
tests/python/script/test_parser_entry.py | 87 ++
tests/python/script/test_parser_host_selection.py | 242 -----
tests/python/script/test_parser_latest_design.py | 500 ----------
tests/python/script/test_parser_scope_helpers.py | 127 ---
tests/python/script/test_parser_scoped_symbols.py | 94 --
.../python/script/test_parser_shared_resolvers.py | 127 ---
tests/python/script/test_parser_span_helpers.py | 216 ----
tests/python/script/test_source_locations.py | 460 +++++++++
...contract.py => test_special_parser_protocol.py} | 52 +-
tests/python/script/test_symbolic_shape.py | 191 ++++
35 files changed, 2868 insertions(+), 3997 deletions(-)
diff --git a/python/tvm/relax/script/builder/__init__.py b/python/tvm/relax/script/builder/__init__.py
index e0a76673df..6d3f1eed09 100644
--- a/python/tvm/relax/script/builder/__init__.py
+++ b/python/tvm/relax/script/builder/__init__.py
@@ -72,7 +72,7 @@ from .parser_protocol import (
return_,
set_mutable_cell_,
setattr,
- setitem,
+ setitem_,
unpack,
)
@@ -256,7 +256,7 @@ __all__ = [
"is_type_var",
"match_cast",
"return_",
- "setitem",
+ "setitem_",
"type_var",
"unpack",
]
diff --git a/python/tvm/relax/script/builder/parser_protocol.py b/python/tvm/relax/script/builder/parser_protocol.py
index e9d631f474..2a88efc25e 100644
--- a/python/tvm/relax/script/builder/parser_protocol.py
+++ b/python/tvm/relax/script/builder/parser_protocol.py
@@ -263,8 +263,8 @@ def continue_(*, span: _Span = None) -> NoReturn:
raise TypeError("Relax does not support continue")
-def setitem(target: Any, index: Any, value: Any, *, span: _Span = None) -> NoReturn:
- """Implements :func:`tvm.script.ir_builder.ir.parser_protocol.setitem`."""
+def setitem_(target: Any, index: Any, value: Any, *, span: _Span = None) -> NoReturn:
+ """Implements :func:`tvm.script.ir_builder.ir.parser_protocol.setitem_`."""
raise TypeError("Relax does not support indexed assignment")
diff --git a/python/tvm/script/ir_builder/ir/parser_protocol.py b/python/tvm/script/ir_builder/ir/parser_protocol.py
index d8cf54d10a..39779390f7 100644
--- a/python/tvm/script/ir_builder/ir/parser_protocol.py
+++ b/python/tvm/script/ir_builder/ir/parser_protocol.py
@@ -706,7 +706,7 @@ def return_(value: Any = None, *, span: _Span = None) -> None:
raise NotImplementedError
-def setitem(target: Any, key: Any, value: Any, *, span: _Span = None) -> None:
+def setitem_(target: Any, key: Any, value: Any, *, span: _Span = None) -> None:
"""Apply an indexed assignment using already-evaluated operands.
Parameters
@@ -738,7 +738,7 @@ def setitem(target: Any, key: Any, value: Any, *, span: _Span = None) -> None:
# Source
A[i] = value
# Generated builder
- X.setitem(A, i, value)
+ X.setitem_(A, i, value)
"""
raise NotImplementedError
diff --git a/python/tvm/script/parser/entry.py b/python/tvm/script/parser/entry.py
index 32e6c5325c..6fbbb7e2d5 100644
--- a/python/tvm/script/parser/entry.py
+++ b/python/tvm/script/parser/entry.py
@@ -145,6 +145,9 @@ def _recompose_builder(
reference = ast.copy_location(ast.Name(alias, ast.Load()), body)
body.args.kwonlyargs.append(ast.arg(captured))
body.args.kw_defaults.append(reference)
+ global_names.intersection_update(
+ item.id for item in ast.walk(body) if isinstance(item, ast.Name)
+ )
if global_names:
body.body.insert(0, ast.copy_location(ast.Global(sorted(global_names)), body))
diff --git a/python/tvm/script/parser/protocol_registry.py b/python/tvm/script/parser/protocol_registry.py
index 5aee83946e..c3d59b356d 100644
--- a/python/tvm/script/parser/protocol_registry.py
+++ b/python/tvm/script/parser/protocol_registry.py
@@ -136,11 +136,11 @@ def args_policy(
.. code:: python
- @args_policy("X.tensor", {"shape": "expr_str"})
- def tensor(shape):
+ @args_policy("M.Tensor", {"shape": "expr_str"})
+ def Tensor(shape):
...
- # Source: X.tensor(("n",))
- # Builder: X.tensor((X.resolve_type_var_("n"),))
+ # Source: M.Tensor(("n",))
+ # Builder: M.Tensor((M.resolve_type_var_("n"),))
"""
fields = dict(fields)
unsupported = set(fields.values()).difference(("expr_str", "global_info"))
diff --git a/python/tvm/script/parser/transpile.py b/python/tvm/script/parser/transpile.py
index f8e584bc2a..a68f286714 100644
--- a/python/tvm/script/parser/transpile.py
+++ b/python/tvm/script/parser/transpile.py
@@ -404,6 +404,10 @@ class IRBuilderTranspiler(ast.NodeTransformer):
and isinstance(node.ctx, ast.Load)
and node.id in self.function.annotation_aliases
):
+ if node.id in self.module.prescan.namespaces:
+ return ast.copy_location(
+ ast.Name(self.function.annotation_aliases[node.id], ast.Load()), node
+ )
return self._call(
self.module.infrastructure_name,
"require_defined",
@@ -909,7 +913,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
# a[start:stop:step] = value
#
# Builder:
- # X.setitem(a, slice(start, stop, step), value)
+ # X.setitem_(a, slice(start, stop, step), value)
# -------------------------------------------------
if isinstance(node, ast.Slice):
return ast.copy_location(
@@ -1076,10 +1080,10 @@ class IRBuilderTranspiler(ast.NodeTransformer):
# a[index] = value
#
# Builder:
- # X.setitem(a, index, value)
+ # X.setitem_(a, index, value)
# -------------------------------------------------
value = self._call_dialect(
- "setitem",
+ "setitem_",
[self.visit(target.value), self._rewrite_index(target.slice), value],
statement,
)
@@ -1203,7 +1207,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
# base = a
# key = index
# old = base[key]
- # X.setitem(base, key, old + value)
+ # X.setitem_(base, key, old + value)
# -------------------------------------------------
# Base, index, load and RHS each evaluate once; name targets use declaration dispatch.
if self.bypass_ast_rewrite:
@@ -1231,7 +1235,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
statements.append(self._assign(index, self._rewrite_index(node.target.slice), node))
key = ast.Name(index, ast.Load())
load = ast.Subscript(ast.Name(base, ast.Load()), key, ast.Load())
- operation = "setitem"
+ operation = "setitem_"
old = self.module.fresh("_old")
statements.append(self._assign(old, self._attach_span(load, node.target), node))
value = self._attach_span(
@@ -1674,6 +1678,22 @@ class IRBuilderTranspiler(ast.NodeTransformer):
return None, ast.Dict([], [])
self._raise_error(node, f"Function {node.name!r} has no construction namespace")
+ def _function_namespace(self, node: ast.FunctionDef, namespace: object) -> str:
+ """Keep a fixed source alias when definition and execution scopes agree."""
+ references = (
+ reference for source in [*node.decorator_list, node] for reference in ast.walk(source)
+ )
+ for reference in references:
+ if (
+ isinstance(reference, ast.Name)
+ and reference.id in self.module.prescan.namespaces
+ and self.module.environment.get(reference.id) is namespace
+ and self.module.bindings.get(reference.id) is namespace
+ ):
+ return reference.id
+ # Implicit roots and conflicting definition scopes need an injected name.
+ return self._inject(namespace, "_X").id
+
def _read_function_annotations(
self, node: ast.FunctionDef, parameters: list[ast.arg], facts: list[Binding]
) -> tuple[list[ast.expr | None], ast.expr | None, dict[str, str]]:
@@ -1956,8 +1976,8 @@ class IRBuilderTranspiler(ast.NodeTransformer):
frame: str,
local_function: bool,
split_declare: bool,
- ) -> list[ast.stmt]:
- """Create the native frame and optionally execute its declaration pass."""
+ ) -> tuple[list[ast.stmt], ast.withitem]:
+ """Create a declaration pass, if needed, and the body frame entry."""
# -------------------- Pattern --------------------
# Python source:
# @X.function
@@ -1970,7 +1990,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
# parameter = X.arg("x", X.int32)
# f = frame.global_var
# -------------------------------------------------
- keywords = [ast.keyword(None, options)]
+ keywords = [ast.keyword(None, options)] if options.keys else []
if split_declare:
keywords.append(ast.keyword("decl", ast.Constant(True)))
if local_function:
@@ -1996,12 +2016,15 @@ class IRBuilderTranspiler(ast.NodeTransformer):
"local_var" if local_function else "global_var",
ast.Load(),
)
- return [
- ast.copy_location(declaration_scope, node),
- self._assign(node.name, reference, node),
- ]
+ return (
+ [
+ ast.copy_location(declaration_scope, node),
+ self._assign(node.name, reference, node),
+ ],
+ ast.withitem(ast.Name(frame, ast.Load())),
+ )
# Ordinary standalone functions enter once for their signature and body.
- return [self._assign(frame, constructor, node)]
+ return [], ast.withitem(constructor, ast.Name(frame, ast.Store()))
def _create_body_parameters(
self,
@@ -2089,8 +2112,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
ordinary body reads retain Python globals/closures. None owns native IR.
"""
kind, options = self.read_function_metadata(node)
- builder = self.module.fresh("_X")
- self.module.bindings[builder] = kind
+ builder = self._function_namespace(node, kind)
frame, body_name = self.module.fresh("_fn"), self.module.fresh("_build")
# Keep the shared module context, but activate fresh lexical state for this
# function. Saving its caller locally avoids a context ownership back-reference.
@@ -2147,28 +2169,35 @@ class IRBuilderTranspiler(ast.NodeTransformer):
definition_aliases.update(self.function.annotation_aliases)
with self._bypass_rewrite():
options = self.visit(options)
- statements.extend(
- self._create_function_frame(
- node,
- options,
- declaration,
- frame=frame,
- local_function=local_function,
- split_declare=split_declare,
- )
+ frame_declaration, body_entry = self._create_function_frame(
+ node,
+ options,
+ declaration,
+ frame=frame,
+ local_function=local_function,
+ split_declare=split_declare,
)
+ statements.extend(frame_declaration)
body = self._create_body_parameters(
node, parameters, constexpr_aliases, frame=frame, special=special
)
+ body.extend(self.transform_statements(node.body))
if not local_function and node.name not in self.module.source_functions:
# Source-text bodies use the supplied environment, not
# definition-only locals introduced for their annotations.
+ referenced = {
+ item.id
+ for statement in body
+ for item in ast.walk(statement)
+ if isinstance(item, ast.Name)
+ }
global_names = sorted(
- name for name, alias in definition_aliases.items() if name == alias
+ name
+ for name, alias in definition_aliases.items()
+ if name == alias and name in referenced
)
if global_names:
body.insert(0, ast.copy_location(ast.Global(global_names), node))
- body.extend(self.transform_statements(node.body))
definition = self._create_definition(body_name, body, node)
if not local_function:
protected = (
@@ -2197,7 +2226,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
)
resumed = ast.copy_location(
ast.With(
- [ast.withitem(ast.Name(frame, ast.Load()))],
+ [body_entry],
[*(() if split_declare else declaration), definition, invocation],
),
node,
@@ -2381,7 +2410,7 @@ class IRBuilderTranspiler(ast.NodeTransformer):
namespace = (
self.module.infrastructure_name
if is_module
- else self._inject(self.read_function_metadata(root)[0], "_X").id
+ else self._function_namespace(root, self.read_function_metadata(root)[0])
)
translated.append(
ast.copy_location(
diff --git a/python/tvm/tirx/script/builder/__init__.py b/python/tvm/tirx/script/builder/__init__.py
index 141995a8bb..df83450d41 100644
--- a/python/tvm/tirx/script/builder/__init__.py
+++ b/python/tvm/tirx/script/builder/__init__.py
@@ -31,6 +31,7 @@ from tvm.script.parser.protocol_registry import constexpr as constexpr
from tvm.script.parser.protocol_registry import (
mutable_cell_decl as _mutable_cell_decl,
)
+from tvm.script.parser.protocol_registry import result_span as _result_span
from tvm.tirx.lang.alloc_pool import SMEMPool as SMEMPool
from tvm.tirx.lang.alloc_pool import TMEMPool as TMEMPool
@@ -72,7 +73,7 @@ from .parser_protocol import (
return_,
set_mutable_cell_,
setattr,
- setitem,
+ setitem_,
unpack,
)
from .tirx import cluster as cluster
@@ -99,6 +100,7 @@ def type_var(name, *, dtype=None, span=None):
return _ir.Var(name, "int64" if dtype is None else dtype, _source_span(span))
+@_result_span("T.Buffer")
@_mutable_cell_decl("T.Buffer", syntax="parameter")
@_args_policy(
"T.Buffer",
diff --git a/python/tvm/tirx/script/builder/parser_protocol.py b/python/tvm/tirx/script/builder/parser_protocol.py
index 030226603c..e17e19b402 100644
--- a/python/tvm/tirx/script/builder/parser_protocol.py
+++ b/python/tvm/tirx/script/builder/parser_protocol.py
@@ -298,8 +298,8 @@ def func_ret_type(annotation: Any, *, span: _Span = None) -> None:
return _native.func_ret(annotation)
-def setitem(target: Any, key: Any, value: Any, *, span: _Span = None) -> None:
- """Implements :func:`tvm.script.ir_builder.ir.parser_protocol.setitem`."""
+def setitem_(target: Any, key: Any, value: Any, *, span: _Span = None) -> None:
+ """Implements :func:`tvm.script.ir_builder.ir.parser_protocol.setitem_`."""
_base.at_(span, _builder.buffer_store(target, value, key))
diff --git a/tests/python/script/conftest.py b/tests/python/script/conftest.py
index 75e35cd567..b9354ab8f2 100644
--- a/tests/python/script/conftest.py
+++ b/tests/python/script/conftest.py
@@ -17,7 +17,7 @@
"""Recording builder fixtures for the production source-to-builder parser."""
import pytest
-from dummy_builder import Language, RecordingSpanEntry
+from minilang import Language, RecordingSpanEntry
from tvm.script.ir_builder import base
from tvm.script.parser import entry, protocol_registry
@@ -41,7 +41,26 @@ def spanned_language(language, monkeypatch):
monkeypatch.setattr(entry, "SpanEntry", base.SpanEntry)
language.I.at_ = base.at_
language.I.with_at_group_ = base.with_at_group_
- language.X.inline = protocol_registry.declaration_kind("X.inline", "helper")(
- entry.make_macro_decorator(language.X)
+ language.M.inline = protocol_registry.declaration_kind("M.inline", "helper")(
+ entry.make_macro_decorator(language.M)
+ )
+ return language
+
+
+@pytest.fixture
+def primitive_language(spanned_language):
+ """Use native primitive operators for minilang expression examples."""
+ from functools import reduce
+
+ from tvm.ir import prim
+
+ language = spanned_language
+ for kind in ("LT", "LE", "GT", "GE", "EQ", "NE"):
+ constructor = getattr(prim._ffi_api, "_Op" + kind)
+ setattr(
+ language.M, kind.lower() + "_", lambda lhs, rhs, make=constructor: make(lhs, rhs, None)
+ )
+ language.M.and_ = lambda *conditions: reduce(
+ lambda rhs, lhs: prim.And(lhs, rhs), reversed(conditions)
)
return language
diff --git a/tests/python/script/dummy_builder.py b/tests/python/script/minilang.py
similarity index 86%
rename from tests/python/script/dummy_builder.py
rename to tests/python/script/minilang.py
index 195e61d8a8..8ae88c245d 100644
--- a/tests/python/script/dummy_builder.py
+++ b/tests/python/script/minilang.py
@@ -29,6 +29,7 @@ from dataclasses import dataclass, field
from types import SimpleNamespace
from tvm.script.ir_builder import IRBuilder
+from tvm.script.ir_builder.base import MISSING
from tvm.script.parser import entry
from tvm.script.parser import protocol_registry as registry
@@ -87,14 +88,12 @@ class Frame:
def __enter__(self):
self.language.stack.append(self)
- self.language.events.append(("enter", self.kind, self.decl, self))
if self.kind == "for":
return self.vars[0] if len(self.vars) == 1 else self.vars
return self
def __exit__(self, error_type, error, traceback):
assert self.language.stack.pop() is self
- self.language.events.append(("exit", self.kind, self.decl, self))
if self.kind == "function":
if self.decl:
self.decl = False
@@ -144,12 +143,12 @@ class RecordingSpanEntry:
class Language:
- """One recording builder namespace X and shared infrastructure namespace I."""
+ """One recording source namespace M and shared infrastructure namespace I."""
def __init__(self):
- self.events, self.stack, self.functions = [], [], Module()
+ self.stack, self.functions = [], Module()
self.references = {}
- self.missing = object()
+ self.missing = MISSING
self.result = None
self.source_stack = []
self.global_infos = {}
@@ -166,14 +165,22 @@ class Language:
check_well_formed_=lambda result: None,
constexpr=registry.constexpr,
)
- self.X = SimpleNamespace(
+ self.M = SimpleNamespace(
supports_mutable_declarations=True,
function_=lambda **kwargs: Frame(self, "function", **kwargs),
func_name=self.func_name,
arg=self.arg,
func_ret_type=self.func_ret_type,
func_ret_value=self.func_ret_value,
- return_=lambda value, **kwargs: self.func_ret_value(value),
+ return_=lambda value=None, **kwargs: self.func_ret_value(value),
+ setitem_=self.setitem_,
+ setattr=self.setattr,
+ unpack=lambda value: value,
+ assert_=lambda condition, message="", **kwargs: self.statement(
+ "assert", condition, message
+ ),
+ break_=lambda **kwargs: self.statement("break"),
+ continue_=lambda **kwargs: self.statement("continue"),
resolve_type_var_=self.resolve_type_var,
bind_=self.bind,
check_well_formed_=lambda result: None,
@@ -202,16 +209,16 @@ class Language:
def operation(*args, name=name):
return Value(name, args)
- setattr(self.X, name + "_", operation)
- entry.register_namespace("X", self.X)
- self.X.script = registry.declaration_kind("X.script", "function")(
- entry.make_decorator(self.X)
+ setattr(self.M, name + "_", operation)
+ entry.register_namespace("M", self.M)
+ self.M.function = registry.declaration_kind("M.function", "function")(
+ entry.make_decorator(self.M)
)
@registry.args_policy(
- "X.tensor", {"shape": "expr_str", "device": "global_info"}, scalar_strings=False
+ "M.Tensor", {"shape": "expr_str", "device": "global_info"}, scalar_strings=False
)
- def tensor(shape=None, dtype="float32", device=None, placement="S[0]"):
+ def Tensor(shape=None, dtype="float32", device=None, placement="S[0]"):
return Value("tensor", (shape, dtype, device, placement))
def symbol(expr=None):
@@ -220,9 +227,9 @@ class Language:
def cell(value=None):
return Value("cell", (value,))
- self.X.tensor = tensor
- self.X.symbol = registry.register_type_var_decl("X.symbol", symbol, dtype="int64")
- self.X.cell = registry.mutable_cell_decl("X.cell")(cell)
+ self.M.Tensor = Tensor
+ self.M.symbol = registry.register_type_var_decl("M.symbol", symbol, dtype="int64")
+ self.M.cell = registry.mutable_cell_decl("M.cell")(cell)
@contextmanager
def context(self):
@@ -243,7 +250,6 @@ class Language:
def func_name(self, name):
self.frame().function.name = name
self.frame().global_var = self.references.setdefault(name, Value("global", (name,)))
- self.events.append(("name", name))
def arg(self, name, annotation, *, span=None, **kwargs):
value = Value("arg", (annotation,), name)
@@ -252,27 +258,22 @@ class Language:
frame = self.frame()
frame.params.append(value)
frame.function.params.append(value)
- self.events.append(("arg", name, value))
return value
def func_ret_type(self, annotation):
self.frame().function.ret_type = annotation() if callable(annotation) else annotation
def func_ret_value(self, value):
- self.events.append(("return", value))
self.frame().function.body.append(("return", value))
def resolve_type_var(self, name, dtype=None, **kwargs):
value = self.frame().resolve_type_var(name, dtype, **kwargs)
- self.events.append(("symbol", name, value))
return value
def resolve_global_info(self, name):
- self.events.append(("global_info", name))
return self.global_infos[name]
def bind(self, value, *, name=None, **kwargs):
- self.events.append(("bind", name, value))
if self.stack[-1].kind in ("then", "else"):
self.stack[-1].result = value
return value
@@ -280,20 +281,18 @@ class Language:
def emit(self, value, *, span=None):
if span is not None:
value = span(value)
- self.events.append(("emit", value))
self.frame().function.body.append(("emit", value))
def record(self, value):
- self.events.append(("record", value))
return value
def decl_mutable(self, value=None, *, name=None, **kwargs):
value = Value("cell", (value,), name)
- self.events.append(("declare", name, value))
+ self.statement("declare", name, value)
return value
def set_mutable(self, variable, value, **kwargs):
- self.events.append(("set", variable, value))
+ self.statement("set", variable, value)
return variable
def for_frame(self, frame, *, names=None, span=None, **kwargs):
@@ -317,7 +316,6 @@ class Language:
variable.name = name
if span is not None:
span(frame)
- self.events.append(("loop_names", names))
return frame
def at(self, location, value):
@@ -333,7 +331,13 @@ class Language:
finally:
self.source_stack.pop()
- def parse(self, source, **captures):
- return entry.parse(
- source, extra_vars={"X": self.X, **captures}, filename="dummy.py", root_builder=self.X
- )
+ def statement(self, kind, *operands):
+ self.frame().function.body.append((kind, operands))
+
+ def setitem_(self, target, key, value, **kwargs):
+ target[key] = value
+ self.statement("setitem", target, key, value)
+
+ def setattr(self, target, name, value, **kwargs):
+ setattr(target, name, value)
+ self.statement("setattr", target, name, value)
diff --git a/tests/python/script/test_basic_usage.py b/tests/python/script/test_basic_usage.py
new file mode 100644
index 0000000000..888d65394b
--- /dev/null
+++ b/tests/python/script/test_basic_usage.py
@@ -0,0 +1,1044 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Ordinary mini-language functions and modules evaluate expressions and control flow.
+
+Bindings, comparisons, loops and branches preserve values, identity and effect order.
+"""
+
+from __future__ import annotations
+
+# Script-local bindings and failing decorated definitions are intentionally observable in IR.
+# ruff: noqa: F841
+import ast
+import inspect
+
+import pytest
+from minilang import Value
+
+from tvm import ir
+from tvm.ir import prim
+from tvm.ir._overload_prim_expr import EqualOp
+from tvm.script import ir as I
+from tvm.script import tirx as T
+from tvm.script.parser import protocol_registry as registry
+
+
+def test_function(language):
+ M = language.M
+
+ @M.function
+ def identity(x: M.Tensor((4,))) -> M.Tensor((4,)):
+ return x
+
+ assert identity.name == "identity"
+ assert identity.params[0].args[0].args[0] == (4,)
+ assert identity.ret_type.args[0] == (4,)
+ assert identity.body == [("return", identity.params[0])]
+
+
+def test_missing_parameter_annotation_keeps_source_range(language):
+ # An unannotated script parameter must report that parameter before constructing IR.
+ M = language.M
+ with pytest.raises(SyntaxError, match="requires an annotation") as caught:
+
+ @M.function
+ def main(value):
+ pass
+
+ error = caught.value
+ # The outer test's fixture argument is not the offending script argument.
+ lines, first = inspect.getsourcelines(test_missing_parameter_annotation_keeps_source_range)
+ node = next(
+ n
+ for n in ast.walk(ast.parse("".join(lines)))
+ if isinstance(n, ast.arg) and n.arg == "value"
+ )
+ expected = (
+ __file__,
+ first + node.lineno - 1,
+ node.col_offset + 1,
+ first + node.end_lineno - 1,
+ node.end_col_offset + 1,
+ )
+ assert type(error) is SyntaxError
+ assert (
+ error.filename,
+ error.lineno,
+ error.offset,
+ error.end_lineno,
+ error.end_offset,
+ ) == expected
+ assert not language.functions
+
+
+def test_invalid_quoted_annotation_keeps_source_range(language):
+ # A malformed quoted annotation must report the original literal, not generated code.
+ M = language.M
+ with pytest.raises(SyntaxError, match="Invalid annotation expression") as caught:
+
+ @M.function
+ def main(value: "invalid +"): # noqa: F722
+ pass
+
+ error = caught.value
+ lines, first = inspect.getsourcelines(test_invalid_quoted_annotation_keeps_source_range)
+ node = next(
+ n
+ for n in ast.walk(ast.parse("".join(lines)))
+ if isinstance(n, ast.Constant) and n.value == "invalid +"
+ )
+ expected = (
+ __file__,
+ first + node.lineno - 1,
+ node.col_offset + 1,
+ first + node.end_lineno - 1,
+ node.end_col_offset + 1,
+ )
+ assert type(error) is SyntaxError
+ assert (
+ error.filename,
+ error.lineno,
+ error.offset,
+ error.end_lineno,
+ error.end_offset,
+ ) == expected
+ assert not language.functions
+
+
+def test_module(language):
+ M = language.M
+
+ # An empty source class must still produce a module without inventing functions.
+ @I.ir_module
+ class Empty:
+ pass
+
+ assert Empty == {}
+
+ @I.ir_module
+ class Module:
+ @M.function
+ def first(x: M.Tensor((4,))):
+ return x
+
+ @M.function
+ def second(y: M.Tensor((4,))):
+ return Module.first(y)
+
+ assert set(Module) == {"first", "second"}
+ assert Module["first"].body == [("return", Module["first"].params[0])]
+ returned = Module["second"].body[0]
+ assert returned[0] == "return"
+ call = returned[1]
+ assert call.op == "call" and call.args[0].args == ("first",)
+ assert call.args[1] is Module["second"].params[0]
+ assert Module["first"].params[0].args[0].args[0] == (4,)
+ assert Module["second"].params[0].args[0].args[0] == (4,)
+
+
+def test_policies_leave_computed_arguments_and_shorthand_strings_alone(language):
+ # Computed arguments must run once without treating their returned strings as symbolic syntax.
+ M = language.M
+ seen = []
+ concrete = object()
+
+ def value(label, result):
+ seen.append(label)
+ return result
+
+ @M.function
+ def main():
+ M.Tensor(
+ value("shape", (4,)), dtype=value("dtype", "float32"), device=value("device", concrete)
+ )
+ M.Tensor("float32", placement="literal[0]")
+
+ assert seen == ["shape", "dtype", "device"]
+ assert main.body[0][1].args == ((4,), "float32", concrete, "S[0]")
+ assert main.body[1][1].args == ("float32", "float32", None, "literal[0]")
+
+
+def test_nested_policy_and_starred_calls_are_evaluated_once(language):
+ # Nested marked calls and starred arguments must preserve their values and evaluation count.
+ M = language.M
+ seen = []
+ mesh = object()
+ language.global_infos["mesh[0]"] = mesh
+ values = (1, 2)
+
+ def outer(value):
+ seen.append(value)
+ return value
+
+ def collect(*values, other):
+ return values, other
+
+ @M.function
+ def main():
+ outer(M.Tensor(("n",), device="mesh[0]"))
+ collect(*values, other=3)
+
+ assert len(seen) == 1 and seen[0].args[2] is mesh
+ dimension = seen[0].args[0][0]
+ assert dimension.op == "symbol" and dimension.name == "n"
+ assert main.body[1][1] == ((1, 2), 3)
+
+
+def test_constexpr_is_lazy_and_executes_in_parent_scope(language):
+ # Compile-time branches keep Python scope and short-circuit without visiting unselected
+ # operands.
+ M = language.M
+ seen = []
+
+ def choose():
+ seen.append("condition")
+ return True
+
+ def operand(value):
+ seen.append(value)
+ return value
+
+ def invalid():
+ pytest.fail("an unselected constexpr arm ran")
+
+ @M.function
+ def main():
+ if M.constexpr(choose()):
+ x = 7
+ else:
+ invalid()
+ M.record(x)
+ M.record(operand(1) if I.constexpr(operand(True)) else invalid())
+ M.record(I.constexpr(operand(0)) and invalid())
+ M.record(I.constexpr(operand(4)) or invalid())
+ M.record(I.constexpr(operand(2)) and operand(7))
+ M.record(I.constexpr(operand(0)) or operand(8))
+
+ assert seen == ["condition", True, 1, 0, 4, 2, 7, 0, 8]
+ assert [value for kind, value in main.body] == [7, 1, 0, 4, 7, 8]
+
+
+def test_unmarked_expressions_build_both_arms_in_source_order(language):
+ # Unmarked selections build IR and evaluate both arms once in source order.
+ M = language.M
+ seen = []
+ condition = Value("condition")
+
+ def operand(value):
+ seen.append(value)
+ return value
+
+ @M.function
+ def main():
+ operand(1) if condition else operand(2)
+ condition and operand(3)
+ condition or operand(4)
+
+ assert seen == [1, 2, 3, 4]
+ assert [value.op for _, value in main.body] == ["select", "and", "or"]
+ assert all(value.args[0] is condition for _, value in main.body)
+
+
+def test_ordinary_tuple_and_outer_branch_assignments_still_store(language):
+ # Unpacking and branch writes must update existing mutable cells rather than replace them.
+ M = language.M
+ first, second = object(), object()
+ calls = []
+
+ def values():
+ calls.append("values")
+ return first, second
+
+ @M.function
+ def main():
+ a = M.cell()
+ b = M.cell()
+ a, b = values()
+ if M.value():
+ a = first
+ else:
+ a = second
+ x = 0
+ x = 1
+ x += 2
+ b += 3
+ M.record(x)
+
+ declarations = dict(operands for kind, operands in main.body if kind == "declare")
+ stores = [operands for kind, operands in main.body if kind == "set"]
+ assert calls == ["values"]
+ assert stores[:-1] == [
+ (declarations["a"], first),
+ (declarations["b"], second),
+ (declarations["a"], first),
+ (declarations["a"], second),
+ ]
+ assert main.body[-1] == ("emit", 3)
+ target, addition = stores[-1]
+ assert target is declarations["b"]
+ assert addition.op == "add" and addition.args == (target, 3)
+
+
+def test_ordinary_callable_aliases_update_mutable_targets(language):
+ # A local callable must shadow its ambient namesake and preserve ordinary mutable stores.
+ M = language.M
+ marker, calls = object(), []
+
+ def axis_alias():
+ pytest.fail("the shadowed ambient callable ran")
+
+ def ordinary():
+ calls.append("ordinary")
+ return marker
+
+ @M.function
+ def main():
+ axis_alias = ordinary
+ cell = M.cell()
+ cell = axis_alias()
+ M.record(cell)
+
+ declaration = next(operands[1] for kind, operands in main.body if kind == "declare")
+ stores = [operands for kind, operands in main.body if kind == "set"]
+ assert calls == ["ordinary"]
+ assert len(stores) == 1 and stores[0][0] is declaration and stores[0][1] is marker
+ assert main.body[-1] == ("emit", declaration)
+
+
+def test_name_collision(language):
+ # Generated helpers must not steal identifiers already bound in source.
+ M = language.M
+
+ @M.function
+ def main():
+ _builder0 = 4
+ _fn0 = 5
+ _build0 = 6
+ _X1 = 7
+ M.record(_builder0 + _fn0 + _build0 + _X1)
+
+ assert main.body[0][1] == 22
+
+
+def test_loop_targets_preserve_names_and_local_rebinding(language):
+ # Scalar and unpacked loop targets must retain names, extents and ordinary rebinding semantics.
+ M = language.M
+
+ @M.function
+ def main():
+ for i in M.grid(4):
+ M.record(i)
+ i = 5
+ M.record(i)
+ for iters in M.grid(4, 5):
+ M.record(iters)
+ for head, *tail in M.grid(2, 3, 4):
+ M.record((head, *tail))
+
+ scalar, rebound, packed, unpacked = [value for _, value in main.body]
+ assert scalar.op == "loop" and scalar.args == (4,) and scalar.name == "i"
+ assert rebound == 5
+ assert [value.name for value in packed] == ["iters_0", "iters_1"]
+ assert [value.args for value in packed] == [(4,), (5,)]
+ assert [value.name for value in unpacked] == ["head", "tail_0", "tail_1"]
+ assert [value.args for value in unpacked] == [(2,), (3,), (4,)]
+
+
+def test_conditional_outputs_share_one_native_frame_result(language):
+ # A value-producing conditional must return the native frame result without rebinding it.
+ M = language.M
+ M.__tvm_value_if__ = True
+ condition, left, right = Value("condition"), Value("left"), Value("right")
+
+ @M.function
+ def main():
+ if condition:
+ y = left
+ else:
+ y = right
+ M.record(y)
+
+ output = main.body[0][1]
+ assert output.op == "if" and output.args == (condition, left, right)
+
+
+def test_bare_callable_alias_does_not_acquire_constexpr_syntax(language):
+ # A bare callable alias must not silently acquire constexpr syntax from the original marker.
+ M = language.M
+ marker = registry.constexpr
+ with pytest.raises(TypeError, match="syntax marker"):
+
+ @M.function
+ def main():
+ if marker(True):
+ M.record(1)
+
+
+def test_void_branch_statements_need_no_synthetic_named_output(language):
+ # Void branch effects must not require or introduce an artificial result binding.
+ M = language.M
+ M.__tvm_value_if__ = True
+ condition = Value("condition")
+
+ @M.function
+ def main():
+ if condition:
+ M.record(None)
+ else:
+ M.record(None)
+ M.record(9)
+
+ assert [value for kind, value in main.body] == [None, None, 9]
+
+
+def test_lexical_range_binding_calls_the_custom_iterator_once(language):
+ # A lexical range alias must call the captured iterator once instead of the builtin range.
+ M = language.M
+ calls = []
+
+ def custom_range(extent):
+ calls.append(extent)
+ return M.grid(2)
+
+ @M.function
+ def main():
+ range = custom_range
+ for i in range(4):
+ M.record(i)
+
+ assert calls == [4]
+ variable = main.body[0][1]
+ assert variable.op == "loop" and variable.args == (2,) and variable.name == "i"
+
+
+def test_body_annotation_reads_a_preceding_ordinary_local(language):
+ # A body annotation must read the current local value, not a stale enclosing capture.
+ M = language.M
+ annotations = []
+
+ def annotation(shape):
+ annotations.append(shape)
+ return M.Tensor(shape)
+
+ M.annotation = annotation
+
+ @M.function
+ def main():
+ shape = (4,)
+ value: M.annotation(shape) = 1
+ M.record(value)
+ shape = (8,)
+ M.record(shape)
+
+ assert annotations == [(4,)]
+ assert main.body == [("emit", 1), ("emit", (8,))]
+
+
+def test_native_concise_scopes_unwind_with_their_parent():
+ # Nested concise thread scopes must preserve the original variables in the constructed IR.
+ from tvm import tirx
+
+ variables = []
+
+ def observe(*items):
+ variables.extend(items)
+
+ @T.prim_func
+ def main():
+ bx = T.launch_thread("blockIdx.x", 2)
+ tx = T.launch_thread("threadIdx.x", 32)
+ observe(bx, tx)
+ T.evaluate(bx + tx)
+
+ bx, tx = variables
+ body = main.body
+ assert isinstance(body, tirx.AttrStmt) and isinstance(body.body, tirx.AttrStmt)
+ assert body.node.var.same_as(bx) and body.body.node.var.same_as(tx)
+ assert body.body.body.value.a.same_as(bx) and body.body.body.value.b.same_as(tx)
+
+
+def test_loop_control_validation_preserves_valid_and_unchecked_ir():
+ # Invalid loop placement must be rejected, while disabled checks preserve the original IR.
+ from tvm import ir, tirx
+
+ @T.prim_func(check_well_formed=False)
+ def invalid():
+ T.evaluate(tirx.break_loop())
+
+ # This is exactly the native node emitted by source `break`; constructing
+ # the intrinsic explicitly keeps the surrounding Python definition valid.
+ ir.assert_structural_equal(invalid.body, tirx.Evaluate(tirx.break_loop()))
+ with pytest.raises(ValueError, match="requires an enclosing loop"):
+
+ @T.prim_func
+ def rejected():
+ T.evaluate(tirx.break_loop())
+
+ @T.prim_func
+ def valid():
+ for i in range(2):
+ break
+
+ assert isinstance(valid.body, tirx.For)
+ ir.assert_structural_equal(valid.body.body, invalid.body)
+
+ @I.ir_module(check_well_formed=False, extra_vars={"invalid": invalid})
+ class Unchecked:
+ bad = invalid
+
+ assert Unchecked["bad"].same_as(invalid)
+ with pytest.raises(ValueError, match="requires an enclosing loop"):
+
+ @I.ir_module(extra_vars={"invalid": invalid})
+ class Rejected:
+ bad = invalid
+
+
+def test_written_comparison_order(primitive_language):
+ # A leading host literal must not reverse the written native comparison or its source span.
+ M = primitive_language.M
+ x = ir.Var("x", "int32")
+
+ @M.function
+ def main():
+ 0 < x
+
+ actual = main.body[0][1]
+ ir.assert_structural_equal(actual, prim.LT(0, x))
+ lines, start = inspect.getsourcelines(test_written_comparison_order)
+ line = start + next(i for i, text in enumerate(lines) if text.strip() == "0 < x")
+ assert (actual.span.line, actual.span.end_line, actual.span.column, actual.span.end_column) == (
+ line,
+ line,
+ 9,
+ 14,
+ )
+
+
+def test_literal_uses_ir_operand_type_and_lanes(primitive_language):
+ # Scalar literals must adopt the vector IR operand type and lanes.
+ M = primitive_language.M
+ x = ir.Var("x", "float32x4")
+
+ @M.function
+ def main():
+ 0 < x
+
+ value = main.body[0][1]
+ assert isinstance(value, prim.LT)
+ assert str(value.a.ty) == str(value.b.ty) == "float32x4"
+ assert value.b.same_as(x)
+
+
+def test_custom_host_comparison_result_is_preserved(primitive_language):
+ # Compile-time comparison must preserve a custom host result instead of forcing IR conversion.
+ M = primitive_language.M
+ result = object()
+ calls = []
+
+ class HostInt(int):
+ def __lt__(self, other):
+ calls.append(other)
+ return result
+
+ left, right = HostInt(0), ir.Var("x", "int32")
+
+ def consume(value):
+ assert value is result
+ return 0
+
+ @M.function
+ def main():
+ consume(M.constexpr(left < right))
+
+ assert len(calls) == 1 and calls[0].same_as(right)
+
+
+def test_host_ordering_does_not_materialize_an_equality_result(primitive_language):
+ # An ordering method may return an equality proxy; constexpr must preserve that object.
+ M = primitive_language.M
+ result = ir.Var("host_result", "int32") == 0
+
+ class Host:
+ def __lt__(self, other):
+ return result
+
+ left, right = Host(), Host()
+
+ def consume(value):
+ assert value is result
+ return 0
+
+ @M.function
+ def main():
+ consume(M.constexpr(left < right))
+
+
+def test_simple_chain_and_complex_operand_rejection(primitive_language):
+ # Simple comparison chains preserve order; unsupported effectful operands fail before execution.
+ M = primitive_language.M
+ x, y = ir.Var("x", "int32"), ir.Var("y", "int32")
+
+ @M.function
+ def main():
+ -1 < x <= y != +3
+
+ expected = prim.And(prim.LT(-1, x), prim.And(prim.LE(x, y), prim.NE(y, 3)))
+ ir.assert_structural_equal(main.body[0][1], expected)
+
+ def operand():
+ pytest.fail("unsupported chain evaluated its operand")
+
+ with pytest.raises(SyntaxError, match="chain") as caught:
+
+ @M.function
+ def invalid():
+ operand() < x < y
+
+ lines, start = inspect.getsourcelines(test_simple_chain_and_complex_operand_rejection)
+ line = start + next(i for i, text in enumerate(lines) if text.strip() == "operand() < x < y")
+ assert (caught.value.filename, caught.value.lineno, caught.value.offset) == (__file__, line, 13)
+
+
+def test_constexpr_keeps_python_comparison_and_chain_short_circuit(primitive_language):
+ # Compile-time comparison chains must short-circuit and retain native identity equality
+ # semantics.
+ M = primitive_language.M
+ seen = []
+ x = ir.Var("x", "int32")
+
+ def operand(index, value):
+ seen.append(index)
+ return value
+
+ def invalid():
+ raise AssertionError("constexpr comparison chain must short-circuit")
+
+ @M.function
+ def main():
+ if M.constexpr(operand(0, 2) < operand(1, 1) < invalid()):
+ 0
+ elif M.constexpr(x == x):
+ 1
+
+ assert seen == [0, 1]
+ assert main.body == [("emit", 1)]
+
+
+def test_comparisons_use_native_promotion_and_broadcast(primitive_language):
+ # Mixed scalar/vector comparisons must use native promotion and broadcast.
+ M = primitive_language.M
+ x, y = ir.Var("x", "int32"), ir.Var("y", "int64x4")
+
+ @M.function
+ def main():
+ x < y
+
+ expected = prim.LT(prim.Broadcast(prim.Cast("int64", x), 4), y)
+ ir.assert_structural_equal(main.body[0][1], expected)
+
+
+def test_symbolic_equality_reaches_typed_consumer_once_in_order(primitive_language):
+ # Symbolic equality must reach its typed consumer once, after operands run in written order.
+ M = primitive_language.M
+ values = [ir.Var("x", "int32"), ir.Var("y", "int32")]
+ seen, comparisons = [], []
+
+ def operand(index):
+ seen.append(index)
+ return values[index]
+
+ def consume(value):
+ assert isinstance(value, prim.EQ)
+ assert str(value.ty) == "bool"
+ comparisons.append(value)
+ return value
+
+ @M.function
+ def main():
+ consume(operand(0) == operand(1))
+
+ assert seen == [0, 1]
+ assert len(comparisons) == 1
+ assert comparisons[0].a.same_as(values[0]) and comparisons[0].b.same_as(values[1])
+ assert comparisons[0].span is not None
+
+
+def test_python_comparisons_remain_python_booleans(primitive_language):
+ # Compile-time comparisons of ordinary containers and strings must return actual Python
+ # booleans.
+ M = primitive_language.M
+ seen = []
+
+ def consume(value):
+ assert type(value) is bool
+ seen.append(value)
+ return int(value)
+
+ @M.function
+ def main():
+ consume(M.constexpr([1, 2] == [1, 2]))
+ consume(M.constexpr("x" != "y"))
+
+ assert seen == [True, True]
+
+
+def test_host_equality_result_is_not_arbitrarily_converted(primitive_language):
+ # A custom equality proxy must not be materialized merely because its base resembles IR
+ # equality.
+ M = primitive_language.M
+
+ class CustomEquality(EqualOp):
+ def asobject(self):
+ raise AssertionError("custom equality subclasses are host values")
+
+ result = CustomEquality(ir.Var("x", "int32"), 0)
+
+ class Host:
+ def __eq__(self, other):
+ return result
+
+ left, right = Host(), Host()
+ seen = []
+
+ def consume(value):
+ assert value is result
+ seen.append(value)
+ return 0
+
+ @M.function
+ def main():
+ consume(M.constexpr(left == right))
+
+ assert len(seen) == 1 and seen[0] is result
+
+
+def test_nested_unmarked_statement_retains_ir_frame(language):
+ # A constexpr outer branch must not turn its unmarked inner branch into Python control flow.
+ M = language.M
+
+ @M.function
+ def main(condition: M.value):
+ if I.constexpr(True):
+ if condition:
+ M.record(1)
+ else:
+ M.record(2)
+
+ assert main.body == [("emit", 1), ("emit", 2)]
+
+
+def test_conditional_branches_require_matching_output_names(language):
+ # Value-producing branches must agree on their output name and locate the mismatched assignment.
+ M = language.M
+ M.__tvm_value_if__ = True
+ condition, left, right = Value("condition"), Value("left"), Value("right")
+ with pytest.raises(SyntaxError, match="same named output") as caught:
+
+ @M.function
+ def main():
+ if condition:
+ y = left
+ else:
+ z = right
+
+ error = caught.value
+ lines, first = inspect.getsourcelines(test_conditional_branches_require_matching_output_names)
+ line = first + next(i for i, text in enumerate(lines) if text.strip() == "z = right")
+ assert (error.filename, error.lineno, error.end_lineno) == (__file__, line, line)
+ assert (error.offset, error.end_offset) == (17, 26)
+ assert not language.functions
+
+
+def test_conditional_binding_can_be_assigned_after_skipped_branch(language):
+ # A skipped constexpr assignment must not prevent a later ordinary assignment.
+ M = language.M
+
+ @M.function
+ def main():
+ if I.constexpr(False):
+ x = 1
+ x = 2
+ M.record(x)
+
+ assert main.body[-1] == ("emit", 2)
+
+
+def test_constexpr_keeps_named_expression_unsupported(language):
+ # Constexpr must reject a multiline assignment expression at its full original range.
+ M = language.M
+ with pytest.raises(SyntaxError, match="Unsupported expression: NamedExpr") as caught:
+
+ @M.function
+ def main():
+ if I.constexpr(
+ bool(
+ value := 1 # Keep the diagnostic range across two source lines.
+ + 2
+ )
+ ):
+ M.record(value)
+
+ error = caught.value
+ lines, first = inspect.getsourcelines(test_constexpr_keeps_named_expression_unsupported)
+ node = next(n for n in ast.walk(ast.parse("".join(lines))) if isinstance(n, ast.NamedExpr))
+ assert type(error) is SyntaxError
+ assert (
+ error.filename,
+ error.lineno,
+ error.offset,
+ error.end_lineno,
+ error.end_offset,
+ ) == (
+ __file__,
+ first + node.lineno - 1,
+ node.col_offset + 1,
+ first + node.end_lineno - 1,
+ node.end_col_offset + 1,
+ )
+ assert not language.functions
+
+
+def test_ir_optional_binding_survives_skipped_host_assignment(language):
+ # A skipped host assignment must not corrupt an optional binding from an IR branch.
+ M = language.M
+
+ @M.function
+ def main(condition: M.value):
+ if condition:
+ x = M.value(1)
+ if I.constexpr(False):
+ x = M.value(2)
+ x = M.value(3)
+ M.record(x)
+
+ assert main.body[-1][1].args == (3,)
+
+
+def test_missing_host_binding_cannot_be_truth_tested(language):
+ # Reading an unexecuted constexpr binding must raise before truth testing it.
+ M = language.M
+ with pytest.raises(NameError):
+
+ @M.function
+ def main():
+ if I.constexpr(False):
+ x = 1
+ M.record(1 if I.constexpr(x) else 0)
+
+
+def test_missing_host_if_binding_raises_before_branch_assignments(language):
+ # An if-condition must reject its missing incoming value before either arm assigns that name.
+ M = language.M
+ with pytest.raises(NameError):
+
+ @M.function
+ def main():
+ if I.constexpr(False):
+ x = 1
+ if I.constexpr(x):
+ x = 2
+ else:
+ x = 3
+
+
+def test_host_lambda_local_does_not_capture_optional_binding(language):
+ # A lambda parameter must shadow an outer optional binding during host selection.
+ M = language.M
+
+ @M.function
+ def main():
+ if I.constexpr(False):
+ x = 1
+ if I.constexpr((lambda x: x)(True)):
+ M.record(222)
+
+ assert main.body[-1] == ("emit", 222)
+
+
+def test_unselected_namespace_call_does_not_require_an_attribute(language):
+ # An unselected source call must remain unevaluated even when its captured name resembles a
+ # parser helper.
+ M = language.M
+ lanes = 1
+
+ @M.function
+ def main():
+ M.record(M.ramp(0, 1, lanes) if I.constexpr(lanes > 1) else 0)
+
+ assert main.body == [("emit", 0)]
+
+
+def test_source_logical_operands_skip_at_construction(language):
+ # Boolean operators inside constexpr must skip the unused source operands.
+ M = language.M
+
+ def fail():
+ raise AssertionError("skipped source operand was evaluated")
+
+ @M.function
+ def main():
+ M.record(1 if I.constexpr(False and fail()) else 2)
+ M.record(3 if I.constexpr(True or fail()) else 4)
+
+ assert [value for _, value in main.body] == [2, 3]
+
+
+def test_unmarked_logical_chain_preserves_left_association(language):
+ # Unmarked logical chains must retain their left-associated native expression structure.
+ M = language.M
+
+ @M.function
+ def main(a: M.value(), b: M.value(), c: M.value()):
+ M.record(a and b and c)
+
+ expression = main.body[0][1]
+ assert expression.op == "and"
+ assert expression.args[1] is main.params[2]
+ assert expression.args[0].op == "and"
+ assert expression.args[0].args == tuple(main.params[:2])
+
+
+def test_ir_branch_incoming_read_and_rebinding_obeys_python_scope(language):
+ # A branch-local write must not read the outer binding before its own initialization.
+ M = language.M
+ with pytest.raises(NameError, match="y"):
+
+ @M.function
+ def main(condition: M.value(), x: M.value()):
+ y = x
+ if condition:
+ y = y + x
+ else:
+ y = x
+ return y
+
+
+def test_module_string_constants_keep_common_constructor_values():
+ # Literal strings passed to common IR constructors must remain concrete values
+ # when a real module definition is parsed.
+ from tvm import ir
+ from tvm.script import ir as I
+
+ @I.ir_module
+ class Module:
+ I.module_attrs({"tag": I.StringImm("label"), "type": I.StringType()})
+
+ expected = ir.IRModule(attrs={"tag": ir.StringImm("label"), "type": ir.StringType()})
+ ir.assert_structural_equal(expected, Module)
+ assert Module.attrs["tag"].value == "label"
+ assert isinstance(Module.attrs["type"], ir.StringType)
+
+
+def test_mutating_stores_and_loop_control_keep_effect_order(language):
+ # Item/attribute augmented stores evaluate targets once; loop control remains emitted IR.
+ M = language.M
+ effects = []
+
+ class Storage:
+ def __init__(self):
+ self.value = 0
+ self.items = [0]
+
+ def __getitem__(self, key):
+ effects.append("read")
+ return self.items[key]
+
+ def __setitem__(self, key, value):
+ self.items[key] = value
+
+ storage = Storage()
+
+ def receiver():
+ effects.append("receiver")
+ return storage
+
+ def key():
+ effects.append("key")
+ return 0
+
+ def increment():
+ effects.append("increment")
+ return 3
+
+ @M.function
+ def main(condition: M.value()):
+ first, second = (2, 4)
+ receiver().value = first
+ receiver()[key()] = second
+ receiver().value += increment()
+ receiver()[key()] += increment()
+ while condition:
+ assert not condition, "keep the assertion in the loop"
+ for i in range(2):
+ if condition:
+ continue
+ else:
+ break
+ break
+ return
+
+ assert storage.value == 5 and storage.items == [7]
+ assert effects == [
+ "receiver",
+ "receiver",
+ "key",
+ "receiver",
+ "increment",
+ "receiver",
+ "key",
+ "read",
+ "increment",
+ ]
+ assert [kind for kind, _ in main.body] == [
+ "setattr",
+ "setitem",
+ "setattr",
+ "setitem",
+ "assert",
+ "continue",
+ "break",
+ "break",
+ "return",
+ ]
+ assertion, message = main.body[4][1]
+ assert assertion.op == "not" and assertion.args[0] is main.params[0]
+ assert message == "keep the assertion in the loop"
+ assert main.body[-1] == ("return", None)
+
+
+def test_numeric_relations_keep_each_written_operation(primitive_language):
+ # A chained bound and its reversed bounds must retain all six written relations.
+ M = primitive_language.M
+ lower, value, upper = [ir.Var(name, "int32") for name in ("lower", "value", "upper")]
+
+ @M.function
+ def main():
+ lower < value <= upper
+ upper > value >= lower
+ value == lower
+ value != upper
+
+ expected = [
+ prim.And(prim.LT(lower, value), prim.LE(value, upper)),
+ prim.And(prim.GT(upper, value), prim.GE(value, lower)),
+ prim.EQ(value, lower),
+ prim.NE(value, upper),
+ ]
+ for (_, actual), reference in zip(main.body, expected):
+ ir.assert_structural_equal(actual, reference)
+ assert len(main.body) == len(expected)
diff --git a/tests/python/script/test_capture_definition_scope.py b/tests/python/script/test_capture_definition_scope.py
deleted file mode 100644
index 0ee469b89d..0000000000
--- a/tests/python/script/test_capture_definition_scope.py
+++ /dev/null
@@ -1,420 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-"""Definition and body scope rules through production parser and recording frames."""
-
-from __future__ import annotations
-
-from types import SimpleNamespace
-
-import pytest
-
-from tvm.script import ir as I
-from tvm.script.ir_builder import IRBuilder
-from tvm.tirx.script.jit import make_jit
-
-EXTENT = 11
-VALUE = 1
-
-
-@pytest.fixture(autouse=True)
-def jit_namespace(language):
- language.X.jit = make_jit(language.X)
-
-
-def _delayed(n, decorator, X):
- @decorator
- def function(output: X.tensor((n,), "int32")):
- local_n = 2
- X.record(local_n + VALUE)
-
- return function
-
-
-@pytest.mark.parametrize("factory", [False, True])
-def test_reused_eager_decorator_captures_application_scope(factory, language):
- # Before: a reused plain/factory decorator is applied where n=4, before body local_n=2.
- # Expected builder: X.arg receives extent 4 from application scope.
- X = language.X
- decorator = X.script() if factory else X.script
- function = _delayed(4, decorator, X)
- assert [int(value) for value in function.params[0].args[0].args[0]] == [4]
-
-
-def test_ir_module_class_annotation_scope_does_not_replace_body_global(language):
- # Before: class EXTENT=3 appears in an annotation and a body global read.
- # Expected builder: X.arg receives 3; X.record receives global 11.
- X = language.X
-
- @I.ir_module
- class Module:
- EXTENT = 3
-
- @X.script
- def function(output: X.tensor((EXTENT,), "int32")):
- X.record(EXTENT)
-
- function = Module["function"]
- assert [int(value) for value in function.params[0].args[0].args[0]] == [3]
- assert function.body[0][1] == 11
-
-
-def test_ordinary_class_annotation_scope_does_not_replace_body_global(language):
- # Before: an ordinary class shadows global EXTENT only for annotations.
- # Expected builder: eager X.arg receives 3; X.record receives global 11.
- X = language.X
-
- class Holder:
- EXTENT = 3
-
- @X.script
- def function(output: X.tensor((EXTENT,), "int32")):
- X.record(EXTENT)
-
- assert [int(value) for value in Holder.function.params[0].args[0].args[0]] == [3]
- assert Holder.function.body[0][1] == 11
-
-
-def test_jit_annotation_snapshot_precedes_later_closure_mutation(language):
- # Before: a delayed annotation/body capture n=4 before the closure changes to 9.
- # Expected builder: specialization uses the retained 4 in both places.
- X = language.X
-
- def build():
- n = 4
-
- @X.jit
- def function(output: X.tensor((n,), "int32")):
- X.record(n)
-
- n = 9
- return function
-
- function = build().specialize()
- assert [int(value) for value in function.params[0].args[0].args[0]] == [4]
- assert function.body[0][1] == 4
-
-
-def test_annotation_constructor_executes_in_required_builder_context(language):
- # Before: a postponed annotation calls a context-sensitive constructor.
- # Expected builder: constructor runs once at specialization inside the real IRBuilder.
- X = language.X
- calls = []
-
- def annotation():
- calls.append(IRBuilder.is_in_scope())
- return X.tensor((5,), "int32")
-
- @X.jit
- def function(output: annotation()):
- X.record(7)
-
- assert calls == [] # postponed annotations are not evaluated at decoration
- result = function.specialize()
- assert calls == [True]
- assert [int(value) for value in result.params[0].args[0].args[0]] == [5]
-
-
-def test_enclosing_namespace_used_only_by_annotation_is_retained(language):
- # Before: an annotation-only namespace creates symbolic shape n after scope exit.
- # Expected builder: resolve_type_var_("n") supplies the named annotation symbol.
- X = language.X
-
- def build():
- dtype_namespace = X
-
- @X.jit
- def function(output: dtype_namespace.tensor(("n",), "int32")):
- X.record(7)
-
- return function
-
- result = build().specialize()
- assert result.params[0].args[0].args[0][0].op == "symbol"
- assert result.params[0].args[0].args[0][0].name == "n"
-
-
-def test_attribute_name_does_not_turn_a_closure_binding_into_a_global(language):
- # Before: closure dtype="int32" and descriptor.dtype="int64" share a spelling.
- # Expected builder: X.record receives ("int32", 3), then ("int64", 4).
- X = language.X
- dtype = "int32"
- descriptor = SimpleNamespace(dtype="int64")
-
- @X.script
- def function():
- X.record((dtype, 3))
- X.record((descriptor.dtype, 4))
-
- first, second = function.body
- assert first[1][0] == "int32"
- assert first[1][1] == 3
- assert second[1][0] == "int64"
- assert second[1][1] == 4
-
-
-def test_active_lexical_ancestor_retains_annotation_only_names(language):
- # Before: nested annotation-only extent/width come from two active ancestors.
- # Expected builder: X.arg receives (extent, 3) for both extent=4 and extent=8.
- X = language.X
-
- def outer(extent):
- def middle(width):
- @X.script
- def function(output: X.tensor((extent, width), "int32")):
- X.record(VALUE)
-
- return function
-
- return middle(3)
-
- for extent in (4, 8):
- function = outer(extent)
- assert [int(value) for value in function.params[0].args[0].args[0]] == [extent, 3]
- assert function.body[0][1] == 1
-
-
-def test_active_lexical_ancestor_respects_nearest_annotation_binding(language):
- # Before: inner extent=3 shadows outer extent=8 in a signature.
- # Expected builder: X.arg receives nearest extent 3.
- X = language.X
-
- def outer(extent):
- def middle():
- extent = 3
-
- @X.script
- def function(output: X.tensor((extent,), "int32")):
- X.record(VALUE)
-
- return function
-
- return middle()
-
- function = outer(8)
- assert [int(value) for value in function.params[0].args[0].args[0]] == [3]
-
-
-def test_unrelated_same_file_caller_does_not_supply_annotation_locals(language):
- # Before: unrelated caller locals shadow both annotation and body global names.
- # Expected builder: X.arg receives global 11; X.record receives global 1.
- X = language.X
-
- def build():
- @X.script
- def function(output: X.tensor((EXTENT,), "int32")):
- X.record(VALUE)
-
- return function
-
- def caller():
- EXTENT = 99 # noqa: F841
- VALUE = 99 # noqa: F841
- return build()
-
- function = caller()
- assert [int(value) for value in function.params[0].args[0].args[0]] == [11]
- assert function.body[0][1] == 1
-
-
-def test_inactive_lexical_ancestor_is_not_replaced_by_unrelated_caller(language):
- # Before: annotation-only extent has left scope while an unrelated caller has extent.
- # Expected builder: raise NameError naming extent; do not capture the caller.
- X = language.X
-
- def outer(extent):
- def middle():
- @X.script
- def function(output: X.tensor((extent,), "int32")):
- X.record(VALUE)
-
- return function
-
- return middle
-
- middle = outer(8)
- extent = 99 # noqa: F841
- with pytest.raises(NameError, match="extent"):
- middle()
-
-
-def test_enclosing_class_locals_are_not_nested_annotation_scope(language):
- # Before: outer class EXTENT=99 surrounds a nested class definition.
- # Expected builder: nested annotation and body read global 11.
- X = language.X
-
- class Outer:
- EXTENT = 99
-
- class Inner:
- @X.script
- def function(output: X.tensor((EXTENT,), "int32")):
- X.record(EXTENT)
-
- function = Outer.Inner.function
- assert [int(value) for value in function.params[0].args[0].args[0]] == [11]
- assert function.body[0][1] == 11
-
-
-@pytest.mark.parametrize("factory", [False, True])
-def test_jit_retains_active_lexical_ancestor_snapshot(factory, language):
- # Before: plain/factory JIT captures extent=8,width=3 before extent changes to 99.
- # Expected builder: delayed X.arg receives (8, 3), X.record receives global 1.
- X = language.X
- decorator = X.jit() if factory else X.jit
-
- def outer(extent):
- def middle(width):
- @decorator
- def function(output: X.tensor((extent, width), "int32")):
- X.record(VALUE)
-
- return function
-
- pending = middle(3)
- extent = 99
- return pending
-
- function = outer(8).specialize()
- assert [int(value) for value in function.params[0].args[0].args[0]] == [8, 3]
- assert function.body[0][1] == 1
-
-
-def test_unrelated_intervening_call_ends_annotation_capture(language):
- # Before: an unrelated invoke(callback) frame interrupts lexical ancestor capture.
- # Expected builder: raise NameError naming unavailable extent.
- X = language.X
-
- def invoke(callback):
- return callback()
-
- def outer(extent):
- def middle():
- @X.script
- def function(output: X.tensor((extent,), "int32")):
- X.record(VALUE)
-
- return function
-
- return invoke(middle)
-
- with pytest.raises(NameError, match="extent"):
- outer(8)
-
-
-def _annotation_bindings(language):
- bindings = []
- original = language.X.bind_
-
- def bind(value, *, name=None, ty=None, **kwargs):
- bindings.append((name, value, ty))
- return original(value, name=name, **kwargs)
-
- language.X.bind_ = bind
- return bindings
-
-
-def test_local_annotation_keeps_rhs_before_constructor_evaluation(language):
- # Before: y: annotation() = value(x)
- # Expected builder: X.bind_(value(x), name="y", ty=annotation()).
- X = language.X
- events = []
- bindings = _annotation_bindings(language)
-
- def annotation():
- events.append("annotation")
- assert IRBuilder.is_in_scope()
- return X.tensor((2,), "float32")
-
- def value(x):
- events.append("rhs")
- return X.value(x, x)
-
- @X.script
- def function(x: X.tensor((2,), "float32")):
- y: annotation() = value(x)
- return y
-
- assert events == ["rhs", "annotation"]
- name, rhs, ty = bindings[0]
- assert name == "y" and ty.args[:2] == ((2,), "float32")
- assert rhs.args == (function.params[0], function.params[0])
- assert function.body[0] == ("return", rhs)
-
-
-def test_local_annotation_scope_does_not_overwrite_ordinary_body_global(language):
- # Before: class EXTENT=3; body reads global EXTENT; y: tensor((EXTENT,), dtype).
- # Expected builder: X.record(11); X.bind_(rhs, ty=X.tensor((3,), "float32")).
- X = language.X
- dtype = "float32"
- bindings = _annotation_bindings(language)
-
- @I.ir_module
- class Module:
- EXTENT = 3
-
- @X.script
- def main(x: X.tensor((3,), "float32")):
- X.record(EXTENT)
- y: X.tensor((EXTENT,), dtype) = X.value(x, x)
- return y
-
- function = Module["main"]
- name, rhs, ty = next(binding for binding in bindings if binding[0] == "y")
- assert function.body[0] == ("emit", 11)
- assert name == "y" and ty.args[:2] == ((3,), "float32")
- assert rhs.args == (function.params[0], function.params[0])
- assert function.body[1] == ("return", rhs)
-
-
-def test_local_annotation_preserves_lambda_and_comprehension_bindings(language):
- # Before: annotation's lambda dtype and comprehension size shadow captured names.
- # Expected builder: X.bind_(rhs, ty=X.tensor((2,), "float32")).
- X = language.X
- dtype = "int64" # noqa: F841
- size = 9 # noqa: F841
- sizes = (2,)
- bindings = _annotation_bindings(language)
-
- @X.script
- def function(x: X.tensor((2,), "float32")):
- y: (lambda dtype: X.tensor(tuple(size for size in sizes), dtype))("float32") = X.value(x, x)
- return y
-
- name, rhs, ty = bindings[0]
- assert name == "y" and ty.args[:2] == ((2,), "float32")
- assert rhs.args == (function.params[0], function.params[0])
- assert function.body[0] == ("return", rhs)
-
-
-def test_class_annotation_scope_keeps_distinct_method_closure(language):
- # Before: an enclosing extent=7 and class extent=3 share a method spelling.
- # Expected builder program: the annotation reads class 3, the body closes over 7.
- X = language.X
- extent = 7
-
- @I.ir_module
- class Module:
- extent = 3
-
- @X.script
- def main(value: X.tensor((extent,))):
- X.record(extent)
-
- function = Module["main"]
- assert function.params[0].args[0].args[0] == (3,)
- assert function.body == [("emit", 7)]
diff --git a/tests/python/script/test_capture_frontend_scope.py b/tests/python/script/test_capture_frontend_scope.py
deleted file mode 100644
index 1d6ce8f73b..0000000000
--- a/tests/python/script/test_capture_frontend_scope.py
+++ /dev/null
@@ -1,232 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-"""Frontend dispatch, lexical capture, and real shared IR source locations."""
-
-import pytest
-from dummy_builder import Function
-
-from tvm import ir
-from tvm.script import ir as I
-from tvm.script.parser import entry, protocol_registry
-from tvm.script.parser.inspect_source import Source
-from tvm.tirx.script.jit import make_jit
-
-VALUE = 1
-MACRO_VALUE = 2
-
-
-def test_ordinary_class_member_constructs_function(language):
- # Before: ordinary class with a decorated member.
- # Expected builder: enter X.function immediately and emit the member body.
- X = language.X
-
- class Holder:
- @X.script
- def function():
- X.record(7)
-
- assert isinstance(Holder.function, Function)
- assert Holder.function.body == [("emit", 7)]
-
-
-def test_ir_module_defers_members_until_forward_signatures_exist(language):
- # Before: main calls Module.identity, declared later in the class.
- # Expected builder: declare both signatures, then call the reserved identity global.
- X = language.X
-
- @I.ir_module
- class Module:
- @X.script
- def main(x: X.tensor((2,), "float32")):
- return Module.identity(x)
-
- @X.script
- def identity(x: X.tensor((2,), "float32")):
- return x
-
- call = Module["main"].body[0][1]
- assert call.op == "call"
- assert call.args[0] is language.references["identity"]
- assert call.args[1] is Module["main"].params[0]
- assert Module["identity"].body[0][1] is Module["identity"].params[0]
- entries = [event[1:3] for event in language.events if event[0] == "enter"]
- assert entries == [
- ("module", False),
- ("function", True),
- ("function", True),
- ("function", False),
- ("function", False),
- ]
-
-
-@pytest.fixture
-def spanned_language(spanned_language):
- language = spanned_language
- language.X.evaluate = lambda value: value
- language.X.call_extern = lambda dtype, name: ir.Call(ir.GlobalVar(name), [], ret_ty=dtype)
- return language
-
-
-def _position(span):
- return span.line, span.column, span.end_line, span.end_column
-
-
-@pytest.mark.parametrize("entrypoint", ["source", "function"])
-def test_source_and_ir_call_spans_use_one_based_columns(spanned_language, entrypoint):
- # Before: evaluate(call_extern(...)) through source and callable entry points.
- # Expected builder: _S[i] attaches the fixed call range with one-based columns.
- language = spanned_language
- X = language.X
-
- def direct():
- X.evaluate(X.call_extern("int32", "direct"))
-
- if entrypoint == "source":
- program = '@X.script\ndef direct():\n X.evaluate(X.call_extern("int32", "direct"))\n'
- source = Source(program)
- function = entry.parse(program, extra_vars={"X": X}, root_builder=spanned_language.X)
- else:
- source = Source(direct)
- function = X.script(direct)
- call = source.as_ast().body[0].body[0].value.args[0]
- expected = source.to_span(call)
- assert expected.column == call.col_offset + source.start_column + 1
- assert expected.end_column == call.end_col_offset + source.start_column + 1
- assert _position(function.body[0][1].span) == _position(expected)
- assert function.body[0][1].span.source_name.name == source.source_name
-
-
-def test_nested_helper_spans_preserve_caller_and_definition_columns(spanned_language):
- # Before: main -> outer inline helper -> inner inline helper -> call.
- # Expected builder: production span stack retains all three call coordinates in order.
- X = spanned_language.X
- text = """@X.inline
-def inner():
- X.evaluate(X.call_extern("int32", "nested"))
-@X.inline
-def outer():
- inner()
-@X.script
-def main():
- outer()
-"""
- source = Source(text)
- inner, outer, main = source.as_ast().body
- calls = [main.body[0].value, outer.body[0].value, inner.body[0].value.args[0]]
- expected = [_position(source.to_span(call)) for call in calls]
- assert [position[1] for position in expected] == [5, 5, 16]
- function = entry.parse(text, extra_vars={"X": X}, root_builder=spanned_language.X)
- span = function.body[0][1].span
- assert isinstance(span, ir.SequentialSpan)
- assert [_position(item) for item in span.spans] == expected
- assert all(item.source_name.name == "<str>" for item in span.spans)
-
-
-def test_annotation_only_enclosing_binding_is_captured(language):
- # Before: a quoted annotation uses an enclosing shape absent from body bytecode.
- # Expected builder: X.arg sees (3,), never the unrelated caller's (7,).
- X = language.X
-
- def build():
- shape = (3,)
-
- @X.script
- def function(A: "X.tensor(shape, 'int32')"):
- X.record(1)
-
- return function
-
- shape = (7,) # noqa: F841
- function = build()
- assert function.params[0].args[0].args[:2] == ((3,), "int32")
- assert function.body == [("emit", 1)]
-
-
-def _build_symbolic_functions(X):
- @I.ir_module
- class Module:
- @X.script
- def first(x: X.tensor(("n",), "float32")):
- n = X.symbol() # noqa: F841
- return x
-
- @X.script
- def second(x: X.tensor(("n",), "float32")):
- n = X.symbol() # noqa: F841
- return x
-
- return Module
-
-
-def test_dynamic_caller_symbol_does_not_join_function_declarations(language):
- # Before: two function signatures declare "n" while a caller has its own n.
- # Expected builder: each function frame resolves its own n, independent of the caller.
- n = ir.Var("n", "int64")
- module = _build_symbolic_functions(language.X)
- first = module["first"].params[0].args[0].args[0][0]
- second = module["second"].params[0].args[0].args[0][0]
- assert first is not second
- assert first is not n
- assert second is not n
- declared = [event[2] for event in language.events if event[:2] == ("symbol", "n")]
- assert all(symbol is first or symbol is second for symbol in declared)
- assert sum(symbol is first for symbol in declared) >= 2
- assert sum(symbol is second for symbol in declared) >= 2
-
-
-@pytest.mark.parametrize("hygienic,expected", [(True, 2), (False, 1)])
-def test_macro_capture_policy_remains_explicit(language, hygienic, expected, monkeypatch):
- # Before: macro captures MACRO_VALUE=2; the global changes to 1 before invocation.
- # Expected builder: hygienic macro records 2; caller-environment macro records 1.
- X = language.X
- X.macro = protocol_registry.declaration_kind("X.macro", "helper")(entry.make_macro_decorator(X))
- monkeypatch.setitem(globals(), "X", X)
- monkeypatch.setitem(globals(), "MACRO_VALUE", 2)
-
- @X.macro(hygienic=hygienic)
- def write():
- X.record(MACRO_VALUE)
-
- monkeypatch.setitem(globals(), "MACRO_VALUE", 1)
-
- @X.script
- def function():
- write()
-
- assert function.body[0] == ("emit", expected)
- assert [event for event in language.events if event[0] == "record"] == [("record", expected)]
-
-
-def _define_delayed_annotation(shape, X):
- @X.jit
- def function(output: "X.tensor(shape, 'int32')"):
- X.record(VALUE)
-
- return function
-
-
-def test_delayed_jit_retains_annotation_only_definition_binding(language):
- # Before: delayed signature captures shape=(3,), body reads lexical global VALUE.
- # Expected builder: after scope exits X.arg sees (3,), X.record sees 1.
- language.X.jit = make_jit(language.X)
- pending = _define_delayed_annotation((3,), language.X)
- shape = (7,) # noqa: F841
- VALUE = 2 # noqa: F841
- function = pending.specialize()
- assert function.params[0].args[0].args[:2] == ((3,), "int32")
- assert function.body == [("emit", 1)]
diff --git a/tests/python/script/test_capture_gallery_source.py b/tests/python/script/test_capture_gallery_source.py
deleted file mode 100644
index 051f81a264..0000000000
--- a/tests/python/script/test_capture_gallery_source.py
+++ /dev/null
@@ -1,134 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Class source recovery uses the exact decorator location in gallery runners."""
-
-import importlib.util
-import linecache
-import sys
-
-import pytest
-
-from tvm import ir
-from tvm.script import ir as I
-from tvm.script.ir_builder import base
-
-
-def _execute(source, filename, monkeypatch, X):
- module = importlib.util.module_from_spec(importlib.util.spec_from_loader("__main__", None))
- module.__dict__.update(I=I, X=X)
- with monkeypatch.context() as patch:
- patch.setitem(sys.modules, "__main__", module)
- exec(compile(source, filename, "exec"), module.__dict__)
- return module
-
-
-@pytest.mark.parametrize("cached", [False, True])
-def test_gallery_classes_retain_distinct_source_locations(tmp_path, monkeypatch, cached, language):
- # Before: repeated class names use plain/factory decorators and file/cache source.
- # Expected builder: each class retains its own body value, filename, and line.
- _native_locations(language)
- source = """
-@I.ir_module
-class Repeated:
- @X.script
- def main(A: X.tensor((1,), "int32")):
- X.store(11)
-first = Repeated
-
-@I.ir_module()
-class Repeated:
- @X.script
- def main(A: X.tensor((1,), "int32")):
- X.store(22)
-second = Repeated
-"""
- filename = "<gallery-cached-classes>" if cached else str(tmp_path / "gallery.py")
- if cached:
- monkeypatch.setitem(
- linecache.cache, filename, (len(source), None, source.splitlines(True), filename)
- )
- else:
- (tmp_path / "gallery.py").write_text(source)
- module = _execute(source, filename, monkeypatch, language.X)
- for result, value, line in ((module.first, 11, 6), (module.second, 22, 13)):
- store = result["main"].body[0][1]
- assert store.args[0].value == value
- assert store.span.line == line
- assert store.span.source_name.name == filename
-
-
-def test_gallery_nested_class_preserves_definition_scope(tmp_path, monkeypatch, language):
- # Before: nested class captures n; an unrelated caller shadows global VALUE.
- # Expected builder: X.arg receives shape=(4,), body records lexical VALUE=7.
- _native_locations(language)
- source = """
-VALUE = 7
-
-def build(n):
- @I.ir_module
- class Module:
- @X.script
- def main(A: X.tensor((n,), "int32")):
- X.store(VALUE)
- return Module
-
-def caller():
- VALUE = 99
- return build(4)
-
-result = caller()
-"""
- path = tmp_path / "nested_gallery.py"
- path.write_text(source)
- module = _execute(source, str(path), monkeypatch, language.X)
- function = module.result["main"]
- assert function.params[0].args[0].args[0][0] == 4
- assert function.body[0][1].args[0].value == 7
-
-
-def test_gallery_empty_class_needs_no_member_source(tmp_path, monkeypatch, language):
- # Before: module class contains only pass.
- # Expected builder: an empty module frame still recovers the class source.
- source = """
-@I.ir_module
-class Empty:
- pass
-"""
- path = tmp_path / "empty_gallery.py"
- path.write_text(source)
- module = _execute(source, str(path), monkeypatch, language.X)
- assert len(module.Empty) == 0
-
-
-def test_unavailable_class_source_still_raises(monkeypatch, language):
- # Before: an uncached synthetic filename has no recoverable class source.
- # Expected builder: source acquisition raises OSError before construction.
- with pytest.raises(OSError, match="source code not available"):
- _execute(
- "@I.ir_module\nclass Empty:\n pass\n",
- "<missing-gallery-source>",
- monkeypatch,
- language.X,
- )
-
-
-def _native_locations(language):
- language.I.at_ = base.at_
- language.I.with_at_group_ = base.with_at_group_
- language.X.store = lambda value: ir.Call(
- ir.GlobalVar("store"), [ir.prim.IntImm("int32", value)], ret_ty="int32"
- )
diff --git a/tests/python/script/test_capture_nonlocal.py b/tests/python/script/test_capture_nonlocal.py
deleted file mode 100644
index 2ae28deddc..0000000000
--- a/tests/python/script/test_capture_nonlocal.py
+++ /dev/null
@@ -1,66 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Enclosing lexical declarations retain captured values during construction."""
-
-import pytest
-
-from tvm.script.parser import entry
-
-
-@pytest.mark.parametrize(
- "source,captured,expected_source",
- [
- (
- """
-@X.script
-def main(x: X.tensor((2,), dtype)):
- nonlocal dtype
- return x
-""",
- {"dtype": "float32"},
- '@X.script\ndef main(x: X.tensor((2,), "float32")):\n return x\n',
- ),
- (
- """
-@X.script
-def main():
- nonlocal value
- X.record(value + 1)
-""",
- {"value": 3},
- "@X.script\ndef main():\n X.record(4)\n",
- ),
- ],
-)
-def test_nonlocal_declaration_preserves_captured_values(
- language, source, captured, expected_source
-):
- # Before: nonlocal appears in signature-only and body-arithmetic captures.
- # Expected builder: preserve annotation/value, return identity, and caller captures.
- captures = {"X": language.X, **captured}
- original_captures = dict(captures)
- expected = entry.parse(expected_source, extra_vars={"X": language.X}, root_builder=language.X)
- actual = entry.parse(source, extra_vars=captures, root_builder=language.X)
- assert actual.name == expected.name
- assert len(actual.params) == len(expected.params)
- if actual.params:
- assert actual.params[0].args[0].args == expected.params[0].args[0].args
- assert actual.body[0] == ("return", actual.params[0])
- assert expected.body[0] == ("return", expected.params[0])
- else:
- assert actual.body == expected.body == [("emit", 4)]
- assert captures == original_captures
diff --git a/tests/python/script/test_cross_function_call.py b/tests/python/script/test_cross_function_call.py
new file mode 100644
index 0000000000..e5c34c6c54
--- /dev/null
+++ b/tests/python/script/test_cross_function_call.py
@@ -0,0 +1,83 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Parsed functions call forward, recursive and aliased module references.
+
+Calls retain the declared callee and the caller parameter identity.
+"""
+
+from __future__ import annotations
+
+# Script-local bindings and failing decorated definitions are intentionally observable in IR.
+from tvm.script import ir as I
+
+
+def test_module_forward_reference(language):
+ # Forward calls must use the later member signature and the original caller parameter.
+ M = language.M
+
+ @I.ir_module
+ class Module:
+ @M.function
+ def first(x: M.Tensor((4,))):
+ second(x)
+
+ @M.function
+ def second(y: M.Tensor((4,))):
+ M.record(y)
+
+ assert set(Module) == {"first", "second"}
+ first, second = Module["first"], Module["second"]
+ call = first.body[0][1]
+ assert call.op == "call"
+ assert call.args[0] is language.references["second"]
+ assert call.args[1] is first.params[0]
+ assert second.body == [("emit", second.params[0])]
+
+
+def test_recursive_source_function_reuses_its_declaration(language):
+ # A recursive source call must reference its own single declared function and original
+ # parameter.
+ M = language.M
+
+ @M.function
+ def main(x: M.Tensor((4,))):
+ main(x)
+
+ call = main.body[0][1]
+ assert call.op == "call" and call.args[0] is language.references["main"]
+ assert call.args[1] is main.params[0]
+
+
+def test_module_alias_keeps_frame_identity_and_caller_dialect(language):
+ # A module alias must preserve the original callee reference rather than becoming a dialect
+ # binding.
+ M = language.M
+
+ @I.ir_module
+ class Module:
+ @M.function
+ def caller(x: M.Tensor((4,))):
+ cls = Module
+ cls.callee(x)
+
+ @M.function
+ def callee(y: M.Tensor((4,))):
+ M.record(y)
+
+ call = Module["caller"].body[0][1]
+ assert call.op == "call"
+ assert call.args == (language.references["callee"], Module["caller"].params[0])
diff --git a/tests/python/script/test_error_handling.py b/tests/python/script/test_error_handling.py
new file mode 100644
index 0000000000..3249387e2d
--- /dev/null
+++ b/tests/python/script/test_error_handling.py
@@ -0,0 +1,165 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Construction failures preserve the original error and allow later scripts to build.
+
+A nested function failure must unwind lexical and construction scopes without
+leaking its source context into a subsequently constructed native expression.
+"""
+
+import inspect
+import traceback
+
+import pytest
+
+from tvm import ir
+from tvm.ir import prim
+
+
+def test_nested_function_failure_preserves_error_and_recovers(spanned_language):
+ # A nested failure keeps the original error/call sites and leaves the next script clean.
+ language = spanned_language
+ M = language.M
+ seen = []
+ failure = ValueError("inner body failure")
+
+ def observe(label, outer, current, scope):
+ seen.append((label, outer, current, scope))
+ if label == "inner":
+ raise failure
+
+ with pytest.raises(ValueError) as caught:
+
+ @M.function
+ def main(x: M.Tensor((4,))):
+ scope = 1
+ observe("outer_before", x, x, scope)
+
+ @M.function
+ def inner(y: M.Tensor((4,))):
+ scope = 2
+ observe("inner", x, y, scope)
+
+ observe("outer_after", x, x, scope)
+
+ assert caught.value is failure and type(caught.value) is ValueError
+ lines, start = inspect.getsourcelines(test_nested_function_failure_preserves_error_and_recovers)
+ statement = 'observe("inner", x, y, scope)'
+ index, line = next((i, line) for i, line in enumerate(lines) if line.strip() == statement)
+ call_line, column = start + index, line.index(statement)
+ frames = traceback.extract_tb(caught.value.__traceback__)
+ calls = [frame for frame in frames if frame.filename == __file__ and frame.lineno == call_line]
+ assert calls
+ helper_frames = [frame for frame in frames if frame.name == "observe"]
+ assert len(helper_frames) == 1 and helper_frames[0].filename == __file__
+ raise_index, raise_source = next(
+ (i, line) for i, line in enumerate(lines) if line.strip() == "raise failure"
+ )
+ assert helper_frames[0].lineno == start + raise_index
+ if getattr(calls[-1], "colno", None) is not None:
+ assert (calls[-1].colno, calls[-1].end_lineno, calls[-1].end_colno) == (
+ column,
+ call_line,
+ column + len(statement),
+ )
+ raise_column = raise_source.index("raise failure")
+ assert (
+ helper_frames[0].colno,
+ helper_frames[0].end_lineno,
+ helper_frames[0].end_colno,
+ ) == (raise_column, start + raise_index, raise_column + len("raise failure"))
+
+ outer, inner = language.functions["main"], language.functions["inner"]
+ assert len(outer.params) == len(inner.params) == 1
+ assert seen == [
+ ("outer_before", outer.params[0], outer.params[0], 1),
+ ("inner", outer.params[0], inner.params[0], 2),
+ ]
+ M.node = lambda value: prim.IntImm("int32", value)
+
+ @M.function
+ def recovered(value: M.Tensor((2,))):
+ M.record(M.node(23))
+ return value
+
+ assert recovered.params[0].args[0].args[0] == (2,)
+ assert len(recovered.body) == 2
+ kind, fresh = recovered.body[0]
+ assert kind == "emit" and fresh.value == 23
+ assert recovered.body[1] == ("return", recovered.params[0])
+ index, line = next(
+ (i, line) for i, line in enumerate(lines) if line.strip() == "M.record(M.node(23))"
+ )
+ column = line.index("M.node(23)") + 1
+ span = fresh.span
+ assert not isinstance(span, ir.SequentialSpan)
+ assert span.source_name.name == __file__
+ assert (span.line, span.column, span.end_line, span.end_column) == (
+ start + index,
+ column,
+ start + index,
+ column + len("M.node(23)"),
+ )
+
+
+def test_namespace_rebinding_reports_source_location(language):
+ # A fixed source namespace cannot become a local binding; the error must identify that target.
+ M = language.M
+ with pytest.raises(SyntaxError, match="namespace|shadow|rebind") as caught:
+
+ @M.function
+ def main():
+ M = 1 # noqa: F841
+
+ error = caught.value
+ lines, first = inspect.getsourcelines(test_namespace_rebinding_reports_source_location)
+ line = first + next(i for i, text in enumerate(lines) if text.strip().startswith("M = 1"))
+ assert (error.filename, error.lineno, error.end_lineno) == (__file__, line, line)
+ assert (error.offset, error.end_offset) == (13, 14)
+ assert not language.functions
+
+
+def test_undefined_name_reports_the_original_source(language):
+ # A missing body value must remain a Python NameError at the user's identifier.
+ M = language.M
+ with pytest.raises(NameError, match="missing_value") as caught:
+
+ @M.function
+ def main():
+ return missing_value # noqa: F821
+
+ assert type(caught.value) is NameError
+ lines, first = inspect.getsourcelines(test_undefined_name_reports_the_original_source)
+ index, line = next(
+ (i, line) for i, line in enumerate(lines) if line.strip().startswith("return missing_value")
+ )
+ location = first + index
+ frames = traceback.extract_tb(caught.value.__traceback__)
+ source_frames = [
+ frame for frame in frames if frame.filename == __file__ and frame.lineno == location
+ ]
+ assert source_frames
+ if getattr(source_frames[-1], "colno", None) is not None:
+ column = line.index("missing_value")
+ assert (
+ source_frames[-1].colno,
+ source_frames[-1].end_lineno,
+ source_frames[-1].end_colno,
+ ) == (
+ column,
+ location,
+ column + len("missing_value"),
+ )
diff --git a/tests/python/script/test_jit_specialization.py b/tests/python/script/test_jit_specialization.py
new file mode 100644
index 0000000000..a35fa4e2f2
--- /dev/null
+++ b/tests/python/script/test_jit_specialization.py
@@ -0,0 +1,187 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Specialize real JIT scripts while retaining runtime parameters and captured values.
+
+A small native TIRx example checks the resulting IR. Mini-language regressions
+cover deferred annotations, nested failures and the lifetime of needed captures.
+"""
+
+from __future__ import annotations
+
+import gc
+import weakref
+
+import pytest
+
+from tvm.ir import assert_structural_equal
+from tvm.script import ir as I
+from tvm.script import tirx as T
+from tvm.script.ir_builder import IRBuilder
+from tvm.tirx.script.jit import make_jit
+
+
+@pytest.fixture
+def jit_language(language):
+ language.M.jit = make_jit(language.M)
+ return language
+
+
+def test_annotation_constructor_executes_in_required_builder_context(jit_language):
+ # Before: a postponed annotation calls a context-sensitive constructor.
+ # Expected builder: constructor runs once at specialization inside the real IRBuilder.
+ M = jit_language.M
+ calls = []
+
+ def annotation():
+ calls.append(IRBuilder.is_in_scope())
+ return M.Tensor((5,), "int32")
+
+ @M.jit
+ def function(output: annotation()):
+ M.record(7)
+
+ assert calls == [] # postponed annotations are not evaluated at decoration
+ result = function.specialize()
+ assert calls == [True]
+ assert [int(value) for value in result.params[0].args[0].args[0]] == [5]
+
+
+def test_jit_retains_lexical_annotation_snapshot(jit_language):
+ # Later enclosing mutations must not replace the annotation/body values captured for JIT.
+ M = jit_language.M
+
+ def outer(extent):
+ def middle(width):
+ type_namespace = M
+
+ @M.jit
+ def function(output: type_namespace.Tensor((extent, width), "int32")):
+ M.record(extent)
+
+ return function
+
+ pending = middle(3)
+ extent = 99
+ return pending
+
+ function = outer(8).specialize()
+ assert function.params[0].args[0].args[:2] == ((8, 3), "int32")
+ assert function.body == [("emit", 8)]
+
+
+def test_reentrant_specialization_restores_root_bindings_after_failure(jit_language):
+ # A nested parse and failed same-name specialization must not overwrite the outer value.
+ M = jit_language.M
+ failure = ValueError("nested failure")
+
+ def fail():
+ raise failure
+
+ def nested():
+ @M.function
+ def kernel(n: M.Tensor((1,))):
+ M.record(n)
+
+ assert len(kernel.params) == 1
+ assert kernel.body[0][1] is kernel.params[0]
+
+ @M.jit
+ def kernel(n: I.constexpr):
+ fail()
+
+ with pytest.raises(ValueError) as caught:
+ kernel.specialize(n=8)
+ assert caught.value is failure
+
+ @M.jit
+ def kernel(n: I.constexpr):
+ nested()
+ M.record(n)
+
+ result = kernel.specialize(n=4)
+ assert result.params == [] and result.body[-1] == ("emit", 4)
+
+
+def test_live_jit_retains_only_needed_scope_across_uncached_builds(jit_language):
+ # A live JIT must release unrelated scope while preserving annotations across uncached builds.
+ class Payload:
+ pass
+
+ M = jit_language.M
+ M.jit = make_jit(M)
+
+ def make():
+ payload = Payload()
+ reference = weakref.ref(payload)
+ width = 7
+
+ @M.jit
+ def kernel(x: M.Tensor((width,)), *, value: I.constexpr):
+ M.record(value)
+
+ return kernel, reference
+
+ enabled = gc.isenabled()
+ gc.disable()
+ try:
+ kernel, reference = make()
+ assert reference() is None
+ first = kernel.specialize(value=1)
+ second = kernel.specialize(value=2)
+ assert reference() is None
+ assert first is kernel.specialize(value=1) and first is not second
+ assert first.params[0].args[0].args[0] == second.params[0].args[0].args[0] == (7,)
+ assert first.body == [("emit", 1)] and second.body == [("emit", 2)]
+ finally:
+ if enabled:
+ gc.enable()
+
+
+def test_recursive_parameters_survive_empty_specialization(jit_language):
+ # Empty specialization must preserve recursive runtime arguments and their identities.
+ M = jit_language.M
+ M.jit = make_jit(M)
+
+ @M.jit
+ def main(x: M.Tensor((4,)), y: M.Tensor((4,))):
+ main(x, y)
+
+ result = main.specialize()
+ assert len(result.params) == 2
+ call = result.body[0][1]
+ assert call.args[0] is jit_language.references["main"]
+ assert all(actual is expected for actual, expected in zip(call.args[1:], result.params))
+ assert len(call.args) == 3
+
+
+def test_tirx_jit_specializes_captured_shape_and_value():
+ # A deferred native function must retain its enclosing shape and substitute the constexpr value.
+ width = 4
+
+ @T.jit(private=True)
+ def fill(output: T.Buffer((width,), "int32"), *, value: T.constexpr):
+ for index in range(width):
+ output[index] = value
+
+ @T.prim_func(private=True)
+ def expected(output: T.Buffer((4,), "int32")):
+ for index in range(4):
+ output[index] = 3
+
+ result = fill.specialize(value=3)
+ assert len(result.params) == 1
+ assert_structural_equal(result, expected, map_free_vars=True)
diff --git a/tests/python/script/test_meta_programming.py b/tests/python/script/test_meta_programming.py
new file mode 100644
index 0000000000..2109214b28
--- /dev/null
+++ b/tests/python/script/test_meta_programming.py
@@ -0,0 +1,478 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+"""Capture surrounding Python values in annotations, bodies and macros.
+
+These scripts distinguish definition-time annotation scope from body lexical
+lookup, and preserve captured identities, side effects and capture lifetimes.
+"""
+
+from __future__ import annotations
+
+import gc
+import weakref
+from contextlib import contextmanager
+from types import SimpleNamespace
+
+import pytest
+
+from tvm import ir
+from tvm.script import ir as I
+from tvm.script.parser import entry, protocol_registry
+
+EXTENT = 11
+VALUE = 1
+MACRO_VALUE = 2
+
+
+def test_attribute_name_does_not_turn_a_closure_binding_into_a_global(language):
+ # Before: closure dtype="int32" and descriptor.dtype="int64" share a spelling.
+ # Expected builder: X.record receives ("int32", 3), then ("int64", 4).
+ M = language.M
+ dtype = "int32"
+ descriptor = SimpleNamespace(dtype="int64")
+
+ @M.function
+ def function():
+ M.record((dtype, 3))
+ M.record((descriptor.dtype, 4))
+
+ first, second = function.body
+ assert first[1][0] == "int32"
+ assert first[1][1] == 3
+ assert second[1][0] == "int64"
+ assert second[1][1] == 4
+
+
+def test_active_lexical_ancestor_retains_annotation_only_names(language):
+ # An annotation must retain an outer-only name while choosing the nearest shadowed name.
+ M = language.M
+
+ def outer(width, extent):
+ def middle():
+ extent = 3
+
+ @M.function
+ def function(output: M.Tensor((width, extent), "int32")):
+ M.record(VALUE)
+
+ return function
+
+ return middle()
+
+ function = outer(4, 8)
+ assert function.params[0].args[0].args[0] == (4, 3)
+ assert function.body == [("emit", 1)]
+
+
+def test_unrelated_same_file_caller_does_not_supply_annotation_locals(language):
+ # Before: unrelated caller locals shadow both annotation and body global names.
+ # Expected builder: X.arg receives global 11; X.record receives global 1.
+ M = language.M
+
+ def build():
+ @M.function
+ def function(output: M.Tensor((EXTENT,), "int32")):
+ M.record(VALUE)
+
+ return function
+
+ def caller():
+ EXTENT = 99 # noqa: F841
+ VALUE = 99 # noqa: F841
+ return build()
+
+ function = caller()
+ assert [int(value) for value in function.params[0].args[0].args[0]] == [11]
+ assert function.body[0][1] == 1
+
+
+def test_enclosing_class_locals_are_not_nested_annotation_scope(language):
+ # Before: outer class EXTENT=99 surrounds a nested class definition.
+ # Expected builder: nested annotation and body read global 11.
+ M = language.M
+
+ class Outer:
+ EXTENT = 99
+
+ class Inner:
+ @M.function
+ def function(output: M.Tensor((EXTENT,), "int32")):
+ M.record(EXTENT)
+
+ function = Outer.Inner.function
+ assert [int(value) for value in function.params[0].args[0].args[0]] == [11]
+ assert function.body[0][1] == 11
+
+
+def test_unrelated_intervening_call_ends_annotation_capture(language):
+ # Before: an unrelated invoke(callback) frame interrupts lexical ancestor capture.
+ # Expected builder: raise NameError naming unavailable extent.
+ M = language.M
+
+ def invoke(callback):
+ return callback()
+
+ def outer(extent):
+ def middle():
+ @M.function
+ def function(output: M.Tensor((extent,), "int32")):
+ M.record(VALUE)
+
+ return function
+
+ return invoke(middle)
+
+ with pytest.raises(NameError, match="extent"):
+ outer(8)
+
+
+def test_local_annotation_keeps_rhs_before_constructor_evaluation(language):
+ # The RHS must run once before its annotation constructor, preserving the returned value.
+ M = language.M
+ events = []
+
+ def annotation():
+ events.append("annotation")
+ return M.Tensor((2,), "float32")
+
+ def value(x):
+ events.append("rhs")
+ return M.value(x, x)
+
+ @M.function
+ def function(x: M.Tensor((2,), "float32")):
+ y: annotation() = value(x)
+ return y
+
+ assert events == ["rhs", "annotation"]
+ kind, result = function.body[0]
+ assert kind == "return" and result.op == "value"
+ assert result.args == (function.params[0], function.params[0])
+
+
+def test_local_annotation_scope_does_not_overwrite_ordinary_body_global(language):
+ # A local annotation reads the class extent and captured dtype; the body still reads global 11.
+ M = language.M
+ dtype = "float32"
+ annotations = []
+
+ def annotation(shape, dtype):
+ annotations.append((shape, dtype))
+ return M.Tensor(shape, dtype)
+
+ @I.ir_module
+ class Module:
+ EXTENT = 3
+
+ @M.function
+ def main(x: M.Tensor((3,), "float32")):
+ M.record(EXTENT)
+ y: annotation((EXTENT,), dtype) = M.value(x, x)
+ return y
+
+ function = Module["main"]
+ assert annotations == [((3,), "float32")]
+ assert function.body[0] == ("emit", 11)
+ kind, result = function.body[1]
+ assert kind == "return" and result.op == "value"
+ assert result.args == (function.params[0], function.params[0])
+
+
+def test_local_annotation_preserves_lambda_and_comprehension_bindings(language):
+ # Lambda/comprehension locals in an annotation must not leak into later body captures.
+ M = language.M
+ dtype = "int64"
+ size = 9
+ sizes = (2,)
+ annotations = []
+
+ def annotate(shape, dtype):
+ annotations.append((shape, dtype))
+ return M.Tensor(shape, dtype)
+
+ @M.function
+ def function(x: M.Tensor((2,), "float32")):
+ y: (lambda dtype: annotate(tuple(size for size in sizes), dtype))("float32") = M.value(x, x)
+ M.record((dtype, size))
+ return y
+
+ assert annotations == [((2,), "float32")]
+ assert function.body[0] == ("emit", ("int64", 9))
+ kind, result = function.body[1]
+ assert kind == "return" and result.op == "value"
+ assert result.args == (function.params[0], function.params[0])
+
+
+def test_class_annotation_scope_keeps_distinct_method_closure(language):
+ # Before: an enclosing extent=7 and class extent=3 share a method spelling.
+ # Expected builder program: the annotation reads class 3, the body closes over 7.
+ M = language.M
+ extent = 7
+
+ @I.ir_module
+ class Module:
+ extent = 3
+
+ @M.function
+ def main(value: M.Tensor((extent,))):
+ M.record(extent)
+
+ function = Module["main"]
+ assert function.params[0].args[0].args[0] == (3,)
+ assert function.body == [("emit", 7)]
+
+
+def test_actual_decorator_preserves_annotation_definition_and_body_scopes(language):
+ # A decorated function uses the enclosing extent in its annotation and a body local.
+ # Expected builder program:
+ # X.arg("x", X.Tensor((definition_extent,))); local_extent = X.bind_(2, name="local_extent")
+ M = language.M
+ calls = []
+
+ def ordinary_decorator(function):
+ calls.append("decorate")
+ return function
+
+ M.ordinary = ordinary_decorator
+
+ def outer(extent):
+ @M.function
+ def main(x: M.Tensor((extent,))):
+ local_extent = 2
+
+ @M.ordinary
+ def helper():
+ calls.append("call")
+ return local_extent
+
+ M.record(helper())
+
+ return main
+
+ result = outer(7)
+ assert result.params[0].args[0].args[0] == (7,)
+ assert result.body[0][1] == 2
+ assert calls == ["decorate", "call"]
+
+
+def test_pyfunc_collection_preserves_original_callable_and_closure(language):
+ # Before: I.pyfunc marks an ordinary closure in a class.
+ # Expected builder program: attach the original callable to __pyfuncs__; no IR declaration.
+ factor = 7
+ calls = []
+
+ class Source:
+ @I.pyfunc
+ def multiply(value):
+ calls.append(value)
+ return value * factor
+
+ original = Source.multiply
+ result = I.ir_module(Source)
+ assert result.__pyfuncs__ == {"multiply": original}
+ assert result == {}
+ assert calls == []
+ assert result.__pyfuncs__["multiply"](3) == 21
+ assert calls == [3]
+
+
+def test_body_preserves_capture_and_parameter_identity(language):
+ # Before: the body reads an enclosing token and its original runtime parameter.
+ # Expected builder program: preserve both identities under their source names,
+ # while preserving their original lexical lookup.
+ token = object()
+ M = language.M
+
+ @M.function
+ def main(x: M.Tensor((4,))):
+ M.record(token)
+ M.record(x)
+
+ assert main.body[0][1] is token
+ assert main.body[1][1] is main.params[0]
+
+
+def _build_symbolic_functions(M):
+ @I.ir_module
+ class Module:
+ @M.function
+ def first(x: M.Tensor(("n",), "float32")):
+ n = M.symbol() # noqa: F841
+ return x
+
+ @M.function
+ def second(x: M.Tensor(("n",), "float32")):
+ n = M.symbol() # noqa: F841
+ return x
+
+ return Module
+
+
+def test_dynamic_caller_symbol_does_not_join_function_declarations(language):
+ # Before: two function signatures declare "n" while a caller has its own n.
+ # Expected builder: each function frame resolves its own n, independent of the caller.
+ n = ir.Var("n", "int64")
+ module = _build_symbolic_functions(language.M)
+ first = module["first"].params[0].args[0].args[0][0]
+ second = module["second"].params[0].args[0].args[0][0]
+ assert first is not second
+ assert first is not n
+ assert second is not n
+
+
+def test_nonlocal_declaration_preserves_captured_values(language):
+ # A nonlocal declaration must preserve annotation/body captures and the returned parameter.
+ M = language.M
+ dtype = "float32"
+ value = 3
+
+ @M.function
+ def main(x: M.Tensor((2,), dtype)):
+ nonlocal dtype, value
+ M.record(value + 1)
+ return x
+
+ assert main.params[0].args[0].args[:2] == ((2,), "float32")
+ assert main.body[0] == ("emit", 4)
+ assert main.body[1][0] == "return" and main.body[1][1] is main.params[0]
+ assert dtype == "float32" and value == 3
+
+
+def test_macro_capture_policy_remains_explicit(language, monkeypatch):
+ # A changed global must distinguish captured macro scope from caller scope: values 2 then 1.
+ M = language.M
+ M.macro = protocol_registry.declaration_kind("M.macro", "helper")(entry.make_macro_decorator(M))
+ monkeypatch.setitem(globals(), "MACRO_VALUE", 2)
+
+ @M.macro
+ def captured():
+ return MACRO_VALUE
+
+ @M.macro(hygienic=False)
+ def caller_scoped():
+ return MACRO_VALUE
+
+ monkeypatch.setitem(globals(), "MACRO_VALUE", 1)
+
+ @M.function
+ def function():
+ M.record(captured())
+ M.record(caller_scoped())
+
+ assert function.body == [("emit", 2), ("emit", 1)]
+
+
+def test_macro_dynamic_global_lookup_uses_temporary_invocation_namespace(language, monkeypatch):
+ # Dynamic global lookup inside a macro must see names absent from its static bytecode reads.
+ M = language.M
+ M.macro = protocol_registry.declaration_kind("M.macro", "helper")(entry.make_macro_decorator(M))
+ monkeypatch.setitem(globals(), "_macro_dynamic_value", 7)
+
+ @M.macro
+ def helper():
+ return globals()["_macro_dynamic_value"]
+
+ @M.function
+ def function():
+ M.record(helper())
+
+ assert function.body == [("emit", 7)]
+
+
+class Payload:
+ pass
+
+
+@contextmanager
+def without_cyclic_gc():
+ enabled = gc.isenabled()
+ gc.disable()
+ try:
+ yield
+ finally:
+ if enabled:
+ gc.enable()
+
+
+def test_eager_entry_releases_unused_scope_but_keeps_annotation_value(language):
+ # Eager annotation capture must keep width without retaining the unrelated enclosing payload.
+ M = language.M
+
+ def make():
+ payload = Payload()
+ reference = weakref.ref(payload)
+ width = 7
+
+ @M.function
+ def function(value: M.Tensor((width,))):
+ M.record(1)
+
+ return function, reference
+
+ with without_cyclic_gc():
+ function, reference = make()
+ assert reference() is None
+ assert function.params[0].args[0].args[0] == (7,)
+ assert function.body == [("emit", 1)]
+
+
+def test_live_macro_does_not_snapshot_unrelated_module_globals(language, monkeypatch):
+ # A live macro must retain its closure value without snapshotting unrelated module globals.
+ M = language.M
+ M.macro = protocol_registry.declaration_kind("M.macro", "helper")(entry.make_macro_decorator(M))
+ monkeypatch.setitem(globals(), "_unrelated_macro_payload", Payload())
+ reference = weakref.ref(globals()["_unrelated_macro_payload"])
+ offset = 3
+
+ with without_cyclic_gc():
+
+ @M.macro
+ def helper(value):
+ return value + offset
+
+ del globals()["_unrelated_macro_payload"]
+ assert reference() is None
+
+ @M.function
+ def function():
+ M.record(helper(2))
+
+ assert function.body == [("emit", 5)]
+
+
+def test_constexpr_uses_registered_namespace_in_source(language):
+ # An ordinary attribute named constexpr must not acquire the registered marker's binding rule.
+ M = language.M
+
+ class Ordinary:
+ constexpr = M.Tensor((1,), "int32")
+
+ @M.function
+ def ordinary(value: Ordinary.constexpr):
+ return value
+
+ assert len(ordinary.params) == 1
+ assert ordinary.params[0].args[0].args[:2] == ((1,), "int32")
+ assert ordinary.body[0][1] is ordinary.params[0]
+
+ with pytest.raises(TypeError, match="requires a specialization binding"):
+
+ @M.function
+ def specialized(value: I.constexpr):
+ M.record(value)
diff --git a/tests/python/script/test_parser.py b/tests/python/script/test_parser.py
deleted file mode 100644
index fd9c889fd6..0000000000
--- a/tests/python/script/test_parser.py
+++ /dev/null
@@ -1,781 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Observable source-to-builder contracts, exercised through the real parser."""
-
-from __future__ import annotations
-
-import ast
-
-import pytest
-from dummy_builder import Value
-
-from tvm.script.parser import entry
-from tvm.script.parser import protocol_registry as registry
-
-
-@pytest.mark.parametrize(
- "arguments",
- [
- '("n + 1", "n"), "float32", "cuda:1"',
- 'shape=("n + 1", "n"), dtype="float32", device="cuda:1"',
- 'shape=["n + 1", "n"], dtype="float32", device="cuda:1"',
- ],
-)
-@pytest.mark.parametrize("quoted", [False, True])
-def test_argument_policies_reuse_symbols_and_resolve_only_marked_literals(
- language, arguments, quoted
-):
- # Before: x: X.tensor(("n + 1", "n"), "float32", "cuda:1")
- # Expected builder program:
- # X.arg("x", X.tensor((X.resolve_type_var_("n") + 1, X.resolve_type_var_("n")),
- # "float32", I.resolve_global_info_("cuda:1")))
- device = object()
- language.global_infos["cuda:1"] = device
- annotation = f"X.tensor({arguments})"
- if quoted:
- annotation = repr(annotation)
- result = language.parse(f"""
-@X.script
-def main(x: {annotation}):
- X.record(x)
-""")
- annotation = result.params[0].args[0]
- shape, dtype, resolved, placement = annotation.args
- assert shape[0].op == "add" and shape[0].args[0] is shape[1]
- assert shape[0].args[1] == 1
- assert (dtype, placement) == ("float32", "S[0]")
- assert resolved is device
- assert [item for item in language.events if item[0] == "global_info"] == [
- ("global_info", "cuda:1")
- ]
- assert ("record", result.params[0]) in language.events
-
-
-def test_policies_leave_computed_arguments_and_shorthand_strings_alone(language):
- # Before: X.tensor(shape(), device=device()); X.tensor("float32")
- # Expected builder program: X.tensor(shape(), device=device()); X.tensor("float32")
- # Policies never inspect a computed argument to reinterpret its strings.
- seen = []
- concrete = object()
-
- def value(label, result):
- seen.append(label)
- return result
-
- result = language.parse(
- """
-@X.script
-def main():
- X.tensor(value("shape", (4,)), dtype=value("dtype", "float32"),
- device=value("device", concrete))
- X.tensor("float32", placement="literal[0]")
-""",
- value=value,
- concrete=concrete,
- )
- assert seen == ["shape", "dtype", "device"]
- assert not any(item[0] == "global_info" for item in language.events)
- assert result.body[0][1].args == ((4,), "float32", concrete, "S[0]")
- assert result.body[1][1].args == ("float32", "float32", None, "literal[0]")
-
-
-def test_nested_policy_and_starred_calls_are_evaluated_once(language):
- # Before: outer(X.tensor(("n",), device="mesh[0]")); collect(*values, other=3)
- # Expected builder program: outer(X.tensor((X.resolve_type_var_("n"),),
- # device=I.resolve_global_info_("mesh[0]")))
- seen = []
- mesh = object()
- language.global_infos["mesh[0]"] = mesh
-
- def outer(value):
- seen.append(value)
- return value
-
- def collect(*values, other):
- return values, other
-
- result = language.parse(
- """
-@X.script
-def main():
- outer(X.tensor(("n",), device="mesh[0]"))
- collect(*values, other=3)
-""",
- outer=outer,
- collect=collect,
- values=(1, 2),
- )
- assert len(seen) == 1 and seen[0].args[2] is mesh
- assert result.body[1][1] == ((1, 2), 3)
- assert [item for item in language.events if item[0] == "global_info"] == [
- ("global_info", "mesh[0]")
- ]
-
-
-@pytest.mark.parametrize("marker", ["I.constexpr", "X.constexpr"])
-def test_constexpr_is_lazy_and_executes_in_parent_scope(language, marker):
- # Before: if I.constexpr(choose()): x = 7; X.record(x)
- # Expected builder program: if choose(): x = X.bind_(7, name="x"); X.emit_(X.record(x))
- seen = []
-
- def choose():
- seen.append("condition")
- return True
-
- result = language.parse(
- f"""
-@X.script
-def main():
- if {marker}(choose()):
- x = 7
- else:
- invalid()
- X.record(x)
- X.record(1 if {marker}(True) else invalid())
- X.record({marker}(0) and invalid())
- X.record({marker}(4) or invalid())
-""",
- choose=choose,
- )
- assert seen == ["condition"]
- assert [value for kind, value in result.body] == [7, 1, 0, 4]
-
-
-def test_unmarked_expressions_build_both_arms_in_source_order(language):
- # Before: first() if condition else second(); condition and last()
- # Expected builder program:
- # X.if_then_else_(condition, first(), second()); X.and_(condition, last())
- seen = []
-
- def operand(value):
- seen.append(value)
- return value
-
- condition = Value("condition")
- result = language.parse(
- """
-@X.script
-def main():
- operand(1) if condition else operand(2)
- condition and operand(3)
- condition or operand(4)
-""",
- condition=condition,
- operand=operand,
- )
- assert seen == [1, 2, 3, 4]
- assert [value.op for _, value in result.body] == ["select", "and", "or"]
- assert all(value.args[0] is condition for _, value in result.body)
-
-
-def test_callee_arguments_and_keywords_evaluate_once_with_caller_context(language):
- # Before: callee()(operand(1), b=operand(2))
- # Expected builder program:
- # X.emit_(_S[i].ctx(lambda: callee()(operand(1), b=operand(2))), span=_S[i])
- seen = []
- result_value = Value("returned")
-
- def callee():
- seen.append("callee")
-
- def function(a, *, b):
- seen.append((a, b))
- assert language.source_stack
- return result_value
-
- return function
-
- def operand(value):
- seen.append(value)
- return value
-
- result = language.parse(
- """
-@X.script
-def main():
- callee()(operand(1), b=operand(2))
-""",
- callee=callee,
- operand=operand,
- )
- assert seen == ["callee", 1, 2, (1, 2)]
- assert result.body[0][1] is result_value
- assert result_value.span[-1][1] == 4
- assert language.source_stack == []
-
-
-def test_binding_and_mutation_have_distinct_operations(language):
- # Before: n = X.symbol(); x = helper(); cell = X.cell(); cell = x
- # Expected builder program:
- # n = X.resolve_type_var_("n", "int64"); x = X.bind_(helper(), name="x")
- # cell = X.decl_mutable_cell_(X.cell(), name="cell"); X.set_mutable_cell_(cell, x)
- from dummy_builder import Value
-
- marker = Value("producer", span=("producer",))
- binds_with_spans, calls = [], []
- bind = language.X.bind_
-
- def observe_binding(value, *, span, value_span, **kwargs):
- assert value is marker and value.span == ("producer",)
- binds_with_spans.append((span.span, value_span.span))
- return bind(value, **kwargs)
-
- def helper():
- calls.append(True)
- assert len(language.source_stack) == 1
- assert language.source_stack[-1][1:] == (5, 5, 9, 17)
- return marker
-
- language.X.bind_ = observe_binding
- result = language.parse(
- """
-@X.script
-def main():
- n = X.symbol()
- x = helper()
- cell = X.cell()
- cell = x
- X.record(x)
-""",
- helper=helper,
- )
- assert result.body[-1][1] is marker
- binds = [event for event in language.events if event[0] == "bind"]
- assert [(event[1], event[2]) for event in binds] == [("x", marker)]
- declaration = next(event for event in language.events if event[0] == "declare")
- mutation = next(event for event in language.events if event[0] == "set")
- assert declaration[1] == "cell" and mutation[1] is declaration[2]
- assert mutation[2] is marker
- assert calls == [True] and len(binds_with_spans) == 1
- target, rhs = binds_with_spans[0]
- assert target.line == rhs.line == 5
- assert (target.column, target.end_column) == (5, 6)
- assert (rhs.column, rhs.end_column) == (9, 17)
-
-
-def test_ordinary_tuple_and_outer_branch_assignments_still_store(language):
- # Before: a = X.cell(); b = X.cell(); a, b = values(); if cond: a = first
- # Expected builder program: declare a/b; unpack values once; set a/b;
- # with X.Then(): def branch(): X.set_mutable_cell_(a, first); branch()
- # Named += uses bind_ for ordinary x and set_mutable_cell_ for declared b.
- first, second = object(), object()
- calls = []
-
- def values():
- calls.append("values")
- return first, second
-
- language.X.unpack = lambda value: value
- result = language.parse(
- """
-@X.script
-def main():
- a = X.cell()
- b = X.cell()
- a, b = values()
- if X.value():
- a = first
- else:
- a = second
- x = 1
- x += 2
- b += 3
- X.record(x)
-""",
- values=values,
- first=first,
- second=second,
- )
- declarations = {event[1]: event[2] for event in language.events if event[0] == "declare"}
- stores = [(event[1], event[2]) for event in language.events if event[0] == "set"]
- assert calls == ["values"]
- assert stores[:-1] == [
- (declarations["a"], first),
- (declarations["b"], second),
- (declarations["a"], first),
- (declarations["a"], second),
- ]
-
- assert [event[2] for event in language.events if event[:2] == ("bind", "x")] == [1, 3]
- assert result.body[-1] == ("emit", 3)
- target, addition = stores[-1]
- assert target is declarations["b"]
- assert addition.op == "add" and addition.args == (target, 3)
-
-
-@pytest.mark.parametrize("scope", ["local", "parameter", "enclosing"])
-def test_ordinary_callable_aliases_update_mutable_targets(language, scope):
- # Before: axis_alias = ordinary; cell = X.cell(); cell = axis_alias()
- # Expected builder program: X.set_mutable_cell_(cell, axis_alias()).
- # Local, parameter and enclosing Python callables all preserve ordinary stores.
- marker, calls = object(), []
-
- def axis_alias():
- raise AssertionError("The shadowed ambient callable must not run")
-
- def ordinary():
- calls.append("ordinary")
- return marker
-
- body = "cell = X.cell()\n cell = axis_alias()\n X.record(cell)"
- if scope == "local":
- source = "@X.script\ndef main():\n axis_alias = ordinary\n " + body
- elif scope == "parameter":
- # A callable parameter is an opaque value supplied by the fake frame.
- def arg(name, annotation, **kwargs):
- frame = language.frame()
- frame.params.append(ordinary)
- frame.function.params.append(ordinary)
- return ordinary
-
- language.X.arg = arg
- source = "@X.script\ndef main(axis_alias: X.tensor(())):\n " + body
- else:
- source = (
- "@X.script\ndef main():\n axis_alias = ordinary\n"
- " @X.script\n def inner():\n " + body.replace("\n", "\n ")
- )
- language.parse(source, axis_alias=axis_alias, ordinary=ordinary)
- declaration = next(event[2] for event in language.events if event[0] == "declare")
- stores = [event for event in language.events if event[0] == "set"]
- assert calls == ["ordinary"]
- assert len(stores) == 1 and stores[0][1] is declaration and stores[0][2] is marker
- assert next(event[1] for event in language.events if event[0] == "record") is declaration
-
-
-def test_quoted_symbols_share_identity_without_introducing_python_bindings(language):
- # Before: def main(x: X.tensor(("n", "n"))): X.record(n)
- # Expected builder program: X.tensor((X.resolve_type_var_("n"), X.resolve_type_var_("n")))
- # The unquoted body name n raises NameError until explicitly declared.
- with pytest.raises(NameError) as error:
- language.parse("""
-@X.script
-def main(x: X.tensor(("n", "n"))):
- X.record(n)
-""")
- assert isinstance(error.value, NameError)
-
-
-@pytest.mark.parametrize(
- "binding", ["X = 1", "for X in range(2):\n pass", "with context() as X:\n pass"]
-)
-def test_script_namespace_cannot_be_rebound(language, binding):
- # Before: X = 1 (also loop/with targets)
- # Expected builder program: reject a binding that shadows the fixed script namespace X.
- with pytest.raises(SyntaxError, match="namespace|shadow|rebind"):
- language.parse(f"@X.script\ndef main():\n {binding}\n")
-
-
-def test_generated_names_do_not_capture_source_bindings(language):
- # Before: _builder = 4; _frame = 5; X.record(_builder + _frame)
- # Expected builder program: generated I/X/frame aliases avoid all source identifiers.
- result = language.parse("""
-@X.script
-def main():
- _builder = 4
- _frame = 5
- _tvm_0 = 6
- X.record(_builder + _frame + _tvm_0)
-""")
- assert result.body[0][1] == 15
-
-
-@pytest.mark.parametrize(
- "target, bounds, arguments, names",
- [
- ("i", "4", "i", ["i"]),
- ("(i,)", "4", "i", ["i"]),
- ("[i]", "4", "i", ["i"]),
- ("i, j", "4, 5", "i, j", ["i", "j"]),
- ("i, *tail", "4, 5, 6", "i, *tail", ["i", "tail_0", "tail_1"]),
- ],
-)
-def test_loop_targets_configure_the_entered_frame(language, target, bounds, arguments, names):
- # Scalar: with X.for_(...) as i. Sequence: loop = X.for_(...);
- # with loop: original_target = loop.vars. Construct once and unpack stable vars.
- grid = language.X.grid
- frames, observed = [], []
-
- def construct(*extents):
- frame = grid(*extents)
- frames.append((frame, tuple(frame.vars)))
- return frame
-
- def observe(*values):
- frame, original = frames[-1]
- assert language.stack[-1] is frame
- assert len(values) == len(original)
- assert all(value is old for value, old in zip(values, original))
- assert [value.name for value in values] == names
- observed.append(values)
-
- language.X.grid = construct
- source = (
- f"@X.script\ndef main():\n for {target} in X.grid({bounds}):\n"
- f" observe({arguments})\n"
- )
- language.parse(source, observe=observe)
- assert len(frames) == len(observed) == 1
- assert language.stack == [] and language.source_stack == []
- if target == "i":
- # A bare target collects the original multi-dimensional entry sequence.
- names = ["iters_0", "iters_1"]
- language.parse(
- "@X.script\ndef main():\n for iters in X.grid(4, 5):\n observe(*iters)\n",
- observe=observe,
- )
- assert len(frames) == len(observed) == 2
- assert language.stack == [] and language.source_stack == []
- if target == "(i,)":
- failure = ValueError("loop body failure")
-
- def fail(*values):
- observe(*values)
- raise failure
-
- with pytest.raises(ValueError) as caught:
- language.parse(source, observe=fail)
- assert caught.value is failure
- assert len(frames) == len(observed) == 2
- assert language.stack == [] and language.source_stack == []
-
-
-def test_actual_decorator_preserves_annotation_definition_and_body_scopes(language):
- # Before: def outer(extent): @X.script def f(x: X.tensor((extent,))): local_extent = 2
- # Expected builder program:
- # X.arg("x", X.tensor((definition_extent,))); local_extent = X.bind_(2, name="local_extent")
- X = language.X
- calls = []
-
- def ordinary_decorator(function):
- calls.append("decorate")
- return function
-
- X.ordinary = ordinary_decorator
-
- def outer(extent):
- @X.script
- def main(x: X.tensor((extent,))):
- local_extent = 2
-
- @X.ordinary
- def helper():
- calls.append("call")
- return local_extent
-
- X.record(helper())
-
- return main
-
- result = outer(7)
- assert result.params[0].args[0].args[0] == (7,)
- assert result.body[0][1] == 2
- assert calls == ["decorate", "call"]
- assert [event[1] for event in language.events if event[0] == "name"] == ["main"]
-
-
-def test_module_declarations_precede_bodies_and_reuse_frames(language):
- # Before: @X.script def first(x: ...): second(x); @X.script def second(y: ...): ...
- # Expected builder program:
- # with X.function_(decl=True) as first_fn: X.arg("x", ...)
- # with X.function_(decl=True) as second_fn: X.arg("y", ...)
- # with first_fn: X.emit_(X.call_global_var_(second_fn.global_var, [first_fn.params[0]]))
- result = language.parse("""
-@I.ir_module
-class Module:
- @X.script
- def first(x: X.tensor((4,))):
- second(x)
- @X.script
- def second(y: X.tensor((4,))):
- X.record(y)
-""")
- entries = [event for event in language.events if event[:2] == ("enter", "function")]
- assert [event[2] for event in entries] == [True, True, False, False]
- assert entries[0][3] is entries[2][3]
- assert entries[1][3] is entries[3][3]
- assert len([event for event in language.events if event[0] == "arg"]) == 2
- call = result["first"].body[0][1]
- assert call.op == "call"
- assert call.args[0] is language.references["second"]
- assert call.args[1] is result["first"].params[0]
- assert result["second"].body[0][1] is result["second"].params[0]
-
-
-def test_conditional_outputs_share_one_native_frame_result(language):
- # Before: if condition: y = left; else: y = right; X.record(y)
- # Expected builder program:
- # with X.If(condition) as frame:
- # with X.Then():
- # def then(): y = X.bind_(left, name="y")
- # then()
- # with X.Else():
- # def otherwise(): y = X.bind_(right, name="y")
- # otherwise()
- # y = frame.var; X.emit_(X.record(y))
- language.X.__tvm_value_if__ = True
- condition, left, right = Value("condition"), Value("left"), Value("right")
- result = language.parse(
- """
-@X.script
-def main():
- if condition:
- y = left
- else:
- y = right
- X.record(y)
-""",
- condition=condition,
- left=left,
- right=right,
- )
- output = result.body[0][1]
- assert output.op == "if" and output.args == (condition, left, right)
- frame = next(event[3] for event in language.events if event[:2] == ("enter", "if"))
- assert output is frame.var
-
-
-@pytest.mark.parametrize("other", ["z = right", "X.record(right)"])
-def test_conditional_branches_require_matching_output_names(language, other):
- # Before: if condition: y = left; else: z = right
- # Expected builder program: reject branches without the same named terminal output.
- language.X.__tvm_value_if__ = True
- with pytest.raises(SyntaxError, match="same named output"):
- language.parse(
- "@X.script\ndef main():\n if condition:\n y = left\n"
- f" else:\n {other}\n"
- )
-
-
-def test_repeated_parses_acquire_independent_symbol_trees(language, monkeypatch):
- # Before: parse the same quoted symbolic annotation twice.
- # Expected builder program: each parse owns fresh syntax and its own symbol map.
- source = '@X.script\ndef main(x: X.tensor(("n + 1",))):\n X.record(x)\n'
- acquire = entry.acquire_source
- acquired = []
- original = []
-
- def capture(*args, **kwargs):
- tree, filename, flags = acquire(*args, **kwargs)
- acquired.append(tree)
- original.append(ast.dump(tree, include_attributes=True))
- return tree, filename, flags
-
- monkeypatch.setattr(entry, "acquire_source", capture)
- first = language.parse(source)
- second = language.parse(source)
- assert len(acquired) == 2 and acquired[0] is not acquired[1]
- assert original[0] == original[1]
- assert first.params[0] is not second.params[0]
- first_shape = first.params[0].args[0].args[0][0]
- second_shape = second.params[0].args[0].args[0][0]
- assert first_shape.op == second_shape.op == "add"
- assert first_shape.args[0] is not second_shape.args[0]
- assert first_shape.args[1] == second_shape.args[1] == 1
- assert first.body[0][1] is first.params[0]
- assert second.body[0][1] is second.params[0]
-
-
-def test_bare_callable_alias_does_not_acquire_constexpr_syntax(language):
- # Before: marker = I.constexpr; if marker(True): ...
- # Expected builder program: an ordinary call to marker raises its runtime syntax-marker error.
- with pytest.raises(TypeError, match="syntax marker"):
- language.parse(
- """
-@X.script
-def main():
- if marker(True):
- X.record(1)
-""",
- marker=registry.constexpr,
- )
-
-
-@pytest.mark.parametrize("recursive", [False, True])
-def test_source_function_uses_declaration_only_when_reference_is_needed(language, recursive):
- # Before: @X.script def main(x: ...): X.record(x) (or main(x))
- # Expected builder program:
- # ordinary: with X.function_(): x = X.arg(...); X.emit_(X.record(x))
- # recursive: with X.function_(decl=True) as fn: X.arg(...)
- # with fn: X.emit_(X.call_global_var_(fn.global_var, [fn.params[0]]))
- statement = "main(x)" if recursive else "X.record(x)"
- result = language.parse(f"@X.script\ndef main(x: X.tensor((4,))):\n {statement}\n")
- entries = [event for event in language.events if event[:2] == ("enter", "function")]
- assert [event[2] for event in entries] == ([True, False] if recursive else [False])
- assert len([event for event in language.events if event[0] == "arg"]) == 1
- if recursive:
- assert entries[0][3] is entries[1][3]
- call = result.body[0][1]
- assert call.op == "call" and call.args[0] is language.references["main"]
- assert call.args[1] is result.params[0]
- else:
- assert result.body[0][1] is result.params[0]
-
-
-@pytest.mark.parametrize("alias", [False, True])
-def test_module_alias_keeps_frame_identity_and_caller_dialect(language, alias):
- # Before: cls = Module; cls.callee(x)
- # Expected builder program:
- # with I.ir_module() as Module: ...
- # cls = Module; X.emit_(X.call_global_var_(cls.callee, [x]))
- setup, owner = ("cls = Module", "cls") if alias else ("pass", "Module")
- result = language.parse(f"""
-@I.ir_module
-class Module:
- @X.script
- def caller(x: X.tensor((4,))):
- {setup}
- {owner}.callee(x)
- @X.script
- def callee(y: X.tensor((4,))):
- X.record(y)
-""")
- call = result["caller"].body[0][1]
- assert call.op == "call"
- assert call.args == (language.references["callee"], result["caller"].params[0])
- modules = [event[3] for event in language.events if event[:2] == ("enter", "module")]
- assert len(modules) == 1
- if alias:
- assert not any(event[:2] == ("bind", "cls") for event in language.events)
- assert modules[0].callee is language.references["callee"]
-
-
-@pytest.mark.parametrize(
- "body, line, column, end_column, message",
- [
- (" X = 1\n", 3, 5, 6, "namespace"),
- (" for X in range(2):\n pass\n", 3, 9, 10, "namespace"),
- (
- " if condition:\n y = left\n else:\n z = right\n",
- 6,
- 9,
- 18,
- "same named output",
- ),
- ],
-)
-def test_syntax_diagnostics_point_to_the_offending_binding(
- language, body, line, column, end_column, message
-):
- # Before: X = 1; or if condition: y = left; else: z = right
- # Expected builder program: reject the offending namespace/output binding
- # at its original filename, line and column, before running the builder.
- language.X.__tvm_value_if__ = True
- with pytest.raises(SyntaxError, match=message) as error:
- language.parse("@X.script\ndef main():\n" + body)
- cause = error.value
- assert isinstance(cause, SyntaxError)
- assert (cause.filename, cause.lineno, cause.offset) == ("dummy.py", line, column)
- assert cause.end_lineno == line
- assert cause.end_offset == end_column
- assert not language.events
-
-
-def test_void_branch_statements_need_no_synthetic_named_output(language):
- # Before: if condition: X.record(None); else: X.record(None)
- # Expected builder program:
- # with X.If(condition):
- # with X.Then(): X.emit_(X.record(None))
- # with X.Else(): X.emit_(X.record(None))
- language.X.__tvm_value_if__ = True
- result = language.parse(
- """
-@X.script
-def main():
- if condition:
- X.record(None)
- else:
- X.record(None)
- X.record(9)
-""",
- condition=Value("condition"),
- )
- assert [value for kind, value in result.body] == [None, None, 9]
- assert not any(event[0] == "bind" for event in language.events)
-
-
-def test_lexical_range_binding_calls_the_custom_iterator_once(language):
- # Before: range = custom_range; for i in range(4): X.record(i)
- # Expected builder program: range = X.bind_(custom_range, name="range")
- # with X.for_(range(4), names="i") as i: X.emit_(X.record(i))
- calls = []
-
- def custom_range(extent):
- calls.append(extent)
- return language.X.grid(2)
-
- result = language.parse(
- "@X.script\ndef main():\n range = custom_range\n"
- " for i in range(4):\n X.record(i)\n",
- custom_range=custom_range,
- )
- assert calls == [4]
- variable = result.body[0][1]
- assert variable.op == "loop" and variable.args == (2,) and variable.name == "i"
-
-
-@pytest.mark.parametrize("expression", ["value", "holder.item", "items[0]", "7"])
-def test_non_call_expression_reads_keep_their_source_range(language, expression):
- # Before: value; holder.item; items[0]; 7
- # Expected builder program: X.emit_(expression, span=_S[i]).
- # Names/literals need no expression wrapper; emission still owns their location.
- # Native expression identity survives; host constants stay ordinary values.
- value, reads, located = Value("read"), [], []
-
- class Holder:
- @property
- def item(self):
- reads.append("attribute")
- return value
-
- def __getitem__(self, index):
- reads.append(index)
- return value
-
- def at(location, result):
- located.append(((str(location[0].name), *location[1:]), result))
- return language.at(location, result)
-
- language.I.at_ = at
- holder = Holder()
- result = language.parse(
- f"@X.script\ndef main():\n {expression}\n", value=value, holder=holder, items=holder
- )
- expected = 7 if expression == "7" else value
- location = ("dummy.py", 3, 3, 5, 5 + len(expression))
- assert result.body[0][1] is expected
- assert [(loc, result) for loc, result in located if loc == location] == [(location, expected)]
- assert reads == {"holder.item": ["attribute"], "items[0]": [0]}.get(expression, [])
-
-
-@pytest.mark.parametrize("dtype", [None, "int32", "int64"])
-def test_argument_policy_preserves_expression_dtype(language, dtype):
- @registry.args_policy("X.shape", {"values": "expr_str"}, dtype=dtype)
- def shape(values):
- return values
-
- language.X.shape = shape
- function = language.parse("""
-@X.script
-def main():
- X.shape(("n", "n + 1"))
-""")
- n, increment = function.body[0][1]
- assert n.args == (dtype,)
- assert increment.op == "add"
- assert increment.args[0] is n
- assert increment.args[1] == 1
diff --git a/tests/python/script/test_parser_alias_restoration.py b/tests/python/script/test_parser_alias_restoration.py
deleted file mode 100644
index 4ead5706a1..0000000000
--- a/tests/python/script/test_parser_alias_restoration.py
+++ /dev/null
@@ -1,84 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""A failed production visitor must restore its caller's annotation scope."""
-
-import ast
-
-import pytest
-
-from tvm.script.parser import entry
-
-
-@pytest.mark.parametrize(
- "source, expression",
- [
- pytest.param("lambda outer: (broken := 1)", True, id="lambda"),
- pytest.param("[(broken := 1) for outer in values]", True, id="comprehension"),
- pytest.param("value: (broken := 1) = 0", False, id="body-annotation"),
- pytest.param(
- "@X.script\ndef nested(x: (broken := 1)):\n pass\n",
- False,
- id="function-signature",
- ),
- pytest.param(
- "@X.script\ndef nested(x: X.tensor((4,))):\n value: outer = 0\n (broken := 1)\n",
- False,
- id="function-body",
- ),
- ],
-)
-def test_failed_visitor_restores_later_name_and_body_annotation(language, source, expression):
- setup = "@X.script\ndef main():\n pass\n"
- tree, filename, _ = entry.acquire_source(setup, filename="alias_restoration.py")
- transformer, namespace = entry._prepare_transpiler(
- tree, setup, {"X": language.X}, {}, filename, track_span=False
- )
- outer = object()
- namespace.update(_outer_value=outer)
- aliases = {"outer": "_outer_value"}
- transformer.function.annotation_aliases = aliases
- transformer.annotation_expression = True
- previous_scope = transformer.function.current_scope
- previous_dialect = transformer.function.dialect_prefix
- failing = ast.parse(source, mode="eval").body if expression else ast.parse(source).body[0]
-
- with pytest.raises(SyntaxError, match="Unsupported expression: NamedExpr"):
- transformer.visit(failing)
-
- # Check the observable rewrite after failure, rather than the manager alone:
- # restored annotation mode and body annotations use the same caller alias map.
- reference = transformer.visit(ast.parse("outer", mode="eval").body)
- assert (
- eval(
- compile(ast.fix_missing_locations(ast.Expression(reference)), filename, "eval"),
- namespace,
- )
- is outer
- )
- assignment = transformer.visit(ast.parse("result: outer = 0").body[0])[0]
- annotation = next(keyword.value for keyword in assignment.value.keywords if keyword.arg == "ty")
- assert (
- eval(
- compile(ast.fix_missing_locations(ast.Expression(annotation)), filename, "eval"),
- namespace,
- )
- is outer
- )
- assert transformer.function.annotation_aliases is aliases
- assert transformer.annotation_expression is True
- assert transformer.function.current_scope is previous_scope
- assert transformer.function.dialect_prefix == previous_dialect
diff --git a/tests/python/script/test_parser_bypass_source_attachment.py b/tests/python/script/test_parser_bypass_source_attachment.py
deleted file mode 100644
index 40476ebe7a..0000000000
--- a/tests/python/script/test_parser_bypass_source_attachment.py
+++ /dev/null
@@ -1,34 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Source attachment preserves common IR identity through direct scope values."""
-
-import pytest
-
-import tvm
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.ir_builder import ir as I
-
-
-@pytest.mark.parametrize("count", [1, 2])
-def test_scope_value_source_attachment_preserves_identity(count):
- span = tvm.ir.Span(tvm.ir.SourceName("scope.py"), 3, 3, 4, 25)
- with IRBuilder():
- values = tuple(tvm.ir.Var("", "int32") for _ in range(count))
- result = values[0] if count == 1 else values
- assert I.at_(span, result) is result
- for value in values:
- assert value.span.same_as(span)
diff --git a/tests/python/script/test_parser_call_scopes.py b/tests/python/script/test_parser_call_scopes.py
deleted file mode 100644
index 2bc208994d..0000000000
--- a/tests/python/script/test_parser_call_scopes.py
+++ /dev/null
@@ -1,126 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Production call scopes preserve common node identity and definition locations."""
-
-import pytest
-
-from tvm import ir
-from tvm.ir import prim
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.ir_builder import ir as I
-from tvm.script.parser import entry
-
-
-def loc(line, name="calls.py"):
- return (name, line, line, 1, 20)
-
-
-def span_lines(value):
- spans = value.span.spans if isinstance(value.span, ir.SequentialSpan) else [value.span]
- return [span.line for span in spans]
-
-
-@pytest.fixture
-def spanned(spanned_language):
- language = spanned_language
- language.X.node = lambda value: prim.IntImm("int32", value)
- return language
-
-
-def test_call_scope_keeps_result_identity_and_restores_after_exception():
- seen = []
- with IRBuilder():
- value = prim.IntImm("int32", 1)
- assert I.with_at_group_(loc(1), lambda: (seen.append(1), value)[1]) is value
- assert seen == [1]
- with pytest.raises(ValueError, match="failure"):
- I.with_at_group_(loc(2), lambda: (_ for _ in ()).throw(ValueError("failure")))
- other = I.at_(loc(3), prim.IntImm("int32", 2))
- assert span_lines(other) == [3]
-
-
-def test_scoped_helper_multiple_emissions_and_normal_return(spanned):
- marker = object()
- with spanned.context(), spanned.X.function_():
-
- def helper():
- first = I.with_at_group_(loc(8), lambda: prim.IntImm("int32", 1))
- second = I.with_at_group_(loc(9), lambda: prim.IntImm("int32", 2))
- spanned.X.emit_(first)
- spanned.X.emit_(second)
- return marker
-
- assert I.with_at_group_(loc(4), helper) is marker
- statements = [value for _, value in spanned.result.body]
- assert len(statements) == 2
- assert [int(value) for value in statements] == [1, 2]
- assert span_lines(statements[0]) == [4, 8]
- assert span_lines(statements[1]) == [4, 9]
-
-
-def test_module_source_calls_have_context_before_annotations(spanned):
- seen = []
-
- def record():
- node = prim.IntImm("int32", 3)
- IRBuilder.current()._set_current_source_span(node)
- seen.append(node.span)
- return {}
-
- spanned.I.module_attrs = lambda attrs: None
- module = spanned.parse(
- """
-@I.ir_module
-class Module:
- I.module_attrs(record())
- @X.script
- def main():
- X.record(1)
-""",
- I=spanned.I,
- record=record,
- )
- assert "main" in module
- assert len(seen) == 1
- assert seen[0] is not None
- assert seen[0].line == 4
-
-
-def test_source_inline_keeps_caller_and_each_definition_location(spanned):
- function = entry.parse(
- """
-@X.inline
-def inner(x):
- X.record(X.node(x))
- X.record(X.node(x + 1))
-@X.script
-def main():
- inner(1)
-""",
- extra_vars={"X": spanned.X},
- filename="inline_scope.py",
- )
- statements = [value for _, value in function.body if isinstance(value, prim.IntImm)]
- assert len(statements) == 2
- assert [int(value) for value in statements] == [1, 2]
- assert span_lines(statements[0]) == [8, 4]
- assert span_lines(statements[1]) == [8, 5]
- assert all(
- str(span.source_name.name) == "inline_scope.py"
- for value in statements
- for span in value.span.spans
- )
diff --git a/tests/python/script/test_parser_capture_lifetime.py b/tests/python/script/test_parser_capture_lifetime.py
deleted file mode 100644
index c7341451b3..0000000000
--- a/tests/python/script/test_parser_capture_lifetime.py
+++ /dev/null
@@ -1,340 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Temporary parser ownership must release captures without cyclic collection."""
-
-from __future__ import annotations
-
-import ast
-import copy
-import gc
-import inspect
-import weakref
-from contextlib import contextmanager
-
-import pytest
-
-from tvm.script import ir as I
-from tvm.script.parser import entry
-
-
-class Payload:
- pass
-
-
-@contextmanager
-def without_cyclic_gc():
- enabled = gc.isenabled()
- gc.disable()
- try:
- yield
- finally:
- if enabled:
- gc.enable()
-
-
-def test_live_jit_retains_only_needed_scope_across_uncached_builds(language):
- # A live deferred owner needs width, but never the unrelated enclosing payload.
- from tvm.tirx.script.jit import make_jit
-
- X = language.X
- X.jit = make_jit(X)
-
- def make():
- payload = Payload()
- reference = weakref.ref(payload)
- width = 7
-
- @X.jit
- def kernel(x: X.tensor((width,)), *, value: I.constexpr):
- X.record(value)
-
- return kernel, reference
-
- with without_cyclic_gc():
- kernel, reference = make()
- assert reference() is None
- first = kernel.specialize(value=1)
- second = kernel.specialize(value=2)
- assert reference() is None
- assert first is kernel.specialize(value=1) and first is not second
- for function, value in ((first, 1), (second, 2)):
- assert function.params[0].args[0].args[0] == (7,)
- assert function.body == [("emit", value)]
-
-
-def test_reentrant_specialization_restores_root_bindings_after_failure(language):
- # An ordinary nested parse of the same name must not inherit selected values;
- # a failing nested specialization must restore the outer selection unchanged.
- from tvm.script.parser import jit_support
-
- source = "@X.script\ndef kernel(n: X.tensor((1,))):\n X.record(n)\n"
- seen = []
- failure = ValueError("nested failure")
-
- def fail():
- raise failure
-
- def nested():
- ordinary = entry.parse(source, extra_vars={"X": language.X}, root_builder=language.X)
- assert len(ordinary.params) == 1
- assert ordinary.body[0][1] is ordinary.params[0]
- with pytest.raises(ValueError) as caught:
- entry.parse(
- "@X.script\ndef kernel(n: I.constexpr):\n fail()\n",
- extra_vars={"X": language.X, "I": I, "fail": fail},
- _specialization_bindings={"n": 8},
- root_builder=language.X,
- )
- assert caught.value is failure
- seen.append(jit_support.read_specialization_bindings("kernel"))
-
- result = entry.parse(
- "@X.script\ndef kernel(n: I.constexpr):\n nested()\n X.record(n)\n",
- extra_vars={"X": language.X, "I": I, "nested": nested},
- _specialization_bindings={"n": 4},
- root_builder=language.X,
- )
- assert result.params == [] and result.body[-1] == ("emit", 4)
- assert seen == [{"n": 4}]
- assert jit_support.read_specialization_bindings("kernel") is None
-
-
-@pytest.mark.parametrize("module", [False, True])
-def test_eager_entry_releases_unused_scope_but_keeps_annotation_value(
- language, module, monkeypatch
-):
- # Before: unrelated payload and annotation-only width share an enclosing scope.
- # Expected builder program: use width, then drop the temporary definition scope.
- X = language.X
- source_references = []
- parse = entry.parse
-
- def observe_parse(source, *args, **kwargs):
- if inspect.isfunction(source):
- source_references.append(weakref.ref(source))
- return parse(source, *args, **kwargs)
-
- monkeypatch.setattr(entry, "parse", observe_parse)
-
- def make():
- payload = Payload()
- reference = weakref.ref(payload)
- width = 7
- if module:
-
- @I.ir_module
- class Result:
- @X.script
- def main(value: X.tensor((width,))):
- X.record(1)
- else:
-
- @X.script
- def Result(value: X.tensor((width,))):
- X.record(1)
-
- return Result, reference
-
- with without_cyclic_gc():
- result, reference = make()
- assert reference() is None
- # An ordinary Python class itself has cyclic type/MRO ownership. Its
- # member lifetime is not a registry leak; the standalone source guard
- # checks release without requiring collection of an ordinary Python class.
- if not module:
- assert source_references and all(reference() is None for reference in source_references)
- function = result["main"] if module else result
- assert function.params[0].args[0].args[0] == (7,)
-
-
-@pytest.mark.parametrize("failure", [False, True])
-@pytest.mark.parametrize("macro", [False, True])
-def test_acquired_ast_context_and_builder_are_temporary(language, monkeypatch, failure, macro):
- # Before: parse a function or invoke a statement macro, succeeding or raising.
- # Expected builder program: consume each freshly acquired tree directly; release
- # syntax, contexts and private builders without cyclic GC, on either exit path.
- X = language.X
-
- def finish():
- if failure:
- raise NameError("body failure")
- return 1
-
- @entry.make_macro_decorator(X)
- def helper():
- X.record(finish())
-
- body = "helper()" if macro else "X.record(finish())"
- source = "@X.script\ndef main():\n " + body + "\n"
- references, acquired, parse_calls = [], [], []
- acquire, prepare, recompose = (
- entry.acquire_source,
- entry._prepare_transpiler,
- entry._recompose_builder,
- )
- parse_ast, shallow_copy, deep_copy = ast.parse, copy.copy, copy.deepcopy
-
- def capture_acquire(*args, **kwargs):
- tree, filename, flags = acquire(*args, **kwargs)
- acquired.append(weakref.ref(tree))
- references.append(weakref.ref(tree))
- return tree, filename, flags
-
- def capture_prepare(tree, *args, **kwargs):
- assert tree is acquired[-1]()
- transformer, namespace = prepare(tree, *args, **kwargs)
- references.extend(
- [
- weakref.ref(transformer),
- weakref.ref(transformer.module),
- weakref.ref(transformer.function),
- ]
- )
- return transformer, namespace
-
- def capture_recompose(translated, **kwargs):
- builder = recompose(translated, **kwargs)
- references.append(weakref.ref(builder))
- return builder
-
- def parse_once(*args, **kwargs):
- parse_calls.append(None)
- return parse_ast(*args, **kwargs)
-
- def forbid_ast_copy(value, *args, **kwargs):
- assert not isinstance(value, ast.AST), "the acquired tree is already owned"
- return shallow_copy(value, *args, **kwargs)
-
- def forbid_ast_deepcopy(value, *args, **kwargs):
- assert not isinstance(value, ast.AST), "the acquired tree is already owned"
- return deep_copy(value, *args, **kwargs)
-
- monkeypatch.setattr(entry, "acquire_source", capture_acquire)
- monkeypatch.setattr(entry, "_prepare_transpiler", capture_prepare)
- monkeypatch.setattr(entry, "_recompose_builder", capture_recompose)
- monkeypatch.setattr(ast, "parse", parse_once)
- monkeypatch.setattr(copy, "copy", forbid_ast_copy)
- monkeypatch.setattr(copy, "deepcopy", forbid_ast_deepcopy)
- with without_cyclic_gc():
- for _ in range(2):
- if failure:
- try:
- language.parse(source, finish=finish, helper=helper)
- except NameError:
- pass
- else:
- pytest.fail("expected failed construction")
- else:
- result = language.parse(source, finish=finish, helper=helper)
- assert result.body[0] == ("emit", 1)
- assert references and all(reference() is None for reference in references)
- assert len(acquired) == (4 if macro else 2)
- # This source contains no expression-string policy or extra source objects.
- # Each acquisition parses once; no second parse substitutes for an AST copy.
- assert len(parse_calls) == len(acquired)
-
-
-class FixedFailure(ValueError):
- def __setattr__(self, name, value):
- if name == "__tvm_script_location__":
- raise AttributeError("exception metadata is immutable")
- super().__setattr__(name, value)
-
-
-@pytest.mark.parametrize("error_type", [ValueError, FixedFailure])
-def test_expression_exception_keeps_original_source_call_traceback(spanned_language, error_type):
- # Before: a normal Python helper fails within a generated function body.
- # Expected builder program: its real call frame retains the source expression range.
- import traceback
-
- # Exercise the production shared span boundary as well as the generated call.
- language = spanned_language
- original = error_type("original expression failure")
-
- def explode():
- raise original
-
- source = "@X.script\ndef main():\n X.record(explode())\n"
- filename = "expression_traceback.py"
- with pytest.raises(error_type) as caught:
- entry.parse(
- source,
- extra_vars={"X": language.X, "explode": explode},
- filename=filename,
- root_builder=spanned_language.X,
- )
- assert caught.value is original
- assert type(caught.value) is error_type
- frames = traceback.extract_tb(caught.value.__traceback__)
- calls = [frame for frame in frames if frame.filename == filename and frame.lineno == 3]
- assert calls
- helper_frames = [frame for frame in frames if frame.name == "explode"]
- assert len(helper_frames) == 1
- assert helper_frames[0].filename == inspect.getsourcefile(explode)
- assert helper_frames[0].lineno == inspect.getsourcelines(explode)[1] + 1
- if getattr(calls[-1], "colno", None) is not None:
- assert (calls[-1].colno, calls[-1].end_lineno, calls[-1].end_colno) == (13, 3, 22)
-
-
-def test_live_macro_does_not_snapshot_unrelated_module_globals(language):
- # Before: an unrelated module global exists while a semantic closure is decorated.
- # Expected builder program: retain the closure value, release the removed global.
- key = "_unrelated_macro_payload"
- globals()[key] = Payload()
- reference = weakref.ref(globals()[key])
- decorator = entry.make_macro_decorator(language.X)
- offset = 3
- try:
- with without_cyclic_gc():
-
- @decorator
- def helper(value):
- return value + offset
-
- del globals()[key]
- assert reference() is None
- result = language.parse(
- "@X.script\ndef main():\n X.record(helper(2))\n", helper=helper
- )
- assert result.body == [("emit", 5)]
- finally:
- globals().pop(key, None)
-
-
-@pytest.mark.parametrize("lookup", ["globals", "eval"])
-def test_macro_dynamic_global_lookup_uses_temporary_invocation_namespace(language, lookup):
- # Before: the macro dynamically names a global that has no LOAD_GLOBAL instruction.
- # Expected builder program: it is available during invocation without a stored snapshot.
- key = "_macro_dynamic_value"
- globals()[key] = 7
- decorator = entry.make_macro_decorator(language.X)
- try:
- if lookup == "globals":
-
- @decorator
- def helper():
- return globals()["_macro_dynamic_value"]
- else:
-
- @decorator
- def helper():
- return eval("_macro_dynamic_value")
-
- result = language.parse("@X.script\ndef main():\n X.record(helper())\n", helper=helper)
- assert result.body == [("emit", 7)]
- finally:
- del globals()[key]
diff --git a/tests/python/script/test_parser_comparison_semantics.py b/tests/python/script/test_parser_comparison_semantics.py
deleted file mode 100644
index 99c4b45541..0000000000
--- a/tests/python/script/test_parser_comparison_semantics.py
+++ /dev/null
@@ -1,388 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Source comparison semantics using recording frames and common primitive IR nodes."""
-
-from functools import reduce
-
-import pytest
-import tvm_ffi
-
-from tvm import ir
-from tvm.ir import prim
-from tvm.ir._overload_prim_expr import EqualOp
-from tvm.script.ir_builder import base
-from tvm.script.parser import entry
-
-OPERATORS = [("<", "LT"), ("<=", "LE"), (">", "GT"), (">=", "GE"), ("==", "EQ"), ("!=", "NE")]
-
-
-@pytest.fixture
-def language(language, monkeypatch):
- def span_entry(span):
- language.events.append(("span_entry", span))
- return base.SpanEntry(span)
-
- monkeypatch.setattr(entry, "SpanEntry", span_entry)
- language.I.at_ = base.at
- language.I.with_at_group_ = base.with_at_group_
- for _, kind in OPERATORS:
- constructor = getattr(prim._ffi_api, "_Op" + kind)
- setattr(
- language.X, kind.lower() + "_", lambda lhs, rhs, make=constructor: make(lhs, rhs, None)
- )
-
- def conjunction(*conditions):
- return reduce(lambda rhs, lhs: prim.And(lhs, rhs), reversed(conditions))
-
- language.X.and_ = conjunction
- return language
-
-
-def parse(language, source, *, track_span=True, **captures):
- return entry.parse(
- source,
- extra_vars={"X": language.X, **captures},
- filename="comparison.py",
- track_span=track_span,
- root_builder=language.X,
- )
-
-
-@pytest.mark.parametrize("operator,kind", OPERATORS)
-@pytest.mark.parametrize("literal_left", [True, False])
-@pytest.mark.parametrize("track_span", [True, False])
-def test_written_comparison_order(language, operator, kind, literal_left, track_span):
- # Before: 0 < x (and each written operator/operand order).
- # Expected builder program: X.emit_(X.lt_(0, x), span=_S[i]).
- x = ir.Var("x", "int32")
- expression = f"0 {operator} x" if literal_left else f"x {operator} 0"
- actual = parse(
- language,
- f"@X.script\ndef main():\n {expression}\n",
- x=x,
- track_span=track_span,
- ).body[0][1]
- operands = (0, x) if literal_left else (x, 0)
- ir.assert_structural_equal(actual, getattr(prim, kind)(*operands))
- entries = [event[1] for event in language.events if event[0] == "span_entry"]
- if track_span:
- assert actual.span is not None
- assert entries
- # The written comparison owns the expression range, not either name/literal.
- body_entries = [span for span in entries if span.line == span.end_line == 3]
- assert body_entries
- assert all(
- (span.column, span.end_column) == (5, 5 + len(expression)) for span in body_entries
- )
- else:
- assert actual.span is None
- assert entries == []
-
-
-@pytest.mark.parametrize("dtype", ["int64", "uint32", "float32", "int32x4", "float32x4"])
-def test_literal_uses_ir_operand_type_and_lanes(language, dtype):
- # Before: 0 < x, where x carries the scalar or vector dtype.
- # Expected builder program: X.lt_(0, x); native IR performs literal promotion/broadcast.
- x = ir.Var("x", dtype)
- value = parse(language, "@X.script\ndef main():\n 0 < x\n", x=x).body[0][1]
- assert isinstance(value, prim.LT)
- assert str(value.a.ty) == str(value.b.ty) == dtype
- assert value.b.same_as(x)
-
-
-@pytest.mark.parametrize("operator,kind", OPERATORS)
-@pytest.mark.parametrize("other_kind", ["host", "primitive"])
-def test_custom_host_comparison_result_is_preserved(language, operator, kind, other_kind):
- # Before: consume(X.constexpr(left < right)).
- # Expected builder program: consume(left < right), preserving the Python overload result.
- class Result:
- def __bool__(self):
- raise AssertionError("custom comparison result must not be truth-tested")
-
- def asobject(self):
- raise AssertionError("custom comparison result must not be converted")
-
- result = Result()
- calls = []
-
- class Host:
- pass
-
- def compare(self, other):
- calls.append(other)
- return result
-
- setattr(Host, "__" + kind.lower() + "__", compare)
-
- def consume(value):
- assert value is result
- return 0
-
- right = ir.Var("x", "int32") if other_kind == "primitive" else object()
- parse(
- language,
- f"@X.script\ndef main():\n consume(X.constexpr(left {operator} right))\n",
- left=Host(),
- right=right,
- consume=consume,
- )
- assert len(calls) == 1
- if other_kind == "primitive":
- assert calls[0].same_as(right)
- else:
- assert calls[0] is right
-
-
-def test_numeric_subclass_keeps_custom_comparison(language):
- seen = []
- result = object()
-
- class HostInt(int):
- def __lt__(self, other):
- seen.append(other)
- return result
-
- def consume(value):
- assert value is result
- return 0
-
- parse(
- language,
- "@X.script\ndef main():\n consume(X.constexpr(left < x))\n",
- x=ir.Var("x", "int32"),
- left=HostInt(0),
- consume=consume,
- )
- assert len(seen) == 1
-
-
-def test_host_ordering_does_not_materialize_an_equality_result(language):
- result = ir.Var("host_result", "int32") == 0
-
- class Host:
- def __lt__(self, other):
- return result
-
- def consume(value):
- assert value is result
- return 0
-
- parse(
- language,
- "@X.script\ndef main():\n consume(X.constexpr(left < right))\n",
- left=Host(),
- right=Host(),
- consume=consume,
- )
-
-
-def test_simple_chain_and_complex_operand_rejection(language):
- # Simple adjacent comparisons lower directly; complex chains fail before
- # operand evaluation. The explicit constexpr boundary stays ordinary Python.
- x, y = ir.Var("x", "int32"), ir.Var("y", "int32")
- actual = parse(
- language,
- "@X.script\ndef main():\n -1 < x <= y != +3\n",
- x=x,
- y=y,
- ).body[0][1]
- expected = prim.And(prim.LT(-1, x), prim.And(prim.LE(x, y), prim.NE(y, 3)))
- ir.assert_structural_equal(actual, expected)
- assert actual.span.line == 3
-
- def operand():
- pytest.fail("unsupported chain evaluated its operand")
-
- class Holder:
- @property
- def value(self):
- return operand()
-
- def __getitem__(self, index):
- return operand()
-
- for expression in (
- "operand() < x < y",
- "x < holder.value < y",
- "x < holder[0] < y",
- "x < x + 1 < y",
- ):
- with pytest.raises(SyntaxError, match="chain") as caught:
- parse(
- language,
- f"@X.script\ndef main():\n {expression}\n",
- operand=operand,
- holder=Holder(),
- x=x,
- y=y,
- )
- assert caught.value.filename == "comparison.py" and caught.value.lineno == 3
- assert caught.value.offset >= 5
-
-
-def test_constexpr_keeps_python_comparison_and_chain_short_circuit(language):
- # Before: if X.constexpr(a() < b() < invalid()): ...
- # Expected builder program: ordinary Python if/chain; the last operand stays lazy.
- seen = []
-
- def operand(index, value):
- seen.append(index)
- return value
-
- def invalid():
- raise AssertionError("constexpr comparison chain must short-circuit")
-
- actual = parse(
- language,
- """
-@X.script
-def main():
- if X.constexpr(operand(0, 2) < operand(1, 1) < invalid()):
- 0
- elif X.constexpr(x == x):
- 1
-""",
- operand=operand,
- invalid=invalid,
- x=ir.Var("x", "int32"),
- )
- assert seen == [0, 1]
- assert actual.body == [("emit", 1)]
-
-
-@pytest.mark.parametrize("operator,kind", OPERATORS)
-@pytest.mark.parametrize("dtype", ["int64", "float32", "int64x4"])
-def test_comparisons_use_native_promotion_and_broadcast(language, operator, kind, dtype):
- # Before: x < y (and each comparison), with distinct primitive types.
- # Expected builder program: X.lt_(x, y); native IR owns the cast and broadcast.
- x, y = ir.Var("x", "int32"), ir.Var("y", dtype)
- actual = parse(
- language,
- f"@X.script\ndef main():\n x {operator} y\n",
- x=x,
- y=y,
- ).body[0][1]
- lhs = prim.Broadcast(prim.Cast("int64", x), 4) if dtype.endswith("x4") else prim.Cast(dtype, x)
- ir.assert_structural_equal(actual, getattr(prim, kind)(lhs, y))
-
-
-@pytest.mark.parametrize("operator,kind", [("==", prim.EQ), ("!=", prim.NE)])
-@pytest.mark.parametrize("track_span", [True, False])
-def test_symbolic_equality_reaches_typed_consumer(language, operator, kind, track_span):
- # Before: consume(x == 0), with source tracking enabled or disabled.
- # Expected builder program: consume(X.eq_(x, 0)), with the original comparison location.
- seen = []
-
- def consume(value):
- assert isinstance(value, kind)
- assert str(value.ty) == "bool"
- seen.append(value)
- return value
-
- x = ir.Var("x", "int32")
- parse(
- language,
- f"@X.script\ndef main():\n consume(x {operator} 0)\n",
- consume=consume,
- x=x,
- track_span=track_span,
- )
- assert len(seen) == 1
- assert seen[0].a.same_as(x)
- if track_span:
- assert seen[0].span is not None
- else:
- assert seen[0].span is None
-
-
-def test_python_comparisons_remain_python_booleans(language):
- seen = []
-
- def consume(value):
- assert type(value) is bool
- seen.append(value)
- return int(value)
-
- parse(
- language,
- """
-@X.script
-def main():
- consume(X.constexpr([1, 2] == [1, 2]))
- consume(X.constexpr("x" != "y"))
-""",
- consume=consume,
- )
- assert seen == [True, True]
-
-
-def test_host_equality_result_is_not_arbitrarily_converted(language):
- class HostResult(tvm_ffi.ObjectConvertible):
- def asobject(self):
- raise AssertionError("host comparison result must not be converted")
-
- result = HostResult()
-
- class Host:
- def __eq__(self, other):
- return result
-
- seen = []
-
- def consume(value):
- assert value is result
- seen.append(value)
- return 0
-
- source = "@X.script\ndef main():\n consume(X.constexpr(left == right))\n"
- parse(language, source, left=Host(), right=Host(), consume=consume)
- assert seen == [result]
- assert seen[0] is result
-
- class CustomEquality(EqualOp):
- def asobject(self):
- raise AssertionError("custom equality subclasses are host values")
-
- custom = CustomEquality(ir.Var("x", "int32"), 0)
- result = custom
- parse(language, source, left=Host(), right=Host(), consume=consume)
- assert seen[-1] is custom
-
-
-def test_comparison_operands_evaluate_once_in_order(language):
- # Before: consume(a() == b()).
- # Expected builder program: evaluate operands once, then pass concrete EQ/And IR to consume.
- seen = []
-
- def operand(index, value):
- seen.append(index)
- return value
-
- def consume(value):
- assert isinstance(value, ir.Expr)
- assert str(value.ty) == "bool"
- return value
-
- expression = "operand(0, value0) == operand(1, value1)"
- parse(
- language,
- "@X.script\ndef main():\n" + f" consume({expression})\n",
- operand=operand,
- consume=consume,
- **{f"value{i}": ir.Var(f"value{i}", "int32") for i in range(2)},
- )
- assert seen == [0, 1]
diff --git a/tests/python/script/test_parser_entry.py b/tests/python/script/test_parser_entry.py
new file mode 100644
index 0000000000..f2d0341a39
--- /dev/null
+++ b/tests/python/script/test_parser_entry.py
@@ -0,0 +1,87 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Public string and callable entries build fresh script objects.
+
+A small spans-off callable also documents the generated builder program exactly.
+"""
+
+from __future__ import annotations
+
+# Keep the single generated-program golden exactly as ast.unparse emits it.
+import ast
+import sys
+from textwrap import dedent
+
+from tvm.script.parser import entry
+
+
+def test_parse_string_returns_fresh_symbols(language):
+ # The direct string API must create independent symbol/parameter objects on repeated calls.
+ # This is the suite's single dedicated parse(str) API case.
+ source = '@M.function\ndef main(x: M.Tensor(("n + 1",))):\n M.record(x)\n'
+ first = entry.parse(source, extra_vars={"M": language.M}, root_builder=language.M)
+ second = entry.parse(source, extra_vars={"M": language.M}, root_builder=language.M)
+ assert first.params[0] is not second.params[0]
+ first_shape = first.params[0].args[0].args[0][0]
+ second_shape = second.params[0].args[0].args[0][0]
+ assert first_shape.op == second_shape.op == "add"
+ assert first_shape.args[0] is not second_shape.args[0]
+ assert first_shape.args[1] == second_shape.args[1] == 1
+ assert first.body[0][1] is first.params[0]
+ assert second.body[0][1] is second.params[0]
+
+
+def test_callable_entry_emits_the_expected_builder_program(language, monkeypatch):
+ # Normal callable parsing must construct the documented builder program without span
+ # scaffolding.
+ M = language.M
+
+ def identity(x: M.Tensor((4,))):
+ M.record(x)
+
+ programs = []
+ recompose = entry._recompose_builder
+
+ def observe(translated, **kwargs):
+ programs.append(ast.unparse(translated))
+ return recompose(translated, **kwargs)
+
+ # Observe actual generated output; the original recomposition and execution still run.
+ monkeypatch.setattr(entry, "_recompose_builder", observe)
+ result = entry.parse(identity, root_builder=M, track_span=False)
+ expected = dedent(
+ """
+ with _I0.IRBuilder() as _builder0:
+ with M.function_() as _fn0:
+ M.func_name('identity')
+ _parameter0 = M.arg('x', _host0(M.Tensor((4,))))
+
+ def _build0():
+ x, = _fn0.params
+ M.emit_(M.record(x))
+ _build0()
+ _result0 = _fn0.function
+ _result0.__name__ = 'identity'
+ M.check_well_formed_(_result0)
+ """
+ ).strip()
+ if sys.version_info[:2] == (3, 10):
+ # Python 3.10's ast.unparse parenthesizes this tuple assignment target.
+ expected = expected.replace("x, = _fn0.params", "(x,) = _fn0.params")
+ assert programs == [expected]
+ assert result.params[0].args[0].args[0] == (4,)
+ assert result.body == [("emit", result.params[0])]
diff --git a/tests/python/script/test_parser_host_selection.py b/tests/python/script/test_parser_host_selection.py
deleted file mode 100644
index a9be98364e..0000000000
--- a/tests/python/script/test_parser_host_selection.py
+++ /dev/null
@@ -1,242 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Compile-time choices preserve source order, scope and missing-value errors."""
-
-import pytest
-
-from tvm.script.parser import entry
-
-
-def parse(language, source, *, extra_vars=None, **options):
- return entry.parse(
- source,
- extra_vars={"X": language.X, **(extra_vars or {})},
- **options,
- root_builder=language.X,
- )
-
-
-def test_marked_expressions_are_lazy_operand_valued_and_ordered(language):
- # Before: value if I.constexpr(condition) else other; I.constexpr(a) and b.
- # Expected builder program: Python conditional/and/or expressions retain lazy operands.
- seen = []
-
- def operand(value):
- seen.append(value)
- return value
-
- result = parse(
- language,
- """
-@X.script
-def main():
- X.record(operand(1) if I.constexpr(operand(True)) else invalid())
- X.record(I.constexpr(operand(0)) and invalid())
- X.record(I.constexpr(operand(4)) or invalid())
- X.record(I.constexpr(operand(2)) and operand(7))
- X.record(I.constexpr(operand(0)) or operand(8))
-""",
- extra_vars={"operand": operand},
- )
- assert seen == [True, 1, 0, 4, 2, 7, 0, 8]
- assert [value for _, value in result.body] == [1, 0, 4, 7, 8]
-
-
-def test_nested_unmarked_statement_retains_ir_frame(language):
- result = parse(
- language,
- """
-@X.script
-def main(condition: X.value):
- if I.constexpr(True):
- if condition:
- X.record(1)
- else:
- X.record(2)
-""",
- )
- assert result.body == [("emit", 1), ("emit", 2)]
- assert [event[1] for event in language.events if event[0] == "enter"] == [
- "function",
- "if",
- "then",
- "else",
- ]
-
-
-@pytest.mark.parametrize("condition", [True, False])
-def test_conditional_binding_can_be_assigned_after_skipped_branch(language, condition):
- result = parse(
- language,
- f"""
-@X.script
-def main():
- if I.constexpr({condition}):
- x = 1
- x = 2
- X.record(x)
-""",
- )
- assert result.body[-1] == ("emit", 2)
-
-
-@pytest.mark.parametrize("track_span", [True, False])
-def test_constexpr_keeps_named_expression_unsupported(language, track_span):
- with pytest.raises(SyntaxError, match="Unsupported expression: NamedExpr"):
- parse(
- language,
- """
-@X.script
-def main():
- if I.constexpr(bool(x := 3)):
- X.record(x)
-""",
- track_span=track_span,
- )
-
-
-def test_ir_optional_binding_survives_skipped_host_assignment(language):
- result = parse(
- language,
- """
-@X.script
-def main(condition: X.value):
- if condition:
- x = X.value(1)
- if I.constexpr(False):
- x = X.value(2)
- x = X.value(3)
- X.record(x)
-""",
- )
- assert result.body[-1][1].args == (3,)
-
-
-@pytest.mark.parametrize(
- "expression", ["1 if I.constexpr(x) else 0", "I.constexpr(x) and 1", "I.constexpr(x) or 1"]
-)
-def test_missing_host_binding_cannot_be_truth_tested(language, expression):
- with pytest.raises(NameError) as caught:
- parse(
- language,
- f"""
-@X.script
-def main():
- if I.constexpr(False):
- x = 1
- X.record({expression})
-""",
- )
- assert isinstance(caught.value, NameError)
-
-
-def test_missing_host_if_binding_raises_before_branch_assignments(language):
- with pytest.raises(NameError) as caught:
- parse(
- language,
- """
-@X.script
-def main():
- if I.constexpr(False):
- x = 1
- if I.constexpr(x):
- x = 2
- else:
- x = 3
-""",
- )
- assert isinstance(caught.value, NameError)
-
-
-def test_host_lambda_local_does_not_capture_optional_binding(language):
- result = parse(
- language,
- """
-@X.script
-def main():
- if I.constexpr(False):
- x = 1
- if I.constexpr((lambda x: x)(True)):
- X.record(222)
-""",
- )
- assert result.body[-1] == ("emit", 222)
-
-
-def test_parser_skips_invalid_ramp_and_preserves_support_name(language):
- # Before: X.ramp(0, 1, _PS) if I.constexpr(_PS > 1) else 0
- # Expected builder program: X.ramp(0, 1, _PS) if _PS > 1 else 0
- # The captured _PS name remains an ordinary user binding.
- source = """
-@X.script
-def main():
- X.record(X.ramp(0, 1, _PS) if I.constexpr(_PS > 1) else 0)
-"""
- function = parse(language, source, extra_vars={"_PS": 1})
- assert function.body == [("emit", 0)]
-
-
-def test_source_logical_operands_skip_at_construction(language):
- # Before: value if I.constexpr(False and fail()) else fallback
- # Expected builder program: value if False and fail() else fallback
- # Calls inside the constexpr operand retain Python short-circuit behavior.
- def fail():
- raise AssertionError("skipped source operand was evaluated")
-
- function = parse(
- language,
- """
-@X.script
-def main():
- X.record(1 if I.constexpr(False and fail()) else 2)
- X.record(3 if I.constexpr(True or fail()) else 4)
-""",
- extra_vars={"fail": fail},
- )
- assert [value for _, value in function.body] == [2, 3]
-
-
-@pytest.mark.parametrize("operator", ["and", "or"])
-def test_unmarked_logical_chain_preserves_left_association(language, operator):
- # Before: a and b and c (also or).
- # Expected builder program: X.and_(X.and_(a, b), c).
- result = language.parse(f"""
-@X.script
-def main(a: X.value(), b: X.value(), c: X.value()):
- X.record(a {operator} b {operator} c)
-""")
- expression = result.body[0][1]
- assert expression.op == operator
- assert expression.args[1] is result.params[2]
- assert expression.args[0].op == operator
- assert expression.args[0].args == tuple(result.params[:2])
-
-
-def test_ir_branch_incoming_read_and_rebinding_obeys_python_scope(language):
- with pytest.raises(NameError) as error:
- language.parse("""
-@X.script
-def main(condition: X.value(), x: X.value()):
- y = x
- if condition:
- y = y + x
- else:
- y = x
- return y
-""")
- assert isinstance(error.value, NameError)
- assert "y" in str(error.value)
diff --git a/tests/python/script/test_parser_latest_design.py b/tests/python/script/test_parser_latest_design.py
deleted file mode 100644
index 14946b3603..0000000000
--- a/tests/python/script/test_parser_latest_design.py
+++ /dev/null
@@ -1,500 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Shared semantic boundaries for the explicit parser/builder contract."""
-
-from __future__ import annotations
-
-import ast
-
-import pytest
-
-from tvm.script import ir as I
-from tvm.script.parser import entry
-from tvm.script.parser import protocol_registry as registry
-
-
-@pytest.mark.parametrize("module", [False, True])
-@pytest.mark.parametrize("enabled", [False, True])
-def test_validation_hook_runs_after_complete_construction(language, monkeypatch, module, enabled):
- # Before: a standalone function or two-member module, with validation enabled/disabled.
- # Expected builder program: complete all bodies/frames, then X/I.check_well_formed_.
- seen = []
-
- def check(result):
- assert language.stack == []
- if module:
- assert set(result) == {"first", "second"}
- assert result["first"].body == [("emit", 1)]
- assert result["second"].body == [("emit", 2)]
- else:
- assert result.body == [("emit", 1)]
- seen.append(result)
-
- if module:
- import tvm.script as script
- from tvm.script.ir_builder.ir import parser_protocol as root_protocol
-
- monkeypatch.setattr(script, "_MODULE_VALIDATORS", [])
- script.register_module_validator(check)
- language.I.check_well_formed_ = root_protocol.check_well_formed_
- else:
- language.I.check_well_formed_ = lambda result: pytest.fail("module hook")
- language.X.check_well_formed_ = (
- check if not module else lambda result: pytest.fail("function hook")
- )
- source = (
- "@I.ir_module\nclass M:\n @X.script\n def first():\n X.record(1)\n"
- " @X.script\n def second():\n X.record(2)\n"
- if module
- else "@X.script\ndef first():\n X.record(1)\n"
- )
- result = entry.parse(
- source, {"X": language.X}, check_well_formed=enabled, root_builder=language.X
- )
- assert seen == ([result] if enabled else [])
- if module and enabled:
- failure = ValueError("dialect validation failure")
-
- def reject(completed):
- assert set(completed) == {"first", "second"}
- assert language.stack == []
- raise failure
-
- monkeypatch.setattr(script, "_MODULE_VALIDATORS", [reject])
- with pytest.raises(ValueError) as caught:
- entry.parse(source, {"X": language.X}, root_builder=language.X)
- assert caught.value is failure
- assert language.stack == [] and language.source_stack == []
-
-
-def test_pyfunc_collection_preserves_original_callable_and_closure(language):
- # Before: I.pyfunc marks an ordinary closure in a class.
- # Expected builder program: attach the original callable to __pyfuncs__; no IR declaration.
- factor = 7
- calls = []
-
- class Source:
- @I.pyfunc
- def multiply(value):
- calls.append(value)
- return value * factor
-
- original = Source.multiply
- result = entry.ir_module(Source)
- assert result.__pyfuncs__ == {"multiply": original}
- assert result == {}
- assert calls == []
- assert result.__pyfuncs__["multiply"](3) == 21
- assert calls == [3]
- assert not any(event[0] in ("arg", "name") for event in language.events)
-
-
-@pytest.mark.parametrize(
- "source, introduction, offending",
- [
- ("@X.script\ndef main(x: X.tensor((n,))):\n n = 2\n", 2, 3),
- ("@X.script\ndef main():\n n = X.symbol()\n n = 2\n", 3, 4),
- (
- "@X.script\ndef main():\n n = X.symbol()\n"
- " if X.constexpr(True):\n n = 2\n",
- 3,
- 5,
- ),
- ("@X.script\ndef main():\n n = X.symbol()\n n += 1\n", 3, 4),
- ],
-)
-def test_symbol_reassignment_reports_introduction_and_exact_write(
- language, source, introduction, offending
-):
- # Before: annotation-free/body-declared n, followed by an ordinary write.
- # Expected diagnostic: n, its introduction line, and the actual offending source target.
- with pytest.raises(SyntaxError) as caught:
- language.parse(source, n=4)
- message = str(caught.value)
- assert "Symbolic variable 'n' cannot be reassigned" in message
- assert f"introduced at line {introduction}" in message
- assert caught.value.filename == "dummy.py"
- cause = caught.value
- assert isinstance(cause, SyntaxError)
- assert (cause.lineno, cause.end_lineno) == (offending, offending)
- assert cause.end_offset - cause.offset == 1
-
-
-def test_repeated_symbol_declarations_reuse_identity(language):
- # Before: annotation n; n = X.symbol(); n = X.symbol().
- # Expected builder program: both declarations resolve the same function-owned symbol.
- result = language.parse(
- "@X.script\ndef main(x: X.tensor(('n',))):\n"
- " n = X.symbol()\n X.record(n)\n n = X.symbol()\n X.record(n)\n"
- )
- symbol = result.params[0].args[0].args[0][0]
- assert result.body == [("emit", symbol), ("emit", symbol)]
- assert result.body[0][1] is result.body[1][1]
-
-
-def test_mutable_loop_and_ordinary_names_remain_assignable(language):
- # Before: cell declaration/update, loop-variable rebinding, ordinary rebinding.
- # Expected builder program: set_mutable_cell_ for cell, ordinary bind_ for i/value.
- result = language.parse(
- "@X.script\ndef main():\n cell = X.cell(0)\n cell = 1\n"
- " value = 2\n value = 3\n for i in X.grid(4):\n"
- " i = 5\n X.record(i + value)\n"
- )
- assert result.body == [("emit", 8)]
- assert len([event for event in language.events if event[0] == "set"]) == 1
-
-
-def test_nested_scope_does_not_reassign_outer_symbol(language):
- # Before: outer n is symbolic; nested function has an independent ordinary n.
- # Expected builder program: each source function has its own declaration/alias context.
- result = language.parse(
- "@X.script\ndef main():\n n = X.symbol()\n"
- " @X.script\n def nested():\n n = 2\n X.record(n)\n X.record(n)\n"
- )
- assert result.body[0][1].op == "symbol"
- assert language.functions["nested"].body == [("emit", 2)]
-
-
-def test_local_callable_keeps_ordinary_python_shadowing(language):
- # Before: a local callable shadows a global of the same spelling.
- # Expected builder program: ordinary local call/bind semantics and one evaluation.
- calls = []
-
- def ordinary():
- calls.append("ordinary")
- return 7
-
- def operation():
- pytest.fail("shadowed global callable executed")
-
- result = language.parse(
- "@X.script\ndef main():\n operation = ordinary\n"
- " value = operation()\n X.record(value)\n",
- operation=operation,
- ordinary=ordinary,
- )
- assert calls == ["ordinary"]
- assert result.body == [("emit", 7)]
- assert any(event[:2] == ("bind", "value") for event in language.events)
-
-
-def test_body_annotation_reads_a_preceding_ordinary_local(language):
- # Before: shape is constructed in the function before a local annotation uses it.
- # Expected builder program: the annotation reads that local shape, not an absent capture.
- bind = language.X.bind_
- annotations = []
-
- def observe(value, **kwargs):
- if "ty" in kwargs:
- annotations.append(kwargs["ty"])
- return bind(value, **kwargs)
-
- language.X.bind_ = observe
- result = language.parse(
- "@X.script\ndef main():\n shape = (4,)\n"
- " value: X.tensor(shape) = 1\n X.record(value)\n"
- " shape = (8,)\n X.record(shape)\n"
- )
- assert len(annotations) == 1 and annotations[0].args[0] == (4,)
- assert result.body == [("emit", 1), ("emit", (8,))]
-
-
-@pytest.mark.parametrize(
- "source, node_type, message",
- [
- ("@X.script\ndef main(value):\n pass\n", ast.arg, "requires an annotation"),
- (
- '@X.script\ndef main(value: "invalid +"):\n pass\n',
- ast.Constant,
- "Invalid annotation expression",
- ),
- (
- "@X.script\ndef main():\n X.record((value :=\n 1))\n",
- ast.NamedExpr,
- "Unsupported expression: NamedExpr",
- ),
- ],
-)
-def test_transpilation_restrictions_keep_original_source_ranges(
- language, source, node_type, message
-):
- # Before: invalid signature, quoted annotation or multiline expression syntax.
- # Expected: direct SyntaxError points at the original node before builder execution.
- expected = next(node for node in ast.walk(ast.parse(source)) if isinstance(node, node_type))
- with pytest.raises(SyntaxError, match=message) as caught:
- entry.parse(
- source, extra_vars={"X": language.X}, filename="restriction.py", root_builder=language.X
- )
- error = caught.value
- assert type(error) is SyntaxError
- assert (error.filename, error.lineno, error.offset, error.end_lineno, error.end_offset) == (
- "restriction.py",
- expected.lineno,
- expected.col_offset + 1,
- expected.end_lineno,
- expected.end_col_offset + 1,
- )
- assert not language.events
-
-
-def test_result_span_contract_keeps_opaque_context_and_nested_calls(language):
- # Before: opaque(arg()) and X.known(arg()).
- # Expected builder program: only the opaque call needs .ctx; each argument
- # still has its own call context and the result keeps its expression span.
- from dummy_builder import Value
-
- seen = []
-
- def opaque(value, *, span=None):
- seen.append(("opaque", len(language.source_stack)))
- return Value("opaque", (value,))
-
- @registry.result_span("X.known")
- def known(value):
- seen.append(("result", len(language.source_stack)))
- return Value("result", (value,))
-
- def argument(value):
- seen.append(("argument", value, len(language.source_stack)))
- return value
-
- language.X.known = known
- function = language.parse(
- "@X.script\ndef main():\n opaque(argument(1))\n X.known(argument(2))\n",
- opaque=opaque,
- argument=argument,
- )
- assert seen == [
- ("argument", 1, 2),
- ("opaque", 1),
- ("argument", 2, 1),
- ("result", 0),
- ]
- assert [value.op for _, value in function.body] == ["opaque", "result"]
- assert [value.span[-1][1] for _, value in function.body] == [3, 4]
-
-
-def test_native_bind_keeps_returned_and_stored_variable_identity():
- # renamed = T.bind(argument(x), var=produced): the native producer alone
- # emits its binding, and assignment preserves its explicit variable/name/span.
- from tvm import ir, tirx
-
- span = ir.Span(ir.SourceName("producer.py"), 7, 7, 2, 19)
- produced = ir.Var("producer_name", "int32", span)
- calls, observed = [], []
-
- def argument(value):
- calls.append(value)
- return value
-
- def observe(value):
- observed.append(value)
- assert value.same_as(produced) and value.span.same_as(span)
- assert value.name == "producer_name"
-
- function = entry.parse(
- "@T.prim_func\ndef main(x: T.int32):\n"
- " renamed = T.bind(argument(x), var=produced)\n"
- " alias = renamed\n called = argument(alias)\n"
- " observe(renamed)\n observe(alias)\n observe(called)\n T.evaluate(called)\n",
- extra_vars={"produced": produced, "argument": argument, "observe": observe},
- )
- binding, use = function.body.seq
- assert isinstance(binding, tirx.Bind) and isinstance(use, tirx.Evaluate)
- assert binding.var.same_as(produced) and use.value.same_as(produced)
- assert len(observed) == 3 and len(calls) == 2
- assert calls[0].same_as(function.params[0]) and calls[1].same_as(produced)
-
-
-def test_native_view_keeps_producer_identity_name_and_span(monkeypatch):
- # renamed = captured.view(mark()); assignment must not rename, attach a
- # consumer span to, or redeclare the ordinary native producer's result.
- from functools import wraps
-
- from tvm import ir, tirx
- from tvm.script.ir_builder import base
- from tvm.tirx.script import builder as T
-
- captured = T.Buffer((4, 4), "float32")
- original = type(captured).view
- seen, produced, observed = [], [], []
- span = base.source_span(("producer.py", 7, 7, 2, 19))
-
- @wraps(original)
- def view(buffer, *args):
- seen.append("view")
- value = base.at_(span, original(buffer, *args))
- produced.append((value, value.name, value.span))
- return value
-
- def mark():
- seen.append("argument")
- return 16
-
- def observe(value):
- observed.append((value, value.span))
-
- monkeypatch.setattr(type(captured), "view", view)
- function = entry.parse(
- "@T.prim_func\ndef main(A: captured):\n"
- " renamed = captured.view(mark())\n alias = renamed\n"
- " observe(renamed)\n observe(alias)\n alias[0] = 0\n"
- " captured.view(mark())\n",
- extra_vars={"captured": captured, "mark": mark, "observe": observe},
- )
- assert seen == ["argument", "view", "argument", "view"]
- value, name, produced_span = produced[0]
- assert len(produced) == len(observed) == 2
- assert name != "renamed" and value.name == name
- assert all(
- item.same_as(value) and location.same_as(produced_span) for item, location in observed
- )
- nodes = list(function.body.seq)
- assert len(nodes) == 3 and not any(isinstance(node, tirx.Bind) for node in nodes)
- assert nodes[0].buffer.same_as(value) and nodes[1].buffer.same_as(value)
- assert nodes[2].buffer.same_as(produced[1][0])
- ir.assert_structural_equal(nodes[0].data, captured.data)
- ir.assert_structural_equal(nodes[2].data, captured.data)
-
-
-def test_native_binding_preserves_metadata_but_binds_buffer_expressions():
- # Metadata passes through bind_ without walking resources. A buffer-valued
- # expression that is not a Var still creates an ordinary native binding.
- from tvm import ir, tirx
- from tvm.script.ir_builder import base
- from tvm.tirx.script import builder as T
-
- producer_span = ir.Span(ir.SourceName("producer.py"), 7, 7, 2, 19)
- buffer = T.Buffer((4,), "float32")
- base.at_(producer_span, buffer)
- layout = T.TileLayout(T.S[4])
-
- @T.meta_class
- class Holder:
- def __init__(self, resource):
- self.resource = resource
-
- holder = Holder(buffer)
- projection = ir.TupleGetItem(ir.Tuple([buffer]), 0)
- values = [layout, holder, projection]
- calls, observed, resource_spans = [], [], []
-
- def make(index):
- resource_spans.append(buffer.span)
- calls.append(index)
- return values[index]
-
- def observe(*items):
- observed.extend(items)
-
- function = entry.parse(
- "@T.prim_func\ndef main(A: buffer):\n"
- " renamed_layout = make(0)\n"
- " renamed_holder = make(1)\n bound = make(2)\n"
- " observe(renamed_layout, renamed_holder)\n"
- " make(0)\n make(1)\n",
- extra_vars={"buffer": buffer, "make": make, "observe": observe},
- filename="binding.py",
- )
- assert calls == [0, 1, 2, 0, 1]
- assert observed[0].same_as(layout)
- assert observed[1] is holder and holder.resource.same_as(buffer)
- assert all(buffer.span.same_as(span) for span in resource_spans)
- binding = function.body
- assert isinstance(binding, tirx.Bind) and binding.value.same_as(projection)
- assert binding.var.name == "bound"
- assert tirx.is_buffer_var(binding.var)
- assert not isinstance(projection, ir.Var)
- assert (binding.var.span.line, binding.var.span.column, binding.var.span.end_column) == (
- 5,
- 5,
- 10,
- )
- assert (projection.span.line, projection.span.column, projection.span.end_column) == (5, 13, 20)
- # Inert metadata statements do not make arbitrary host values valid IR.
- with pytest.raises(TypeError):
- entry.parse(
- "@T.prim_func\ndef invalid():\n unsupported()\n",
- extra_vars={"unsupported": object},
- )
-
-
-def test_native_concise_scopes_unwind_with_their_parent():
- # bx = launch_thread(...); tx = launch_thread(...); evaluate(bx + tx).
- # bind_ enters native children; parent callbacks close nested scopes in order.
- from tvm import tirx
- from tvm.script.ir_builder import IRBuilder
- from tvm.tirx.script import builder as T
-
- with IRBuilder() as builder:
- with T.function_() as parent:
- T.func_name("main")
- block = T.launch_thread("blockIdx.x", 2)
- assert builder.frames[-1].same_as(parent)
- bx = T.bind_(block, name="bx")
- thread = T.launch_thread("threadIdx.x", 32)
- tx = T.bind_(thread, name="tx")
- assert builder.frames[-1].same_as(thread)
- T.evaluate(bx + tx)
- assert not builder.frames
- # Callback storage is native and not exposed as Python frame state;
- # empty active frames and the nested result establish actual unwinding.
- body = builder.get().body
- assert isinstance(body, tirx.AttrStmt) and isinstance(body.body, tirx.AttrStmt)
- assert body.node.var.same_as(bx) and body.body.node.var.same_as(tx)
- assert body.body.body.value.a.same_as(bx) and body.body.body.value.b.same_as(tx)
-
-
-@pytest.mark.parametrize("control", ["break", "continue"])
-def test_native_loop_control_is_checked_only_after_construction(monkeypatch, control):
- # Before: break/continue outside a loop, with validation disabled or enabled.
- # Expected builder program: construct the same native IR in both cases;
- # the final native hook rejects invalid placement after the function frame exits.
- from tvm import ir, s_tir, tirx
- from tvm.script.ir_builder import IRBuilder
-
- check = s_tir.analysis.verify_well_formed
- checked = []
-
- def check_completed(module):
- assert not IRBuilder.is_in_scope()
- checked.append(module)
- return check(module)
-
- monkeypatch.setattr(s_tir.analysis, "verify_well_formed", check_completed)
- source = f"@T.prim_func\ndef main():\n {control}\n"
- invalid = entry.parse(source, check_well_formed=False)
- assert isinstance(invalid.body, tirx.Evaluate)
- assert invalid.body.value.op.same_as(getattr(tirx, control + "_loop")().op)
- assert checked == []
- with pytest.raises(ValueError, match="requires an enclosing loop"):
- entry.parse(source)
- assert len(checked) == 1
- ir.assert_structural_equal(checked[0]["main"], invalid)
- valid = entry.parse(f"@T.prim_func\ndef main():\n for i in range(2):\n {control}\n")
- assert isinstance(valid.body, tirx.For)
- assert valid.body.body.value.op.same_as(invalid.body.value.op)
- assert len(checked) == 2 and checked[-1]["main"].same_as(valid)
-
- # No source function decorator appears here: root validation must still
- # check a captured native member, after the complete module is assembled.
- module_source = "@I.ir_module\nclass M:\n bad = invalid\n"
- module = entry.parse(module_source, extra_vars={"invalid": invalid}, check_well_formed=False)
- assert module["bad"].same_as(invalid)
- with pytest.raises(ValueError, match="requires an enclosing loop"):
- entry.parse(module_source, extra_vars={"invalid": invalid})
- assert len(checked) == 3 and checked[-1]["bad"].same_as(invalid)
diff --git a/tests/python/script/test_parser_scope_helpers.py b/tests/python/script/test_parser_scope_helpers.py
deleted file mode 100644
index 347c9929ae..0000000000
--- a/tests/python/script/test_parser_scope_helpers.py
+++ /dev/null
@@ -1,127 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Lexical-only body helpers execute inside their already-entered frames."""
-
-import traceback
-
-import pytest
-
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.parser import entry, jit_support
-
-
-@pytest.mark.parametrize("fail", [False, True])
-def test_nested_helper_frames_capture_parameters_and_unwind(language, fail):
- seen = []
- failure = ValueError("inner body failure")
-
- def observe(label, outer, current, scope):
- frame = language.frame()
- assert language.stack[-1] is frame
- seen.append((label, frame, outer, current, scope))
- if fail and label == "inner":
- raise failure
-
- source = """
-@X.script
-def main(x: X.tensor((4,))):
- scope = 1
- observe("outer_before", x, x, scope)
- @X.script
- def inner(y: X.tensor((4,))):
- scope = 2
- observe("inner", x, y, scope)
- observe("outer_after", x, x, scope)
-"""
- if fail:
- with pytest.raises(ValueError) as caught:
- language.parse(source, observe=observe)
- assert caught.value is failure
- source_line = next(
- index for index, line in enumerate(source.splitlines(), 1) if 'observe("inner"' in line
- )
- assert any(
- frame.filename == "dummy.py" and frame.lineno == source_line
- for frame in traceback.extract_tb(caught.value.__traceback__)
- )
- else:
- result = language.parse(source, observe=observe)
- assert result is language.functions["main"]
-
- entries = [event for event in language.events if event[:2] == ("enter", "function")]
- exits = [event for event in language.events if event[:2] == ("exit", "function")]
- assert [event[2] for event in entries] == [False, True, False]
- outer, inner = entries[0][3], entries[1][3]
- assert entries[2][3] is inner
- assert [event[3] for event in exits] == [inner, inner, outer]
- assert len(outer.params) == len(inner.params) == 1
- assert seen[0] == ("outer_before", outer, outer.params[0], outer.params[0], 1)
- assert seen[1] == ("inner", inner, outer.params[0], inner.params[0], 2)
- if fail:
- assert len(seen) == 2
- else:
- assert seen[2] == ("outer_after", outer, outer.params[0], outer.params[0], 1)
- assert language.stack == [] and language.source_stack == []
- assert not IRBuilder.is_in_scope()
-
-
-def test_body_preserves_capture_and_parameter_identity(language):
- # Before: the body reads an enclosing token and its original runtime parameter.
- # Expected builder program: preserve both identities under their source names,
- # while preserving their original lexical lookup.
- token = object()
- X = language.X
-
- @X.script
- def main(x: X.tensor((4,))):
- X.record(token)
- X.record(x)
-
- assert main.body[0][1] is token
- assert main.body[1][1] is main.params[0]
-
-
-@pytest.mark.parametrize("parameter_count,bindings", [(0, None), (2, None), (2, {})])
-def test_parameter_setup_depends_on_explicit_specialization_request(
- language, monkeypatch, parameter_count, bindings
-):
- # Before: a recursive signature parsed normally or with explicit empty JIT bindings.
- # Expected builder program: ordinary X.arg/frame.params bindings need no JIT
- # state/read/selectors/iterator; an explicit {} still enables specialization.
- signature = ", ".join(f"{name}: X.tensor((4,))" for name in ("x", "y")[:parameter_count])
- arguments = ", ".join(("x", "y")[:parameter_count])
- requested = bindings is not None
- reads = []
- read_bindings = jit_support.read_specialization_bindings
-
- def observe_read(name):
- reads.append(name)
- return read_bindings(name)
-
- monkeypatch.setattr(jit_support, "read_specialization_bindings", observe_read)
- result = entry.parse(
- f"@X.script\ndef main({signature}):\n main({arguments})\n",
- extra_vars={"X": language.X},
- _specialization_bindings=bindings,
- root_builder=language.X,
- )
- assert reads == (["main"] if requested else [])
- assert len(result.params) == parameter_count
- call = result.body[0][1]
- assert call.args[0] is language.references["main"]
- assert all(actual is expected for actual, expected in zip(call.args[1:], result.params))
- assert len(call.args) == parameter_count + 1
diff --git a/tests/python/script/test_parser_scoped_symbols.py b/tests/python/script/test_parser_scoped_symbols.py
deleted file mode 100644
index b628311756..0000000000
--- a/tests/python/script/test_parser_scoped_symbols.py
+++ /dev/null
@@ -1,94 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Quoted symbols share identity without adding Python bindings."""
-
-import pytest
-
-from tvm.script.parser import entry
-
-
-def parse(language, source, *, extra_vars=None, **options):
- language.X.tuple = lambda *fields: fields
- return entry.parse(
- source,
- extra_vars={"X": language.X, **(extra_vars or {})},
- **options,
- root_builder=language.X,
- )
-
-
-@pytest.mark.parametrize("track_span", [True, False])
-def test_signature_symbols_cross_nested_calls_parameters_return_and_body(language, track_span):
- # Before: quoted "n" in nested annotations, then n = X.symbol() in the body.
- # Expected builder program: annotations use X.resolve_type_var_("n");
- # n = X.resolve_type_var_("n", "int64") introduces the same object in the body.
- function = parse(
- language,
- """
-@X.script
-def main(
- x: X.tuple(X.tensor(("n",), "float32"), X.tensor(("n",), "float32")),
- y: X.tensor(("n",), "float32"),
-) -> X.tensor(("n",), "float32"):
- n = X.symbol()
- X.record(n)
- return y
-""",
- track_span=track_span,
- )
- n = function.params[0].args[0][0].args[0][0]
- assert function.params[0].args[0][1].args[0][0] is n
- assert function.params[1].args[0].args[0][0] is n
- assert function.ret_type.args[0][0] is n
- assert function.body[0][1] is n
-
-
-@pytest.mark.parametrize(
- "annotation",
- [
- 'X.tensor((n, "n"), "float32")',
- 'X.tuple(X.tensor((n,), "float32"), X.tensor(("n",), "float32"))',
- ],
-)
-def test_signature_read_before_introduction_remains_unbound(language, annotation):
- source = f"""
-@X.script
-def main(x: {annotation}):
- return x
-"""
- with pytest.raises(NameError) as error:
- parse(language, source)
- assert isinstance(error.value, NameError)
- assert "n" in str(error.value)
-
-
-def test_signature_strings_do_not_replace_captured_python_names(language):
- function = parse(
- language,
- """
-@X.script
-def main(x: X.tensor((n, "n"), "float32"), y: X.tensor((n,), "float32")):
- X.record(n)
- return y
-""",
- extra_vars={"n": 7},
- )
- first, n = function.params[0].args[0].args[0]
- assert first == 7
- assert function.params[1].args[0].args[0][0] == 7
- assert function.body[0][1] == 7
- assert n.name == "n"
diff --git a/tests/python/script/test_parser_shared_resolvers.py b/tests/python/script/test_parser_shared_resolvers.py
deleted file mode 100644
index dc49c464d1..0000000000
--- a/tests/python/script/test_parser_shared_resolvers.py
+++ /dev/null
@@ -1,127 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Common IR constructors and argument policy metadata."""
-
-import pytest
-
-from tvm import ir
-from tvm.script import parser
-from tvm.script import tirx as T
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.ir_builder import ir as I
-
-
-def test_global_info_selectors_use_module_map():
- # Before: X.Tensor((4,), "float32", vdevice="cuda:1")
- # Expected builder program: I.resolve_global_info_("cuda:1") is mod.global_infos["vdevice"][2]
- with IRBuilder(), I.ir_module() as module:
- devices = [I.vdevice("llvm"), I.vdevice("cuda"), I.vdevice("cuda", 1)]
- I.module_global_infos({"vdevice": devices, "other": [I.dummy_global_info()]})
- for spelling, index in (("cuda:1", 2), ("vdevice[1]", 1), ("cuda", 1)):
- resolved = I.resolve_global_info_(spelling)
- assert resolved.__chandle__() == devices[index].__chandle__()
- assert resolved.__chandle__() == module.global_infos["vdevice"][index].__chandle__()
- assert I.resolve_global_info_("other[0]") is not None
-
-
-def test_module_string_constants_keep_common_constructor_values():
- actual = parser.parse("""
-@I.ir_module
-class Module:
- I.module_attrs({"tag": I.StringImm("label"), "type": I.StringType()})
-""")
- with IRBuilder() as builder, I.ir_module():
- I.module_attrs({"tag": ir.StringImm("label"), "type": ir.StringType()})
- ir.assert_structural_equal(builder.get(), actual)
- assert actual.attrs["tag"].value == "label"
- assert isinstance(actual.attrs["type"], ir.StringType)
-
-
-def test_constexpr_uses_registered_namespace_in_source():
- class Ordinary:
- constexpr = T.int32
-
- source = "@T.prim_func\ndef main(value: annotation):\n T.evaluate(value)\n"
- ordinary = parser.parse(
- source.replace("annotation", "Ordinary.constexpr"), extra_vars={"Ordinary": Ordinary}
- )
- assert len(ordinary.params) == 1 and ordinary.params[0].ty.dtype == "int32"
- with pytest.raises(TypeError, match="requires a specialization binding"):
- parser.parse(source.replace("annotation", "I.constexpr"))
-
-
-@pytest.mark.parametrize("dialect_name", ["tirx", "relax"])
-def test_native_function_reentry_retains_its_symbol_map_and_reference(dialect_name):
- # Source: independently declared functions use the same symbolic spelling.
- # Builder: each native frame owns its map across declaration/body entries.
- from tvm.relax.script import builder as R
- from tvm.tirx.script import builder as B
-
- dialect = B if dialect_name == "tirx" else R
- with IRBuilder() as builder, I.ir_module() as module:
- with pytest.raises(ValueError, match="function"):
- dialect.resolve_type_var_("n")
- with dialect.function_(decl=True) as frame:
- dialect.func_name("identity")
- n = dialect.resolve_type_var_("n")
- annotation = B.Buffer((n,), "float32") if dialect is B else R.Tensor((n,), "float32")
- parameter = dialect.arg("x", annotation)
- assert frame.type_var_map["n"].same_as(n)
- assert builder.frames[-1].same_as(frame)
- reference = frame.global_var
- assert module.identity.same_as(reference)
- with frame:
- assert dialect.resolve_type_var_("n").same_as(n)
- assert frame.params[0].same_as(parameter)
- # A separate nested build must resolve its own same-spelling symbol.
- with IRBuilder(), dialect.function_() as nested:
- inner_n = dialect.resolve_type_var_("n")
- assert not inner_n.same_as(n)
- assert nested.type_var_map["n"].same_as(inner_n)
- if dialect is B:
- B.evaluate(0)
- else:
- R.func_ret_value(R.const(0))
- assert dialect.resolve_type_var_("n").same_as(n)
- if dialect is B:
- B.evaluate(parameter[0])
- else:
- R.func_ret_value(parameter)
- assert frame.global_var.same_as(reference)
- assert frame.function.params[0].same_as(parameter)
- assert builder.get().get_global_var("identity").same_as(reference)
- assert builder.get()["identity"].params[0].same_as(parameter)
-
-
-@pytest.mark.parametrize("annotation", ["T.Buffer(shape, 'float32')", "R.Tensor(shape, 'float32')"])
-def test_captured_shape_requires_concrete_symbols(annotation):
- # Literal source strings are decoded; a captured tuple reaches its concrete
- # constructor unchanged and must already contain native symbols.
- decorator, body = (
- ("T.prim_func", "T.evaluate(0)")
- if annotation.startswith("T.")
- else ("R.function", "return x")
- )
- source = f"@{decorator}\ndef main(x: {annotation}):\n {body}\n"
- n = ir.Var("n", "int64")
- function = parser.parse(source, extra_vars={"shape": (n, 16)})
- assert function.params[0].ty.shape[0].same_as(n)
- with pytest.raises(
- TypeError, match="^Builder expression arguments require concrete symbols, not strings$"
- ) as caught:
- parser.parse(source, extra_vars={"shape": ("n", 16)})
- assert type(caught.value) is TypeError
diff --git a/tests/python/script/test_parser_span_helpers.py b/tests/python/script/test_parser_span_helpers.py
deleted file mode 100644
index 367a745877..0000000000
--- a/tests/python/script/test_parser_span_helpers.py
+++ /dev/null
@@ -1,216 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-"""Production source-span composition on common IR nodes."""
-
-import pytest
-
-from tvm import ir
-from tvm.ir import prim
-from tvm.script.ir_builder import IRBuilder
-from tvm.script.ir_builder import ir as I
-
-
-def loc(line, name="source.py", column=1, end_column=20):
- return (name, line, line, column, end_column)
-
-
-def locations(value):
- span = value.span
- spans = span.spans if isinstance(span, ir.SequentialSpan) else [span]
- return [(str(s.source_name.name), s.line, s.column, s.end_column) for s in spans]
-
-
-def test_at_preserves_native_expression_identity():
- with IRBuilder():
- value = prim.IntImm("int32", 2)
- assert I.at_(loc(4), value) is value
- assert locations(value) == [("source.py", 4, 1, 20)]
-
-
-def test_enclosing_locations_collapse_without_losing_definition():
- with IRBuilder():
- value = prim.IntImm("int32", 1)
- I.at_(loc(10, column=5, end_column=10), value)
- I.at_(loc(10), value)
- assert locations(value) == [("source.py", 10, 5, 10)]
- I.at_(loc(10, column=6, end_column=8), value)
- assert locations(value) == [("source.py", 10, 6, 8)]
-
-
-def test_composition_merges_shared_prefix_of_distinct_definition_chains():
- with IRBuilder():
- caller = ir.Span(ir.SourceName("caller.py"), 1, 1, 1, 20)
- original = ir.Span(ir.SourceName("definition.py"), 2, 2, 1, 20)
- value = prim.IntImm("int32", 1)
- I.at_(ir.SequentialSpan([caller, original]), value)
- with IRBuilder.current().with_source_span(caller):
- I.at_(loc(3, "definition.py"), value)
- assert locations(value) == [
- ("caller.py", 1, 1, 20),
- ("definition.py", 3, 1, 20),
- ("definition.py", 2, 1, 20),
- ]
-
-
-def test_span_entry_reuses_fixed_metadata_with_each_dynamic_caller():
- # Before: two callers invoke one inline helper at a fixed definition location.
- # Expected builder program: caller.ctx(lambda: definition(value)) composes
- # the current caller each time; no caller is frozen into the definition entry.
- from tvm.script.ir_builder import base
-
- definition_span = base.source_span(loc(8, "definition.py"))
- definition = base.SpanEntry(definition_span)
- assert definition.span is definition_span
- with IRBuilder():
- for line in (2, 5):
- caller = base.SpanEntry(base.source_span(loc(line, "caller.py")))
- value = prim.IntImm("int32", line)
- assert caller.ctx(lambda: definition(value)) is value
- assert locations(value) == [("caller.py", line, 1, 20), ("definition.py", 8, 1, 20)]
- marker = object()
- assert definition(marker) is marker
- assert definition.ctx(lambda: marker) is marker
- fresh = definition(prim.IntImm("int32", 9))
- assert locations(fresh) == [("definition.py", 8, 1, 20)]
- assert definition.span is definition_span
-
- # An assignment keeps construction context without restamping its result.
- from tvm.tirx.script import builder as T
-
- producer_span = base.source_span(loc(12, "producer.py"))
- kept = ir.Var("producer", "int32", producer_span)
- emitted = []
- with IRBuilder() as builder:
-
- def construct():
- emitted.append(T.evaluate(definition(prim.IntImm("int32", 4))).value)
- return kept
-
- assert caller.ctx(construct, attach_result=False) is kept
- assert kept.span.same_as(producer_span) and kept.name == "producer"
- assert len(emitted) == 1 and builder.get().same_as(emitted[0])
- assert locations(emitted[0]) == [("caller.py", 5, 1, 20)]
- assert locations(emitted[0].value) == [
- ("caller.py", 5, 1, 20),
- ("definition.py", 8, 1, 20),
- ]
-
-
-@pytest.mark.parametrize("existing_definition", [False, True])
-def test_explicit_emission_keeps_receipt_identity_and_normalized_span(existing_definition):
- # emit_ adds the definition to either a scalar caller or a chain with the
- # same prefix; the already-stored native statement is never emitted twice.
- from tvm.script.ir_builder import base
- from tvm.tirx.script import builder as T
-
- caller = base.SpanEntry(base.source_span(loc(3, "caller.py")))
- definition = base.SpanEntry(base.source_span(loc(7, "definition.py")))
- with IRBuilder() as builder:
-
- def construct():
- receipt = T.evaluate(6)
- assert isinstance(receipt, base.AlreadyEmitted)
- if existing_definition:
- assert definition(receipt) is receipt
- T.emit_(receipt, span=definition)
- return receipt
-
- receipt = caller.ctx(construct)
- assert builder.get().same_as(receipt.value)
- assert locations(receipt.value) == [("caller.py", 3, 1, 20), ("definition.py", 7, 1, 20)]
- # Conversion of a plain emitted literal must also annotate the native value.
- with IRBuilder() as builder:
- T.emit_(4, span=definition)
- assert locations(builder.get()) == [("definition.py", 7, 1, 20)]
- assert locations(builder.get().value) == [("definition.py", 7, 1, 20)]
-
-
-@pytest.mark.parametrize(
- "producer, dimensions, names",
- [
- ("serial", 1, None),
- ("parallel", 1, "i"),
- ("vectorized", 1, None),
- ("unroll", 1, "i"),
- ("grid", 1, ("i",)),
- ("grid", 2, ("i", "*tail")),
- ],
-)
-def test_native_loop_keeps_entered_variables_and_stored_span_at_exit(producer, dimensions, names):
- # Ordinary Python scalar entry and multi-loop entry preserve the native vars,
- # independent of names spelling. Stored spans win over unrelated exit context.
- from tvm import tirx
- from tvm.script.ir_builder import base
- from tvm.tirx.script import builder as T
-
- caller = base.SpanEntry(base.source_span(loc(3, "caller.py")))
- definition = base.SpanEntry(base.source_span(loc(7, "definition.py")))
- created = []
- with IRBuilder() as builder:
-
- def construct():
- frame = getattr(T, producer)(*(2, 3)[:dimensions])
- created.append((frame, tuple(frame.vars), [value.name for value in frame.vars]))
- return (
- definition(frame) if names is None else T.for_(frame, names=names, span=definition)
- )
-
- frame = caller.ctx(construct, attach_result=False)
- assert len(created) == 1
- variables = frame.vars
- expected_names = created[0][2] if names is None else ["i", "tail_0"][:dimensions]
- assert all(value.name for value in variables)
- assert [value.name for value in variables] == expected_names
- entered = frame.__enter__()
- assert entered.same_as(variables[0] if dimensions == 1 else variables)
- assert all(value.same_as(old) for value, old in zip(variables, created[0][1]))
- assert [value.name for value in variables] == expected_names
- value = variables[0] if dimensions == 1 else variables[0] + variables[1]
- receipt = T.evaluate(value)
- with builder.with_source_span(base.source_span(loc(90, "unrelated.py"))):
- frame.__exit__(None, None, None)
- node = builder.get()
- expected = [("caller.py", 3, 1, 20), ("definition.py", 7, 1, 20)]
- for variable in variables:
- assert isinstance(node, tirx.For)
- assert locations(node) == expected and node.loop_var.same_as(variable)
- node = node.body
- assert node.same_as(receipt.value)
-
-
-def test_span_entry_restores_context_after_original_exception():
- # Before: a nested opaque call raises while constructing its result.
- # Expected builder program: nested .ctx calls propagate that exact exception
- # and restore the caller; later operations see only their own source location.
- from tvm.script.ir_builder import base
-
- caller = base.SpanEntry(base.source_span(loc(2, "caller.py")))
- definition = base.SpanEntry(base.source_span(loc(8, "definition.py")))
- failure = ValueError("original failure")
- calls = []
-
- def fail():
- calls.append(True)
- raise failure
-
- with IRBuilder():
- with pytest.raises(ValueError) as caught:
- caller.ctx(lambda: definition.ctx(fail, attach_result=False))
- assert caught.value is failure
- assert calls == [True]
- fresh = definition(prim.IntImm("int32", 1))
- assert locations(fresh) == [("definition.py", 8, 1, 20)]
diff --git a/tests/python/script/test_source_locations.py b/tests/python/script/test_source_locations.py
new file mode 100644
index 0000000000..92d1c07de7
--- /dev/null
+++ b/tests/python/script/test_source_locations.py
@@ -0,0 +1,460 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Generated IR retains the user's call sites and definition locations.
+Fileless documentation execution also recovers the correct decorated class source.
+"""
+
+from __future__ import annotations
+
+# Script-local assignments are observed through the constructed IR.
+# ruff: noqa: F841
+import inspect
+import sys
+from types import ModuleType
+
+import pytest
+from minilang import Value
+
+from tvm import ir
+from tvm.ir import prim
+from tvm.script import ir as I
+from tvm.script import tirx as T
+
+
+def span_lines(value):
+ spans = value.span.spans if isinstance(value.span, ir.SequentialSpan) else [value.span]
+ return [span.line for span in spans]
+
+
+@pytest.fixture
+def spanned(spanned_language):
+ spanned_language.M.node = lambda value: prim.IntImm("int32", value)
+ return spanned_language
+
+
+def source_line(test, fragment):
+ lines, start = inspect.getsourcelines(test)
+ return start + next(i for i, line in enumerate(lines) if line.strip() == fragment)
+
+
+def test_module_source_calls_have_context_before_annotations(spanned):
+ # Module construction calls must receive the class-body source context, including calls
+ # before member annotations.
+ M = spanned.M
+ seen = []
+
+ def record():
+ node = prim.IntImm("int32", 3)
+ seen.append(node)
+ return node
+
+ @I.ir_module(extra_vars={"record": record})
+ class Module:
+ record()
+
+ @M.function
+ def main():
+ M.record(1)
+
+ assert "main" in Module
+ # The ordinary Python class body runs before the decorator; only the parsed
+ # class call has a source range attached to its returned expression.
+ assert len(seen) == 2
+ assert seen[0].span is None
+ assert seen[1].span is not None
+ assert seen[1].span.line == source_line(
+ test_module_source_calls_have_context_before_annotations, "record()"
+ )
+
+
+def test_source_inline_keeps_caller_and_each_definition_location(spanned):
+ # A macro emitting twice must retain both original definition lines under the caller,
+ # without reusing the first emission span.
+ M = spanned.M
+
+ @M.inline
+ def inner(x):
+ M.record(M.node(x))
+ M.record(M.node(x + 1))
+
+ @M.function
+ def main():
+ inner(1)
+
+ statements = [value for _, value in main.body if isinstance(value, prim.IntImm)]
+ assert len(statements) == 2
+ assert [int(value) for value in statements] == [1, 2]
+ test = test_source_inline_keeps_caller_and_each_definition_location
+ caller = source_line(test, "inner(1)")
+ assert span_lines(statements[0]) == [caller, source_line(test, "M.record(M.node(x))")]
+ assert span_lines(statements[1]) == [caller, source_line(test, "M.record(M.node(x + 1))")]
+ assert all(
+ str(span.source_name.name) == __file__ for value in statements for span in value.span.spans
+ )
+
+
+@pytest.fixture
+def calls(spanned_language):
+ M = spanned_language.M
+ M.evaluate = lambda value: value
+ M.call_extern = lambda dtype, name: ir.Call(ir.GlobalVar(name), [], ret_ty=dtype)
+ return M
+
+
+def _position(test, statement, expression):
+ lines, start = inspect.getsourcelines(test)
+ index, line = next((i, line) for i, line in enumerate(lines) if line.strip() == statement)
+ column = line.index(expression) + 1
+ return start + index, column, start + index, column + len(expression)
+
+
+def _span_position(span):
+ return span.line, span.column, span.end_line, span.end_column
+
+
+def test_source_and_ir_call_spans_use_one_based_columns(calls):
+ # Generated expression calls must retain the original one-based start/end columns on the
+ # emitted IR.
+ M = calls
+
+ @M.function
+ def direct():
+ M.evaluate(M.call_extern("int32", "direct"))
+
+ expected = _position(
+ test_source_and_ir_call_spans_use_one_based_columns,
+ 'M.evaluate(M.call_extern("int32", "direct"))',
+ 'M.call_extern("int32", "direct")',
+ )
+ span = direct.body[0][1].span
+ assert _span_position(span) == expected
+ assert span.source_name.name == __file__
+
+
+def test_nested_helper_spans_preserve_caller_and_definition_columns(calls):
+ # Nested macros must compose all caller and definition ranges instead of replacing or
+ # duplicating them.
+ M = calls
+
+ @M.inline
+ def inner():
+ M.evaluate(M.call_extern("int32", "nested"))
+
+ @M.inline
+ def outer():
+ inner()
+
+ @M.function
+ def main():
+ outer()
+ outer()
+
+ test = test_nested_helper_spans_preserve_caller_and_definition_columns
+ expected = [
+ _position(test, "outer()", "outer()"),
+ _position(test, "inner()", "inner()"),
+ _position(
+ test,
+ 'M.evaluate(M.call_extern("int32", "nested"))',
+ 'M.call_extern("int32", "nested")',
+ ),
+ ]
+ calls = [value for _, value in main.body if isinstance(value, ir.Call)]
+ assert len(calls) == 2
+ for index, value in enumerate(calls):
+ span = value.span
+ assert isinstance(span, ir.SequentialSpan)
+ caller = expected[0]
+ actual_expected = [
+ (caller[0] + index, caller[1], caller[2] + index, caller[3]),
+ *expected[1:],
+ ]
+ assert [_span_position(item) for item in span.spans] == actual_expected
+ assert all(item.source_name.name == __file__ for item in span.spans)
+
+
+@pytest.fixture
+def gallery(monkeypatch, spanned_language):
+ monkeypatch.setitem(sys.modules, "__main__", ModuleType("__main__"))
+ monkeypatch.setitem(globals(), "__name__", "__main__")
+ M = spanned_language.M
+ M.store = lambda value: ir.Call(
+ ir.GlobalVar("store"), [ir.prim.IntImm("int32", value)], ret_ty="int32"
+ )
+ return M
+
+
+def test_gallery_classes_retain_distinct_source_locations(gallery):
+ # Repeated class names in a fileless gallery must recover their own source, not the first match.
+ M = gallery
+ width = 4
+
+ @I.ir_module
+ class Repeated:
+ @M.function
+ def main(A: M.Tensor((width,), "int32")):
+ M.store(11)
+
+ first = Repeated
+
+ @I.ir_module
+ class Repeated:
+ @M.function
+ def main(A: M.Tensor((width,), "int32")):
+ M.store(22)
+
+ second = Repeated
+ source, start = inspect.getsourcelines(test_gallery_classes_retain_distinct_source_locations)
+ for result, value in ((first, 11), (second, 22)):
+ function = result["main"]
+ assert function.params[0].args[0].args[0] == (4,)
+ store = function.body[0][1]
+ line = next(i for i, text in enumerate(source) if text.strip() == f"M.store({value})")
+ assert store.args[0].value == value
+ assert store.span.line == start + line
+ assert store.span.source_name.name == __file__
+
+
+def _line_of(function, statement):
+ lines, first = inspect.getsourcelines(function)
+ return first + next(index for index, line in enumerate(lines) if line.strip() == statement)
+
+
+def test_callee_arguments_and_keywords_evaluate_once_with_caller_context(language):
+ # Nested callees and keyword arguments must run once in written order and retain the call
+ # location.
+ M = language.M
+ seen = []
+ result_value = Value("returned")
+
+ def callee():
+ seen.append("callee")
+
+ def function(a, *, b):
+ seen.append((a, b))
+ return result_value
+
+ return function
+
+ def operand(value):
+ seen.append(value)
+ return value
+
+ @M.function
+ def main():
+ callee()(operand(1), b=operand(2))
+
+ assert seen == ["callee", 1, 2, (1, 2)]
+ assert main.body[0][1] is result_value
+ assert result_value.span[-1][1] == _line_of(
+ test_callee_arguments_and_keywords_evaluate_once_with_caller_context,
+ "callee()(operand(1), b=operand(2))",
+ )
+
+
+def test_native_bind_keeps_returned_and_stored_variable_identity():
+ # Aliases of an explicitly produced native variable must not rename, respan or bind it again.
+ from tvm import ir, tirx
+
+ span = ir.Span(ir.SourceName("producer.py"), 7, 7, 2, 19)
+ produced = ir.Var("producer_name", "int32", span)
+ calls, observed = [], []
+
+ def argument(value):
+ calls.append(value)
+ return value
+
+ def observe(value):
+ observed.append(value)
+ assert value.same_as(produced) and value.span.same_as(span)
+ assert value.name == "producer_name"
+
+ @T.prim_func
+ def main(x: T.int32):
+ renamed = T.bind(argument(x), var=produced)
+ alias = renamed
+ called = argument(alias)
+ observe(renamed)
+ observe(alias)
+ observe(called)
+ T.evaluate(called)
+
+ binding, use = main.body.seq
+ assert isinstance(binding, tirx.Bind) and isinstance(use, tirx.Evaluate)
+ assert binding.var.same_as(produced) and use.value.same_as(produced)
+ assert len(observed) == 3 and len(calls) == 2
+ assert calls[0].same_as(main.params[0]) and calls[1].same_as(produced)
+
+
+def test_native_view_keeps_producer_identity_name_and_span(monkeypatch):
+ # A native view must preserve its producer name/span and declare its storage exactly once.
+ from functools import wraps
+
+ from tvm import ir, tirx
+ from tvm.script.ir_builder import base
+
+ captured = T.Buffer((4, 4), "float32")
+ original = type(captured).view
+ seen, produced, observed = [], [], []
+ span = base.source_span(("producer.py", 7, 7, 2, 19))
+
+ @wraps(original)
+ def view(buffer, *args):
+ seen.append("view")
+ value = base.at_(span, original(buffer, *args))
+ produced.append((value, value.name, value.span))
+ return value
+
+ def mark():
+ seen.append("argument")
+ return 16
+
+ def observe(value):
+ observed.append((value, value.span))
+
+ monkeypatch.setattr(type(captured), "view", view)
+
+ @T.prim_func
+ def main(A: captured):
+ renamed = captured.view(mark())
+ alias = renamed
+ observe(renamed)
+ observe(alias)
+ alias[0] = 0
+ captured.view(mark())
+
+ assert seen == ["argument", "view", "argument", "view"]
+ value, name, produced_span = produced[0]
+ assert len(produced) == len(observed) == 2
+ assert name != "renamed" and value.name == name
+ assert all(
+ item.same_as(value) and location.same_as(produced_span) for item, location in observed
+ )
+ nodes = list(main.body.seq)
+ assert len(nodes) == 3 and not any(isinstance(node, tirx.Bind) for node in nodes)
+ assert nodes[0].buffer.same_as(value) and nodes[1].buffer.same_as(value)
+ assert nodes[2].buffer.same_as(produced[1][0])
+ ir.assert_structural_equal(nodes[0].data, captured.data)
+ ir.assert_structural_equal(nodes[2].data, captured.data)
+
+
+def test_native_binding_preserves_metadata_but_binds_buffer_expressions():
+ # Inert metadata must retain resource identity; a non-Var buffer expression still needs a
+ # located binding.
+ from tvm import ir, tirx
+ from tvm.script.ir_builder import base
+
+ producer_span = ir.Span(ir.SourceName("producer.py"), 7, 7, 2, 19)
+ buffer = T.Buffer((4,), "float32")
+ base.at_(producer_span, buffer)
+ layout = T.TileLayout(T.S[4])
+
+ @T.meta_class
+ class Holder:
+ def __init__(self, resource):
+ self.resource = resource
+
+ holder = Holder(buffer)
+ projection = ir.TupleGetItem(ir.Tuple([buffer]), 0)
+ values = [layout, holder, projection]
+ calls, observed, resource_spans = [], [], []
+
+ def make(index):
+ resource_spans.append(buffer.span)
+ calls.append(index)
+ return values[index]
+
+ def observe(*items):
+ observed.extend(items)
+
+ @T.prim_func
+ def main(A: buffer):
+ renamed_layout = make(0)
+ renamed_holder = make(1)
+ bound = make(2)
+ observe(renamed_layout, renamed_holder)
+ make(0)
+ make(1)
+
+ assert calls == [0, 1, 2, 0, 1]
+ assert observed[0].same_as(layout)
+ assert observed[1] is holder and holder.resource.same_as(buffer)
+ assert all(buffer.span.same_as(span) for span in resource_spans)
+ binding = main.body
+ assert isinstance(binding, tirx.Bind) and binding.value.same_as(projection)
+ assert binding.var.name == "bound"
+ assert tirx.is_buffer_var(binding.var)
+ assert not isinstance(projection, ir.Var)
+ line = _line_of(
+ test_native_binding_preserves_metadata_but_binds_buffer_expressions, "bound = make(2)"
+ )
+ assert (binding.var.span.line, binding.var.span.column, binding.var.span.end_column) == (
+ line,
+ 9,
+ 14,
+ )
+ assert (projection.span.line, projection.span.column, projection.span.end_column) == (
+ line,
+ 17,
+ 24,
+ )
+ with pytest.raises(TypeError):
+
+ @T.prim_func
+ def invalid():
+ object()
+
+
+def test_non_call_expression_reads_keep_their_source_range():
+ # Bare name, property, item and literal emissions must retain their written
+ # ranges in actual IR, with each host read evaluated once.
+ value = prim.IntImm("int32", 1)
+ attribute = prim.IntImm("int32", 2)
+ item = prim.IntImm("int32", 3)
+ reads = []
+
+ class Holder:
+ @property
+ def value(self):
+ reads.append("attribute")
+ return attribute
+
+ def __getitem__(self, index):
+ reads.append(index)
+ return item
+
+ holder = Holder()
+
+ @T.prim_func
+ def main():
+ value
+ holder.value
+ holder[0]
+ 7
+
+ nodes = list(main.body.seq)
+ assert [int(node.value) for node in nodes] == [1, 2, 3, 7]
+ assert nodes[0].value.same_as(value)
+ assert nodes[1].value.same_as(attribute)
+ assert nodes[2].value.same_as(item)
+ assert reads == ["attribute", 0]
+ for node, expression in zip(nodes, ("value", "holder.value", "holder[0]", "7")):
+ expected = _position(
+ test_non_call_expression_reads_keep_their_source_range, expression, expression
+ )
+ assert _span_position(node.span) == expected
+ assert node.span.source_name.name == __file__
diff --git a/tests/python/script/test_builder_protocol_contract.py b/tests/python/script/test_special_parser_protocol.py
similarity index 52%
rename from tests/python/script/test_builder_protocol_contract.py
rename to tests/python/script/test_special_parser_protocol.py
index 480ed495a4..abb0d8ee1f 100644
--- a/tests/python/script/test_builder_protocol_contract.py
+++ b/tests/python/script/test_special_parser_protocol.py
@@ -14,36 +14,66 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-"""Parsed argument-policy behavior across successful and failed construction."""
+"""Registered constructor arguments resolve module names during construction.
+A failed constructor must not change the next function's interpretation of literals.
+"""
import pytest
from tvm.script.parser import protocol_registry as registry
-def test_registration_persists_across_successful_and_failed_parse_contexts(language):
+def test_constructor_policy_survives_a_failed_definition(language):
+ # A failing definition must not erase registered argument semantics for the next one.
+ M = language.M
calls = []
first, invalid, second = object(), object(), object()
failure = RuntimeError("constructor failed")
- @registry.args_policy("X.constructor", {"device": "global_info"})
+ @registry.args_policy("M.constructor", {"device": "global_info"})
def constructor(device):
calls.append(device)
if device is invalid:
raise failure
return device
- language.X.constructor = constructor
- source = (
- '@X.script\ndef main():\n X.record(X.constructor(device="mesh"))\n'
- ' X.record(alias(device="mesh"))\n'
- )
+ M.constructor = constructor
+
+ def build():
+ @M.function
+ def main():
+ M.record(M.constructor(device="mesh"))
+ M.record(constructor(device="mesh"))
+
+ return main
+
language.global_infos["mesh"] = first
- assert language.parse(source, alias=constructor).body == [("emit", first), ("emit", "mesh")]
+ assert build().body == [("emit", first), ("emit", "mesh")]
language.global_infos["mesh"] = invalid
with pytest.raises(RuntimeError, match="constructor failed") as caught:
- language.parse(source, alias=constructor)
+ build()
assert caught.value is failure
language.global_infos["mesh"] = second
- assert language.parse(source, alias=constructor).body == [("emit", second), ("emit", "mesh")]
+ assert build().body == [("emit", second), ("emit", "mesh")]
assert calls == [first, "mesh", invalid, second, "mesh"]
+
+
+def test_argument_policy_preserves_expression_dtype(language):
+ # Shape-expression parsing must retain the declared int32 symbol dtype.
+ M = language.M
+
+ @registry.args_policy("M.shape", {"values": "expr_str"}, dtype="int32")
+ def shape(values):
+ return values
+
+ M.shape = shape
+
+ @M.function
+ def main():
+ M.shape(("n", "n + 1"))
+
+ n, increment = main.body[0][1]
+ assert n.args == ("int32",)
+ assert increment.op == "add"
+ assert increment.args[0] is n
+ assert increment.args[1] == 1
diff --git a/tests/python/script/test_symbolic_shape.py b/tests/python/script/test_symbolic_shape.py
new file mode 100644
index 0000000000..fe281434b5
--- /dev/null
+++ b/tests/python/script/test_symbolic_shape.py
@@ -0,0 +1,191 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Symbolic dimensions in function signatures share identity with body declarations.
+Quoted dimensions must not create or replace ordinary Python bindings.
+"""
+
+from __future__ import annotations
+
+# Invalid script examples deliberately contain unresolved or unused bindings.
+# ruff: noqa: F821, F841
+import inspect
+
+import pytest
+
+from tvm import ir
+from tvm.script import tirx as T
+
+
+def test_signature_symbols_cross_nested_calls_parameters_return_and_body(language):
+ # Repeated quoted dimensions in nested annotations and body declarations must resolve to
+ # the same symbol.
+ M = language.M
+ M.tuple = lambda *fields: fields
+
+ @M.function
+ def main(
+ x: M.tuple(M.Tensor(("n",), "float32"), M.Tensor(("n",), "float32")),
+ y: M.Tensor(("n",), "float32"),
+ ) -> M.Tensor(("n",), "float32"):
+ n = M.symbol()
+ M.record(n)
+ return y
+
+ n = main.params[0].args[0][0].args[0][0]
+ assert main.params[0].args[0][1].args[0][0] is n
+ assert main.params[1].args[0].args[0][0] is n
+ assert main.ret_type.args[0][0] is n
+ assert main.body[0][1] is n
+
+
+def test_signature_read_before_introduction_remains_unbound(language):
+ # A quoted dimension must not introduce an ordinary Python name before its declaration.
+ M = language.M
+ with pytest.raises(NameError, match="n"):
+
+ @M.function
+ def main(x: M.Tensor((n, "n"), "float32")):
+ return x
+
+
+def test_signature_strings_do_not_replace_captured_python_names(language):
+ # A captured Python dimension and a same-spelling quoted symbol must remain distinct.
+ M = language.M
+ n = 7
+
+ @M.function
+ def main(x: M.Tensor((n, "n"), "float32"), y: M.Tensor((n,), "float32")):
+ M.record(n)
+ return y
+
+ first, symbol = main.params[0].args[0].args[0]
+ assert first == 7
+ assert main.params[1].args[0].args[0][0] == 7
+ assert main.body[0][1] == 7
+ assert symbol.name == "n"
+
+
+def test_captured_shape_requires_concrete_symbols():
+ # Captured tuples bypass literal decoding; native shape construction must
+ # preserve concrete symbols and reject captured strings instead of inventing vars.
+ def build(shape):
+ @T.prim_func
+ def main(x: T.Buffer(shape, "float32")):
+ T.evaluate(0)
+
+ return main
+
+ n = ir.Var("n", "int64")
+ function = build((n, 16))
+ assert function.params[0].ty.shape[0].same_as(n)
+ with pytest.raises(
+ TypeError, match="^Builder expression arguments require concrete symbols, not strings$"
+ ) as caught:
+ build(("n", 16))
+ assert type(caught.value) is TypeError
+
+
+def _line_of(function, statement):
+ lines, first = inspect.getsourcelines(function)
+ return first + next(index for index, line in enumerate(lines) if line.strip() == statement)
+
+
+def test_argument_policies_reuse_symbols_and_resolve_only_marked_literals(language):
+ # Quoted shapes must reuse one symbol while only marked device strings resolve.
+ M = language.M
+ device = object()
+ language.global_infos["cuda:1"] = device
+
+ @M.function
+ def main(x: M.Tensor(shape=("n + 1", "n"), dtype="float32", device="cuda:1")):
+ M.record(x)
+
+ annotation = main.params[0].args[0]
+ shape, dtype, resolved, placement = annotation.args
+ assert shape[0].op == "add" and shape[0].args[0] is shape[1]
+ assert shape[0].args[1] == 1
+ assert (dtype, placement) == ("float32", "S[0]")
+ assert resolved is device
+ assert main.body == [("emit", main.params[0])]
+
+
+def test_quoted_symbols_do_not_introduce_python_bindings(language):
+ # Resolving a quoted dimension must not silently define its unquoted body name.
+ M = language.M
+ with pytest.raises(NameError, match="n"):
+
+ @M.function
+ def main(x: M.Tensor(("n", "n"))):
+ M.record(n)
+
+
+def test_symbol_reassignment_reports_introduction_and_exact_write(language):
+ # Ordinary writes to a symbolic dimension must point to its original introduction and
+ # exact target.
+ M = language.M
+ with pytest.raises(SyntaxError) as caught:
+
+ @M.function
+ def main():
+ n = M.symbol()
+ n = 2
+
+ message = str(caught.value)
+ introduction = _line_of(
+ test_symbol_reassignment_reports_introduction_and_exact_write, "n = M.symbol()"
+ )
+ offending = _line_of(test_symbol_reassignment_reports_introduction_and_exact_write, "n = 2")
+ assert "Symbolic variable 'n' cannot be reassigned" in message
+ assert f"introduced at line {introduction}" in message
+ error = caught.value
+ assert (error.filename, error.lineno, error.end_lineno) == (__file__, offending, offending)
+ assert (error.offset, error.end_offset) == (13, 14)
+
+
+def test_repeated_symbol_declarations_reuse_identity(language):
+ # Repeated explicit declarations must reuse the annotation-owned symbol.
+ M = language.M
+
+ @M.function
+ def main(x: M.Tensor(("n",))):
+ n = M.symbol()
+ M.record(n)
+ n = M.symbol()
+ M.record(n)
+
+ symbol = main.params[0].args[0].args[0][0]
+ assert main.body == [("emit", symbol), ("emit", symbol)]
+ assert main.body[0][1] is main.body[1][1]
+
+
+def test_nested_scope_does_not_reassign_outer_symbol(language):
+ # An ordinary nested local must not overwrite an enclosing symbolic dimension.
+ M = language.M
+
+ @M.function
+ def main():
+ n = M.symbol()
+
+ @M.function
+ def nested():
+ n = 2
+ M.record(n)
+
+ M.record(n)
+
+ assert main.body[0][1].op == "symbol"
+ assert language.functions["nested"].body == [("emit", 2)]