Halide
printer.h
Go to the documentation of this file.
1 #ifndef HALIDE_RUNTIME_PRINTER_H
2 #define HALIDE_RUNTIME_PRINTER_H
3 namespace Halide {
4 namespace Runtime {
5 namespace 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 
28 namespace {
29 template<int type, uint64_t length = 1024>
30 class Printer {
31 public:
32  char *buf, *dst, *end;
33  void *user_context;
34  bool own_mem;
35  char scratch[length <= 256 ? length : 1];
36 
37  Printer(void *ctx, char *mem = NULL)
38  : user_context(ctx), own_mem(mem == NULL) {
39  if (mem != NULL) {
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 NULL here is a big debugging time sink.
59  if (arg == NULL) {
60  dst = halide_string_to_string(dst, end, "<NULL>");
61  } else {
63  }
64  return *this;
65  }
66 
67  Printer &operator<<(int64_t arg) {
68  dst = halide_int64_to_string(dst, end, arg, 1);
69  return *this;
70  }
71 
72  Printer &operator<<(int32_t arg) {
73  dst = halide_int64_to_string(dst, end, arg, 1);
74  return *this;
75  }
76 
77  Printer &operator<<(uint64_t arg) {
78  dst = halide_uint64_to_string(dst, end, arg, 1);
79  return *this;
80  }
81 
82  Printer &operator<<(uint32_t arg) {
83  dst = halide_uint64_to_string(dst, end, arg, 1);
84  return *this;
85  }
86 
87  Printer &operator<<(double arg) {
88  dst = halide_double_to_string(dst, end, arg, 1);
89  return *this;
90  }
91 
92  Printer &operator<<(float arg) {
93  dst = halide_double_to_string(dst, end, arg, 0);
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.
188 class SinkPrinter {
189 public:
190  SinkPrinter(void *user_context) {
191  }
192 };
193 template<typename T>
194 SinkPrinter operator<<(const SinkPrinter &s, T) {
195  return s;
196 }
197 
198 typedef Printer<BasicPrinter> print;
199 typedef Printer<ErrorPrinter> error;
200 typedef Printer<StringStreamPrinter> stringstream;
201 
202 #ifdef DEBUG_RUNTIME
203 typedef Printer<BasicPrinter> debug;
204 #else
205 typedef SinkPrinter debug;
206 #endif
207 } // namespace
208 
209 } // namespace Internal
210 } // namespace Runtime
211 } // namespace Halide
212 #endif
int32_t
signed __INT32_TYPE__ int32_t
Definition: runtime_internal.h:20
halide_double_to_string
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
Halide::Runtime::Internal::BasicPrinter
@ BasicPrinter
Definition: printer.h:7
halide_type_to_string
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
NULL
#define NULL
Definition: runtime_internal.h:32
uint16_t
unsigned __INT16_TYPE__ uint16_t
Definition: runtime_internal.h:23
Halide::Runtime::Internal::PrinterType
PrinterType
Definition: printer.h:7
halide_float16_bits_to_double
double halide_float16_bits_to_double(uint16_t)
Read bits representing a half precision floating point number and return the double that represents t...
own_mem
bool own_mem
Definition: printer.h:34
halide_type_t
A runtime tag for a type in the halide type system.
Definition: HalideRuntime.h:426
halide_buffer_to_string
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:19
Halide::Runtime::Internal::ErrorPrinter
@ ErrorPrinter
Definition: printer.h:8
malloc
void * malloc(size_t)
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
halide_error
void halide_error(void *user_context, const char *)
Halide calls this function on runtime errors (for example bounds checking failures).
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::print
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:18
end
char * end
Definition: printer.h:32
halide_int64_to_string
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
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.
dst
char * dst
Definition: printer.h:32
halide_msan_annotate_memory_is_initialized
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.
free
void free(void *)
scratch
char scratch[length<=256 ? length :1]
Definition: printer.h:35
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition: HalideRuntime.h:1404
halide_uint64_to_string
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
halide_pointer_to_string
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
halide_print
void halide_print(void *user_context, const char *)
Print a message to stderr.
buf
char * buf
Definition: printer.h:32
halide_string_to_string
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
uint32_t
unsigned __INT32_TYPE__ uint32_t
Definition: runtime_internal.h:21
user_context
void * user_context
Definition: printer.h:33
Halide::Runtime::Internal::StringStreamPrinter
@ StringStreamPrinter
Definition: printer.h:9