Loading...
Searching...
No Matches
IRMutator.h
Go to the documentation of this file.
#ifndef HALIDE_IR_MUTATOR_H
#define HALIDE_IR_MUTATOR_H
/** \file
* Defines a base class for passes over the IR that modify it
*/
#include <map>
#include "IR.h"
namespace Halide {
namespace Internal {
/** A base class for passes over the IR which modify it
* (e.g. replacing a variable with a value (Substitute.h), or
* constant-folding).
*
* Your mutator should override the visit() methods you care about and return
* the new expression or stmt. The default implementations recursively
* mutate their children. To mutate sub-expressions and sub-statements you
* should override the mutate() method, which will dispatch to
* the appropriate visit() method and then return the value of expr or
* stmt after the call to visit.
*/
class IRMutator {
public:
IRMutator() = default;
virtual ~IRMutator() = default;
/** This is the main interface for using a mutator. Also call
* these in your subclass to mutate sub-expressions and
* sub-statements.
*/
virtual Expr mutate(const Expr &expr);
virtual Stmt mutate(const Stmt &stmt);
// Mutate all the Exprs and return the new list in ret, along with
// a flag that is true iff at least one item in the list changed.
std::pair<std::vector<Expr>, bool> mutate_with_changes(const std::vector<Expr> &);
// Like mutate_with_changes, but discard the changes flag.
std::vector<Expr> mutate(const std::vector<Expr> &exprs) {
return mutate_with_changes(exprs).first;
}
protected:
// ExprNode<> and StmtNode<> are allowed to call visit (to implement mutate_expr/mutate_stmt())
template<typename T>
friend struct ExprNode;
template<typename T>
friend struct StmtNode;
virtual Expr visit(const IntImm *);
virtual Expr visit(const UIntImm *);
virtual Expr visit(const FloatImm *);
virtual Expr visit(const StringImm *);
virtual Expr visit(const Cast *);
virtual Expr visit(const Reinterpret *);
virtual Expr visit(const Variable *);
virtual Expr visit(const Add *);
virtual Expr visit(const Sub *);
virtual Expr visit(const Mul *);
virtual Expr visit(const Div *);
virtual Expr visit(const Mod *);
virtual Expr visit(const Min *);
virtual Expr visit(const Max *);
virtual Expr visit(const EQ *);
virtual Expr visit(const NE *);
virtual Expr visit(const LT *);
virtual Expr visit(const LE *);
virtual Expr visit(const GT *);
virtual Expr visit(const GE *);
virtual Expr visit(const And *);
virtual Expr visit(const Or *);
virtual Expr visit(const Not *);
virtual Expr visit(const Select *);
virtual Expr visit(const Load *);
virtual Expr visit(const Ramp *);
virtual Expr visit(const Broadcast *);
virtual Expr visit(const Call *);
virtual Expr visit(const Let *);
virtual Expr visit(const Shuffle *);
virtual Expr visit(const VectorReduce *);
virtual Stmt visit(const LetStmt *);
virtual Stmt visit(const AssertStmt *);
virtual Stmt visit(const ProducerConsumer *);
virtual Stmt visit(const For *);
virtual Stmt visit(const Store *);
virtual Stmt visit(const Provide *);
virtual Stmt visit(const Allocate *);
virtual Stmt visit(const Free *);
virtual Stmt visit(const Realize *);
virtual Stmt visit(const Block *);
virtual Stmt visit(const IfThenElse *);
virtual Stmt visit(const Evaluate *);
virtual Stmt visit(const Prefetch *);
virtual Stmt visit(const Acquire *);
virtual Stmt visit(const Fork *);
virtual Stmt visit(const Atomic *);
virtual Stmt visit(const HoistedStorage *);
};
/** A mutator that caches and reapplies previously-done mutations, so
* that it can handle graphs of IR that have not had CSE done to
* them. */
class IRGraphMutator : public IRMutator {
protected:
std::map<Expr, Expr, ExprCompare> expr_replacements;
std::map<Stmt, Stmt, Stmt::Compare> stmt_replacements;
public:
Stmt mutate(const Stmt &s) override;
Expr mutate(const Expr &e) override;
std::vector<Expr> mutate(const std::vector<Expr> &exprs) {
return IRMutator::mutate(exprs);
}
};
/** A helper function for mutator-like things to mutate regions */
template<typename Mutator, typename... Args>
std::pair<Region, bool> mutate_region(Mutator *mutator, const Region &bounds, Args &&...args) {
Region new_bounds(bounds.size());
bool bounds_changed = false;
for (size_t i = 0; i < bounds.size(); i++) {
Expr old_min = bounds[i].min;
Expr old_extent = bounds[i].extent;
Expr new_min = mutator->mutate(old_min, args...);
Expr new_extent = mutator->mutate(old_extent, args...);
if (!new_min.same_as(old_min)) {
bounds_changed = true;
}
if (!new_extent.same_as(old_extent)) {
bounds_changed = true;
}
new_bounds[i] = Range(new_min, new_extent);
}
return {new_bounds, bounds_changed};
}
} // namespace Internal
} // namespace Halide
#endif
IR.h
Subtypes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt).
Halide::Internal::IRGraphMutator
A mutator that caches and reapplies previously-done mutations, so that it can handle graphs of IR tha...
Definition IRMutator.h:108
Halide::Internal::IRGraphMutator::expr_replacements
std::map< Expr, Expr, ExprCompare > expr_replacements
Definition IRMutator.h:110
Halide::Internal::IRGraphMutator::mutate
Expr mutate(const Expr &e) override
This is the main interface for using a mutator.
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:117
Halide::Internal::IRGraphMutator::stmt_replacements
std::map< Stmt, Stmt, Stmt::Compare > stmt_replacements
Definition IRMutator.h:111
Halide::Internal::IRMutator::mutate
std::vector< Expr > mutate(const std::vector< Expr > &exprs)
Definition IRMutator.h:43
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Free *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Broadcast *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const VectorReduce *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Call *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Atomic *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Let *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Provide *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Ramp *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Mod *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const LE *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Reinterpret *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Store *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Max *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const HoistedStorage *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Cast *)
Halide::Internal::IRMutator::mutate
virtual Stmt mutate(const Stmt &stmt)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Block *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Load *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Fork *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Allocate *)
Halide::Internal::IRMutator::StmtNode
friend struct StmtNode
Definition IRMutator.h:52
Halide::Internal::IRMutator::visit
virtual Stmt visit(const LetStmt *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Or *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const And *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Acquire *)
Halide::Internal::IRMutator::mutate_with_changes
std::pair< std::vector< Expr >, bool > mutate_with_changes(const std::vector< Expr > &)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const ProducerConsumer *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Variable *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Shuffle *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const LT *)
Halide::Internal::IRMutator::IRMutator
IRMutator()=default
Halide::Internal::IRMutator::visit
virtual Stmt visit(const For *)
Halide::Internal::IRMutator::~IRMutator
virtual ~IRMutator()=default
Halide::Internal::IRMutator::visit
virtual Expr visit(const Min *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const AssertStmt *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const EQ *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const GT *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Not *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Add *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const NE *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Div *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Sub *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Evaluate *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Prefetch *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const Realize *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const IntImm *)
Halide::Internal::IRMutator::mutate
virtual Expr mutate(const Expr &expr)
This is the main interface for using a mutator.
Halide::Internal::IRMutator::visit
virtual Expr visit(const GE *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const UIntImm *)
Halide::Internal::IRMutator::visit
virtual Stmt visit(const IfThenElse *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const FloatImm *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Select *)
Halide::Internal::IRMutator::visit
virtual Expr visit(const Mul *)
Halide::Internal::IRMutator::ExprNode
friend struct ExprNode
Definition IRMutator.h:50
Halide::Internal::IRMutator::visit
virtual Expr visit(const StringImm *)
Halide::Internal
Definition AbstractGenerator.h:24
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:124
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::OutputFileType::stmt
@ stmt
Definition Module.h:43
Halide::Region
std::vector< Range > Region
A multi-dimensional box.
Definition Expr.h:350
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::Internal::Acquire
Definition IR.h:872
Halide::Internal::Add
The sum of two expressions.
Definition IR.h:56
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
Halide::Internal::And
Logical and - are both expressions true.
Definition IR.h:175
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:294
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition IR.h:994
Halide::Internal::Block
A sequence of statements to be executed in-order.
Definition IR.h:442
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition IR.h:259
Halide::Internal::Call
A function call.
Definition IR.h:490
Halide::Internal::Cast
The actual IR nodes begin here.
Definition IR.h:30
Halide::Internal::Div
The ratio of two expressions.
Definition IR.h:83
Halide::Internal::EQ
Is the first expression equal to the second.
Definition IR.h:121
Halide::Internal::Evaluate
Evaluate and discard an expression, presumably because it has some side-effect.
Definition IR.h:476
Halide::Internal::FloatImm
Floating point constants.
Definition Expr.h:236
Halide::Internal::For
A for loop.
Definition IR.h:848
Halide::Internal::Fork
A pair of statements executed concurrently.
Definition IR.h:457
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition IR.h:413
Halide::Internal::GE
Is the first expression greater than or equal to the second.
Definition IR.h:166
Halide::Internal::GT
Is the first expression greater than the second.
Definition IR.h:157
Halide::Internal::HoistedStorage
Represents a location where storage will be hoisted to for a Func / Realize node with a given name.
Definition IR.h:978
Halide::Internal::IfThenElse
An if-then-else block.
Definition IR.h:466
Halide::Internal::IntImm
Integer constants.
Definition Expr.h:218
Halide::Internal::IntrusivePtr::same_as
HALIDE_ALWAYS_INLINE bool same_as(const IntrusivePtr &other) const
Definition IntrusivePtr.h:171
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition IR.h:148
Halide::Internal::LT
Is the first expression less than the second.
Definition IR.h:139
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition IR.h:271
Halide::Internal::LetStmt
The statement form of a let node.
Definition IR.h:282
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition IR.h:217
Halide::Internal::Max
The greater of two values.
Definition IR.h:112
Halide::Internal::Min
The lesser of two values.
Definition IR.h:103
Halide::Internal::Mod
The remainder of a / b.
Definition IR.h:94
Halide::Internal::Mul
The product of two expressions.
Definition IR.h:74
Halide::Internal::NE
Is the first expression not equal to the second.
Definition IR.h:130
Halide::Internal::Not
Logical not - true if the expression false.
Definition IR.h:193
Halide::Internal::Or
Logical or - is at least one of the expression true.
Definition IR.h:184
Halide::Internal::Prefetch
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition IR.h:956
Halide::Internal::ProducerConsumer
This node is a helpful annotation to do with permissions.
Definition IR.h:315
Halide::Internal::Provide
This defines the value of a function at a multi-dimensional location.
Definition IR.h:354
Halide::Internal::Ramp
A linear ramp vector node.
Definition IR.h:247
Halide::Internal::Realize
Allocate a multi-dimensional buffer of the given type and size.
Definition IR.h:427
Halide::Internal::Reinterpret
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Definition IR.h:47
Halide::Internal::Select
A ternary operator.
Definition IR.h:204
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition IR.h:884
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
Halide::Internal::StringImm
String constants.
Definition Expr.h:245
Halide::Internal::Sub
The difference of two expressions.
Definition IR.h:65
Halide::Internal::UIntImm
Unsigned integer constants.
Definition Expr.h:227
Halide::Internal::Variable
A named variable.
Definition IR.h:801
Halide::Internal::VectorReduce
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition IR.h:1012
Halide::Range
A single-dimensional span.
Definition Expr.h:342