Halide 20.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
IRPrinter.h
Go to the documentation of this file.
1#ifndef HALIDE_IR_PRINTER_H
2#define HALIDE_IR_PRINTER_H
3
4/** \file
5 * This header file defines operators that let you dump a Halide
6 * expression, statement, or type directly into an output stream
7 * in a human readable form.
8 * E.g:
9 \code
10 Expr foo = ...
11 std::cout << "Foo is " << foo << "\n";
12 \endcode
13 *
14 * These operators are implemented using \ref Halide::Internal::IRPrinter
15 */
16
17#include <ostream>
18
19#include "IRVisitor.h"
20#include "Module.h"
21#include "Scope.h"
22
23namespace Halide {
24
25/** Emit an expression on an output stream (such as std::cout) in
26 * human-readable form */
27std::ostream &operator<<(std::ostream &stream, const Expr &);
28
29/** Emit a halide type on an output stream (such as std::cout) in
30 * human-readable form */
31std::ostream &operator<<(std::ostream &stream, const Type &);
32
33/** Emit a halide Module on an output stream (such as std::cout) in
34 * human-readable form */
35std::ostream &operator<<(std::ostream &stream, const Module &);
36
37/** Emit a halide device api type in human-readable form */
38std::ostream &operator<<(std::ostream &stream, const DeviceAPI &);
39
40/** Emit a halide memory type in human-readable form */
41std::ostream &operator<<(std::ostream &stream, const MemoryType &);
42
43/** Emit a halide tail strategy in human-readable form */
44std::ostream &operator<<(std::ostream &stream, const TailStrategy &);
45
46/** Emit a halide loop partitioning policy in human-readable form */
47std::ostream &operator<<(std::ostream &stream, const Partition &);
48
49/** Emit a halide LoopLevel in human-readable form */
50std::ostream &operator<<(std::ostream &stream, const LoopLevel &);
51
52struct Target;
53/** Emit a halide Target in a human readable form */
54std::ostream &operator<<(std::ostream &stream, const Target &);
55
56namespace Internal {
57
58struct AssociativePattern;
59struct AssociativeOp;
60class Closure;
61struct Interval;
62struct ConstantInterval;
63struct ModulusRemainder;
64enum class IRNodeType;
65
66/** Emit a halide node type on an output stream (such as std::cout) in
67 * human-readable form */
68std::ostream &operator<<(std::ostream &stream, IRNodeType);
69
70/** Emit a halide associative pattern on an output stream (such as std::cout)
71 * in a human-readable form */
72std::ostream &operator<<(std::ostream &stream, const AssociativePattern &);
73
74/** Emit a halide associative op on an output stream (such as std::cout) in a
75 * human-readable form */
76std::ostream &operator<<(std::ostream &stream, const AssociativeOp &);
77
78/** Emit a halide statement on an output stream (such as std::cout) in
79 * a human-readable form */
80std::ostream &operator<<(std::ostream &stream, const Stmt &);
81
82/** Emit a halide for loop type (vectorized, serial, etc) in a human
83 * readable form */
84std::ostream &operator<<(std::ostream &stream, const ForType &);
85
86/** Emit a horizontal vector reduction op in human-readable form. */
87std::ostream &operator<<(std::ostream &stream, const VectorReduce::Operator &);
88
89/** Emit a halide name mangling value in a human readable format */
90std::ostream &operator<<(std::ostream &stream, const NameMangling &);
91
92/** Emit a halide LoweredFunc in a human readable format */
93std::ostream &operator<<(std::ostream &stream, const LoweredFunc &);
94
95/** Emit a halide linkage value in a human readable format */
96std::ostream &operator<<(std::ostream &stream, const LinkageType &);
97
98/** Emit a halide dimension type in human-readable format */
99std::ostream &operator<<(std::ostream &stream, const DimType &);
100
101/** Emit a Closure in human-readable form */
102std::ostream &operator<<(std::ostream &out, const Closure &c);
103
104/** Emit an Interval in human-readable form */
105std::ostream &operator<<(std::ostream &out, const Interval &c);
106
107/** Emit a ConstantInterval in human-readable form */
108std::ostream &operator<<(std::ostream &out, const ConstantInterval &c);
109
110/** Emit a ModulusRemainder in human-readable form */
111std::ostream &operator<<(std::ostream &out, const ModulusRemainder &c);
112
115};
116std::ostream &operator<<(std::ostream &stream, const Indentation &);
117
118template<typename T>
119struct Ansi {
120 const T &cnt;
121 const char *open, *close;
122};
123
124template<typename T>
125std::ostream &operator<<(std::ostream &out, const Ansi<T> &a) {
126 if (a.open) {
127 out << a.open;
128 }
129 out << a.cnt;
130 if (a.close) {
131 out << a.close;
132 }
133 return out;
134}
135
136/** An IRVisitor that emits IR to the given output stream in a human
137 * readable form. Can be subclassed if you want to modify the way in
138 * which it prints.
139 */
140class IRPrinter : public IRVisitor {
141public:
142 /** Construct an IRPrinter pointed at a given output stream
143 * (e.g. std::cout, or a std::ofstream) */
144 explicit IRPrinter(std::ostream &);
145
146 /** emit an expression on the output stream */
147 void print(const Expr &);
148
149 /** Emit an expression on the output stream without enclosing parens */
150 void print_no_parens(const Expr &);
151
152 /** emit a statement on the output stream */
153 void print(const Stmt &);
154
155 /** emit a statement summary on the output stream */
156 void print_summary(const Stmt &);
157
158 /** emit a comma delimited list of exprs, without any leading or
159 * trailing punctuation. */
160 void print_list(const std::vector<Expr> &exprs);
161
162 static void test();
163
164protected:
166 return Indentation{indent};
167 }
168
169 /** The stream on which we're outputting */
170 std::ostream &stream;
171
172 /** The current indentation level, useful for pretty-printing
173 * statements */
174 int indent = 0;
175
176 /** Certain expressions do not need parens around them, e.g. the
177 * args to a call are already separated by commas and a
178 * surrounding set of parens. */
179 bool implicit_parens = false;
180
181 /** Print only a summary of a statement, with sub-statements replaced by
182 * ellipses (...). */
183 bool is_summary = false;
184
185 bool ansi = false;
186 int paren_depth = 0;
187
188 const char *ansi_hl = "";
189 const char *ansi_dim = "";
190 const char *ansi_kw = "";
191 const char *ansi_imm_int = "";
192 const char *ansi_imm_float = "";
193 const char *ansi_imm_str = "";
194 const char *ansi_var = "";
195 const char *ansi_buf = "";
196 const char *ansi_fn = "";
197 const char *ansi_type = "";
198 const char *ansi_reset_col = "";
199 const char *ansi_reset = "";
200
201 // clang-format off
202 template<typename T> Ansi<T> hl(const T &t);
203 template<typename T> Ansi<T> kw(const T &t);
204 template<typename T> Ansi<T> imm_int(const T &t);
205 template<typename T> Ansi<T> imm_float(const T &t);
206 template<typename T> Ansi<T> imm_str(const T &t);
207 template<typename T> Ansi<T> var(const T &t);
208 template<typename T> Ansi<T> buf(const T &t);
209 template<typename T> Ansi<T> fn(const T &t);
210 template<typename T> Ansi<T> type(const T &t);
211 template<typename T> Ansi<T> typep(const T &t);
212 template<typename T> Ansi<T> paren(const T &t, bool bold = true, int d = -1);
213 // clang-format on
214
215 /** Either emits "(" or "", depending on the value of implicit_parens */
216 void open();
217
218 /** Either emits ")" or "", depending on the value of implicit_parens */
219 void close();
220
221 /** Emits "(" always */
222 void openf();
223
224 /** Emits "name(" always */
225 void openf(const char *name);
226
227 /** Emits ")" always */
228 void closef();
229
230 /** The symbols whose types can be inferred from values printed
231 * already. */
233
234 /** A helper for printing a chain of lets with line breaks */
235 void print_lets(const Let *let);
236
237 /** A helper for printing a braced statement */
238 void print_braced_stmt(const Stmt &, int extra_indent = 2);
239
240 void visit(const IntImm *) override;
241 void visit(const UIntImm *) override;
242 void visit(const FloatImm *) override;
243 void visit(const StringImm *) override;
244 void visit(const Cast *) override;
245 void visit(const Reinterpret *) override;
246 void visit(const Variable *) override;
247 void visit(const Add *) override;
248 void visit(const Sub *) override;
249 void visit(const Mul *) override;
250 void visit(const Div *) override;
251 void visit(const Mod *) override;
252 void visit(const Min *) override;
253 void visit(const Max *) override;
254 void visit(const EQ *) override;
255 void visit(const NE *) override;
256 void visit(const LT *) override;
257 void visit(const LE *) override;
258 void visit(const GT *) override;
259 void visit(const GE *) override;
260 void visit(const And *) override;
261 void visit(const Or *) override;
262 void visit(const Not *) override;
263 void visit(const Select *) override;
264 void visit(const Load *) override;
265 void visit(const Ramp *) override;
266 void visit(const Broadcast *) override;
267 void visit(const Call *) override;
268 void visit(const Let *) override;
269 void visit(const LetStmt *) override;
270 void visit(const AssertStmt *) override;
271 void visit(const ProducerConsumer *) override;
272 void visit(const For *) override;
273 void visit(const Acquire *) override;
274 void visit(const Store *) override;
275 void visit(const Provide *) override;
276 void visit(const Allocate *) override;
277 void visit(const Free *) override;
278 void visit(const Realize *) override;
279 void visit(const Block *) override;
280 void visit(const Fork *) override;
281 void visit(const IfThenElse *) override;
282 void visit(const Evaluate *) override;
283 void visit(const Shuffle *) override;
284 void visit(const VectorReduce *) override;
285 void visit(const Prefetch *) override;
286 void visit(const Atomic *) override;
287 void visit(const HoistedStorage *) override;
288};
289
290/** Debugging helpers for LLDB */
291/// @{
292std::string lldb_string(const Expr &);
294std::string lldb_string(const Stmt &);
295/// @}
296
297} // namespace Internal
298} // namespace Halide
299
300#endif
Defines the base class for things that recursively walk over the IR.
Defines Module, an IR container that fully describes a Halide program.
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
A helper class to manage closures.
Definition Closure.h:26
An IRVisitor that emits IR to the given output stream in a human readable form.
Definition IRPrinter.h:140
void visit(const EQ *) override
Ansi< T > var(const T &t)
void visit(const Sub *) override
Ansi< T > buf(const T &t)
void visit(const Provide *) override
void visit(const And *) override
Scope known_type
The symbols whose types can be inferred from values printed already.
Definition IRPrinter.h:232
void visit(const GE *) override
void visit(const Div *) override
void visit(const Free *) override
void visit(const Or *) override
int indent
The current indentation level, useful for pretty-printing statements.
Definition IRPrinter.h:174
Ansi< T > imm_int(const T &t)
Ansi< T > type(const T &t)
void print(const Expr &)
emit an expression on the output stream
void visit(const Load *) override
void visit(const UIntImm *) override
void print_no_parens(const Expr &)
Emit an expression on the output stream without enclosing parens.
void print_lets(const Let *let)
A helper for printing a chain of lets with line breaks.
Ansi< T > typep(const T &t)
Ansi< T > imm_str(const T &t)
void visit(const Variable *) override
void visit(const Max *) override
void visit(const Block *) override
Ansi< T > imm_float(const T &t)
void visit(const Reinterpret *) override
Ansi< T > kw(const T &t)
void visit(const Add *) override
Ansi< T > fn(const T &t)
void visit(const Ramp *) override
void print_list(const std::vector< Expr > &exprs)
emit a comma delimited list of exprs, without any leading or trailing punctuation.
void visit(const Acquire *) override
void closef()
Emits ")" always.
void visit(const AssertStmt *) override
Indentation get_indent() const
Definition IRPrinter.h:165
void visit(const Mul *) override
void print_summary(const Stmt &)
emit a statement summary on the output stream
void visit(const Prefetch *) override
void openf(const char *name)
Emits "name(" always.
void visit(const Atomic *) override
void openf()
Emits "(" always.
bool implicit_parens
Certain expressions do not need parens around them, e.g.
Definition IRPrinter.h:179
void visit(const Cast *) override
void visit(const VectorReduce *) override
void visit(const LetStmt *) override
void visit(const StringImm *) override
void visit(const Realize *) override
void visit(const LE *) override
void visit(const Broadcast *) override
void visit(const Evaluate *) override
std::ostream & stream
The stream on which we're outputting.
Definition IRPrinter.h:170
void visit(const Select *) override
void visit(const FloatImm *) override
bool is_summary
Print only a summary of a statement, with sub-statements replaced by ellipses (......
Definition IRPrinter.h:183
void visit(const IntImm *) override
Ansi< T > hl(const T &t)
IRPrinter(std::ostream &)
Construct an IRPrinter pointed at a given output stream (e.g.
void visit(const LT *) override
void visit(const For *) override
void visit(const Store *) override
void visit(const Mod *) override
void print_braced_stmt(const Stmt &, int extra_indent=2)
A helper for printing a braced statement.
void close()
Either emits ")" or "", depending on the value of implicit_parens.
void visit(const ProducerConsumer *) override
void visit(const HoistedStorage *) override
void visit(const Shuffle *) override
void visit(const Not *) override
void visit(const GT *) override
void visit(const NE *) override
Ansi< T > paren(const T &t, bool bold=true, int d=-1)
void print(const Stmt &)
emit a statement on the output stream
void visit(const Min *) override
void open()
Either emits "(" or "", depending on the value of implicit_parens.
void visit(const IfThenElse *) override
void visit(const Call *) override
void visit(const Fork *) override
void visit(const Allocate *) override
void visit(const Let *) override
A base class for algorithms that need to recursively walk over the IR.
Definition IRVisitor.h:19
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
A reference to a site in a Halide statement at the top of the body of a particular for loop.
Definition Schedule.h:203
DimType
Each Dim below has a dim_type, which tells you what transformations are legal on it.
Definition Schedule.h:352
ForType
An enum describing a type of loop traversal.
Definition Expr.h:406
std::string lldb_string(const Expr &)
Debugging helpers for LLDB.
ConstantInterval operator<<(const ConstantInterval &a, const ConstantInterval &b)
IRNodeType
All our IR node types get unique IDs for the purposes of RTTI.
Definition Expr.h:25
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition Module.h:52
@ Internal
Not visible externally, similar to 'static' linkage in C.
TailStrategy
Different ways to handle a tail case in a split when the factor does not provably divide the extent.
Definition Schedule.h:33
std::ostream & operator<<(std::ostream &stream, const Expr &)
Emit an expression on an output stream (such as std::cout) in human-readable form.
NameMangling
An enum to specify calling convention for extern stages.
Definition Function.h:26
DeviceAPI
An enum describing a type of device API.
Definition DeviceAPI.h:15
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition Expr.h:353
Partition
Different ways to handle loops with a potentially optimizable boundary conditions.
A fragment of Halide syntax.
Definition Expr.h:258
The sum of two expressions.
Definition IR.h:56
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
Logical and - are both expressions true.
Definition IR.h:175
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition IR.h:294
Represent the equivalent associative op of an update definition.
Represent an associative op with its identity.
Lock all the Store nodes in the body statement.
Definition IR.h:994
A base class for expression nodes.
Definition Expr.h:143
A sequence of statements to be executed in-order.
Definition IR.h:442
A vector with 'lanes' elements, in which every element is 'value'.
Definition IR.h:259
A function call.
Definition IR.h:490
The actual IR nodes begin here.
Definition IR.h:30
A class to represent ranges of integers.
The ratio of two expressions.
Definition IR.h:83
Is the first expression equal to the second.
Definition IR.h:121
Evaluate and discard an expression, presumably because it has some side-effect.
Definition IR.h:476
Floating point constants.
Definition Expr.h:236
A for loop.
Definition IR.h:848
A pair of statements executed concurrently.
Definition IR.h:457
Free the resources associated with the given buffer.
Definition IR.h:413
Is the first expression greater than or equal to the second.
Definition IR.h:166
Is the first expression greater than the second.
Definition IR.h:157
Represents a location where storage will be hoisted to for a Func / Realize node with a given name.
Definition IR.h:978
An if-then-else block.
Definition IR.h:466
Integer constants.
Definition Expr.h:218
A class to represent ranges of Exprs.
Definition Interval.h:14
Is the first expression less than or equal to the second.
Definition IR.h:148
Is the first expression less than the second.
Definition IR.h:139
A let expression, like you might find in a functional language.
Definition IR.h:271
The statement form of a let node.
Definition IR.h:282
Load a value from a named symbol if predicate is true.
Definition IR.h:217
Definition of a lowered function.
Definition Module.h:101
The greater of two values.
Definition IR.h:112
The lesser of two values.
Definition IR.h:103
The remainder of a / b.
Definition IR.h:94
The result of modulus_remainder analysis.
The product of two expressions.
Definition IR.h:74
Is the first expression not equal to the second.
Definition IR.h:130
Logical not - true if the expression false.
Definition IR.h:193
Logical or - is at least one of the expression true.
Definition IR.h:184
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition IR.h:956
This node is a helpful annotation to do with permissions.
Definition IR.h:315
This defines the value of a function at a multi-dimensional location.
Definition IR.h:354
A linear ramp vector node.
Definition IR.h:247
Allocate a multi-dimensional buffer of the given type and size.
Definition IR.h:427
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Definition IR.h:47
A ternary operator.
Definition IR.h:204
Construct a new vector by taking elements from another sequence of vectors.
Definition IR.h:884
A reference-counted handle to a statement node.
Definition Expr.h:427
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
String constants.
Definition Expr.h:245
The difference of two expressions.
Definition IR.h:65
Unsigned integer constants.
Definition Expr.h:227
A named variable.
Definition IR.h:801
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition IR.h:1012
A struct representing a target machine and os to generate code for.
Definition Target.h:19