Halide  12.0.1
Halide compiler and libraries
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 
23 namespace Halide {
24 
25 /** Emit an expression on an output stream (such as std::cout) in
26  * human-readable form */
27 std::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 */
31 std::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 */
35 std::ostream &operator<<(std::ostream &stream, const Module &);
36 
37 /** Emit a halide device api type in human-readable form */
38 std::ostream &operator<<(std::ostream &stream, const DeviceAPI &);
39 
40 /** Emit a halide memory type in human-readable form */
41 std::ostream &operator<<(std::ostream &stream, const MemoryType &);
42 
43 /** Emit a halide tail strategy in human-readable form */
44 std::ostream &operator<<(std::ostream &stream, const TailStrategy &t);
45 
46 /** Emit a halide LoopLevel in human-readable form */
47 std::ostream &operator<<(std::ostream &stream, const LoopLevel &);
48 
49 struct Target;
50 /** Emit a halide Target in a human readable form */
51 std::ostream &operator<<(std::ostream &stream, const Target &);
52 
53 namespace Internal {
54 
55 struct AssociativePattern;
56 struct AssociativeOp;
57 
58 /** Emit a halide associative pattern on an output stream (such as std::cout)
59  * in a human-readable form */
60 std::ostream &operator<<(std::ostream &stream, const AssociativePattern &);
61 
62 /** Emit a halide associative op on an output stream (such as std::cout) in a
63  * human-readable form */
64 std::ostream &operator<<(std::ostream &stream, const AssociativeOp &);
65 
66 /** Emit a halide statement on an output stream (such as std::cout) in
67  * a human-readable form */
68 std::ostream &operator<<(std::ostream &stream, const Stmt &);
69 
70 /** Emit a halide for loop type (vectorized, serial, etc) in a human
71  * readable form */
72 std::ostream &operator<<(std::ostream &stream, const ForType &);
73 
74 /** Emit a horizontal vector reduction op in human-readable form. */
75 std::ostream &operator<<(std::ostream &stream, const VectorReduce::Operator &);
76 
77 /** Emit a halide name mangling value in a human readable format */
78 std::ostream &operator<<(std::ostream &stream, const NameMangling &);
79 
80 /** Emit a halide LoweredFunc in a human readable format */
81 std::ostream &operator<<(std::ostream &stream, const LoweredFunc &);
82 
83 /** Emit a halide linkage value in a human readable format */
84 std::ostream &operator<<(std::ostream &stream, const LinkageType &);
85 
86 /** Emit a halide dimension type in human-readable format */
87 std::ostream &operator<<(std::ostream &stream, const DimType &);
88 
89 struct Indentation {
90  int indent;
91 };
92 std::ostream &operator<<(std::ostream &stream, const Indentation &);
93 
94 /** An IRVisitor that emits IR to the given output stream in a human
95  * readable form. Can be subclassed if you want to modify the way in
96  * which it prints.
97  */
98 class IRPrinter : public IRVisitor {
99 public:
100  /** Construct an IRPrinter pointed at a given output stream
101  * (e.g. std::cout, or a std::ofstream) */
102  explicit IRPrinter(std::ostream &);
103 
104  /** emit an expression on the output stream */
105  void print(const Expr &);
106 
107  /** Emit an expression on the output stream without enclosing parens */
108  void print_no_parens(const Expr &);
109 
110  /** emit a statement on the output stream */
111  void print(const Stmt &);
112 
113  /** emit a comma delimited list of exprs, without any leading or
114  * trailing punctuation. */
115  void print_list(const std::vector<Expr> &exprs);
116 
117  static void test();
118 
119 protected:
121  return Indentation{indent};
122  }
123 
124  /** The stream on which we're outputting */
125  std::ostream &stream;
126 
127  /** The current indentation level, useful for pretty-printing
128  * statements */
129  int indent = 0;
130 
131  /** Certain expressions do not need parens around them, e.g. the
132  * args to a call are already separated by commas and a
133  * surrounding set of parens. */
134  bool implicit_parens = false;
135 
136  /** Either emits "(" or "", depending on the value of implicit_parens */
137  void open();
138 
139  /** Either emits ")" or "", depending on the value of implicit_parens */
140  void close();
141 
142  /** The symbols whose types can be inferred from values printed
143  * already. */
145 
146  /** A helper for printing a chain of lets with line breaks */
147  void print_lets(const Let *let);
148 
149  void visit(const IntImm *) override;
150  void visit(const UIntImm *) override;
151  void visit(const FloatImm *) override;
152  void visit(const StringImm *) override;
153  void visit(const Cast *) override;
154  void visit(const Variable *) override;
155  void visit(const Add *) override;
156  void visit(const Sub *) override;
157  void visit(const Mul *) override;
158  void visit(const Div *) override;
159  void visit(const Mod *) override;
160  void visit(const Min *) override;
161  void visit(const Max *) override;
162  void visit(const EQ *) override;
163  void visit(const NE *) override;
164  void visit(const LT *) override;
165  void visit(const LE *) override;
166  void visit(const GT *) override;
167  void visit(const GE *) override;
168  void visit(const And *) override;
169  void visit(const Or *) override;
170  void visit(const Not *) override;
171  void visit(const Select *) override;
172  void visit(const Load *) override;
173  void visit(const Ramp *) override;
174  void visit(const Broadcast *) override;
175  void visit(const Call *) override;
176  void visit(const Let *) override;
177  void visit(const LetStmt *) override;
178  void visit(const AssertStmt *) override;
179  void visit(const ProducerConsumer *) override;
180  void visit(const For *) override;
181  void visit(const Acquire *) override;
182  void visit(const Store *) override;
183  void visit(const Provide *) override;
184  void visit(const Allocate *) override;
185  void visit(const Free *) override;
186  void visit(const Realize *) override;
187  void visit(const Block *) override;
188  void visit(const Fork *) override;
189  void visit(const IfThenElse *) override;
190  void visit(const Evaluate *) override;
191  void visit(const Shuffle *) override;
192  void visit(const VectorReduce *) override;
193  void visit(const Prefetch *) override;
194  void visit(const Atomic *) override;
195 };
196 
197 } // namespace Internal
198 } // namespace Halide
199 
200 #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.
An IRVisitor that emits IR to the given output stream in a human readable form.
Definition: IRPrinter.h:98
void visit(const EQ *) override
void visit(const Sub *) override
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:144
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:129
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.
void visit(const Variable *) override
void visit(const Max *) override
void visit(const Block *) override
void visit(const Add *) override
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 visit(const AssertStmt *) override
Indentation get_indent() const
Definition: IRPrinter.h:120
void visit(const Mul *) override
void visit(const Prefetch *) override
void visit(const Atomic *) override
bool implicit_parens
Certain expressions do not need parens around them, e.g.
Definition: IRPrinter.h:134
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:125
void visit(const Select *) override
void visit(const FloatImm *) override
void visit(const IntImm *) override
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 close()
Either emits ")" or "", depending on the value of implicit_parens.
void visit(const ProducerConsumer *) override
void visit(const Shuffle *) override
void visit(const Not *) override
void visit(const GT *) override
void visit(const NE *) override
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:153
DimType
Each Dim below has a dim_type, which tells you what transformations are legal on it.
Definition: Schedule.h:303
ForType
An enum describing a type of loop traversal.
Definition: Expr.h:395
std::ostream & operator<<(std::ostream &stream, const Stmt &)
Emit a halide statement on an output stream (such as std::cout) in a human-readable form.
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:48
@ 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:32
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:24
DeviceAPI
An enum describing a type of device API.
Definition: DeviceAPI.h:15
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:346
A fragment of Halide syntax.
Definition: Expr.h:256
The sum of two expressions.
Definition: IR.h:38
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Logical and - are both expressions true.
Definition: IR.h:157
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition: IR.h:276
Represent the equivalent associative op of an update definition.
Definition: Associativity.h:61
Represent an associative op with its identity.
Lock all the Store nodes in the body statement.
Definition: IR.h:853
A sequence of statements to be executed in-order.
Definition: IR.h:417
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:241
A function call.
Definition: IR.h:464
The actual IR nodes begin here.
Definition: IR.h:29
The ratio of two expressions.
Definition: IR.h:65
Is the first expression equal to the second.
Definition: IR.h:103
Evaluate and discard an expression, presumably because it has some side-effect.
Definition: IR.h:450
Floating point constants.
Definition: Expr.h:234
A for loop.
Definition: IR.h:730
A pair of statements executed concurrently.
Definition: IR.h:431
Free the resources associated with the given buffer.
Definition: IR.h:388
Is the first expression greater than or equal to the second.
Definition: IR.h:148
Is the first expression greater than the second.
Definition: IR.h:139
An if-then-else block.
Definition: IR.h:440
Integer constants.
Definition: Expr.h:216
Is the first expression less than or equal to the second.
Definition: IR.h:130
Is the first expression less than the second.
Definition: IR.h:121
A let expression, like you might find in a functional language.
Definition: IR.h:253
The statement form of a let node.
Definition: IR.h:264
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
Definition of a lowered function.
Definition: Module.h:96
The greater of two values.
Definition: IR.h:94
The lesser of two values.
Definition: IR.h:85
The remainder of a / b.
Definition: IR.h:76
The product of two expressions.
Definition: IR.h:56
Is the first expression not equal to the second.
Definition: IR.h:112
Logical not - true if the expression false.
Definition: IR.h:175
Logical or - is at least one of the expression true.
Definition: IR.h:166
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition: IR.h:830
This node is a helpful annotation to do with permissions.
Definition: IR.h:297
This defines the value of a function at a multi-dimensional location.
Definition: IR.h:336
A linear ramp vector node.
Definition: IR.h:229
Allocate a multi-dimensional buffer of the given type and size.
Definition: IR.h:402
A ternary operator.
Definition: IR.h:186
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:761
A reference-counted handle to a statement node.
Definition: Expr.h:413
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
String constants.
Definition: Expr.h:243
The difference of two expressions.
Definition: IR.h:47
Unsigned integer constants.
Definition: Expr.h:225
A named variable.
Definition: IR.h:683
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition: IR.h:871
A struct representing a target machine and os to generate code for.
Definition: Target.h:19