Halide
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 class Closure;
58 
59 /** Emit a halide associative pattern on an output stream (such as std::cout)
60  * in a human-readable form */
61 std::ostream &operator<<(std::ostream &stream, const AssociativePattern &);
62 
63 /** Emit a halide associative op on an output stream (such as std::cout) in a
64  * human-readable form */
65 std::ostream &operator<<(std::ostream &stream, const AssociativeOp &);
66 
67 /** Emit a halide statement on an output stream (such as std::cout) in
68  * a human-readable form */
69 std::ostream &operator<<(std::ostream &stream, const Stmt &);
70 
71 /** Emit a halide for loop type (vectorized, serial, etc) in a human
72  * readable form */
73 std::ostream &operator<<(std::ostream &stream, const ForType &);
74 
75 /** Emit a horizontal vector reduction op in human-readable form. */
76 std::ostream &operator<<(std::ostream &stream, const VectorReduce::Operator &);
77 
78 /** Emit a halide name mangling value in a human readable format */
79 std::ostream &operator<<(std::ostream &stream, const NameMangling &);
80 
81 /** Emit a halide LoweredFunc in a human readable format */
82 std::ostream &operator<<(std::ostream &stream, const LoweredFunc &);
83 
84 /** Emit a halide linkage value in a human readable format */
85 std::ostream &operator<<(std::ostream &stream, const LinkageType &);
86 
87 /** Emit a halide dimension type in human-readable format */
88 std::ostream &operator<<(std::ostream &stream, const DimType &);
89 
90 /** Emit a Closure in human-readable format */
91 std::ostream &operator<<(std::ostream &out, const Closure &c);
92 
93 struct Indentation {
94  int indent;
95 };
96 std::ostream &operator<<(std::ostream &stream, const Indentation &);
97 
98 /** An IRVisitor that emits IR to the given output stream in a human
99  * readable form. Can be subclassed if you want to modify the way in
100  * which it prints.
101  */
102 class IRPrinter : public IRVisitor {
103 public:
104  /** Construct an IRPrinter pointed at a given output stream
105  * (e.g. std::cout, or a std::ofstream) */
106  explicit IRPrinter(std::ostream &);
107 
108  /** emit an expression on the output stream */
109  void print(const Expr &);
110 
111  /** Emit an expression on the output stream without enclosing parens */
112  void print_no_parens(const Expr &);
113 
114  /** emit a statement on the output stream */
115  void print(const Stmt &);
116 
117  /** emit a comma delimited list of exprs, without any leading or
118  * trailing punctuation. */
119  void print_list(const std::vector<Expr> &exprs);
120 
121  static void test();
122 
123 protected:
125  return Indentation{indent};
126  }
127 
128  /** The stream on which we're outputting */
129  std::ostream &stream;
130 
131  /** The current indentation level, useful for pretty-printing
132  * statements */
133  int indent = 0;
134 
135  /** Certain expressions do not need parens around them, e.g. the
136  * args to a call are already separated by commas and a
137  * surrounding set of parens. */
138  bool implicit_parens = false;
139 
140  /** Either emits "(" or "", depending on the value of implicit_parens */
141  void open();
142 
143  /** Either emits ")" or "", depending on the value of implicit_parens */
144  void close();
145 
146  /** The symbols whose types can be inferred from values printed
147  * already. */
149 
150  /** A helper for printing a chain of lets with line breaks */
151  void print_lets(const Let *let);
152 
153  void visit(const IntImm *) override;
154  void visit(const UIntImm *) override;
155  void visit(const FloatImm *) override;
156  void visit(const StringImm *) override;
157  void visit(const Cast *) override;
158  void visit(const Reinterpret *) override;
159  void visit(const Variable *) override;
160  void visit(const Add *) override;
161  void visit(const Sub *) override;
162  void visit(const Mul *) override;
163  void visit(const Div *) override;
164  void visit(const Mod *) override;
165  void visit(const Min *) override;
166  void visit(const Max *) override;
167  void visit(const EQ *) override;
168  void visit(const NE *) override;
169  void visit(const LT *) override;
170  void visit(const LE *) override;
171  void visit(const GT *) override;
172  void visit(const GE *) override;
173  void visit(const And *) override;
174  void visit(const Or *) override;
175  void visit(const Not *) override;
176  void visit(const Select *) override;
177  void visit(const Load *) override;
178  void visit(const Ramp *) override;
179  void visit(const Broadcast *) override;
180  void visit(const Call *) override;
181  void visit(const Let *) override;
182  void visit(const LetStmt *) override;
183  void visit(const AssertStmt *) override;
184  void visit(const ProducerConsumer *) override;
185  void visit(const For *) override;
186  void visit(const Acquire *) override;
187  void visit(const Store *) override;
188  void visit(const Provide *) override;
189  void visit(const Allocate *) override;
190  void visit(const Free *) override;
191  void visit(const Realize *) override;
192  void visit(const Block *) override;
193  void visit(const Fork *) override;
194  void visit(const IfThenElse *) override;
195  void visit(const Evaluate *) override;
196  void visit(const Shuffle *) override;
197  void visit(const VectorReduce *) override;
198  void visit(const Prefetch *) override;
199  void visit(const Atomic *) override;
200 };
201 
202 } // namespace Internal
203 } // namespace Halide
204 
205 #endif
Halide::Internal::Acquire
Definition: IR.h:807
Halide::Internal::IRPrinter::stream
std::ostream & stream
The stream on which we're outputting.
Definition: IRPrinter.h:129
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
Scope.h
Halide::Internal::IRPrinter::print_lets
void print_lets(const Let *let)
A helper for printing a chain of lets with line breaks.
Halide::Internal::operator<<
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.
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::IRVisitor
A base class for algorithms that need to recursively walk over the IR.
Definition: IRVisitor.h:19
Halide::Internal::For
A for loop.
Definition: IR.h:788
Halide::Internal::DimType
DimType
Each Dim below has a dim_type, which tells you what transformations are legal on it.
Definition: Schedule.h:326
Halide::Internal::FloatImm
Floating point constants.
Definition: Expr.h:235
Halide::Internal::IRPrinter
An IRVisitor that emits IR to the given output stream in a human readable form.
Definition: IRPrinter.h:102
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::ForType
ForType
An enum describing a type of loop traversal.
Definition: Expr.h:400
Halide::Internal::IntImm
Integer constants.
Definition: Expr.h:217
Halide::Internal::LetStmt
The statement form of a let node.
Definition: IR.h:274
Halide::Internal::Scope
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition: ModulusRemainder.h:17
Halide::Internal::Cast
The actual IR nodes begin here.
Definition: IR.h:29
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition: IR.h:140
Halide::Internal::IRPrinter::IRPrinter
IRPrinter(std::ostream &)
Construct an IRPrinter pointed at a given output stream (e.g.
Halide::Internal::NE
Is the first expression not equal to the second.
Definition: IR.h:122
Halide::Internal::IRPrinter::implicit_parens
bool implicit_parens
Certain expressions do not need parens around them, e.g.
Definition: IRPrinter.h:138
Halide::Internal::Fork
A pair of statements executed concurrently.
Definition: IR.h:449
Halide::TailStrategy
TailStrategy
Different ways to handle a tail case in a split when the factor does not provably divide the extent.
Definition: Schedule.h:32
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:418
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::LinkageType
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition: Module.h:48
Halide::Internal::IRPrinter::close
void close()
Either emits ")" or "", depending on the value of implicit_parens.
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition: IR.h:263
Halide::Internal::VectorReduce::Operator
Operator
Definition: IR.h:933
Halide::Internal::Ramp
A linear ramp vector node.
Definition: IR.h:239
Halide::Internal::IRPrinter::visit
void visit(const IntImm *) override
Halide::Internal::IRPrinter::print_list
void print_list(const std::vector< Expr > &exprs)
emit a comma delimited list of exprs, without any leading or trailing punctuation.
IRVisitor.h
Halide::Internal::Evaluate
Evaluate and discard an expression, presumably because it has some side-effect.
Definition: IR.h:468
Halide::Internal::IRPrinter::print
void print(const Expr &)
emit an expression on the output stream
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::Indentation
Definition: IRPrinter.h:93
Halide::Internal::Variable
A named variable.
Definition: IR.h:741
Halide::Internal::IRPrinter::get_indent
Indentation get_indent() const
Definition: IRPrinter.h:124
Halide::NameMangling
NameMangling
An enum to specify calling convention for extern stages.
Definition: Function.h:25
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::IRPrinter::open
void open()
Either emits "(" or "", depending on the value of implicit_parens.
Halide::Internal::Mod
The remainder of a / b.
Definition: IR.h:86
Halide::Internal::IRPrinter::indent
int indent
The current indentation level, useful for pretty-printing statements.
Definition: IRPrinter.h:133
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::IRPrinter::test
static void test()
Halide::Internal::Call
A function call.
Definition: IR.h:482
Halide::operator<<
std::ostream & operator<<(std::ostream &stream, const Expr &)
Emit an expression on an output stream (such as std::cout) in human-readable form.
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::MemoryType
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:347
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::Indentation::indent
int indent
Definition: IRPrinter.h:94
Halide::Internal::IRPrinter::print_no_parens
void print_no_parens(const Expr &)
Emit an expression on the output stream without enclosing parens.
Module.h
Halide::Internal::UIntImm
Unsigned integer constants.
Definition: Expr.h:226
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::DeviceAPI
DeviceAPI
An enum describing a type of device API.
Definition: DeviceAPI.h:15
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::IRPrinter::known_type
Scope known_type
The symbols whose types can be inferred from values printed already.
Definition: IRPrinter.h:148