35 Commits

Author SHA1 Message Date
Tianqi Chen 302aaf9f96 [IR] Rename Var name_hint field to name (#20016)
Rename the reflected local `Var` field from `name_hint` to `name` and
update its typed C++ consumers. Preserve distinct named-node APIs and
the Python constructor keyword compatibility path, while making `.name`
the sole stored Var property. Upgrade legacy compact JSON records for
current and pre-unification Var schemas.

Validation: full runtime/compiler build, focused C++ Var copy-helper
test, focused Python IR/Relax/TIRx/script tests, Vulkan codegen syntax
build, touched-file pre-commit checks, and `git diff --check`.
2026-07-17 05:34:31 +08:00
Tianqi Chen 91fb997563 [REFACTOR][IR] Use CamelCase Var copy helpers (#20008) 2026-07-16 07:43:00 +08:00
Tianqi Chen adf8d6a463 [TIRx] Phase out duplicate Var type_annotation (#19944)
## Rationale

TIRx variables use inherited `ExprNode::ty` as their single semantic
type. Retaining a primitive handle surrogate erases the distinction
between scalar values, typed pointers, and true opaque pointers, then
forces later passes and code generators to reconstruct information that
the IR already owns.

## Changes

- Remove the duplicate reflected `Var::type_annotation` state and
preserve exact `PrimType` or `PointerType` through construction,
visitors, transforms, specialization, builders, printers, and code
generation.
- Keep scalar-only boundaries explicit through `PrimExpr`, `PrimVar`,
and `PrimType`; pointer-capable values remain general `Expr` or `Var`.
- Keep helper boundaries no broader than their contracts: TE tensor
variable indices use `PrimVar`, while expression deep equality recurses
through general `Expr` only where pointer-bearing `Call` arguments
require it and does not generalize private arithmetic subclasses.
- Keep core statement reflection typed as `Expr`, name general
reinterpret targets as `target_ty`, and preserve exact pointer calls in
the general vectorization path with explicit scalarization behavior.
- Delete `PrimType::Handle()` and `PrimType::IsHandle()`. True opaque
pointers use `PointerType::VoidPointerTy()`; TVMScript renders the
canonical global type as `T.handle`, standalone values as `T.handle()`,
and scoped void pointers with a keyword-only storage scope.
- Make `CodeGenSourceBase::SSAGetID` a single `Type` boundary across
source backends, without a separate primitive-type or runtime-dtype
variant.
- Keep WebGPU semantic argument classification type-aware: storage
buffers are identified from `PointerType`, POD arguments from
`PrimType`, and only the final `FunctionInfo` launch ABI is serialized
to `DLDataType`.
- Preserve exact pointer semantics at runtime boundaries, including
access pointers, packed calls and returns, external calls, storage
rewrites, and target-specific lowering.

## Migration guide

- **Variable types:** In C++, replace `var->type_annotation` with
`var->ty`; in Python, replace `var.type_annotation` with `var.ty`. The
result is the exact `Type`: scalar variables carry `PrimType`, while
pointer variables carry `PointerType`.
- **Scalar boundaries:** Use `PrimVar` and `PrimExpr` for variables and
expressions that are semantically scalar. When starting from a general
view, narrow explicitly with `var.as_or_throw<PrimVar>()` or
`expr.as_or_throw<PrimExpr>()`. Keep pointer-capable fields and call
arguments as `Var` or `Expr`. A default-constructed `PrimVar` is
nullable, so construct local scalar variables explicitly, for example
`PrimVar i("i")`.
- **Opaque pointers:** Replace `PrimType::Handle()` with
`PointerType::VoidPointerTy()`. Replace `IsHandle()` tests with explicit
`PointerType` inspection; use `PointerType(element_type, storage_scope)`
when the pointee type is known instead of erasing it to a runtime handle
dtype.
- **TVMScript handles:** Use `arg: T.handle` for a global void-pointer
annotation and `arg = T.handle()` for a standalone value. Use
`T.handle(storage_scope="shared")` for a scoped void pointer. Typed
pointers use forms such as `T.handle("float32")`, `T.handle("float32",
"global")`, or `T.handle("float32", "shared")`. Legacy
`T.handle("void")` input remains parse-compatible, but the printer
canonicalizes it to `T.handle` (or the keyword-only scoped form).
- The separate `tirx.type_annotation` intrinsic used by access-pointer
APIs is unchanged; this migration removes only the duplicate variable
field.

## Validation

- Complete native C++ test executable: 122/122 passed, including
`IRF.CountVar`.
- Relax binding-rewrite suite: 12/12 passed, including transferred-user
bookkeeping.
- Canonical typed/void/scoped TVMScript handle printer and round-trip
checks: 5/5 passed.
2026-07-04 21:54:04 -04:00
Tianqi Chen 1e1920bcbd [REFACTOR][IR] Unify PrimExpr type mechanism to PrimType instead of DataType (#19875)
In the past we have been using `DataType` in PrimExpr.dtype field to
check type information for PrimExpr while still having BaseExpr.ty for
richer type information. DataType is also used both in runtime and
compiler. This PR streamlines the boundary:

- PrimExpr.ty now carries PrimType that replaces original use of
`DataType`
- Runtime use will now favor DLPack DLDataType, removing one layer of
indirection.
- Constants attributes where values are usually runtime values, will use
`DLDataType`
- DataType will be phased out after this PR

We also brings up helper functions in PrimType, but also limits them to
a more concise set so the functions do not grow with the data type codes
in DLPack.

This is a major refactor that changes the IR primitive. It helps to
bring possible future benefits:
- Unified type mechanism through Expr.ty
- Possibility of carry future Type nodes 

Migration Guide:
- Use `PrimType` when code reasons about compiler expression types,
tensor element compiler types, or constructs a `PrimExpr`/compiler type.
- Use existing source types such as `expr.ty()`, `ExprOp.expr_ty()`, or
TE tensor element `dtype` where possible instead of rebuilding a type
from dtype text.
- Use raw `DLDataType` for runtime constants, ABI paths, dtype-valued
attrs, and storage/runtime helper logic.
- Prefer direct `PrimType` equality, `MatchesCode(...)`,
`MatchesElementType(...)`, and `WithCode(...)` over local wrappers or
string dtype checks.

Performance:

Using Object type instead of DLDataType would indeed bring some
performance impact to the IR. We have done the following performance
optimizations:
- Make sure most of the outputs reuse one of the PrimType from inputs
- Cache a thread local PrimType based on input so we don't repeatly
realloc

We did benchmarks show that rewrite simplify operation stays within
+-10% overhead of original one. Which merits the refactor given the
benefit the unfication brings
2026-06-24 21:31:47 -04:00
Tianqi Chen a79101d656 [REFACTOR][IR] Phase out Downcast usages (#19857)
This PR phases out the legacy `Downcast` helper and removes
`tvm/ir/cast.h`, replacing mandatory object casts with the strict
`as_or_throw` style APIs after bumping tvm-ffi to the latest fix.

The migration keeps nullable-object behavior explicit. Optional `Any`
conversions that need to preserve null use the nullable
`value_or(nullptr).as_or_throw<ffi::Optional<T>>()` pattern, while
simple mandatory casts use direct receivers such as
`obj.as_or_throw<T>()`.
2026-06-22 15:43:23 -04:00
Tianqi Chen 556571fc9d [REFACTOR][RUNTIME] Phase out include/tvm/runtime/object.h (#19476)
## Summary

include/tvm/runtime/object.h was a vestige of the pre-tvm-ffi world — a
thin compat layer re-exporting
Object/ObjectRef/ObjectPtr/GetRef/GetObjectPtr
aliases into tvm::runtime:: and tvm::, plus a few TVM-specific macros
and
an enum TypeIndex with mostly-dead constants.

This PR phases the header out entirely, with no shim:

- `TVM_DEFINE_OBJECT_REF_COW_METHOD` relocated to a new
`include/tvm/ir/cow.h`
  (its consumer set is entirely IR/TIRX/relax/arith/te).
-
`TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE_WITHOUT_DEFAULT_CONSTRUCTOR`
  inlined at its 2 callers (rare; not worth a new home).
- `TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN` inlined at its 1 caller.
- `TVM_STR_CONCAT` removed; callers switch to `TVM_FFI_STR_CONCAT`
  (already in tvm-ffi; the local copy was a duplicate).
- `kRuntimeRPCObjectRef` / `kRuntimeDiscoDRef` inlined into
`rpc_session.h`
  / `disco/session.h` respectively (the only live type-index constants).
- All using-aliases (`tvm::runtime::Object` etc.) rewritten to fully-
qualified `tvm::ffi::Object` at use sites — no using-injections
anywhere.
- `include/tvm/runtime/object.h` deleted.

## Test plan

- ninja build clean (USE_LLVM=ON, default targets) — exit 0.
- ./cpptest — 118/118.
- pytest tests/python/all-platform-minimal-test/ — green.
- pytest tests/python/runtime/ — green.
- pre-commit clean.
2026-04-29 20:05:42 -04:00
Tianqi Chen ca2d1e8732 [REFACTOR][IR] Migrate include/tvm/node into include/tvm/ir (#19463)
## Summary

`include/tvm/node/` is a leftover separation from when "node" was a
distinct concept from "ir". Today everything in `include/tvm/node/` is
just lower-level IR plumbing routinely included from `ir/`. This PR
collapses the two by moving surviving headers into `ir/`, deleting dead
ones, and redirecting the rest to tvm-ffi where the machinery already
lives.

Main changes:

- Migrate
`include/tvm/node/{functor,cast,script_printer,attr_registry_map,repr}.h`
→ `include/tvm/ir/` (functor renamed to `node_functor.h` to preserve
type-name connection)
- Delete `repr_printer.h`, `structural_equal.h`, `structural_hash.h`
(post-#19461 shims and forwarding stubs; redirect 3 cpptest consumers to
`tvm/ffi/extra/structural_equal.h`)
- Remove inline `AccessStep`/`AccessPath` `operator<<` definitions
(existing `__ffi_repr__` registrations already cover these; migrate to
generic ObjectRef streaming via kRepr)
- Move `src/node/{repr,script_printer}.cc` → `src/ir/`
- Delete `src/node/` and empty `include/tvm/node/` directories entirely
(no shims)
- Update 35 includers across IR, relax, script, target, tirx, and tests
2026-04-28 14:33:15 -04:00
Tianqi Chen 141c22fd8a [Refactor] Bring up tirx namespace (#18913)
This PR brings up the tirx namespace. We have been spliting out the
original tir namespace to include high-level component s_tir and this PR
updates the remaining low-level part as tirx namespace
2026-03-19 21:27:54 -07:00
Tianqi Chen e5f483cd5f [REFACTOR][NODE] Remove node redirect headers (#18829) 2026-02-27 06:36:57 -05:00
Tianqi Chen f533d0b3c1 [REFACTOR] Migrate CHECK macros to tvm-ffi ones (#18803) 2026-02-21 09:00:44 -05:00
Tianqi Chen 349df2bc26 [FFI][REFACTOR] Cleanup namespace (#18280)
* [FFI][REFACTOR] Cleanup namespace

This PR cleansup the namespace to ensure all ffi classes
are accessed through ffi:: namespace.
It will helps to cleanup the ffi package before isolation.

* fix hexagon
2025-09-08 08:58:30 -04:00
Tianqi Chen a531d170b9 [REFACTOR] Phase out relay c++ components (#17660)
* cleanup relay c++

* [REFACTOR] Phase out relay c++ components

This PR phases out the relay C++ components and
simplifies the overall codegen runtime logic.

---------

Co-authored-by: Siyuan Feng <hzfengsy@sjtu.edu.cn>
2025-02-17 22:21:37 +08:00
Junru Shao c452e6966c [TVMScript] IR Fragment Printing (#13742)
This PR introduces support for TIR fragment printing.
Fragment printing makes it possible to print TIR fragments in the text
format consistency with TVMScript PrimFunc/IRModule printing.

This PR still preserves the legacy ReprPrinter format by introducing an
API `LegacyTIRPrint` for TIR PrimExpr. This method is used in
AutoScheduler and TIR CSE for full backward compatibility.
2023-01-13 22:36:47 -05:00
Wuwei Lin c6415d1492 Canonicalize type annotation during construction of Var and SizeVar (#11443)
* Canonicalize type annotation during construction of Var and SizeVar

* Update tests/cpp/expr_test.cc

Co-authored-by: Junru Shao <junrushao1994@gmail.com>

* lint

* fix

Co-authored-by: Junru Shao <junrushao1994@gmail.com>
2022-05-30 07:53:29 -04:00
Christopher Sidebottom b77a7d4fc6 Apply CPPLint to C++ Unit Tests (#8827)
This change enables `cpplint` for the tests in `tests/cpp` and corrects any current linting errors. I had to use `NOLINT` in some of the PackedFunc tests due to a bug (see: https://github.com/cpplint/cpplint/issues/131) in CPPLint where `int(int)` is picked up as a cast rather than a nameless argument.
2021-08-24 13:45:52 -07:00
Christopher Sidebottom 356879d4c3 Use CTest for C++ tests (#8809)
By using the `gtest_discover_tests` CMake macro the CPP and CRT tests can be configured to build binaries with a single test runner each. Once CTest has information about tests it can be used in IDE extensions such as [CMake Test Explorer](https://marketplace.visualstudio.com/items?itemName=fredericbonnet.cmake-test-adapter).

`ctest` can also run tests in parallel using the `-j` flag, which could be interesting in future.
2021-08-23 17:08:18 -07:00
Robert Kimball 8695a57619 More CHECK to ICHECK (#6758)
* Address apps, docs, and nnvm directories

* Catch some that were missed

* crt has it's own logging.h

* Fix missing include
2020-10-26 15:55:43 -07:00
Tianqi Chen 7bad56b74e [LINT] clang-format the h,cc,m files. (#5557)
This PR prepares for our migration to use the clang-format
as part of the linter system.
2020-05-10 21:43:33 -07:00
Tianqi Chen 55d8192528 [REFACTOR] top->te (#4759)
Bring up namespace te -- Tensor expression language DSL.
2020-01-21 11:58:21 -08:00
Tianqi Chen cf59b206b8 [REFACTOR] Establish tir (#4740)
TIR is the new namespace for low-level IR
for tensor-level optimizations and loop transformations.

This PR establishes the namespace and files.

- lowered_func.h,buffer.h,data_layout.h -> tir/buffer.h,tir/data_layout.h,tir/lowered_func.h
- ir.h -> tir/expr.h, tir/stmt.h
- ir_functor_ext.h -> tir/expr_functor.h, tir/stmt_functor.h
2020-01-18 22:44:50 -08:00
Tianqi Chen b826142672 [REFACTOR] top - namespace for Tensor Operation DSL (#4727)
* [REFACTOR] introduce top - Tensor Operation DSL.

Historically we put Tensor, Schedule and compute under the root tvm namespace.
This is no longer a good idea as the project's scope grows larger
than the tensor operation DSL.

This PR introduces top -- a namespace for tensor operational
DSL concepts such as schedule, tensor, compute.
We moved the related files to the new top subfolder.

* Move relevant files into include/tvm/top and src/top
2020-01-16 15:23:54 -08:00
Tianqi Chen d6a23cf50f [REFACTOR][IR] tvm::Expr -> PrimExpr(Primitive Expr) (#4669)
* [REFACTOR][IR] tvm::Expr -> PrimExpr(Primitive Expr)

As part of unified IR, we will need to unify relay::Expr
and the current tvm::Expr under the same base type.

From the techinical point of view. tvm::Expr is a "primitive"
expression that only contains POD types and handles and does
not do life-cycle management.

This PR renames Expr->PrimExpr to clarify that.
We will send a subsequent PR to introduce the base expr class.

* Remove legacy VarExpr and ExprHash/Equal
2020-01-09 15:30:23 -08:00
Tianqi Chen f4c5f93b4a [REFACTOR][IR] Add Node suffix to low-level IR nodes (#4649)
* [REFACTOR][IR] Variable -> VarNode

* [REFACTOR][IR] Add/Sub/Mul/Div -> AddNode/SubNode etc.

* [REFACTOR][IR] Min/Max/FloorDiv/FloorMod -> MinNode/MaxNode etc.

* [REFACTOR][IR] EQ/NE/LT/LE/GT/GE/Select -> EQNode/NENode etc.

* [REFACTOR][IR] Add Node suffix to Select/Call/Load/Ramp/Shuffle/Let

* [REFACTOR][IR] Add node suffix to IntImm/UIntImm/FloatImm/StringImm

* [REFACTOR][IR] Add Node suffix to Any, AttrStmt, AssertStmt

* [REFACTOR][IR] Add Node suffix to Store/Provide/Allocate/Free

* [REFACTOR][IR] Add Node suffix to ProducerConsumer

* Fix lint

* style updates, test fixes
2020-01-08 09:01:00 -08:00
Tianqi Chen a8c369218e [REFACTOR][OBJECT] Consoldiate NodePtr/Ref/Hash/Equal to Object (#4603)
* [REFACTOR][OBJECT] Consoldiate NodePtr/Ref/Hash/Equal and macros to Object.

Historically, we have classes like NodePtr/Ref/HashEqual.
After unified object protocol, these names are just alias of the object counterpart.
Moreover, there are helper macros defined over the places for defining these object.

This PR consoldiate the terminologies into the corresponding ones
in the Object system so we have a clean and consistent API moving forward.

* Update include/tvm/attrs.h

Co-Authored-By: Wei Chen <ipondering.weic@gmail.com>

* fix compilation

Co-authored-by: Wei Chen <ipondering.weic@gmail.com>
2019-12-31 09:35:03 -08:00
Tianqi Chen 7895adb243 [REFACTOR][NODE][RUNTIME] Move Node to the new Object protocol. (#4161)
* [REFACTOR][NODE][RUNTIME] Move Node to the new Object protocol.

This PR removes the original node system, and make node as a subclass of Object.
This is a major refactor towards a better unified runtime object system.

List of changes in the refactor:

- We now hide data_ field, use Downcast explicitly to get a sub-class object.
- Removed the node system FFI in python.
- Removed the node C API, instead use PackedFunc for list and get attrs.
- Change relay::Op::set_attr_type_key(attr_key_name) to relay::Op::set_attr_type<AttrType>().
  - This change was necessary because of the new Object registration mechanism.
  - Subsequent changes to the op registrations
  - The change revealed a few previous problems that is now fixed.
- Patched up a few missing node type registration.
  - Now we will raise an error if we register object that is not registered.
- The original node.h and container.h are kept in the same location.
- Calling convention: kObjectHandle now equals the old kNodeHandle, kNodeHandle is removed.
- IRFunctor now dispatches on ObjectRef.
- Update to the new type checking API: is_type, derived_from are replaced by IsInstance.
- Removed .hash member function, instead use C++ convention hasher functors.

* Address review comments
2019-10-20 18:30:41 -07:00
Tianqi Chen 025a6c8077 [CPP] Refactor remove tvm/tvm.h (#3523) 2019-07-09 18:40:10 -07:00
Tianqi Chen cffb4fba03 [HEADER] Add Header to Comply with ASF Release Policy (#2982)
* [HEADER] ASF header dir=include

* [HEADER] ASF Header dir=src

* [HEADER] ASF Header -dir=python

* [HEADER] ASF header dir=topi

* [HEADER] ASF Header dir=nnvm

* [HEADER] ASF Header -dir=tutorials

* [HEADER] ASF Header dir=tests

* [HEADER] ASF Header -dir=docker

* fix whitespace

* [HEADER] ASF Header -dir=jvm

* [HEADER] ASF Header -dir=web

* [HEADER] ASF Header --dir=apps

* [HEADER] ASF Header --dir=vta

* [HEADER] ASF Header -dir=go

* temp

* [HEADER] ASF Header --dir=rust

* [HEADER] Add ASF Header --dir=cmake

* [HEADER] ASF Header --dir=docs

* [HEADER] Header for Jenkinsfile

* [HEADER] ASF Header to toml and md

* [HEADER] ASF Header to gradle

* Finalize rat cleanup

* Fix permission

* Fix java test

* temporary remove nnvm onnx test
2019-04-07 21:14:02 -07:00
Tianqi Chen ec0d497c69 [NODE][RELAY] Move most of the reference related code to node (#1747) 2018-09-20 20:17:24 -07:00
Tianqi Chen 819728db57 Update halideIR, add more device query for shared memory (#1087) 2018-04-07 20:56:00 -07:00
Tianqi Chen 5fced923ef [LANG] Enable json load/save and pickle (#10) 2017-01-12 10:07:37 -08:00
tqchen e011edd175 Add Tensor, cleanup test, all present tests pass 2016-10-28 17:17:21 -07:00
Haichen Shen 1338392811 add var binding for expr 2016-10-22 12:58:14 -07:00
tqchen 3c0dc79d2e Simplify for cxx 2016-10-21 12:42:13 -07:00
tqchen 5f829774f2 Add domain 2016-10-19 12:13:30 -07:00
tqchen 8278e02fd6 checkin basic cpp test 2016-10-15 17:35:32 -07:00