Skip to content

[IR][Relax][TIRx] Unify Var identity#20004

Merged
tqchen merged 41 commits into
apache:mainfrom
tqchen:tvm-consolidate-var-unification-cleanup
Jul 15, 2026
Merged

[IR][Relax][TIRx] Unify Var identity#20004
tqchen merged 41 commits into
apache:mainfrom
tqchen:tvm-consolidate-var-unification-cleanup

Conversation

@tqchen

@tqchen tqchen commented Jul 14, 2026

Copy link
Copy Markdown
Member

Motivation

IR, Relax, and TIRx previously carried overlapping variable representations
and repeated runtime checks for ordinary, primitive, and dataflow values. That
made identity-sensitive maps, substitutions, and visitors fragile: logically
identical variables could cross subsystem boundaries through different views,
while downstream code repeatedly reconstructed the same type invariant.

This change gives each variable one canonical identity and expresses its role
through checked typed views and explicit semantic boundaries.

Design

  • IR, Relax, and TIRx share one canonical Var identity. Exact ordinary
    variables with PrimType admit the zero-state checked PrimVar view;
    DataflowVar remains a runtime dataflow subtype even when primitive-typed.
  • Primitive-only containers carry PrimVar directly. Mixed-domain maps retain
    Var only where Relax values are legitimate inputs.
  • Generic node dispatch is exact-first with normal nearest-ancestor fallback.
    Relax visitors explicitly define their primitive-expression boundary, and
    dependent type fields use a narrow callback that does not recurse through
    unrelated runtime structure.
  • Python exposes the canonical public .name API while the internal C++
    storage field remains name_hint.
  • Relax TVMScript distinguishes symbolic declarations from runtime bindings.
    Direct zero-argument T.dtype() assignments declare symbols;
    I.meta_var(...) is the explicit shared parser-time escape. Other primitive
    expressions produce normal Relax bindings, and printing remains marker-free
    with explicit PrimType annotations.
  • Runtime compound primitive expressions are normalized into host-side
    computations before VM shape lowering. Shape lowering derives demand from
    live uses and dependent shape/type edges, uses canonical Var leaves and
    UndefinedVars for dependencies, and keeps definition roots out of demand.
    One slot map controls heap loads and runtime-result capture without an
    operator whitelist or whole-compound-root scalar classification.

Compatibility

  • Python callers use .name for public variable names; name_hint remains an
    internal C++ storage detail.
  • Legacy Relax variable aliases and the Python dtype= constructor spelling
    are removed in favor of canonical Var plus PrimType APIs.
  • R.prim_value(...) remains accepted input syntax but is not emitted by the
    printer. T.meta_var remains an alias of the shared I.meta_var
    implementation.

Validation

  • Core source and shared libraries build successfully; the CUTLASS translation unit also passes the inherited-overload warning gate.
  • Shape lowering and primitive computation: 32/32 passed.
  • Parser, primitive type, TIRx constructor, and Relax expression visitor:
    25/25 passed.
  • End-to-end compound primitive runtime cases: 6/6 passed.
  • Changed Python coverage: 845 passed, 134 skipped, 7 xfailed.
  • C++ CTest: 122/122 passed.
  • Complete Python coverage: 8,873 passed, 6,863 skipped, 56 xfailed, 5 xpassed.
    All 147 residual failures reproduced unchanged in an independent baseline
    checkout, with no change-only failure.
  • Repository formatting/lint hooks and the two upstream DLight broadcast-epilogue
    regressions pass.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request unifies the representation of variables across TVM by replacing dialect-specific variable classes with a canonical tvm.ir.Var and introducing tirx.PrimVar for primitive variables. This refactoring spans C++ and Python APIs, parsers, printers, and transformation passes. A critical issue was found in the TVMScript parser where tvm.ir.Type instances are compared using == (pointer equality) instead of structural equality, which would cause type verification for declared symbolic variables to always fail.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/tvm/relax/script/parser/parser.py
@tqchen tqchen changed the title [IR][Relax][TIRx] Consolidate canonical Var unification cleanup [IR][Relax][TIRx] Unify Var identity Jul 14, 2026
tqchen added 27 commits July 15, 2026 02:25
Classify slot-backed primitive right-hand sides and demanded runtime results during shape-slot collection, then apply that plan during lowering.

Emit runtime leaf matches directly while preserving structural match rounds and immediate compound scheduling.
Apply the pinned clang-format result to the changed C++ range, update Ruff union-style isinstance syntax, and forward-port the upstream DLight loop-factor fixes so obsolete F821 suppressions can be removed.
Restore natural TIRx recursion and the existing UndefinedVars-based leaf discovery. Production pipelines normalize runtime compound expressions with ComputePrimValue before VMShapeLower, while compound shape and type expressions remain slot-map-driven.
@tqchen tqchen force-pushed the tvm-consolidate-var-unification-cleanup branch from 327d095 to c98615b Compare July 15, 2026 04:58
@tqchen tqchen merged commit c717c5b into apache:main Jul 15, 2026
8 checks passed
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.

2 participants