Halide  12.0.1
Halide compiler and libraries
IRMutator.h
Go to the documentation of this file.
1 #ifndef HALIDE_IR_MUTATOR_H
2 #define HALIDE_IR_MUTATOR_H
3 
4 /** \file
5  * Defines a base class for passes over the IR that modify it
6  */
7 
8 #include <map>
9 
10 #include "IR.h"
11 
12 namespace Halide {
13 namespace Internal {
14 
15 /** A base class for passes over the IR which modify it
16  * (e.g. replacing a variable with a value (Substitute.h), or
17  * constant-folding).
18  *
19  * Your mutator should override the visit() methods you care about and return
20  * the new expression or stmt. The default implementations recursively
21  * mutate their children. To mutate sub-expressions and sub-statements you
22  * should override the mutate() method, which will dispatch to
23  * the appropriate visit() method and then return the value of expr or
24  * stmt after the call to visit.
25  */
26 class IRMutator {
27 public:
28  IRMutator() = default;
29  virtual ~IRMutator() = default;
30 
31  /** This is the main interface for using a mutator. Also call
32  * these in your subclass to mutate sub-expressions and
33  * sub-statements.
34  */
35  virtual Expr mutate(const Expr &expr);
36  virtual Stmt mutate(const Stmt &stmt);
37 
38 protected:
39  // ExprNode<> and StmtNode<> are allowed to call visit (to implement mutate_expr/mutate_stmt())
40  template<typename T>
41  friend struct ExprNode;
42  template<typename T>
43  friend struct StmtNode;
44 
45  virtual Expr visit(const IntImm *);
46  virtual Expr visit(const UIntImm *);
47  virtual Expr visit(const FloatImm *);
48  virtual Expr visit(const StringImm *);
49  virtual Expr visit(const Cast *);
50  virtual Expr visit(const Variable *);
51  virtual Expr visit(const Add *);
52  virtual Expr visit(const Sub *);
53  virtual Expr visit(const Mul *);
54  virtual Expr visit(const Div *);
55  virtual Expr visit(const Mod *);
56  virtual Expr visit(const Min *);
57  virtual Expr visit(const Max *);
58  virtual Expr visit(const EQ *);
59  virtual Expr visit(const NE *);
60  virtual Expr visit(const LT *);
61  virtual Expr visit(const LE *);
62  virtual Expr visit(const GT *);
63  virtual Expr visit(const GE *);
64  virtual Expr visit(const And *);
65  virtual Expr visit(const Or *);
66  virtual Expr visit(const Not *);
67  virtual Expr visit(const Select *);
68  virtual Expr visit(const Load *);
69  virtual Expr visit(const Ramp *);
70  virtual Expr visit(const Broadcast *);
71  virtual Expr visit(const Call *);
72  virtual Expr visit(const Let *);
73  virtual Expr visit(const Shuffle *);
74  virtual Expr visit(const VectorReduce *);
75 
76  virtual Stmt visit(const LetStmt *);
77  virtual Stmt visit(const AssertStmt *);
78  virtual Stmt visit(const ProducerConsumer *);
79  virtual Stmt visit(const For *);
80  virtual Stmt visit(const Store *);
81  virtual Stmt visit(const Provide *);
82  virtual Stmt visit(const Allocate *);
83  virtual Stmt visit(const Free *);
84  virtual Stmt visit(const Realize *);
85  virtual Stmt visit(const Block *);
86  virtual Stmt visit(const IfThenElse *);
87  virtual Stmt visit(const Evaluate *);
88  virtual Stmt visit(const Prefetch *);
89  virtual Stmt visit(const Acquire *);
90  virtual Stmt visit(const Fork *);
91  virtual Stmt visit(const Atomic *);
92 };
93 
94 /** A mutator that caches and reapplies previously-done mutations, so
95  * that it can handle graphs of IR that have not had CSE done to
96  * them. */
97 class IRGraphMutator : public IRMutator {
98 protected:
99  std::map<Expr, Expr, ExprCompare> expr_replacements;
100  std::map<Stmt, Stmt, Stmt::Compare> stmt_replacements;
101 
102 public:
103  Stmt mutate(const Stmt &s) override;
104  Expr mutate(const Expr &e) override;
105 };
106 
107 /** A helper function for mutator-like things to mutate regions */
108 template<typename Mutator, typename... Args>
109 std::pair<Region, bool> mutate_region(Mutator *mutator, const Region &bounds, Args &&...args) {
110  Region new_bounds(bounds.size());
111  bool bounds_changed = false;
112 
113  for (size_t i = 0; i < bounds.size(); i++) {
114  Expr old_min = bounds[i].min;
115  Expr old_extent = bounds[i].extent;
116  Expr new_min = mutator->mutate(old_min, std::forward<Args>(args)...);
117  Expr new_extent = mutator->mutate(old_extent, std::forward<Args>(args)...);
118  if (!new_min.same_as(old_min)) {
119  bounds_changed = true;
120  }
121  if (!new_extent.same_as(old_extent)) {
122  bounds_changed = true;
123  }
124  new_bounds[i] = Range(new_min, new_extent);
125  }
126  return {new_bounds, bounds_changed};
127 }
128 
129 } // namespace Internal
130 } // namespace Halide
131 
132 #endif
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
A mutator that caches and reapplies previously-done mutations, so that it can handle graphs of IR tha...
Definition: IRMutator.h:97
std::map< Expr, Expr, ExprCompare > expr_replacements
Definition: IRMutator.h:99
Expr mutate(const Expr &e) override
This is the main interface for using a mutator.
Stmt mutate(const Stmt &s) override
std::map< Stmt, Stmt, Stmt::Compare > stmt_replacements
Definition: IRMutator.h:100
A base class for passes over the IR which modify it (e.g.
Definition: IRMutator.h:26
virtual Stmt visit(const Free *)
virtual Expr visit(const Broadcast *)
virtual Expr visit(const VectorReduce *)
virtual Expr visit(const Call *)
virtual Stmt visit(const Atomic *)
virtual Expr visit(const Let *)
virtual Stmt visit(const Provide *)
virtual Expr visit(const Ramp *)
virtual Expr visit(const Mod *)
virtual Expr visit(const LE *)
virtual Stmt visit(const Store *)
virtual Expr visit(const Max *)
virtual Expr visit(const Cast *)
virtual Stmt mutate(const Stmt &stmt)
virtual Stmt visit(const Block *)
virtual Expr visit(const Load *)
virtual Stmt visit(const Fork *)
virtual Stmt visit(const Allocate *)
virtual Stmt visit(const LetStmt *)
virtual Expr visit(const Or *)
virtual Expr visit(const And *)
virtual Stmt visit(const Acquire *)
virtual Stmt visit(const ProducerConsumer *)
virtual Expr visit(const Variable *)
virtual Expr visit(const Shuffle *)
virtual Expr visit(const LT *)
virtual Stmt visit(const For *)
virtual ~IRMutator()=default
virtual Expr visit(const Min *)
virtual Stmt visit(const AssertStmt *)
virtual Expr visit(const EQ *)
virtual Expr visit(const GT *)
virtual Expr visit(const Not *)
virtual Expr visit(const Add *)
virtual Expr visit(const NE *)
virtual Expr visit(const Div *)
virtual Expr visit(const Sub *)
virtual Stmt visit(const Evaluate *)
virtual Stmt visit(const Prefetch *)
virtual Stmt visit(const Realize *)
virtual Expr visit(const IntImm *)
virtual Expr mutate(const Expr &expr)
This is the main interface for using a mutator.
virtual Expr visit(const GE *)
virtual Expr visit(const UIntImm *)
virtual Stmt visit(const IfThenElse *)
virtual Expr visit(const FloatImm *)
virtual Expr visit(const Select *)
virtual Expr visit(const Mul *)
virtual Expr visit(const StringImm *)
std::pair< Region, bool > mutate_region(Mutator *mutator, const Region &bounds, Args &&...args)
A helper function for mutator-like things to mutate regions.
Definition: IRMutator.h:109
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
std::vector< Range > Region
A multi-dimensional box.
Definition: Expr.h:343
A fragment of Halide syntax.
Definition: Expr.h:256
The sum of two expressions.
Definition: IR.h:38
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Logical and - are both expressions true.
Definition: IR.h:157
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition: IR.h:276
Lock all the Store nodes in the body statement.
Definition: IR.h:853
A sequence of statements to be executed in-order.
Definition: IR.h:417
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:241
A function call.
Definition: IR.h:464
The actual IR nodes begin here.
Definition: IR.h:29
The ratio of two expressions.
Definition: IR.h:65
Is the first expression equal to the second.
Definition: IR.h:103
Evaluate and discard an expression, presumably because it has some side-effect.
Definition: IR.h:450
We use the "curiously recurring template pattern" to avoid duplicated code in the IR Nodes.
Definition: Expr.h:156
Floating point constants.
Definition: Expr.h:234
A for loop.
Definition: IR.h:730
A pair of statements executed concurrently.
Definition: IR.h:431
Free the resources associated with the given buffer.
Definition: IR.h:388
Is the first expression greater than or equal to the second.
Definition: IR.h:148
Is the first expression greater than the second.
Definition: IR.h:139
An if-then-else block.
Definition: IR.h:440
Integer constants.
Definition: Expr.h:216
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition: IntrusivePtr.h:168
Is the first expression less than or equal to the second.
Definition: IR.h:130
Is the first expression less than the second.
Definition: IR.h:121
A let expression, like you might find in a functional language.
Definition: IR.h:253
The statement form of a let node.
Definition: IR.h:264
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
The greater of two values.
Definition: IR.h:94
The lesser of two values.
Definition: IR.h:85
The remainder of a / b.
Definition: IR.h:76
The product of two expressions.
Definition: IR.h:56
Is the first expression not equal to the second.
Definition: IR.h:112
Logical not - true if the expression false.
Definition: IR.h:175
Logical or - is at least one of the expression true.
Definition: IR.h:166
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition: IR.h:830
This node is a helpful annotation to do with permissions.
Definition: IR.h:297
This defines the value of a function at a multi-dimensional location.
Definition: IR.h:336
A linear ramp vector node.
Definition: IR.h:229
Allocate a multi-dimensional buffer of the given type and size.
Definition: IR.h:402
A ternary operator.
Definition: IR.h:186
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:761
A reference-counted handle to a statement node.
Definition: Expr.h:413
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
String constants.
Definition: Expr.h:243
The difference of two expressions.
Definition: IR.h:47
Unsigned integer constants.
Definition: Expr.h:225
A named variable.
Definition: IR.h:683
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition: IR.h:871
A single-dimensional span.
Definition: Expr.h:335