tvm
stmt_functor.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef TVM_TIR_STMT_FUNCTOR_H_
27 #define TVM_TIR_STMT_FUNCTOR_H_
28 
29 #include <tvm/node/functor.h>
30 #include <tvm/tir/expr.h>
31 #include <tvm/tir/expr_functor.h>
32 #include <tvm/tir/function.h>
33 #include <tvm/tir/stmt.h>
34 
35 #include <unordered_map>
36 #include <utility>
37 
38 namespace tvm {
39 namespace tir {
45 template <typename FType>
47 
48 #define STMT_FUNCTOR_DEFAULT \
49  { return VisitStmtDefault_(op, std::forward<Args>(args)...); }
50 
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)...); \
54  });
55 
56 template <typename R, typename... Args>
57 class StmtFunctor<R(const Stmt& n, Args... args)> {
58  private:
59  using TSelf = StmtFunctor<R(const Stmt& n, Args... args)>;
60  using FType = NodeFunctor<R(const ObjectRef& n, TSelf* self, Args... args)>;
61 
62  public:
64  using result_type = R;
66  virtual ~StmtFunctor() {}
73  R operator()(const Stmt& n, Args... args) { return VisitStmt(n, std::forward<Args>(args)...); }
80  virtual R VisitStmt(const Stmt& n, Args... args) {
81  static FType vtable = InitVTable();
82  return vtable(n, this, std::forward<Args>(args)...);
83  }
84  // Functions that can be overriden by subclass
85  virtual R VisitStmt_(const LetStmtNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
86  virtual R VisitStmt_(const AttrStmtNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
87  virtual R VisitStmt_(const IfThenElseNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
88  virtual R VisitStmt_(const ForNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
89  virtual R VisitStmt_(const WhileNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
90  virtual R VisitStmt_(const AllocateNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
91  virtual R VisitStmt_(const AllocateConstNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
92  virtual R VisitStmt_(const StoreNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
93  virtual R VisitStmt_(const BufferStoreNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
94  virtual R VisitStmt_(const BufferRealizeNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
95  virtual R VisitStmt_(const AssertStmtNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
96  virtual R VisitStmt_(const ProducerStoreNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
97  virtual R VisitStmt_(const ProducerRealizeNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
98  virtual R VisitStmt_(const PrefetchNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
99  virtual R VisitStmt_(const SeqStmtNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
100  virtual R VisitStmt_(const EvaluateNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
101  virtual R VisitStmt_(const BlockNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
102  virtual R VisitStmt_(const BlockRealizeNode* op, Args... args) STMT_FUNCTOR_DEFAULT;
103  virtual R VisitStmtDefault_(const Object* op, Args...) {
104  LOG(FATAL) << "Do not have a default for " << op->GetTypeKey();
105  return R();
106  }
107 
108  private:
109  // initialize the vtable.
110  static FType InitVTable() {
111  FType vtable;
130  return vtable;
131  }
132 };
133 
134 #undef IR_STMT_FUNCTOR_DISPATCH
135 #undef STMT_FUNCTOR_DEFAULT
136 
140 class TVM_DLL StmtVisitor : protected StmtFunctor<void(const Stmt&)> {
141  public:
142  using StmtFunctor::operator();
143 
144  protected:
145  using StmtFunctor::VisitStmt;
153  virtual void VisitExpr(const PrimExpr& e) {}
154  // statement visitor
155  void VisitStmt_(const AttrStmtNode* op) override;
156  void VisitStmt_(const IfThenElseNode* op) override;
157  void VisitStmt_(const LetStmtNode* op) override;
158  void VisitStmt_(const ForNode* op) override;
159  void VisitStmt_(const WhileNode* op) override;
160  void VisitStmt_(const AllocateNode* op) override;
161  void VisitStmt_(const AllocateConstNode* op) override;
162  void VisitStmt_(const StoreNode* op) override;
163  void VisitStmt_(const BufferStoreNode* op) override;
164  void VisitStmt_(const BufferRealizeNode* op) override;
165  void VisitStmt_(const AssertStmtNode* op) override;
166  void VisitStmt_(const ProducerStoreNode* op) override;
167  void VisitStmt_(const ProducerRealizeNode* op) override;
168  void VisitStmt_(const PrefetchNode* op) override;
169  void VisitStmt_(const SeqStmtNode* op) override;
170  void VisitStmt_(const EvaluateNode* op) override;
171  void VisitStmt_(const BlockNode* op) override;
172  void VisitStmt_(const BlockRealizeNode* op) override;
173 };
174 
178 class TVM_DLL StmtMutator : protected StmtFunctor<Stmt(const Stmt&)> {
179  public:
189  allow_copy_on_write_ = true;
190  return VisitStmt(stmt);
191  }
192 
193  protected:
194  // We perform copy on write optimizations on the StmtMutator
195  // so that an unique copy of parent can be mutated inplace
196  // when some of its children changed.
197  // We only do such optimization for Stmt nests(instead of Exprs) for now
198  // as Stmt's parent state is more likely remain unchanged when one of
199  // its child block changes.
204  bool allow_copy_on_write_{false};
214  template <typename TNode>
215  ObjectPtr<TNode> CopyOnWrite(const TNode* node) {
216  static_assert(std::is_base_of<StmtNode, TNode>::value,
217  "StmtMutator:: CopyOnWrite requires us to track uniqueness of all parent "
218  "nodes during the recursion. Because the child classes do not necessarily "
219  "check the Array, Expr and other structures during the visit, it is only safe to "
220  "call this function with StmtNodes for now. "
221  "Please create a new node directly in other cases.");
222  if (allow_copy_on_write_) {
223  // return the old node.
224  return runtime::GetObjectPtr<TNode>(const_cast<TNode*>(node));
225  } else {
226  // Make a new copy of the node.
227  // need to rely on the default copy constructor
228  return runtime::make_object<TNode>(*node);
229  }
230  }
237  Stmt VisitStmt(const Stmt& stmt) override {
238  if (allow_copy_on_write_ && !stmt.unique()) {
239  allow_copy_on_write_ = false;
240  Stmt ret = StmtFunctor::VisitStmt(stmt);
241  allow_copy_on_write_ = true;
242  return ret;
243  } else {
244  return StmtFunctor::VisitStmt(stmt);
245  }
246  }
254  virtual PrimExpr VisitExpr(const PrimExpr& e) { return e; }
255  // statement visitor
256  Stmt VisitStmt_(const AttrStmtNode* op) override;
257  Stmt VisitStmt_(const IfThenElseNode* op) override;
258  Stmt VisitStmt_(const LetStmtNode* op) override;
259  Stmt VisitStmt_(const ForNode* op) override;
260  Stmt VisitStmt_(const WhileNode* op) override;
261  Stmt VisitStmt_(const AllocateNode* op) override;
262  Stmt VisitStmt_(const AllocateConstNode* op) override;
263  Stmt VisitStmt_(const StoreNode* op) override;
264  Stmt VisitStmt_(const BufferStoreNode* op) override;
265  Stmt VisitStmt_(const BufferRealizeNode* op) override;
266  Stmt VisitStmt_(const AssertStmtNode* op) override;
267  Stmt VisitStmt_(const ProducerStoreNode* op) override;
268  Stmt VisitStmt_(const ProducerRealizeNode* op) override;
269  Stmt VisitStmt_(const PrefetchNode* op) override;
270  Stmt VisitStmt_(const SeqStmtNode* op) override;
271  Stmt VisitStmt_(const EvaluateNode* op) override;
272  Stmt VisitStmt_(const BlockNode* op) override;
273  Stmt VisitStmt_(const BlockRealizeNode* op) override;
286  Stmt VisitSeqStmt_(const SeqStmtNode* op, bool flatten_before_visit,
287  std::function<Stmt(const Stmt&)> fmutate = nullptr);
288 
289  // internal helper.
290  class Internal;
291 };
292 
296 class StmtExprVisitor : public StmtVisitor, public ExprVisitor {
297  public:
298  using StmtVisitor::operator();
299  using ExprVisitor::operator();
300 
301  protected:
302  using ExprVisitor::VisitExpr;
303  using StmtVisitor::VisitStmt;
304 
305  void VisitExpr(const PrimExpr& e) override { return ExprVisitor::VisitExpr(e); }
306 };
307 
311 class StmtExprMutator : public StmtMutator, public ExprMutator {
312  public:
313  using StmtMutator::operator();
314  using ExprMutator::operator();
315 
316  protected:
317  using ExprMutator::VisitExpr;
319 
320  PrimExpr VisitExpr(const PrimExpr& e) override { return ExprMutator::VisitExpr(e); }
321 };
322 
338 TVM_DLL Stmt IRTransform(Stmt stmt, const runtime::PackedFunc& preorder,
339  const runtime::PackedFunc& postorder,
340  Optional<Array<String>> only_enable = NullOpt);
341 
348 TVM_DLL void PostOrderVisit(const ObjectRef& node, std::function<void(const ObjectRef&)> fvisit);
349 
356 TVM_DLL Stmt Substitute(Stmt stmt, std::function<Optional<PrimExpr>(const Var& var)> vmap);
357 
364 TVM_DLL PrimExpr Substitute(PrimExpr expr, std::function<Optional<PrimExpr>(const Var& var)> vmap);
365 
372 TVM_DLL Array<Range> Substitute(const Array<Range>& region, const Map<Var, PrimExpr>& vmap);
373 
381 template <typename T>
382 inline auto Substitute(T input, const Map<Var, PrimExpr>& value_map) {
383  auto vmap = [&](const Var& var) -> Optional<PrimExpr> {
384  auto it = value_map.find(var);
385  if (it != value_map.end()) return (*it).second;
386  return Optional<PrimExpr>(nullptr);
387  };
388  return Substitute(std::move(input), vmap);
389 }
390 
398 template <typename T>
399 inline T Substitute(T input, const std::unordered_map<const VarNode*, PrimExpr>& value_map) {
400  auto vmap = [&](const Var& var) -> Optional<PrimExpr> {
401  auto it = value_map.find(var.get());
402  if (it != value_map.end()) return (*it).second;
403  return Optional<PrimExpr>(nullptr);
404  };
405  return Substitute(std::move(input), vmap);
406 }
407 
415 TVM_DLL void PreOrderVisit(const ObjectRef& stmt_or_expr,
416  const std::function<bool(const ObjectRef&)>& fvisit);
417 
425 TVM_DLL PrimFunc RenewDefs(const PrimFunc& func);
426 } // namespace tir
427 } // namespace tvm
428 
429 #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:117
Store value into mult-dimensional array that will be read by the consumer of the producer.
Definition: stmt.h:404
A prefetch hint for a buffer.
Definition: stmt.h:1027
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:305
A custom smart pointer for Object.
Definition: object.h:358
StmtMutator that mutates the statements.
Definition: stmt_functor.h:178
A block realization node represents execution of the block at the binding values. ...
Definition: stmt.h:1277
virtual PrimExpr VisitExpr(const PrimExpr &e)
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:254
Visitor that recursively visit stmts and exprs on them.
Definition: stmt_functor.h:296
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
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:209
virtual void VisitExpr(const PrimExpr &e)
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:153
The container of seq statement. Represent a sequence of statements.
Definition: stmt.h:688
a named variable in TIR
Definition: var.h:88
IfThenElse statment.
Definition: stmt.h:785
PrimExpr VisitExpr(const PrimExpr &e) override
Visitor to Exprs, can be overriden to do recursive changes to Exprs.
Definition: stmt_functor.h:320
Same as ExprFunctor except it is applied on statements.
Definition: stmt_functor.h:46
virtual ~StmtFunctor()
virtual destructor
Definition: stmt_functor.h:66
virtual R VisitStmt_(const StoreNode *op, Args... args)
Definition: stmt_functor.h:92
TIR Function.
base class of all object containers.
Definition: object.h:167
Stmt operator()(Stmt stmt)
Mutate stmt.
Definition: stmt_functor.h:188
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:237
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.
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:458
virtual R VisitStmt_(const ProducerStoreNode *op, Args... args)
Definition: stmt_functor.h:96
TIR statements.
TIR expressions.
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
Managed reference to PrimFuncNode.
Definition: function.h:156
A While loop.
Definition: stmt.h:987
virtual R VisitStmt(const Stmt &n, Args... args)
The functor call.
Definition: stmt_functor.h:80
iterator find(const K &key) const
Definition: map.h:1380
virtual R VisitStmtDefault_(const Object *op, Args...)
Definition: stmt_functor.h:103
Container of all statements.
Definition: stmt.h:57
R result_type
the result type of this functor
Definition: stmt_functor.h:64
ObjectPtr< TNode > CopyOnWrite(const TNode *node)
Perform copy on write on node.
Definition: stmt_functor.h:215
Allocate a buffer that can be used in body.
Definition: stmt.h:513
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition: stmt.h:834
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:1193
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:595
Defines the Functor data structures.
Base class of all object reference.
Definition: object.h:511
Store value to the buffer.
Definition: stmt.h:229
std::string GetTypeKey() const
Definition: object.h:180
virtual R VisitStmt_(const IfThenElseNode *op, Args... args)
Definition: stmt_functor.h:87
Stmt Substitute(Stmt stmt, std::function< Optional< PrimExpr >(const Var &var)> vmap)
Substitute the var specified by vmap.
Mutator that recursively mutates stmts and exprs on them.
Definition: stmt_functor.h:311
Store value to the high dimension buffer.
Definition: stmt.h:286
iterator end() const
Definition: map.h:1378
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:166
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:1268
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&#39;t visit the children of the node.
A for loop, with poissible type annotations.
Definition: stmt.h:905
#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:65
Reference to PrimExprNode.
Definition: expr.h:112
StmtVisitor.
Definition: stmt_functor.h:140
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:341
virtual R VisitStmt_(const EvaluateNode *op, Args... args)
Definition: stmt_functor.h:100
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:256