tvm.script.parser#

tvm.script.parser#

Canonical TVMScript parser with language variant namespace initialization.

tvm.script.parser.register_namespace(alias: str, namespace: object) → None#

Register an opaque fixed namespace; its first alias is the syntax key root.

Parameters:
  • alias (str) – Source name for the namespace, such as "ir" or "minilang". The first registered alias for an object is its canonical syntax-policy prefix; additional aliases for that object resolve to the same prefix.

  • namespace (object) – Namespace object exposing construction operations or source decorators. It is borrowed without copying or inspecting its members.

Returns:

Registration updates the namespaces available to subsequent parses.

Return type:

None

Notes

Each parse borrows these namespaces. Replacing an alias affects future parses only; registration enters no builder frame and inspects no namespace members.

tvm.script.parser.register_namespace_initializer(initializer: Callable[[], None], *, aliases: tuple[str, ...] = ()) → None#

Register a lazy bootstrap callback without importing the language variant.

Parameters:
  • initializer (Callable[[], None]) – Callback that registers the language variant’s namespaces. It receives no arguments and is called during the parser’s first initialization. If initialization has already completed, a newly registered callback runs immediately. Re-registering the same callback object does not enqueue or invoke it again.

  • aliases (tuple[str, ...], optional) – Names whose attribute lookup should trigger initialization. Defaults to an empty tuple. These names advertise lazy namespaces; the callback must still register their actual objects with register_namespace(). Aliases are added even when the callback was previously registered.

Returns:

The callback is registered, and invoked immediately when the parser is already initialized.

Return type:

None

tvm.script.parser.jit_support#

These helpers carry const_args, the selected fixed argument map, into generated builder execution. Parameter validation and caching remain with the JIT entry point.

Scoped transport of already selected JIT inputs into builder execution.

JIT owns validation, defaults and caching. These contexts carry only the root function’s selected values (including explicit None absences), restoring the prior values after nested parsing or an exception. Native builder frames own no JIT state, and ordinary parsing does not import the TIRx JIT entry point.

tvm.script.parser.jit_support.use_specialization(name: str | None, const_args: Mapping[str, Any] | None) → Iterator[None]#

Pass fixed JIT arguments to one root builder execution.

Parameters:
  • name (str or None) – Source name of the standalone root function. Readers must request this exact name. None is used when the parsed root is not a function.

  • const_args (Mapping[str, Any] or None) – Parameter names mapped to fixed values, including explicit None values for omitted optional parameters. None selects ordinary parsing; an empty mapping still selects specialization with no compile-time values. The mapping is copied on entry, without copying its values.

Yields:

None – The enclosed builder execution sees this selection. The previous selection is restored on exit, including nested parsing and exceptions.

tvm.script.parser.jit_support.read_specialization_bindings(name: str) → dict[str, Any] | None#

Read the current root’s fixed JIT arguments.

Parameters:

name (str) – Source function name to match against the active root selection.

Returns:

Parameter names mapped to fixed values when the root name matches. An empty dictionary means an active specialization with no selected values. None means ordinary parsing or a different root name. The returned dictionary is borrowed from the current context and should not be mutated.

Return type:

dict[str, Any] or None

tvm.script.parser.protocol_registry#

Language variants register source syntax policies independently of their construction hooks. Namespace objects and lazy initialization callbacks are registered through tvm.script.parser.register_namespace() and tvm.script.parser.register_namespace_initializer().

Static syntax policies keyed by registered builder namespace paths.

Each language variant registers its namespace through register_namespace and supplies explicit canonical keys such as tirx.int32 or relax.Tensor at its marker sites. The first registered alias names that namespace. Source aliases normalize to this same root; only direct root members select special syntax. Parser consumers read the internal metadata tables directly. Ordinary captured callables, local aliases, instance methods and descriptors do not acquire syntax policies through identity or receiver inference.

Tables retain static syntax facts only, never source functions, captures, frames or constructed results. Registration decorators return the same callable. Shared builder operations are documented in tvm.script.ir_builder.parser_protocol; concrete language variants own registration and namespace initialization.

tvm.script.parser.protocol_registry.register_mutable_decl(namespace_path: str, *, syntax: str = 'call') → Callable[[_Callable], _Callable]#

Register persistent mutable storage syntax at a fixed namespace path.

Parameters:
  • namespace_path (str) – Canonical registered namespace alias and exported constructor path.

  • syntax (str, optional) – Declaration form: "call" (the default) for a constructor call, "annotation" for an annotated local declaration, or "parameter" for a function parameter annotation. Repeated registration adds forms for the same path without removing existing ones.

Returns:

Registration decorator that returns its constructor unchanged.

Return type:

Callable

Raises:

ValueError – If syntax is not one of the three supported forms.

Notes

Explicit declarations take precedence over mutable target updates; storage creation and updates stay outside ordinary bind_.

register_mutable_decl("tirx.int32", syntax="annotation")(T.int32)
# Source: x: T.int32 = 0
# Builder: x = X.decl_mutable_cell_(0, ty=X.int32, name="x")
tvm.script.parser.protocol_registry.register_scalar_annotation(namespace_path: str, constructor: _Callable, *, dtype: object | None = None) → _Callable#

Register the dtype of a fixed scalar annotation without evaluating it.

Parameters:
  • namespace_path (str) – Canonical registered namespace alias and exported callable path, such as "tirx.int32". A later registration at this path replaces its dtype.

  • constructor (Callable) – Callable providing the eager construction operation. Registration records its syntax path without invoking or wrapping this callable.

  • dtype (object, optional) – Static scalar dtype for an explicit PEP 695 symbol bound. None (the default) leaves the annotation without a supported scalar bound dtype.

Returns:

The exact constructor object.

Return type:

Callable

Notes

This metadata does not give constructor calls special assignment semantics. Scalar runtime parameters retain their ordinary annotation construction.

register_scalar_annotation("tirx.int32", T.int32, dtype="int32")
# Source: def f[n: T.int32](...):
# Builder: n = I.dynamic("n", "int32")

The language variant aliases below share the public construction namespaces documented in tvm.script. Parser entry points above use the canonical frontend.

tvm.script.parser.ir#

Module construction and shared parser support.

tvm.script.parser.relax#

Public canonical TVMScript dialect namespace.

tvm.script.parser.tirx#

Public canonical TVMScript dialect namespace.