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 <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  // Mutate all the Exprs and return the new list in ret, along with
39  // a flag that is true iff at least one item in the list changed.
40  std::pair<std::vector<Expr>, bool> mutate_with_changes(const std::vector<Expr> &);
41 
42  // Like mutate_with_changes, but discard the changes flag.
43  std::vector<Expr> mutate(const std::vector<Expr> &exprs) {
44  return mutate_with_changes(exprs).first;
45  }
46 
47 protected:
48  // ExprNode<> and StmtNode<> are allowed to call visit (to implement mutate_expr/mutate_stmt())
49  template<typename T>
50  friend struct ExprNode;
51  template<typename T>
52  friend struct StmtNode;
53 
54  virtual Expr visit(const IntImm *);
55  virtual Expr visit(const UIntImm *);
56  virtual Expr visit(const FloatImm *);
57  virtual Expr visit(const StringImm *);
58  virtual Expr visit(const Cast *);
59  virtual Expr visit(const Reinterpret *);
60  virtual Expr visit(const Variable *);
61  virtual Expr visit(const Add *);
62  virtual Expr visit(const Sub *);
63  virtual Expr visit(const Mul *);
64  virtual Expr visit(const Div *);
65  virtual Expr visit(const Mod *);
66  virtual Expr visit(const Min *);
67  virtual Expr visit(const Max *);
68  virtual Expr visit(const EQ *);
69  virtual Expr visit(const NE *);
70  virtual Expr visit(const LT *);
71  virtual Expr visit(const LE *);
72  virtual Expr visit(const GT *);
73  virtual Expr visit(const GE *);
74  virtual Expr visit(const And *);
75  virtual Expr visit(const Or *);
76  virtual Expr visit(const Not *);
77  virtual Expr visit(const Select *);
78  virtual Expr visit(const Load *);
79  virtual Expr visit(const Ramp *);
80  virtual Expr visit(const Broadcast *);
81  virtual Expr visit(const Call *);
82  virtual Expr visit(const Let *);
83  virtual Expr visit(const Shuffle *);
84  virtual Expr visit(const VectorReduce *);
85 
86  virtual Stmt visit(const LetStmt *);
87  virtual Stmt visit(const AssertStmt *);
88  virtual Stmt visit(const ProducerConsumer *);
89  virtual Stmt visit(const For *);
90  virtual Stmt visit(const Store *);
91  virtual Stmt visit(const Provide *);
92  virtual Stmt visit(const Allocate *);
93  virtual Stmt visit(const Free *);
94  virtual Stmt visit(const Realize *);
95  virtual Stmt visit(const Block *);
96  virtual Stmt visit(const IfThenElse *);
97  virtual Stmt visit(const Evaluate *);
98  virtual Stmt visit(const Prefetch *);
99  virtual Stmt visit(const Acquire *);
100  virtual Stmt visit(const Fork *);
101  virtual Stmt visit(const Atomic *);
102 };
103 
104 /** A mutator that caches and reapplies previously-done mutations, so
105  * that it can handle graphs of IR that have not had CSE done to
106  * them. */
107 class IRGraphMutator : public IRMutator {
108 protected:
109  std::map<Expr, Expr, ExprCompare> expr_replacements;
110  std::map<Stmt, Stmt, Stmt::Compare> stmt_replacements;
111 
112 public:
113  Stmt mutate(const Stmt &s) override;
114  Expr mutate(const Expr &e) override;
115 
116  std::vector<Expr> mutate(const std::vector<Expr> &exprs) {
117  return IRMutator::mutate(exprs);
118  }
119 };
120 
121 /** A helper function for mutator-like things to mutate regions */
122 template<typename Mutator, typename... Args>
123 std::pair<Region, bool> mutate_region(Mutator *mutator, const Region &bounds, Args &&...args) {
124  Region new_bounds(bounds.size());
125  bool bounds_changed = false;
126 
127  for (size_t i = 0; i < bounds.size(); i++) {
128  Expr old_min = bounds[i].min;
129  Expr old_extent = bounds[i].extent;
130  Expr new_min = mutator->mutate(old_min, std::forward<Args>(args)...);
131  Expr new_extent = mutator->mutate(old_extent, std::forward<Args>(args)...);
132  if (!new_min.same_as(old_min)) {
133  bounds_changed = true;
134  }
135  if (!new_extent.same_as(old_extent)) {
136  bounds_changed = true;
137  }
138  new_bounds[i] = Range(new_min, new_extent);
139  }
140  return {new_bounds, bounds_changed};
141 }
142 
143 } // namespace Internal
144 } // namespace Halide
145 
146 #endif
Halide::Internal::Acquire
Definition: IR.h:807
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:363
Halide::Internal::Add
The sum of two expressions.
Definition: IR.h:48
Halide::Region
std::vector< Range > Region
A multi-dimensional box.
Definition: Expr.h:344
Halide::Internal::VectorReduce
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition: IR.h:929
Halide::Internal::GE
Is the first expression greater than or equal to the second.
Definition: IR.h:158
Halide::Internal::For
A for loop.
Definition: IR.h:788
Halide::Internal::IRGraphMutator::stmt_replacements
std::map< Stmt, Stmt, Stmt::Compare > stmt_replacements
Definition: IRMutator.h:110
Halide::Internal::FloatImm
Floating point constants.
Definition: Expr.h:235
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:251
Halide::Internal::Div
The ratio of two expressions.
Definition: IR.h:75
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:123
Halide::Internal::ExprNode
We use the "curiously recurring template pattern" to avoid duplicated code in the IR Nodes.
Definition: Expr.h:157
Halide::Internal::IntImm
Integer constants.
Definition: Expr.h:217
Halide::Internal::LetStmt
The statement form of a let node.
Definition: IR.h:274
IR.h
Halide::Internal::Cast
The actual IR nodes begin here.
Definition: IR.h:29
Halide::Range
A single-dimensional span.
Definition: Expr.h:336
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition: IR.h:140
Halide::Internal::NE
Is the first expression not equal to the second.
Definition: IR.h:122
Halide::Internal::Fork
A pair of statements executed concurrently.
Definition: IR.h:449
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:418
Halide::Internal::IRGraphMutator
A mutator that caches and reapplies previously-done mutations, so that it can handle graphs of IR tha...
Definition: IRMutator.h:107
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition: IR.h:209
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition: IR.h:405
Halide::Internal::Realize
Allocate a multi-dimensional buffer of the given type and size.
Definition: IR.h:419
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::Internal::Or
Logical or - is at least one of the expression true.
Definition: IR.h:176
Halide::Internal::EQ
Is the first expression equal to the second.
Definition: IR.h:113
Halide::Internal::Provide
This defines the value of a function at a multi-dimensional location.
Definition: IR.h:346
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Max
The greater of two values.
Definition: IR.h:104
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition: IR.h:263
Halide::OutputFileType::stmt
@ stmt
Halide::Internal::Ramp
A linear ramp vector node.
Definition: IR.h:239
Halide::Internal::IRGraphMutator::mutate
Stmt mutate(const Stmt &s) override
Halide::Internal::IRGraphMutator::mutate
std::vector< Expr > mutate(const std::vector< Expr > &exprs)
Definition: IRMutator.h:116
Halide::Internal::IRGraphMutator::expr_replacements
std::map< Expr, Expr, ExprCompare > expr_replacements
Definition: IRMutator.h:109
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:468
Halide::Internal::IRMutator::mutate_with_changes
std::pair< std::vector< Expr >, bool > mutate_with_changes(const std::vector< Expr > &)
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:325
Halide::Internal::Variable
A named variable.
Definition: IR.h:741
Halide::Internal::Min
The lesser of two values.
Definition: IR.h:95
Halide::Internal::ProducerConsumer
This node is a helpful annotation to do with permissions.
Definition: IR.h:307
Halide::Internal::Mod
The remainder of a / b.
Definition: IR.h:86
Halide::Internal::IRMutator
A base class for passes over the IR which modify it (e.g.
Definition: IRMutator.h:26
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:286
Halide::Internal::Call
A function call.
Definition: IR.h:482
Halide::Internal::IRMutator::mutate
std::vector< Expr > mutate(const std::vector< Expr > &exprs)
Definition: IRMutator.h:43
Halide::Internal::Reinterpret
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Definition: IR.h:39
Halide::Internal::Select
A ternary operator.
Definition: IR.h:196
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
Halide::Internal::Prefetch
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition: IR.h:888
Halide::Internal::GT
Is the first expression greater than the second.
Definition: IR.h:149
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition: IR.h:911
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:819
Halide::Internal::IRMutator::IRMutator
IRMutator()=default
Halide::Internal::UIntImm
Unsigned integer constants.
Definition: Expr.h:226
Halide::Internal::StmtNode
Definition: Expr.h:167
Halide::Internal::IfThenElse
An if-then-else block.
Definition: IR.h:458
Halide::Internal::Sub
The difference of two expressions.
Definition: IR.h:57
Halide::Internal::And
Logical and - are both expressions true.
Definition: IR.h:167
Halide::Internal::StringImm
String constants.
Definition: Expr.h:244
Halide::Internal::Not
Logical not - true if the expression false.
Definition: IR.h:185
Halide::Internal::LT
Is the first expression less than the second.
Definition: IR.h:131
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:66
Halide::Internal::Block
A sequence of statements to be executed in-order.
Definition: IR.h:434
Halide::Internal::IRMutator::~IRMutator
virtual ~IRMutator()=default
Halide::Internal::IntrusivePtr::same_as
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition: IntrusivePtr.h:168