19#ifndef TVM_RELAX_EXPR_H_
20#define TVM_RELAX_EXPR_H_
23#include <tvm/ffi/container/array.h>
24#include <tvm/ffi/container/map.h>
25#include <tvm/ffi/extra/structural_equal.h>
26#include <tvm/ffi/extra/structural_hash.h>
27#include <tvm/ffi/reflection/registry.h>
44using ::tvm::TupleGetItem;
45using ::tvm::TupleGetItemObj;
56 namespace refl = tvm::ffi::reflection;
65 explicit ShapeExpr(ffi::ObjectPtr<ShapeExprObj> node) :
Expr(std::move(node)) {}
77 namespace refl = tvm::ffi::reflection;
78 refl::ObjectDef<DataflowVarObj>();
87 TVM_DLL
explicit DataflowVar(ffi::String name, ffi::Optional<Type> ty_annotation,
90 explicit DataflowVar(ffi::ObjectPtr<DataflowVarObj> node) :
Var(std::move(node)) {}
97 ffi::Optional<Type> ty_annotation = std::nullopt,
111 namespace refl = tvm::ffi::reflection;
112 refl::ObjectDef<BindingObj>()
113 .def_ro(
"loc", &
BindingObj::loc, refl::AttachFieldFlag::SEqHashIgnore())
115 .def_ro(
"var", &
BindingObj::var, refl::AttachFieldFlag::SEqHashDefPattern());
128 explicit Binding(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
159 namespace refl = tvm::ffi::reflection;
160 refl::ObjectDef<MatchCastObj>()
163 .def_ro(
"ty", &
MatchCastObj::ty, refl::AttachFieldFlag::SEqHashDefPattern());
191 namespace refl = tvm::ffi::reflection;
194 refl::TypeAttrDef<VarBindingObj>()
200 ffi::TypedFunction<
bool(AnyView, AnyView,
bool, AnyView)>
equal)
const;
201 int64_t
SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t,
bool)> hash)
const;
218 namespace refl = tvm::ffi::reflection;
219 refl::ObjectDef<BindingBlockObj>()
232 explicit BindingBlock(ffi::UnsafeInit tag, ffi::ObjectPtr<BindingBlockObj> n)
233 : ffi::ObjectRef(tag) {
234 data_ = std::move(n);
244 namespace refl = tvm::ffi::reflection;
245 refl::ObjectDef<DataflowBlockObj>();
253 explicit DataflowBlock(ffi::UnsafeInit tag, ffi::ObjectPtr<DataflowBlockObj> n)
255 data_ = std::move(n);
274 namespace refl = tvm::ffi::reflection;
275 refl::ObjectDef<SeqExprObj>()
278 refl::TypeAttrDef<SeqExprObj>()
284 ffi::TypedFunction<
bool(AnyView, AnyView,
bool, AnyView)>
equal)
const {
291 int64_t
SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t,
bool)> hash)
const {
292 int64_t hash_value = init_hash;
293 hash_value = hash(
blocks, hash_value,
false);
294 hash_value = hash(
ty, hash_value,
false);
295 hash_value = hash(
body, hash_value,
false);
317 explicit SeqExpr(ffi::ObjectPtr<SeqExprObj> node) :
Expr(std::move(node)) {}
351 namespace refl = tvm::ffi::reflection;
352 refl::ObjectDef<IfExprObj>()
383 explicit IfExpr(ffi::ObjectPtr<IfExprObj> node) :
Expr(std::move(node)) {}
405 namespace refl = tvm::ffi::reflection;
406 refl::ObjectDef<FunctionObj>()
440 TVM_DLL
explicit Function(ffi::Array<Var> params,
Expr body, ffi::Optional<Type> ret_ty,
464 namespace refl = tvm::ffi::reflection;
505 "const_name_to_constant"};
508inline constexpr ffi::reflection::AttrKey<bool>
kPrimitive{
"Primitive"};
513inline constexpr ffi::reflection::AttrKey<ffi::String>
kCodegen{
"Codegen"};
515inline constexpr ffi::reflection::AttrKey<ffi::String>
kComposite{
"Composite"};
518 "PartitionedFromPattern"};
520inline constexpr ffi::reflection::AttrKey<int64_t>
kWorkspaceSize{
"WorkspaceSize"};
526inline constexpr ffi::reflection::AttrKey<bool>
kForcePure{
"relax.force_pure"};
533inline constexpr ffi::reflection::AttrKey<int64_t>
kNumInput{
"num_input"};
534inline constexpr ffi::reflection::AttrKey<int64_t>
kOpPattern{
"op_pattern"};
536inline constexpr ffi::reflection::AttrKey<ffi::ObjectRef>
kOpAttrs{
"op_attrs"};
538inline constexpr ffi::reflection::AttrKey<ffi::String>
kOperatorName{
"operator_name"};
540inline constexpr ffi::reflection::AttrKey<ffi::String>
kLibraryKernel{
"library_kernel"};
542inline constexpr ffi::reflection::AttrKey<ffi::String>
kCSource{
"c_source"};
544inline constexpr ffi::reflection::AttrKey<ffi::String>
kCSourceFmt{
"c_source_fmt"};
547 "tir_var_upper_bound"};
550 "tir_var_lower_bound"};
553 "tir_non_negative_var"};
558 "relax.rewrite_cuda_graph.capture_symbolic_vars"};
560inline constexpr ffi::reflection::AttrKey<int64_t>
kScoped{
"scoped"};
Shared primitive construction and constant helpers.
Device-independent tensor allocation, copies, views and serialization.
Base node of all functions.
Definition function.h:94
Managed reference to BaseFuncObj.
Definition function.h:202
Managed reference to DictAttrsObj.
Definition attrs.h:108
Base type of all the expressions.
Definition expr.h:66
Type ty
The deduced or annotated type of the expression.
Definition expr.h:80
Managed reference to ExprObj.
Definition expr.h:101
Managed reference to a generic constant.
Definition expr.h:256
Non-nullable source-location metadata.
Definition location.h:91
Managed reference to TypeObj.
Definition type.h:75
static TVM_TIRX_DLL Type Missing()
Construct a MissingType for type information not yet populated.
Reference to the shared immutable unknown location.
Definition location.h:105
A local variable in the IR.
Definition expr.h:552
Managed reference to VarObj.
Definition expr.h:571
static void RegisterReflection()
Definition expr.h:217
Location loc
Definition expr.h:215
TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.BindingBlock", BindingBlockObj, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:225
ffi::Array< Binding > bindings
Definition expr.h:214
BindingBlockObj * CopyOnWrite()
BindingBlock(ffi::Array< Binding > bindings, Location loc=UnknownLoc())
BindingBlock(ffi::UnsafeInit tag, ffi::ObjectPtr< BindingBlockObj > n)
Definition expr.h:232
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BindingBlock, ffi::ObjectRef, BindingBlockObj)
The base class of a variable binding in Relax.
Definition expr.h:101
Var var
The return variable to bound to.
Definition expr.h:108
Location loc
Definition expr.h:106
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:118
BindingObj(Var var)
Definition expr.h:103
static void RegisterReflection()
Definition expr.h:110
TVM_FFI_DECLARE_OBJECT_INFO("relax.expr.Binding", BindingObj, ffi::Object)
BindingObj(ffi::UnsafeInit)
Definition expr.h:104
const BindingObj * get() const
Definition expr.h:134
Binding(const Binding &)=default
static constexpr bool _type_is_nullable
Definition expr.h:135
Binding(Binding &&)=default
Binding & operator=(Binding &&)=default
const BindingObj * operator->() const
Definition expr.h:133
Binding(ffi::UnsafeInit tag)
Definition expr.h:128
Binding & operator=(const Binding &)=default
static void RegisterReflection()
Definition expr.h:243
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataflowBlock", DataflowBlockObj, BindingBlockObj)
DataflowBlock(ffi::Array< Binding > bindings, Location loc=UnknownLoc())
TVM_DEFINE_OBJECT_REF_COW_METHOD(DataflowBlockObj)
DataflowBlock(ffi::UnsafeInit tag, ffi::ObjectPtr< DataflowBlockObj > n)
Definition expr.h:253
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataflowBlock, BindingBlock, DataflowBlockObj)
A sub-type of the variable node used to mark dataflow variables from normal visible "function local" ...
Definition expr.h:74
static void RegisterReflection()
Definition expr.h:76
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:81
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.DataflowVar", DataflowVarObj, VarObj)
DataflowVar(ffi::String name, ffi::Optional< Type > ty_annotation, Location loc=UnknownLoc())
DataflowVar(ffi::ObjectPtr< DataflowVarObj > node)
Definition expr.h:90
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DataflowVar, Var, DataflowVarObj)
The extern function, which can represent packed function.
Definition expr.h:458
ffi::String global_symbol
The name of global symbol.
Definition expr.h:461
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.ExternFunc", ExternFuncObj, BaseFuncObj)
static void RegisterReflection()
Definition expr.h:463
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExternFunc, BaseFunc, ExternFuncObj)
ExternFunc(ffi::ObjectPtr< ExternFuncObj > node)
Definition expr.h:475
ExternFunc(ffi::String global_symbol, Location loc=UnknownLoc())
TVM_DEFINE_OBJECT_REF_COW_METHOD(ExternFuncObj)
ExternFunc(ffi::String global_symbol, Type ty, Location loc=UnknownLoc())
A Relax function.
Definition expr.h:390
ffi::Array< Var > params
The parameters to the function.
Definition expr.h:396
static void RegisterReflection()
Definition expr.h:404
FunctionObj(ffi::UnsafeInit)
Definition expr.h:393
SeqExpr body
The body of the function.
Definition expr.h:398
Type ret_ty
The return type of the function.
Definition expr.h:400
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:413
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.Function", FunctionObj, BaseFuncObj)
FunctionObj(SeqExpr body)
Definition expr.h:392
bool is_pure
Whether the function is annotated as pure or not.
Definition expr.h:402
Function(ffi::Array< Var > params, Expr body, ffi::Optional< Type > ret_ty, bool is_pure=true, DictAttrs attrs=DictAttrs(), Location loc=UnknownLoc())
Construct a Relax Function.
TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionObj)
Function(ffi::ObjectPtr< FunctionObj > node)
Definition expr.h:451
static Function CreateEmpty(ffi::Array< Var > params, Type ret_ty, bool is_pure=true, DictAttrs attrs=DictAttrs(), Location loc=UnknownLoc())
Mimics the constructor but without body Expr.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Function, BaseFunc, FunctionObj)
Condition expression.
Definition expr.h:334
static void RegisterReflection()
Definition expr.h:350
SeqExpr false_branch
The expression evaluated when condition is false.
Definition expr.h:348
IfExprObj(Expr cond, SeqExpr true_branch, SeqExpr false_branch)
Definition expr.h:336
IfExprObj(ffi::UnsafeInit)
Definition expr.h:340
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.IfExpr", IfExprObj, ExprObj)
SeqExpr true_branch
The expression evaluated when condition is true.
Definition expr.h:346
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition expr.h:358
Expr cond
The condition.
Definition expr.h:344
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IfExpr, Expr, IfExprObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(IfExprObj)
IfExpr(Expr cond, Expr true_branch, Expr false_branch, Location loc=UnknownLoc())
The constructor.
IfExpr(ffi::ObjectPtr< IfExprObj > node)
Definition expr.h:383
Runtime-match the value to the type.
Definition expr.h:146
MatchCastObj(Var var, Expr value)
Definition expr.h:148
MatchCastObj(ffi::UnsafeInit)
Definition expr.h:150
Expr value
The input value to match cast.
Definition expr.h:154
Type ty
The type pattern to match to.
Definition expr.h:156
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.MatchCast", MatchCastObj, BindingObj)
static void RegisterReflection()
Definition expr.h:158
Managed reference to MatchCastObj.
Definition expr.h:172
MatchCast(Var var, Expr value, Type ty, Location loc=UnknownLoc())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(MatchCast, Binding, MatchCastObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(MatchCastObj)
A sequence of blocks followed by an expression.
Definition expr.h:265
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
Definition expr.h:291
bool SEqual(const SeqExprObj *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
Definition expr.h:283
SeqExprObj(Expr body)
Definition expr.h:267
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.SeqExpr", SeqExprObj, ExprObj)
ffi::Array< BindingBlock > blocks
Definition expr.h:270
SeqExprObj(ffi::UnsafeInit)
Definition expr.h:268
static void RegisterReflection()
Definition expr.h:273
Expr body
Definition expr.h:271
SeqExpr(ffi::Array< BindingBlock > blocks, Expr body, Location loc=UnknownLoc())
TVM_DEFINE_OBJECT_REF_COW_METHOD(SeqExprObj)
SeqExpr(ffi::ObjectPtr< SeqExprObj > node)
Definition expr.h:317
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SeqExpr, Expr, SeqExprObj)
A shape expression which allows users to construct a shape containing PrimExpr.
Definition expr.h:50
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.ShapeExpr", ShapeExprObj, ExprObj)
static void RegisterReflection()
Definition expr.h:55
ffi::Array< PrimExpr > values
Definition expr.h:53
ShapeExpr(ffi::ObjectPtr< ShapeExprObj > node)
Definition expr.h:65
ShapeExpr(ffi::Array< PrimExpr > values, Location loc=UnknownLoc())
TVM_DEFINE_OBJECT_REF_COW_METHOD(ShapeExprObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ShapeExpr, Expr, ShapeExprObj)
bool SEqual(const VarBindingObj *other, ffi::TypedFunction< bool(AnyView, AnyView, bool, AnyView)> equal) const
static void RegisterReflection()
Definition expr.h:190
VarBindingObj(Var var, Expr value)
Definition expr.h:182
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("relax.expr.VarBinding", VarBindingObj, BindingObj)
Expr value
The binding value.
Definition expr.h:188
VarBindingObj(ffi::UnsafeInit)
Definition expr.h:184
int64_t SHash(int64_t init_hash, ffi::TypedFunction< int64_t(AnyView, int64_t, bool)> hash) const
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(VarBinding, Binding, VarBindingObj)
TVM_DEFINE_OBJECT_REF_COW_METHOD(VarBindingObj)
VarBinding(Var var, Expr value, Location loc=UnknownLoc())
Copy-on-write helper macro for IR ffi::ObjectRef types.
Parent compiler symbol visibility.
Relax types, including the richer dependent Relax type nodes.
Source locations and source-text lookup.
constexpr ffi::reflection::AttrKey< ffi::String > kCSourceFmt
Definition expr.h:544
constexpr ffi::reflection::AttrKey< ffi::String > kOperatorName
Definition expr.h:538
constexpr ffi::reflection::AttrKey< int64_t > kWorkspaceSize
The required workspace for an external function.
Definition expr.h:520
constexpr ffi::reflection::AttrKey< bool > kPrimitive
Mark the function as a primitive function.
Definition expr.h:508
constexpr const char * kMemoryPlanDynamicFuncOutput
Definition expr.h:555
constexpr ffi::reflection::AttrKey< ffi::Map< ffi::String, IntImm > > kTirVarUpperBound
Definition expr.h:546
constexpr ffi::reflection::AttrKey< int64_t > kNumInput
The number of inputs of a function. If a function has the num_input attribute, the last func->params....
Definition expr.h:533
constexpr ffi::reflection::AttrKey< ffi::Map< ffi::String, IntImm > > kTirVarLowerBound
Definition expr.h:549
constexpr ffi::reflection::AttrKey< bool > kForcePure
Override checking purity for this function and treat as pure (is_pure must be set to true)
Definition expr.h:526
constexpr ffi::reflection::AttrKey< ffi::Map< ffi::String, ffi::Tensor > > kConstNameToConstant
All the named Tensors accumulated during compilation by external codegen. Generally the associated ru...
Definition expr.h:504
constexpr ffi::reflection::AttrKey< int64_t > kOpPattern
Definition expr.h:534
constexpr ffi::reflection::AttrKey< ffi::String > kComposite
Treat the function as a composite operator.
Definition expr.h:515
constexpr ffi::reflection::AttrKey< ffi::String > kCodegen
Indicate the codegen that should be used for building this function. When this is unset or set to "de...
Definition expr.h:513
constexpr ffi::reflection::AttrKey< ffi::Array< ffi::String > > kTirNonNegativeVar
Definition expr.h:552
constexpr ffi::reflection::AttrKey< ffi::String > kCSource
Definition expr.h:542
constexpr ffi::reflection::AttrKey< ffi::ObjectRef > kOpAttrs
Definition expr.h:536
constexpr ffi::reflection::AttrKey< int64_t > kScoped
Definition expr.h:560
constexpr ffi::reflection::AttrKey< ffi::String > kLibraryKernel
Definition expr.h:540
constexpr ffi::reflection::AttrKey< ffi::String > kPartitionedFromPattern
Indicate the function was created by the Pattern Partitioning Pass.
Definition expr.h:517
constexpr ffi::reflection::AttrKey< ffi::Array< ffi::String > > kCaptureSymbolicVars
Definition expr.h:557
GenericConst MakeTensorConst(ffi::Tensor data, ffi::Optional< Type > ty_annotation=std::nullopt, Location loc=UnknownLoc())
Construct a tensor constant, inferring shape and dtype when type is omitted.
Expr GetShapeOf(const Expr &expr)
Get the shape of Expr.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
TVM_TIRX_DLL PrimExpr equal(PrimExpr a, PrimExpr b, Location loc=UnknownLoc())
equal