Halide 16.0.0
Halide compiler and libraries
Simplify_Internal.h
Go to the documentation of this file.
1#ifndef HALIDE_SIMPLIFY_VISITORS_H
2#define HALIDE_SIMPLIFY_VISITORS_H
3
4/** \file
5 * The simplifier is separated into multiple compilation units with
6 * this single shared header to speed up the build. This file is not
7 * exported in Halide.h. */
8
9#include "Bounds.h"
10#include "IRMatch.h"
11#include "IRVisitor.h"
12#include "Scope.h"
13
14// Because this file is only included by the simplify methods and
15// doesn't go into Halide.h, we're free to use any old names for our
16// macros.
17
18#define LOG_EXPR_MUTATIONS 0
19#define LOG_STMT_MUTATIONS 0
20
21// On old compilers, some visitors would use large stack frames,
22// because they use expression templates that generate large numbers
23// of temporary objects when they are built and matched against. If we
24// wrap the expressions that imply lots of temporaries in a lambda, we
25// can get these large frames out of the recursive path.
26#define EVAL_IN_LAMBDA(x) (([&]() HALIDE_NEVER_INLINE { return (x); })())
27
28namespace Halide {
29namespace Internal {
30
32 int64_t result;
33 if (mul_with_overflow(64, a, b, &result)) {
34 return result;
35 } else if ((a > 0) == (b > 0)) {
36 return INT64_MAX;
37 } else {
38 return INT64_MIN;
39 }
40}
41
42class Simplify : public VariadicVisitor<Simplify, Expr, Stmt> {
44
45public:
46 Simplify(bool r, const Scope<Interval> *bi, const Scope<ModulusRemainder> *ai);
47
48 struct ExprInfo {
49 // We track constant integer bounds when they exist
50 // TODO: Use ConstantInterval?
51 int64_t min = 0, max = 0;
52 bool min_defined = false, max_defined = false;
53 // And the alignment of integer variables
55
57 if (alignment.modulus == 0) {
58 min_defined = max_defined = true;
60 } else if (alignment.modulus > 1) {
61 if (min_defined) {
63 if (new_min < min) {
64 new_min += alignment.modulus;
65 }
66 min = new_min;
67 }
68 if (max_defined) {
70 if (new_max > max) {
71 new_max -= alignment.modulus;
72 }
73 max = new_max;
74 }
75 }
76
77 if (min_defined && max_defined && min == max) {
80 }
81 }
82
83 // Mix in existing knowledge about this Expr
84 void intersect(const ExprInfo &other) {
85 if (min_defined && other.min_defined) {
86 min = std::max(min, other.min);
87 } else if (other.min_defined) {
88 min_defined = true;
89 min = other.min;
90 }
91
92 if (max_defined && other.max_defined) {
93 max = std::min(max, other.max);
94 } else if (other.max_defined) {
95 max_defined = true;
96 max = other.max;
97 }
98
100
102 }
103 };
104
107 if (b) {
108 *b = ExprInfo{};
109 }
110 }
111
112#if (LOG_EXPR_MUTATIONS || LOG_STMT_MUTATIONS)
113 static int debug_indent;
114#endif
115
116#if LOG_EXPR_MUTATIONS
117 Expr mutate(const Expr &e, ExprInfo *b) {
118 const std::string spaces(debug_indent, ' ');
119 debug(1) << spaces << "Simplifying Expr: " << e << "\n";
120 debug_indent++;
121 Expr new_e = Super::dispatch(e, b);
122 debug_indent--;
123 if (!new_e.same_as(e)) {
124 debug(1)
125 << spaces << "Before: " << e << "\n"
126 << spaces << "After: " << new_e << "\n";
127 }
128 internal_assert(e.type() == new_e.type());
129 return new_e;
130 }
131
132#else
134 Expr mutate(const Expr &e, ExprInfo *b) {
135 // This gets inlined into every call to mutate, so do not add any code here.
136 return Super::dispatch(e, b);
137 }
138#endif
139
140#if LOG_STMT_MUTATIONS
141 Stmt mutate(const Stmt &s) {
142 const std::string spaces(debug_indent, ' ');
143 debug(1) << spaces << "Simplifying Stmt: " << s << "\n";
144 debug_indent++;
145 Stmt new_s = Super::dispatch(s);
146 debug_indent--;
147 if (!new_s.same_as(s)) {
148 debug(1)
149 << spaces << "Before: " << s << "\n"
150 << spaces << "After: " << new_s << "\n";
151 }
152 return new_s;
153 }
154#else
155 Stmt mutate(const Stmt &s) {
156 return Super::dispatch(s);
157 }
158#endif
159
162
164 bool may_simplify(const Type &t) const {
165 return !no_float_simplify || !t.is_float();
166 }
167
168 // Returns true iff t is an integral type where overflow is undefined
171 return t.is_int() && t.bits() >= 32;
172 }
173
176 return t.is_scalar() && no_overflow_int(t);
177 }
178
179 // Returns true iff t does not have a well defined overflow behavior.
182 return t.is_float() || no_overflow_int(t);
183 }
184
185 struct VarInfo {
188 };
189
190 // Tracked for all let vars
192
193 // Only tracked for integer let vars
195
196 // Symbols used by rewrite rules
209
210 // Tracks whether or not we're inside a vector loop. Certain
211 // transformations are not a good idea if the code is to be
212 // vectorized.
213 bool in_vector_loop = false;
214
215 // Tracks whether or not the current IR is unconditionally unreachable.
216 bool in_unreachable = false;
217
218 // If we encounter a reference to a buffer (a Load, Store, Call,
219 // or Provide), there's an implicit dependence on some associated
220 // symbols.
221 void found_buffer_reference(const std::string &name, size_t dimensions = 0);
222
223 // Wrappers for as_const_foo that are more convenient to use in
224 // the large chains of conditions in the visit methods below.
225 bool const_float(const Expr &e, double *f);
226 bool const_int(const Expr &e, int64_t *i);
227 bool const_uint(const Expr &e, uint64_t *u);
228
229 // Put the args to a commutative op in a canonical order
231 bool should_commute(const Expr &a, const Expr &b) {
232 if (a.node_type() < b.node_type()) {
233 return true;
234 }
235 if (a.node_type() > b.node_type()) {
236 return false;
237 }
238
239 if (a.node_type() == IRNodeType::Variable) {
240 const Variable *va = a.as<Variable>();
241 const Variable *vb = b.as<Variable>();
242 return va->name.compare(vb->name) > 0;
243 }
244
245 return false;
246 }
247
248 std::set<Expr, IRDeepCompare> truths, falsehoods;
249
250 struct ScopedFact {
252
253 std::vector<const Variable *> pop_list;
254 std::vector<const Variable *> bounds_pop_list;
255 std::vector<Expr> truths, falsehoods;
256
257 void learn_false(const Expr &fact);
258 void learn_true(const Expr &fact);
261
262 // Replace exprs known to be truths or falsehoods with const_true or const_false.
265
267 : simplify(s) {
268 }
270
271 // allow move but not copy
272 ScopedFact(const ScopedFact &that) = delete;
273 ScopedFact(ScopedFact &&that) = default;
274 };
275
276 // Tell the simplifier to learn from and exploit a boolean
277 // condition, over the lifetime of the returned object.
279 ScopedFact f(this);
280 f.learn_true(fact);
281 return f;
282 }
283
284 // Tell the simplifier to assume a boolean condition is false over
285 // the lifetime of the returned object.
287 ScopedFact f(this);
288 f.learn_false(fact);
289 return f;
290 }
291
293 return mutate(s);
294 }
295 Expr mutate_let_body(const Expr &e, ExprInfo *bounds) {
296 return mutate(e, bounds);
297 }
298
299 template<typename T, typename Body>
300 Body simplify_let(const T *op, ExprInfo *bounds);
301
302 Expr visit(const IntImm *op, ExprInfo *bounds);
303 Expr visit(const UIntImm *op, ExprInfo *bounds);
304 Expr visit(const FloatImm *op, ExprInfo *bounds);
305 Expr visit(const StringImm *op, ExprInfo *bounds);
306 Expr visit(const Broadcast *op, ExprInfo *bounds);
307 Expr visit(const Cast *op, ExprInfo *bounds);
308 Expr visit(const Reinterpret *op, ExprInfo *bounds);
309 Expr visit(const Variable *op, ExprInfo *bounds);
310 Expr visit(const Add *op, ExprInfo *bounds);
311 Expr visit(const Sub *op, ExprInfo *bounds);
312 Expr visit(const Mul *op, ExprInfo *bounds);
313 Expr visit(const Div *op, ExprInfo *bounds);
314 Expr visit(const Mod *op, ExprInfo *bounds);
315 Expr visit(const Min *op, ExprInfo *bounds);
316 Expr visit(const Max *op, ExprInfo *bounds);
317 Expr visit(const EQ *op, ExprInfo *bounds);
318 Expr visit(const NE *op, ExprInfo *bounds);
319 Expr visit(const LT *op, ExprInfo *bounds);
320 Expr visit(const LE *op, ExprInfo *bounds);
321 Expr visit(const GT *op, ExprInfo *bounds);
322 Expr visit(const GE *op, ExprInfo *bounds);
323 Expr visit(const And *op, ExprInfo *bounds);
324 Expr visit(const Or *op, ExprInfo *bounds);
325 Expr visit(const Not *op, ExprInfo *bounds);
326 Expr visit(const Select *op, ExprInfo *bounds);
327 Expr visit(const Ramp *op, ExprInfo *bounds);
329 Expr visit(const Load *op, ExprInfo *bounds);
330 Expr visit(const Call *op, ExprInfo *bounds);
331 Expr visit(const Shuffle *op, ExprInfo *bounds);
332 Expr visit(const VectorReduce *op, ExprInfo *bounds);
333 Expr visit(const Let *op, ExprInfo *bounds);
334 Stmt visit(const LetStmt *op);
336 Stmt visit(const For *op);
337 Stmt visit(const Provide *op);
338 Stmt visit(const Store *op);
339 Stmt visit(const Allocate *op);
340 Stmt visit(const Evaluate *op);
342 Stmt visit(const Block *op);
343 Stmt visit(const Realize *op);
344 Stmt visit(const Prefetch *op);
345 Stmt visit(const Free *op);
346 Stmt visit(const Acquire *op);
347 Stmt visit(const Fork *op);
348 Stmt visit(const Atomic *op);
349
350 std::pair<std::vector<Expr>, bool> mutate_with_changes(const std::vector<Expr> &old_exprs, ExprInfo *bounds);
351};
352
353} // namespace Internal
354} // namespace Halide
355
356#endif
Methods for computing the upper and lower bounds of an expression, and the regions of a function read...
#define internal_assert(c)
Definition: Errors.h:19
#define HALIDE_ALWAYS_INLINE
Definition: HalideRuntime.h:40
Defines a method to match a fragment of IR against a pattern containing wildcards.
Defines the base class for things that recursively walk over the IR.
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition: Scope.h:94
Expr visit(const Min *op, ExprInfo *bounds)
Stmt visit(const ProducerConsumer *op)
HALIDE_ALWAYS_INLINE Expr mutate(const Expr &e, ExprInfo *b)
Scope< ExprInfo > bounds_and_alignment_info
bool const_uint(const Expr &e, uint64_t *u)
IRMatcher::WildConst< 5 > c5
void found_buffer_reference(const std::string &name, size_t dimensions=0)
Expr visit(const Cast *op, ExprInfo *bounds)
Expr visit(const LT *op, ExprInfo *bounds)
Stmt visit(const Block *op)
Expr visit(const VectorReduce *op, ExprInfo *bounds)
Stmt visit(const AssertStmt *op)
Expr visit(const UIntImm *op, ExprInfo *bounds)
Stmt mutate(const Stmt &s)
HALIDE_ALWAYS_INLINE void clear_bounds_info(ExprInfo *b)
Expr visit(const Load *op, ExprInfo *bounds)
Stmt visit(const Evaluate *op)
Expr visit(const Not *op, ExprInfo *bounds)
Body simplify_let(const T *op, ExprInfo *bounds)
Simplify(bool r, const Scope< Interval > *bi, const Scope< ModulusRemainder > *ai)
Stmt visit(const Prefetch *op)
HALIDE_ALWAYS_INLINE bool no_overflow(Type t)
Expr visit(const Div *op, ExprInfo *bounds)
IRMatcher::WildConst< 1 > c1
std::pair< std::vector< Expr >, bool > mutate_with_changes(const std::vector< Expr > &old_exprs, ExprInfo *bounds)
Expr visit(const Let *op, ExprInfo *bounds)
Expr visit(const Reinterpret *op, ExprInfo *bounds)
Expr visit(const And *op, ExprInfo *bounds)
Stmt visit(const IfThenElse *op)
Expr visit(const NE *op, ExprInfo *bounds)
Expr visit(const FloatImm *op, ExprInfo *bounds)
Expr visit(const Shuffle *op, ExprInfo *bounds)
Expr visit(const Add *op, ExprInfo *bounds)
IRMatcher::WildConst< 0 > c0
ScopedFact scoped_truth(const Expr &fact)
IRMatcher::WildConst< 3 > c3
IRMatcher::WildConst< 2 > c2
Expr visit(const Ramp *op, ExprInfo *bounds)
Expr visit(const IntImm *op, ExprInfo *bounds)
Expr visit(const Max *op, ExprInfo *bounds)
Expr visit(const Variable *op, ExprInfo *bounds)
HALIDE_ALWAYS_INLINE bool may_simplify(const Type &t) const
Stmt visit(const For *op)
Stmt visit(const Atomic *op)
bool const_float(const Expr &e, double *f)
Expr visit(const GT *op, ExprInfo *bounds)
Stmt visit(const Provide *op)
Expr visit(const Sub *op, ExprInfo *bounds)
Expr visit(const LE *op, ExprInfo *bounds)
Expr visit(const Call *op, ExprInfo *bounds)
Stmt mutate_let_body(const Stmt &s, ExprInfo *)
Stmt visit(const Acquire *op)
Expr visit(const Broadcast *op, ExprInfo *bounds)
Stmt visit(const Fork *op)
HALIDE_ALWAYS_INLINE bool no_overflow_int(Type t)
Expr visit(const StringImm *op, ExprInfo *bounds)
std::set< Expr, IRDeepCompare > truths
Expr visit(const Select *op, ExprInfo *bounds)
ScopedFact scoped_falsehood(const Expr &fact)
HALIDE_ALWAYS_INLINE bool should_commute(const Expr &a, const Expr &b)
Expr visit(const Or *op, ExprInfo *bounds)
Expr visit(const Mul *op, ExprInfo *bounds)
Expr mutate_let_body(const Expr &e, ExprInfo *bounds)
Stmt visit(const Store *op)
Expr visit(const Mod *op, ExprInfo *bounds)
HALIDE_ALWAYS_INLINE bool no_overflow_scalar_int(Type t)
bool const_int(const Expr &e, int64_t *i)
Stmt visit(const Free *op)
IRMatcher::WildConst< 4 > c4
Stmt visit(const Allocate *op)
Stmt visit(const Realize *op)
Stmt visit(const LetStmt *op)
std::set< Expr, IRDeepCompare > falsehoods
Expr visit(const EQ *op, ExprInfo *bounds)
Expr visit(const GE *op, ExprInfo *bounds)
A visitor/mutator capable of passing arbitrary arguments to the visit methods using CRTP and returnin...
Definition: IRVisitor.h:159
HALIDE_ALWAYS_INLINE Stmt dispatch(const Stmt &s, Args &&...args)
Definition: IRVisitor.h:330
For optional debugging during codegen, use the debug class as follows:
Definition: Debug.h:49
T mod_imp(T a, T b)
Implementations of division and mod that are specific to Halide.
Definition: IROperator.h:239
HALIDE_MUST_USE_RESULT bool mul_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
int64_t saturating_mul(int64_t a, int64_t b)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:584
Expr max(const FuncRef &a, const FuncRef &b)
Definition: Func.h:587
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
A fragment of Halide syntax.
Definition: Expr.h:257
HALIDE_ALWAYS_INLINE Type type() const
Get the type of this expression node.
Definition: Expr.h:321
The sum of two expressions.
Definition: IR.h:48
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:363
Logical and - are both expressions true.
Definition: IR.h:167
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition: IR.h:286
Lock all the Store nodes in the body statement.
Definition: IR.h:911
A sequence of statements to be executed in-order.
Definition: IR.h:434
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:251
A function call.
Definition: IR.h:482
The actual IR nodes begin here.
Definition: IR.h:29
The ratio of two expressions.
Definition: IR.h:75
Is the first expression equal to the second.
Definition: IR.h:113
Evaluate and discard an expression, presumably because it has some side-effect.
Definition: IR.h:468
Floating point constants.
Definition: Expr.h:235
A for loop.
Definition: IR.h:788
A pair of statements executed concurrently.
Definition: IR.h:449
Free the resources associated with the given buffer.
Definition: IR.h:405
Is the first expression greater than or equal to the second.
Definition: IR.h:158
Is the first expression greater than the second.
Definition: IR.h:149
const T * as() const
Downcast this ir node to its actual type (e.g.
Definition: Expr.h:204
IRNodeType node_type() const
Definition: Expr.h:211
An if-then-else block.
Definition: IR.h:458
Integer constants.
Definition: Expr.h:217
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:140
Is the first expression less than the second.
Definition: IR.h:131
A let expression, like you might find in a functional language.
Definition: IR.h:263
The statement form of a let node.
Definition: IR.h:274
Load a value from a named symbol if predicate is true.
Definition: IR.h:209
The greater of two values.
Definition: IR.h:104
The lesser of two values.
Definition: IR.h:95
The remainder of a / b.
Definition: IR.h:86
The result of modulus_remainder analysis.
static ModulusRemainder intersect(const ModulusRemainder &a, const ModulusRemainder &b)
The product of two expressions.
Definition: IR.h:66
Is the first expression not equal to the second.
Definition: IR.h:122
Logical not - true if the expression false.
Definition: IR.h:185
Logical or - is at least one of the expression true.
Definition: IR.h:176
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition: IR.h:888
This node is a helpful annotation to do with permissions.
Definition: IR.h:307
This defines the value of a function at a multi-dimensional location.
Definition: IR.h:346
A linear ramp vector node.
Definition: IR.h:239
Allocate a multi-dimensional buffer of the given type and size.
Definition: IR.h:419
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Definition: IR.h:39
A ternary operator.
Definition: IR.h:196
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:819
void intersect(const ExprInfo &other)
ScopedFact(ScopedFact &&that)=default
void learn_false(const Expr &fact)
std::vector< const Variable * > bounds_pop_list
ScopedFact(const ScopedFact &that)=delete
std::vector< const Variable * > pop_list
void learn_lower_bound(const Variable *v, int64_t val)
void learn_upper_bound(const Variable *v, int64_t val)
A reference-counted handle to a statement node.
Definition: Expr.h:418
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:325
String constants.
Definition: Expr.h:244
The difference of two expressions.
Definition: IR.h:57
Unsigned integer constants.
Definition: Expr.h:226
A named variable.
Definition: IR.h:741
std::string name
Definition: IR.h:742
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition: IR.h:929
Types in the halide type system.
Definition: Type.h:276
HALIDE_ALWAYS_INLINE bool is_int() const
Is this type a signed integer type?
Definition: Type.h:424
HALIDE_ALWAYS_INLINE int bits() const
Return the bit size of a single element of this type.
Definition: Type.h:338
HALIDE_ALWAYS_INLINE bool is_scalar() const
Is this type a scalar type? (lanes() == 1).
Definition: Type.h:406
HALIDE_ALWAYS_INLINE bool is_float() const
Is this type a floating point type (float or double).
Definition: Type.h:412