Skip to content

[SCRIPT] Support PEP 695 symbolic variables in Relax and TIR - #20107

Open
tqchen wants to merge 3 commits into
apache:mainfrom
tqchen:tvm-type-var-parser-syntax
Open

[SCRIPT] Support PEP 695 symbolic variables in Relax and TIR#20107
tqchen wants to merge 3 commits into
apache:mainfrom
tqchen:tvm-type-var-parser-syntax

Conversation

@tqchen

@tqchen tqchen commented Aug 8, 2026

Copy link
Copy Markdown
Member

Relax and TIR dependent type fields need symbolic integer variables to be declared in a function signature without persisting parser-only metadata in IR.

  • Accept standard module-level TypeVar declarations on Python 3.10+ and bare or int-bound PEP 695 parameters on Python 3.12+.
  • Treat declarations as syntax sugar for fresh int64 TIR variables used by dependent signature fields; unused declarations disappear and no function attrs, type-parameter fields, or runtime parameters are added.
  • Parse both versioned forms through shared signature plumbing while retaining dialect-specific annotation and body handling.

Representative Python 3.12+ output uses PEP 695 and postponed annotations, so dependent expressions are unquoted:

from __future__ import annotations

@R.function(private=True)
def main[M](x: R.Tensor((M, M * 2), dtype="float32")):
    return x

Before Python 3.12, the portable output uses a module-level TypeVar and quotes compound dependent expressions:

M = TypeVar("M")

@R.function(private=True)
def main(x: R.Tensor((M, "M * 2"), dtype="float32")):
    return x

The same declaration and printing rules apply to TIR buffer signatures.

Validation:

  • Latest-main native build completed successfully.
  • Consolidated Relax and TIR TypeVar round-trip tests passed.
  • Related Relax/TIR parser, printer, and well-formedness suites passed 661 tests with 4 expected failures.
  • Touched-file pre-commit hooks and diff checks passed.
  • The implementation does not change Relax well-formedness analysis.

@tqchen
tqchen force-pushed the tvm-type-var-parser-syntax branch from 5719007 to af9465b Compare August 8, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant