C++ API#

This page contains the API reference for the C++ API. The full API index below can be a bit dense, so we recommend the following tips first:

  • The Key Classes lists the key classes that are most commonly used.

  • See Header Organization for how the modules are organized.

  • TVM-FFI Structural Utilities lists the TVM-FFI utilities for walking and rewriting IR.

  • You can go to the Full API Index at the bottom of this page to access the full list of APIs.

    • We usually group the APIs by files. You can look at the file hierarchy in the full API index and navigate to the specific file to find the APIs in that file.

Header Organization#

The C++ headers are organized into the following folders, in dependency order: each folder builds on those above it.

Folder

Description

tvm/ffi/

From TVM-FFI: objects, containers, functions, and the structural utilities (see TVM-FFI Structural Utilities).

tvm/support/

Compiler-independent common utilities.

tvm/runtime/

Minimal runtime foundation for executing compiled code.

tvm/ir/

Core IR that language variants build upon, extend, and customize.

tvm/sym/

Symbolic analysis: integer arithmetic, integer sets, and iterator affine maps.

tvm/script/

Extensible IR builder, printer, and Python parser support for language variants.

tvm/target/

Target configuration schema and tag registry.

tvm/tirx/

Tensor program types, layouts, and operations.

tvm/codegen/

Generic host code generation and intrinsic lowering rules.

tvm/backend/

Backend-specific attributes, intrinsics, passes, and code generation.

Key Classes#

This section lists the key classes of the core IR and tensor programs.

The IR follows a common convention: each IR node is a TVM FFI Object whose class name carries the Obj suffix, such as tvm::prim::AddObj, paired with a named managed reference class, such as tvm::prim::Add. C++ extensions written against the former FooNode spelling must use FooObj and rebuild. Managed reference names, runtime type keys, and serialized identities remain unchanged. A reference class can define its own constructors, operators, and member functions, and inherit from other reference classes to share and specialize its interface. It can also impose additional constraints on the node it holds, as described below. The tables list the reference classes; to find the node class behind a reference, check its ContainerType member. IR reference classes are non-nullable; use ffi::Optional<T> when a value may be absent. See also Object and Class.

Besides the named reference for each node, some reference types add constraints on the node they hold. For example, TypedExpr<T> holds any tvm::ExprObj whose ty field is a T, and tvm::PrimExpr, defined as TypedExpr<PrimType>, holds any ExprObj whose ty is a tvm::PrimType. These constraints are defined by the TVM FFI TypeTraits specialization of the reference type.

Type#

Class

Description

tvm::Type

The base class of all types.

tvm::MissingType

Type information that has not been supplied or computed.

tvm::OpaqueType

Type marker for opaque values that must be removed from finished IR.

tvm::PrimType

A primitive scalar or vector data type, including the void sentinel.

tvm::NoneType

The concrete type whose only value is None.

tvm::AnyType

The top type, which admits any value.

tvm::TensorRegionType

The type of a multi-dimensional tensor region.

tvm::StringType

Semantic string type, independent of the function’s calling convention.

tvm::PtrType

A pointer to an element type in a specified memory scope.

tvm::TupleType

The type of tuple values.

tvm::FuncType

Function type.

Expr#

Class

Description

tvm::Expr

Base class of typed IR expressions.

tvm::StagingExpr

A traversable expression eliminated before executable IR.

tvm::OpaqueExpr

Base class for opaque values that must be removed from finished IR.

tvm::Constant

Base class of literal constants.

tvm::GenericConst

A literal payload with an explicit expression type.

tvm::StringImm

A string literal with StringType.

tvm::DataTypeImm

A data type literal whose expression type is AnyType.

tvm::IntImm

Integer constant.

tvm::FloatImm

Floating-point constant.

tvm::Tuple

Tuple expression that groups several fields together.

tvm::TupleGetItem

Get the index-th item from a tuple.

tvm::TensorLoad

Load an element or vector from an indexed expression source.

tvm::Var

A local variable in the IR.

tvm::LambdaExpr

A typed staging expression representing a lambda computation.

tvm::GlobalVar

A global variable that refers to a function in the module.

tvm::Call

Call a registered operator or another callable IR expression.

tvm::TensorRegion

A region of an indexed expression source.

tvm::BaseFunc

Base class for callable IR function definitions.

tvm::Op

A registered primitive operator used as a Call callee.

tvm::OpAttrMap

The values of one operator attribute, keyed by operator.

Stmt#

Class

Description

tvm::Stmt

Base class of all statements.

tvm::SeqStmt

Sequence of statements.

tvm::Bind

Bind a variable to a value in the enclosing scope.

tvm::RegionStmt

An operation with enclosing-scope operands and one lexical body.

tvm::AssertStmt

Check a condition and raise the specified error when it is false.

tvm::TensorStore

Store a primitive value into a tensor destination.

tvm::Evaluate

Evaluate an expression for its effects and discard its result.

tvm::If

Execute one branch according to a boolean condition.

tvm::For

Loop over an integer range.

tvm::While

Repeat the body while the condition evaluates to true.

tvm::Return

Exit the enclosing function with a return value.

tvm::Break

Exit the innermost enclosing loop.

tvm::Continue

Skip to the next iteration of the innermost enclosing loop.

Location, IRModule, and Supporting Classes#

Class

Description

tvm::SourceName

An identifier for a source location.

tvm::Location

Source-location metadata attached to IR nodes.

tvm::UnknownLoc

The location used when source information is unavailable.

tvm::SourceLoc

A source range in frontend coordinates.

tvm::CallSiteLoc

A callee’s source location together with its caller’s location.

tvm::IRModule

A collection of global functions and module-level metadata; the basic unit of IR transformations.

tvm::GlobalInfo

Base class for module-level information referenced by the IR.

tvm::Attrs

Base class for structured operator and function attributes.

tvm::DictAttrs

A string-keyed attribute map for functions, modules, and calls.

tvm::EnvFunc

A registered global function referenced and serialized by name.

tvm::Range

A half-open interval stored as begin and extent.

Tensor Program#

Class

Description

tvm::tirx::TensorType

Type of a tensor value: shape, dtype, and memory scope.

tvm::tirx::TensorMapType

Type of a hardware tensor map (TMA descriptor).

tvm::tirx::ThreadScope

Canonical thread-scope identity, named as backend.scope.

tvm::tirx::IndexMap

Mapping between index spaces used for layout transforms.

tvm::tirx::CallFFIKernelAttr

Attributes describing an FFI kernel call and its launch values.

tvm::tirx::Layout

Base reference for tensor-program coordinate layouts.

tvm::tirx::TileLayout

Tiled layout over hardware and memory axes.

tvm::tirx::ComposeLayout

Tile layout composed with an XOR swizzle on the memory coordinate.

tvm::tirx::TensorVar

Variable bound to a tensor.

tvm::tirx::Function

TIRx tensor-program function.

TVM-FFI Structural Utilities#

TIRx builds on TVM-FFI and supports it as a first-class foundation. Every TIRx IR node is a TVM-FFI object that is shared across languages, so TIRx uses the native TVM-FFI utilities for walking and rewriting IR directly. We list these utilities here for completeness. See the TVM-FFI documentation for more details.

See also

The headers tvm/tirx/structural_visit_with_constraint_context.h and tvm/tirx/structural_mutate_with_constraint_context.h provide variants of these utilities that also track constraint information, such as loop ranges and branch conditions, at each node: tvm::tirx::StructuralWalkWithConstraintContext(), tvm::tirx::StructuralVisitWithConstraintContext(), tvm::tirx::StructuralMapWithConstraintContext(), and tvm::tirx::StructuralMutateWithConstraintContext().

Functions#

Function

Description

tvm::ffi::StructuralWalk()

Walk every occurrence in pre- or post-order with typed callbacks.

tvm::ffi::StructuralMap()

Rebuild a value with typed callbacks that return replacements.

tvm::ffi::StructuralVisit()

Visit a value with callbacks that own the descent into children.

tvm::ffi::StructuralMutate()

Mutate a value with callbacks that own the descent into children.

tvm::ffi::StructuralEqual::Equal()

Compare two values by structure.

tvm::ffi::StructuralHash::Hash()

Hash a value by structure, consistently with structural equality.

Each function has an Expected form, such as tvm::ffi::StructuralWalkExpected(), that returns errors instead of throwing.

Classes#

Class

Description

tvm::ffi::StructuralVisitorObj

Base of structural visitors, including the visitor behind StructuralWalk.

tvm::ffi::StructuralMutatorObj

Base of structural mutators, including the mutator behind StructuralMap.

tvm::ffi::StructuralEqual

Structural equality comparators.

tvm::ffi::StructuralHash

Structural hash.

tvm::ffi::StructuralKey

A key that compares and hashes its value by structure.

tvm::ffi::WalkResult

Per-node control signal of a walk: advance, skip, or interrupt.

tvm::ffi::WalkOrder

Callback order of a walk or map: pre-order or post-order.

tvm::ffi::VisitInterrupt

The interrupt that stops a traversal, with an optional payload.

tvm::ffi::Unchanged

Tag that keeps the original value in a map or mutation.

tvm::ffi::UnchangedOr

A replacement value or Unchanged.

tvm::ffi::InplaceMode

Whether a mutation may update a value in place.

Full API Index#

Browse the full API index