26 #ifndef TVM_TIR_STMT_FUNCTOR_H_ 27 #define TVM_TIR_STMT_FUNCTOR_H_ 35 #include <unordered_map> 45 template <
typename FType>
48 #define STMT_FUNCTOR_DEFAULT \ 49 { return VisitStmtDefault_(op, std::forward<Args>(args)...); } 51 #define IR_STMT_FUNCTOR_DISPATCH(OP) \ 52 vtable.template set_dispatch<OP>([](const ObjectRef& n, TSelf* self, Args... args) { \ 53 return self->VisitStmt_(static_cast<const OP*>(n.get()), std::forward<Args>(args)...); \ 56 template <
typename R,
typename... Args>
73 R
operator()(
const Stmt& n, Args... args) {
return VisitStmt(n, std::forward<Args>(args)...); }
81 static FType vtable = InitVTable();
82 return vtable(n,
this, std::forward<Args>(args)...);
104 LOG(FATAL) <<
"Do not have a default for " << op->
GetTypeKey();
109 static FType InitVTable() {
133 #undef IR_STMT_FUNCTOR_DISPATCH 134 #undef STMT_FUNCTOR_DEFAULT 141 using StmtFunctor::operator();
144 using StmtFunctor::VisitStmt;
157 void VisitStmt_(
const ForNode* op)
override;
158 void VisitStmt_(
const WhileNode* op)
override;
170 void VisitStmt_(
const BlockNode* op)
override;
188 allow_copy_on_write_ =
true;
189 return VisitStmt(stmt);
203 bool allow_copy_on_write_{
false};
213 template <
typename TNode>
215 static_assert(std::is_base_of<StmtNode, TNode>::value,
216 "StmtMutator:: CopyOnWrite requires us to track uniqueness of all parent " 217 "nodes during the recursion. Because the child classes do not necessarily " 218 "check the Array, Expr and other structures during the visit, it is only safe to " 219 "call this function with StmtNodes for now. " 220 "Please create a new node directly in other cases.");
221 if (allow_copy_on_write_) {
223 return runtime::GetObjectPtr<TNode>(
const_cast<TNode*
>(node));
227 return runtime::make_object<TNode>(*node);
237 if (allow_copy_on_write_ && !stmt.
unique()) {
238 allow_copy_on_write_ =
false;
239 Stmt ret = StmtFunctor::VisitStmt(stmt);
240 allow_copy_on_write_ =
true;
243 return StmtFunctor::VisitStmt(stmt);
286 std::function<
Stmt(
const Stmt&)> fmutate =
nullptr);
297 using StmtVisitor::operator();
298 using ExprVisitor::operator();
301 using ExprVisitor::VisitExpr;
302 using StmtVisitor::VisitStmt;
312 using StmtMutator::operator();
313 using ExprMutator::operator();
316 using ExprMutator::VisitExpr;
371 template <
typename T>
373 return arr.
Map([&vmap](
const auto& elem) {
return Substitute(elem, vmap); });
398 template <
typename Obj>
401 return Substitute(std::forward<Obj>(obj), func);
413 template <
typename Obj,
typename Expr,
414 typename = std::enable_if_t<std::is_base_of_v<PrimExpr, Expr>>>
417 if (
auto opt = vmap.
Get(var)) {
423 return Substitute(std::forward<Obj>(obj), func);
435 template <
typename Obj,
typename Expr,
436 typename = std::enable_if_t<std::is_base_of_v<PrimExpr, Expr>>>
437 auto Substitute(Obj&& obj,
const std::unordered_map<const VarNode*, Expr>& vmap) {
439 if (
auto it = vmap.find(var.
get()); it != vmap.end()) {
445 return Substitute(std::forward<Obj>(obj), func);
457 template <
typename Obj,
typename Expr,
typename Hasher,
typename EqualityChecker,
458 typename = std::enable_if_t<std::is_base_of_v<PrimExpr, Expr>>>
459 auto Substitute(Obj&& obj,
const std::unordered_map<Var, Expr, Hasher, EqualityChecker>& vmap) {
461 if (
auto it = vmap.find(var); it != vmap.end()) {
467 return Substitute(std::forward<Obj>(obj), func);
479 template <
typename Obj,
typename Expr,
480 typename = std::enable_if_t<std::is_base_of_v<PrimExpr, Expr>>>
481 auto Substitute(Obj&& obj,
const std::unordered_map<IterVar, Expr>& iter_vmap) {
482 std::unordered_map<const VarNode*, PrimExpr> vmap;
483 for (
const auto& [iter_var, expr] : iter_vmap) {
484 vmap[iter_var->var.get()] = expr;
488 if (
auto it = vmap.find(var.
get()); it != vmap.end()) {
494 return Substitute(std::forward<Obj>(obj), func);
531 const std::function<
bool(
const ObjectRef&)>& fvisit);
552 template <
typename Node,
typename = std::enable_if_t<std::is_base_of_v<StmtNode, Node>>>
556 void VisitStmt(
const Stmt& stmt)
final {
560 StmtVisitor::VisitStmt(stmt);
563 void VisitStmt_(
const Node* block)
override { contains_node =
true; }
565 bool contains_node{
false};
570 return visitor.contains_node;
576 #endif // TVM_TIR_STMT_FUNCTOR_H_ virtual R VisitStmt_(const ProducerRealizeNode *op, Args... args)
Definition: stmt_functor.h:97
virtual R VisitStmt_(const LetStmtNode *op, Args... args)
Definition: stmt_functor.h:85
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Define certain auxiliary attribute for the body to be a symbolic value. This provide auxiliary inform...
Definition: stmt.h:120
Store value into mult-dimensional array that will be read by the consumer of the producer.
Definition: stmt.h:344
A prefetch hint for a buffer.
Definition: stmt.h:1023
Declare a buffer that can be used in the body.
Definition: stmt.h:628
virtual R VisitStmt_(const SeqStmtNode *op, Args... args)
Definition: stmt_functor.h:99
void VisitExpr(const PrimExpr &e) override
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:304
A custom smart pointer for Object.
Definition: object.h:358
StmtMutator that mutates the statements.
Definition: stmt_functor.h:177
A block realization node represents execution of the block at the binding values. ...
Definition: stmt.h:1275
virtual PrimExpr VisitExpr(const PrimExpr &e)
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:253
Visitor that recursively visit stmts and exprs on them.
Definition: stmt_functor.h:295
virtual R VisitStmt_(const WhileNode *op, Args... args)
Definition: stmt_functor.h:89
virtual R VisitStmt_(const AssertStmtNode *op, Args... args)
Definition: stmt_functor.h:95
static Range FromMinExtent(PrimExpr min, PrimExpr extent, Span span=Span())
construct a new range with min and extent The corresponding constructor is removed, because that is counter convention of tradition meaning of range(begin, end)
virtual R VisitStmt_(const AllocateConstNode *op, Args... args)
Definition: stmt_functor.h:91
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
ExprVisitor.
Definition: expr_functor.h:206
virtual void VisitExpr(const PrimExpr &e)
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:152
The container of seq statement. Represent a sequence of statements.
Definition: stmt.h:666
a named variable in TIR
Definition: var.h:88
IfThenElse statment.
Definition: stmt.h:778
PrimExpr VisitExpr(const PrimExpr &e) override
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:319
Same as ExprFunctor except it is applied on statements.
Definition: stmt_functor.h:46
virtual ~StmtFunctor()
virtual destructor
Definition: stmt_functor.h:66
Optional< V > Get(const K &key) const
Definition: map.h:1385
base class of all object containers.
Definition: object.h:167
Stmt operator()(Stmt stmt)
Mutate stmt.
Definition: stmt_functor.h:187
void PostOrderVisit(const ObjectRef &node, std::function< void(const ObjectRef &)> fvisit)
Recursively visit the ir in post DFS order node, apply fvisit Each node is guaranteed to be visited o...
Stmt VisitStmt(const Stmt &stmt) override
Internal mutator that everyone calls.
Definition: stmt_functor.h:236
PrimFunc RenewDefs(const PrimFunc &func)
Renew the definition nodes for a TIR, including Var, Buffer and IterVar. This pass works as a simple ...
#define IR_STMT_FUNCTOR_DISPATCH(OP)
Definition: stmt_functor.h:51
Functors for tir expressions.
IndexMap Substitute(const IndexMap &index_map, std::function< Optional< PrimExpr >(const Var &var)> f_subst)
Substitute variables in an index map.
R operator()(const Stmt &n, Args... args)
Same as call.
Definition: stmt_functor.h:73
Annotate the bounds where the data produced by the producer need to be written and read in body...
Definition: stmt.h:399
virtual R VisitStmt_(const ProducerStoreNode *op, Args... args)
Definition: stmt_functor.h:96
Range constainer.
Definition: expr.h:715
Stmt SubstituteWithDataTypeLegalization(Stmt stmt, std::function< Optional< PrimExpr >(const Var &)> vmap)
Substitute the var specified by vmap and legalize data types after substitution.
T value() const
Definition: optional.h:92
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Managed reference to PrimFuncNode.
Definition: function.h:145
A While loop.
Definition: stmt.h:982
virtual R VisitStmt(const Stmt &n, Args... args)
The functor call.
Definition: stmt_functor.h:80
virtual R VisitStmtDefault_(const Object *op, Args...)
Definition: stmt_functor.h:103
Container of all statements.
Definition: stmt.h:59
R result_type
the result type of this functor
Definition: stmt_functor.h:64
virtual R VisitStmt_(const DeclBufferNode *op, Args... args)
Definition: stmt_functor.h:92
ObjectPtr< TNode > CopyOnWrite(const TNode *node)
Perform copy on write on node.
Definition: stmt_functor.h:214
bool ContainsNode(const Stmt &stmt)
Check if the statement contains the specified node type.
Definition: stmt_functor.h:553
Allocate a buffer that can be used in body.
Definition: stmt.h:455
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition: stmt.h:828
bool unique() const
Definition: object.h:550
virtual R VisitStmt_(const BufferRealizeNode *op, Args... args)
Definition: stmt_functor.h:94
virtual R VisitStmt_(const PrefetchNode *op, Args... args)
Definition: stmt_functor.h:98
const VarNode * get() const
Get pointer to the internal value.
Definition: var.h:128
A block is a basic schedule unit in TIR.
Definition: stmt.h:1191
virtual R VisitStmt_(const AttrStmtNode *op, Args... args)
Definition: stmt_functor.h:86
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
Allocate a buffer that can be used in body.
Definition: stmt.h:537
Defines the Functor data structures.
Base class of all object reference.
Definition: object.h:511
std::string GetTypeKey() const
Definition: object.h:180
Definition: stmt_functor.h:57
virtual R VisitStmt_(const IfThenElseNode *op, Args... args)
Definition: stmt_functor.h:87
Mutator that recursively mutates stmts and exprs on them.
Definition: stmt_functor.h:310
tvm::RelayExpr Expr
Definition: expr.h:54
Store value to the high dimension buffer.
Definition: stmt.h:226
virtual R VisitStmt_(const BlockRealizeNode *op, Args... args)
Definition: stmt_functor.h:102
virtual R VisitStmt_(const ForNode *op, Args... args)
Definition: stmt_functor.h:88
Assert condition, if an error occurs, return the error message.
Definition: stmt.h:170
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Stmt IRTransform(Stmt stmt, const runtime::PackedFunc &preorder, const runtime::PackedFunc &postorder, Optional< Array< String >> only_enable=NullOpt)
recursively visit the ir nodes in post DFS order, and transform it
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
void PreOrderVisit(const ObjectRef &stmt_or_expr, const std::function< bool(const ObjectRef &)> &fvisit)
Recursively visit the IR in pre DFS order node, apply fvisit. If fvisit returns false, it won't visit the children of the node.
A for loop, with poissible type annotations.
Definition: stmt.h:900
#define STMT_FUNCTOR_DEFAULT
Definition: stmt_functor.h:48
virtual R VisitStmt_(const BufferStoreNode *op, Args... args)
Definition: stmt_functor.h:93
Let binding, bind var to value, then run body.
Definition: stmt.h:67
Reference to PrimExprNode.
Definition: expr.h:114
StmtVisitor.
Definition: stmt_functor.h:139
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
Annotate the region where the buffer need to be read and write in the body. We only need to allocate ...
Definition: stmt.h:281
virtual R VisitStmt_(const EvaluateNode *op, Args... args)
Definition: stmt_functor.h:100
Array< U > Map(F fmap) const
Helper function to apply a map function onto the array.
Definition: array.h:651
virtual R VisitStmt_(const BlockNode *op, Args... args)
Definition: stmt_functor.h:101
virtual R VisitStmt_(const AllocateNode *op, Args... args)
Definition: stmt_functor.h:90
ExprMutator that mutates expressions.
Definition: expr_functor.h:252