Halide 13.0.2
Halide compiler and libraries
printer.h
Go to the documentation of this file.
1#ifndef HALIDE_RUNTIME_PRINTER_H
2#define HALIDE_RUNTIME_PRINTER_H
3namespace Halide {
4namespace Runtime {
5namespace Internal {
6
10
11// A class for constructing debug messages from the runtime. Dumps
12// items into a stack array, then prints them when the object leaves
13// scope using halide_print. Think of it as a stringstream that prints
14// when it dies. Use it like this:
15
16// debug(user_context) << "A" << b << c << "\n";
17
18// If you use it like this:
19
20// debug d(user_context);
21// d << "A";
22// d << b;
23// d << c << "\n";
24
25// Then remember the print only happens when the debug object leaves
26// scope, which may print at a confusing time.
27
28namespace {
29template<int type, uint64_t length = 1024>
30class Printer {
31public:
32 char *buf, *dst, *end;
34 bool own_mem;
35 char scratch[length <= 256 ? length : 1];
36
37 Printer(void *ctx, char *mem = nullptr)
38 : user_context(ctx), own_mem(mem == nullptr) {
39 if (mem != nullptr) {
40 buf = mem;
41 } else if (length <= sizeof(scratch)) {
42 buf = scratch;
43 } else {
44 buf = (char *)malloc(length);
45 }
46
47 dst = buf;
48 if (dst) {
49 end = buf + (length - 1);
50 *end = 0;
51 } else {
52 // Pointers equal ensures no writes to buffer via formatting code
53 end = dst;
54 }
55 }
56
57 Printer &operator<<(const char *arg) {
58 // Crashing on nullptr here is a big debugging time sink.
59 if (arg == nullptr) {
60 dst = halide_string_to_string(dst, end, "<nullptr>");
61 } else {
63 }
64 return *this;
65 }
66
67 Printer &operator<<(int64_t arg) {
69 return *this;
70 }
71
72 Printer &operator<<(int32_t arg) {
74 return *this;
75 }
76
77 Printer &operator<<(uint64_t arg) {
79 return *this;
80 }
81
82 Printer &operator<<(uint32_t arg) {
84 return *this;
85 }
86
87 Printer &operator<<(double arg) {
89 return *this;
90 }
91
92 Printer &operator<<(float arg) {
94 return *this;
95 }
96
97 Printer &operator<<(const void *arg) {
99 return *this;
100 }
101
102 Printer &write_float16_from_bits(const uint16_t arg) {
103 double value = halide_float16_bits_to_double(arg);
104 dst = halide_double_to_string(dst, end, value, 1);
105 return *this;
106 }
107
108 Printer &operator<<(const halide_type_t &t) {
110 return *this;
111 }
112
113 Printer &operator<<(const halide_buffer_t &buf) {
115 return *this;
116 }
117
118 // Use it like a stringstream.
119 const char *str() {
120 if (buf) {
121 if (type == StringStreamPrinter) {
122 msan_annotate_is_initialized();
123 }
124 return buf;
125 } else {
126 return allocation_error();
127 }
128 }
129
130 // Clear it. Useful for reusing a stringstream.
131 void clear() {
132 dst = buf;
133 if (dst) {
134 dst[0] = 0;
135 }
136 }
137
138 // Returns the number of characters in the buffer
139 uint64_t size() const {
140 return (uint64_t)(dst - buf);
141 }
142
143 uint64_t capacity() const {
144 return length;
145 }
146
147 // Delete the last N characters
148 void erase(int n) {
149 if (dst) {
150 dst -= n;
151 if (dst < buf) {
152 dst = buf;
153 }
154 dst[0] = 0;
155 }
156 }
157
158 const char *allocation_error() {
159 return "Printer buffer allocation failed.\n";
160 }
161
162 void msan_annotate_is_initialized() {
164 }
165
166 ~Printer() {
167 if (!buf) {
168 halide_error(user_context, allocation_error());
169 } else {
170 msan_annotate_is_initialized();
171 if (type == ErrorPrinter) {
173 } else if (type == BasicPrinter) {
175 } else {
176 // It's a stringstream. Do nothing.
177 }
178 }
179
180 if (own_mem && buf != scratch) {
181 free(buf);
182 }
183 }
184};
185
186// A class that supports << with all the same types as Printer, but
187// does nothing and should compile to a no-op.
188class SinkPrinter {
189public:
190 SinkPrinter(void *user_context) {
191 }
192};
193template<typename T>
194SinkPrinter operator<<(const SinkPrinter &s, T) {
195 return s;
196}
197
198typedef Printer<BasicPrinter> print;
199typedef Printer<ErrorPrinter> error;
200typedef Printer<StringStreamPrinter> stringstream;
201
202#ifdef DEBUG_RUNTIME
203typedef Printer<BasicPrinter> debug;
204#else
205typedef SinkPrinter debug;
206#endif
207} // namespace
208
209} // namespace Internal
210} // namespace Runtime
211} // namespace Halide
212#endif
double halide_float16_bits_to_double(uint16_t)
Read bits representing a half precision floating point number and return the double that represents t...
int halide_msan_annotate_memory_is_initialized(void *user_context, const void *ptr, uint64_t len)
Annotate that a given range of memory has been initialized; only used when Target::MSAN is enabled.
void halide_print(void *user_context, const char *)
Print a message to stderr.
void halide_error(void *user_context, const char *)
Halide calls this function on runtime errors (for example bounds checking failures).
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
std::ostream & operator<<(std::ostream &stream, const Expr &)
Emit an expression on an output stream (such as std::cout) in human-readable form.
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
char * buf
Definition: printer.h:32
char * dst
Definition: printer.h:32
void * user_context
Definition: printer.h:33
char scratch[length<=256 ? length :1]
Definition: printer.h:35
bool own_mem
Definition: printer.h:34
char * end
Definition: printer.h:32
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
void * malloc(size_t)
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
signed __INT32_TYPE__ int32_t
unsigned __INT16_TYPE__ uint16_t
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
unsigned __INT32_TYPE__ uint32_t
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
void free(void *)
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
The raw representation of an image passed around by generated Halide code.
A runtime tag for a type in the halide type system.