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