26#ifndef TVM_TIRX_STMT_FUNCTOR_H_
27#define TVM_TIRX_STMT_FUNCTOR_H_
35#include <unordered_map>
50template <
typename FType>
53template <
typename R,
typename... Args>
67 return Dispatch(node, std::forward<Args>(args)...);
70 TVM_FFI_INLINE
virtual R
Dispatch(
const Stmt& node, Args... args) {
71 TVM_FFI_ICHECK(node.defined()) <<
"Cannot dispatch a null statement";
73 return (*vtable_)(node,
this, std::forward<Args>(args)...);
74 }
catch (ffi::Error& error) {
75 ffi::detail::UpdateVisitErrorContext(&error, node);
81 return DispatchDefault_(node, std::forward<Args>(args)...);
84 return DispatchDefault_(node, std::forward<Args>(args)...);
87 return DispatchDefault_(node, std::forward<Args>(args)...);
90 return DispatchDefault_(node, std::forward<Args>(args)...);
93 return DispatchDefault_(node, std::forward<Args>(args)...);
96 return DispatchDefault_(node, std::forward<Args>(args)...);
99 return DispatchDefault_(node, std::forward<Args>(args)...);
102 return DispatchDefault_(node, std::forward<Args>(args)...);
105 return DispatchDefault_(node, std::forward<Args>(args)...);
108 return DispatchDefault_(node, std::forward<Args>(args)...);
111 return DispatchDefault_(node, std::forward<Args>(args)...);
114 return DispatchDefault_(node, std::forward<Args>(args)...);
118 TVM_FFI_THROW(InternalError) <<
"Do not have a default for " << node->GetTypeKey();
119 TVM_FFI_UNREACHABLE();
129 vtable->template SetDispatch<StmtObj>(
130 [](
const ffi::ObjectRef& node,
TSelf* self, Args... args) -> R {
131 return self->DispatchDefault_(node.get(), std::forward<Args>(args)...);
133 SetDispatch<TSelf, BindObj>(vtable);
134 SetDispatch<TSelf, RegionStmtObj>(vtable);
135 SetDispatch<TSelf, IfObj>(vtable);
136 SetDispatch<TSelf, ForObj>(vtable);
137 SetDispatch<TSelf, WhileObj>(vtable);
138 SetDispatch<TSelf, ReturnObj>(vtable);
139 SetDispatch<TSelf, BreakObj>(vtable);
140 SetDispatch<TSelf, ContinueObj>(vtable);
141 SetDispatch<TSelf, TensorStoreObj>(vtable);
142 SetDispatch<TSelf, AssertStmtObj>(vtable);
143 SetDispatch<TSelf, SeqStmtObj>(vtable);
144 SetDispatch<TSelf, EvaluateObj>(vtable);
147 template <
typename Self,
typename Node>
149 vtable->template SetDispatch<Node>(
150 [](
const ffi::ObjectRef& node,
TSelf* self, Args... args) -> R {
151 return static_cast<Self*>(self)->Dispatch_(static_cast<const Node*>(node.get()),
152 std::forward<Args>(args)...);
157 static const VTable* GlobalVTable() {
158 static const VTable table = [] {
166 const VTable* vtable_;
240 InplaceMode inplace_mode = InplaceMode::kDisallow) {
241 return ffi::detail::UnchangedOrUnsafe::MoveFromTVMFFIAny<Stmt>(
242 ffi::detail::UnchangedOrUnsafe::MoveToTVMFFIAny(Mutate(ffi::AnyView(stmt), inplace_mode)));
247 InplaceMode inplace_mode = InplaceMode::kDisallow) {
248 auto result = Mutate(
static_cast<const Stmt&
>(stmt), inplace_mode);
249 if (result.UnchangedOrSameAs(stmt))
return ffi::Unchanged();
250 return SeqStmt(std::move(result).ValueUnchecked());
253 TVM_FFI_INLINE UnchangedOr<ffi::Optional<SeqStmt>>
Mutate(
254 const ffi::Optional<SeqStmt>& stmt, InplaceMode inplace_mode = InplaceMode::kDisallow) {
255 if (!stmt.has_value())
return ffi::Unchanged();
256 auto result = Mutate(stmt.value(), inplace_mode);
257 if (result.IsUnchanged())
return ffi::Unchanged();
258 return ffi::Optional<SeqStmt>(std::move(result).ValueUnchecked());
262 TVM_FFI_INLINE UnchangedOr<ffi::Optional<Stmt>>
Mutate(
263 const ffi::Optional<Stmt>& stmt, InplaceMode inplace_mode = InplaceMode::kDisallow) {
264 if (!stmt.has_value())
return ffi::Unchanged();
265 auto result = Mutate(stmt.value(), inplace_mode);
266 if (result.IsUnchanged())
return ffi::Unchanged();
267 return ffi::Optional<Stmt>(std::move(result).ValueUnchecked());
272 virtual UnchangedOr<Stmt>
Mutate_(
const IfObj* op, InplaceMode inplace_mode);
273 virtual UnchangedOr<Stmt>
Mutate_(
const ForObj* op, InplaceMode inplace_mode);
302 Stmt stmt, ffi::TypedFunction<ffi::Optional<PrimExpr>(
const Var&)> vmap);
314 PrimExpr expr, ffi::TypedFunction<ffi::Optional<PrimExpr>(
const Var&)> vmap);
326template <
typename Node,
typename = std::enable_if_t<std::is_base_of_v<StmtObj, Node>>>
330 ffi::Optional<VisitInterrupt> Visit(ffi::AnyView value)
final {
331 if (contains_node || value.as<
ExprObj>()) {
334 if (value.as<Node>()) {
335 contains_node =
true;
338 return StmtExprVisitor::Visit(value);
341 bool contains_node{
false};
344 auto visitor = ffi::make_object<Visitor>();
345 visitor->Visit(stmt);
346 return visitor->contains_node;
Native visiting and mutation of core IR expressions.
#define TVM_TIRX_DLL
Definition base.h:55
TIRx statements and shared statement entry header.
Assert condition, if an error occurs, return the error message.
Definition stmt.h:267
Bind a variable to a value in the enclosing scope.
Definition stmt.h:134
A Break in control flow.
Definition stmt.h:621
Call corresponds to callable invocation.
Definition expr.h:788
A Continue in control flow.
Definition stmt.h:648
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition stmt.h:366
Structural Expr mutator with native overrides for core expressions and structural fallback for other ...
Definition expr_functor.h:436
virtual UnchangedOr< Expr > Mutate_(const OpaqueExprObj *node, InplaceMode inplace_mode)
TVM_FFI_INLINE UnchangedOr< PrimExpr > Mutate(const PrimExpr &expr, InplaceMode inplace_mode=InplaceMode::kDisallow)
Mutate a primitive expression, preserving its expression category.
Definition expr_functor.h:451
Base type of all the expressions.
Definition expr.h:66
Structural Expr visitor with native overrides for core expressions.
Definition expr_functor.h:341
virtual ffi::Optional< VisitInterrupt > Visit(ffi::AnyView value)
Construct a visitor with the core expression hooks.
Definition object_functor.h:242
virtual ffi::Optional< VisitInterrupt > Visit_(const OpaqueExprObj *node)
A for loop, with possible type annotations.
Definition stmt.h:471
If statement.
Definition stmt.h:401
A dynamically dispatched functor on the type of the first argument.
Definition object_functor.h:85
Base node for opaque construction-time expressions.
Definition expr.h:136
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
A single-body statement whose semantics are defined by an operator. Operands are evaluated in the enc...
Definition stmt.h:188
A return from the current function.
Definition stmt.h:587
The container of seq statement. Represent a sequence of statements.
Definition stmt.h:74
Sequence statement.
Definition stmt.h:91
Container of all statements.
Definition stmt.h:61
Load a value from an indexed expression source.
Definition expr.h:497
Store a primitive value to an indexed destination expression.
Definition stmt.h:315
Get the index-th field out of a tuple.
Definition expr.h:459
Tuple container.
Definition expr.h:429
A local variable in the IR.
Definition expr.h:552
Managed reference to VarObj.
Definition expr.h:571
A While loop.
Definition stmt.h:549
Create a vector where all the elements are value.
Definition vector_expr.h:82
Let binding. Bind var to value then evaluate body.
Definition expr.h:727
Construct a vector with lanes elements where its i-th element equals base + i * stride....
Definition vector_expr.h:45
Shuffle instruction. vec = concat(vectors) result = (vec[indices[0]], vec[indices[1]] ....
Definition vector_expr.h:119
Mutator that recursively mutates stmts and exprs on them.
Definition stmt_functor.h:226
virtual UnchangedOr< Stmt > Mutate_(const TensorStoreObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const BindObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const IfObj *op, InplaceMode inplace_mode)
TVM_FFI_INLINE UnchangedOr< Stmt > Mutate(const Stmt &stmt, InplaceMode inplace_mode=InplaceMode::kDisallow)
Mutate a borrowed statement through the virtual generic entry.
Definition stmt_functor.h:239
static void InitVTable(VTable *vtable)
virtual UnchangedOr< Stmt > Mutate_(const AssertStmtObj *op, InplaceMode inplace_mode)
TVM_FFI_INLINE UnchangedOr< ffi::Optional< SeqStmt > > Mutate(const ffi::Optional< SeqStmt > &stmt, InplaceMode inplace_mode=InplaceMode::kDisallow)
Definition stmt_functor.h:253
StmtExprMutator(const VTable *vtable)
Definition stmt_functor.h:284
virtual UnchangedOr< Stmt > Mutate_(const RegionStmtObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const ContinueObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const WhileObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const BreakObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const ForObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const EvaluateObj *op, InplaceMode inplace_mode)
TVM_FFI_INLINE UnchangedOr< ffi::Optional< Stmt > > Mutate(const ffi::Optional< Stmt > &stmt, InplaceMode inplace_mode=InplaceMode::kDisallow)
Mutate an optional statement, preserving absence.
Definition stmt_functor.h:262
virtual UnchangedOr< Stmt > Mutate_(const ReturnObj *op, InplaceMode inplace_mode)
virtual UnchangedOr< Stmt > Mutate_(const SeqStmtObj *op, InplaceMode inplace_mode)
TVM_FFI_INLINE UnchangedOr< SeqStmt > Mutate(const SeqStmt &stmt, InplaceMode inplace_mode=InplaceMode::kDisallow)
Mutate a scope body without collapsing its sequence container.
Definition stmt_functor.h:246
Native visitor for TIRx statements and their expression operands.
Definition stmt_functor.h:181
virtual ffi::Optional< VisitInterrupt > Visit_(const AssertStmtObj *op)
ffi::Optional< VisitInterrupt > Visit_(const TupleObj *op) override
ffi::Optional< VisitInterrupt > Visit_(const prim::RampObj *op) override
static void InitVTable(VTable *vtable)
ffi::Optional< VisitInterrupt > Visit_(const prim::LetObj *op) override
ffi::Optional< VisitInterrupt > Visit_(const VarObj *op) override
ffi::Optional< VisitInterrupt > Visit_(const prim::BroadcastObj *op) override
virtual ffi::Optional< VisitInterrupt > Visit_(const IfObj *op)
ffi::Optional< VisitInterrupt > Visit_(const TensorLoadObj *op) override
virtual ffi::Optional< VisitInterrupt > Visit_(const RegionStmtObj *op)
virtual ffi::Optional< VisitInterrupt > Visit_(const WhileObj *op)
ffi::Optional< VisitInterrupt > Visit_(const CallObj *op) override
virtual ffi::Optional< VisitInterrupt > Visit_(const ContinueObj *op)
virtual ffi::Optional< VisitInterrupt > Visit_(const BindObj *op)
virtual ffi::Optional< VisitInterrupt > Visit_(const ForObj *op)
ffi::Optional< VisitInterrupt > Visit_(const prim::ShuffleObj *op) override
ffi::Optional< VisitInterrupt > Visit_(const OpaqueExprObj *op) override
virtual ffi::Optional< VisitInterrupt > Visit_(const BreakObj *op)
ffi::Optional< VisitInterrupt > Visit_(const TupleGetItemObj *op) override
virtual ffi::Optional< VisitInterrupt > Visit_(const SeqStmtObj *op)
virtual ffi::Optional< VisitInterrupt > Visit_(const TensorStoreObj *op)
virtual ffi::Optional< VisitInterrupt > Visit_(const ReturnObj *op)
StmtExprVisitor(const VTable *vtable)
Definition stmt_functor.h:214
virtual ffi::Optional< VisitInterrupt > Visit_(const EvaluateObj *op)
Definition stmt_functor.h:54
virtual R Dispatch_(const BindObj *node, Args... args)
Definition stmt_functor.h:80
virtual R Dispatch_(const BreakObj *node, Args... args)
Definition stmt_functor.h:98
virtual R Dispatch_(const ContinueObj *node, Args... args)
Definition stmt_functor.h:101
virtual R Dispatch_(const SeqStmtObj *node, Args... args)
Definition stmt_functor.h:110
virtual R Dispatch_(const IfObj *node, Args... args)
Definition stmt_functor.h:86
virtual ~StmtFunctor()=default
Destroy through the statement functor base.
R result_type
The result type of this functor.
Definition stmt_functor.h:60
virtual R Dispatch_(const WhileObj *node, Args... args)
Definition stmt_functor.h:92
virtual R Dispatch_(const AssertStmtObj *node, Args... args)
Definition stmt_functor.h:107
static void SetDispatch(VTable *vtable)
Register an additional node hook implemented by Self.
Definition stmt_functor.h:148
StmtFunctor(const VTable *vtable)
Construct with a finalized table that outlives the functor.
Definition stmt_functor.h:126
virtual R DispatchDefault_(const ffi::Object *node, Args...)
Default behavior for statement hooks not overridden by a subclass.
Definition stmt_functor.h:117
TVM_FFI_INLINE R operator()(const Stmt &node, Args... args)
Dispatch a statement, forwarding additional arguments to its hook.
Definition stmt_functor.h:66
static void InitVTable(VTable *vtable)
Register statement hooks in a fresh mutable table.
Definition stmt_functor.h:128
virtual R Dispatch_(const TensorStoreObj *node, Args... args)
Definition stmt_functor.h:104
virtual R Dispatch_(const ForObj *node, Args... args)
Definition stmt_functor.h:89
virtual R Dispatch_(const ReturnObj *node, Args... args)
Definition stmt_functor.h:95
virtual R Dispatch_(const EvaluateObj *node, Args... args)
Definition stmt_functor.h:113
virtual R Dispatch_(const RegionStmtObj *node, Args... args)
Definition stmt_functor.h:83
virtual TVM_FFI_INLINE R Dispatch(const Stmt &node, Args... args)
Dispatch to a node hook, including registered ancestor hooks.
Definition stmt_functor.h:70
StmtFunctor()
Construct a functor with the TIRx statement hooks.
Definition stmt_functor.h:62
Type-dispatched statement functor with a caller-selected signature.
Definition stmt_functor.h:51
TVM_TIRX_DLL Stmt SubstituteWithDataTypeLegalization(Stmt stmt, ffi::TypedFunction< ffi::Optional< PrimExpr >(const Var &)> vmap)
Substitute the var specified by vmap and legalize data types after substitution.
bool ContainsNode(const Stmt &stmt)
Check if the statement contains the specified node type.
Definition stmt_functor.h:327
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Native object dispatch, visiting and mutation with structural fallback.
#define TVM_DEFINE_OBJECT_FUNCTOR_DEFAULT_CONSTRUCTOR(Class, Parent)
Define a default constructor backed by one initialized native dispatch table.
Definition object_functor.h:529