Loading...
Searching...
No Matches
printer.h
Go to the documentation of this file.
#ifndef HALIDE_RUNTIME_PRINTER_H
#define HALIDE_RUNTIME_PRINTER_H
#include "HalideRuntime.h"
// This is useful for debugging threading issues in the Halide runtime:
// prefix all `debug()` statements with the thread id that did the logging.
// Left here (but disabled) for future reference.
#ifndef HALIDE_RUNTIME_PRINTER_LOG_THREADID
#define HALIDE_RUNTIME_PRINTER_LOG_THREADID 0
#endif
#if HALIDE_RUNTIME_PRINTER_LOG_THREADID
extern "C" int pthread_threadid_np(long thread, uint64_t *thread_id);
#endif
namespace Halide {
namespace Runtime {
namespace Internal {
enum PrinterType { BasicPrinterType = 0,
ErrorPrinterType = 1,
StringStreamPrinterType = 2 };
constexpr uint64_t default_printer_buffer_length = 1024;
// A class for constructing debug messages from the runtime. Dumps
// items into a stack array, then prints them when the object leaves
// scope using halide_print. Think of it as a stringstream that prints
// when it dies. Use it like this:
// debug(user_context) << "A" << b << c << "\n";
// If you use it like this:
// debug d(user_context);
// d << "A";
// d << b;
// d << c << "\n";
// Then remember the print only happens when the debug object leaves
// scope, which may print at a confusing time.
class PrinterBase {
protected:
char *dst;
char *const end;
char *const start;
void *const user_context;
NEVER_INLINE void allocation_error() const {
halide_error(user_context, "Printer buffer allocation failed.\n");
}
public:
// This class will stream text into the range [start, start + size - 1].
// It does *not* assume any ownership of the memory; it assumes
// the memory will remain valid for its lifespan, and doesn't
// attempt to free any allocations. It also doesn't do any sanity
// checking of the pointers, so if you pass in a null or bogus value,
// it will attempt to use it.
NEVER_INLINE PrinterBase(void *user_context_, char *start_, uint64_t size_)
: dst(start_),
// (If start is null, set end = start to ensure no writes are done)
end(start_ ? start_ + size_ - 1 : start_),
start(start_),
user_context(user_context_) {
if (end > start) {
// null-terminate the final byte to ensure string isn't $ENDLESS
*end = 0;
}
}
NEVER_INLINE const char *str() {
(void)halide_msan_annotate_memory_is_initialized(user_context, start, dst - start + 1);
return start;
}
uint64_t size() const {
halide_debug_assert(user_context, dst >= start);
return (uint64_t)(dst - start);
}
uint64_t capacity() const {
halide_debug_assert(user_context, end >= start);
return (uint64_t)(end - start);
}
NEVER_INLINE void clear() {
dst = start;
if (dst) {
dst[0] = 0;
}
}
NEVER_INLINE void erase(int n) {
if (dst) {
dst -= n;
if (dst < start) {
dst = start;
}
dst[0] = 0;
}
}
struct Float16Bits {
uint16_t bits;
};
// These are NEVER_INLINE because Clang will aggressively inline
// all of them, but the code size of calling out-of-line here is slightly
// smaller, and we ~always prefer smaller code size when using Printer
// in the runtime (it's a modest but nonzero difference).
NEVER_INLINE PrinterBase &operator<<(const char *arg) {
dst = halide_string_to_string(dst, end, arg);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(int64_t arg) {
dst = halide_int64_to_string(dst, end, arg, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(int32_t arg) {
dst = halide_int64_to_string(dst, end, arg, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(uint64_t arg) {
dst = halide_uint64_to_string(dst, end, arg, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(uint32_t arg) {
dst = halide_uint64_to_string(dst, end, arg, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(double arg) {
dst = halide_double_to_string(dst, end, arg, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(float arg) {
dst = halide_double_to_string(dst, end, arg, 0);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(Float16Bits arg) {
double value = halide_float16_bits_to_double(arg.bits);
dst = halide_double_to_string(dst, end, value, 1);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(const void *arg) {
dst = halide_pointer_to_string(dst, end, arg);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(const halide_type_t &t) {
dst = halide_type_to_string(dst, end, &t);
return *this;
}
NEVER_INLINE PrinterBase &operator<<(const halide_buffer_t &buf) {
dst = halide_buffer_to_string(dst, end, &buf);
return *this;
}
template<typename... Args>
void append(const Args &...args) {
((*this << args), ...);
}
// Not movable, not copyable
PrinterBase(const PrinterBase &copy) = delete;
PrinterBase &operator=(const PrinterBase &) = delete;
PrinterBase(PrinterBase &&) = delete;
PrinterBase &operator=(PrinterBase &&) = delete;
};
namespace {
template<PrinterType printer_type, uint64_t buffer_length = default_printer_buffer_length>
class HeapPrinter : public PrinterBase {
public:
NEVER_INLINE explicit HeapPrinter(void *user_context_)
: PrinterBase(user_context_, (char *)malloc(buffer_length), buffer_length) {
if (!start) {
allocation_error();
}
#if HALIDE_RUNTIME_PRINTER_LOG_THREADID
uint64_t tid;
pthread_threadid_np(0, &tid);
*this << "(TID:" << tid << ")";
#endif
}
NEVER_INLINE ~HeapPrinter() {
if (printer_type == ErrorPrinterType) {
halide_error(user_context, str());
} else if (printer_type == BasicPrinterType) {
halide_print(user_context, str());
} else {
// It's a stringstream. Do nothing.
}
free(start);
}
};
// A class that supports << with all the same types as Printer, but
// does nothing and should compile to a no-op.
class SinkPrinter {
public:
ALWAYS_INLINE explicit SinkPrinter(void *user_context) {
}
};
template<typename T>
ALWAYS_INLINE SinkPrinter operator<<(const SinkPrinter &s, T) {
return s;
}
template<uint64_t buffer_length = default_printer_buffer_length>
using BasicPrinter = HeapPrinter<BasicPrinterType, buffer_length>;
template<uint64_t buffer_length = default_printer_buffer_length>
using ErrorPrinter = HeapPrinter<ErrorPrinterType, buffer_length>;
template<uint64_t buffer_length = default_printer_buffer_length>
using StringStreamPrinter = HeapPrinter<StringStreamPrinterType, buffer_length>;
using print = BasicPrinter<>;
using error = ErrorPrinter<>;
using stringstream = StringStreamPrinter<>;
#ifdef DEBUG_RUNTIME
using debug = BasicPrinter<>;
#else
using debug = SinkPrinter;
#endif
// A Printer that automatically reserves stack space for the printer buffer, rather than malloc.
// Note that this requires an explicit buffer_length, and it (generally) should be <= 256.
template<PrinterType printer_type, uint64_t buffer_length>
class StackPrinter : public PrinterBase {
char scratch[buffer_length];
public:
explicit StackPrinter(void *user_context_)
: PrinterBase(user_context_, scratch, buffer_length) {
static_assert(buffer_length <= 256, "StackPrinter is meant only for small buffer sizes; you are probably making a mistake.");
}
};
template<uint64_t buffer_length = default_printer_buffer_length>
using StackBasicPrinter = StackPrinter<BasicPrinterType, buffer_length>;
template<uint64_t buffer_length = default_printer_buffer_length>
using StackErrorPrinter = StackPrinter<ErrorPrinterType, buffer_length>;
template<uint64_t buffer_length = default_printer_buffer_length>
using StackStringStreamPrinter = StackPrinter<StringStreamPrinterType, buffer_length>;
} // namespace
} // namespace Internal
} // namespace Runtime
} // namespace Halide
#endif
debug
#define debug(n)
For optional debugging during codegen, use the debug macro as follows:
Definition Debug.h:52
HalideRuntime.h
This file declares the routines used by Halide internally in its runtime.
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...
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.
halide_print
void halide_print(void *user_context, const char *)
Print a message to stderr.
halide_error
void halide_error(void *user_context, const char *)
Halide calls this function on runtime errors (for example bounds checking failures).
Halide::Runtime::Internal::PrinterBase
Definition printer.h:44
Halide::Runtime::Internal::PrinterBase::clear
NEVER_INLINE void clear()
Definition printer.h:89
Halide::Runtime::Internal::PrinterBase::erase
NEVER_INLINE void erase(int n)
Definition printer.h:96
Halide::Runtime::Internal::PrinterBase::size
uint64_t size() const
Definition printer.h:79
Halide::Runtime::Internal::PrinterBase::operator=
PrinterBase & operator=(PrinterBase &&)=delete
Halide::Runtime::Internal::PrinterBase::capacity
uint64_t capacity() const
Definition printer.h:84
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(float arg)
Definition printer.h:144
Halide::Runtime::Internal::PrinterBase::append
void append(const Args &...args)
Definition printer.h:171
Halide::Runtime::Internal::PrinterBase::allocation_error
NEVER_INLINE void allocation_error() const
Definition printer.h:51
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(uint32_t arg)
Definition printer.h:134
Halide::Runtime::Internal::PrinterBase::PrinterBase
NEVER_INLINE PrinterBase(void *user_context_, char *start_, uint64_t size_)
Definition printer.h:62
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(uint64_t arg)
Definition printer.h:129
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(int32_t arg)
Definition printer.h:124
Halide::Runtime::Internal::PrinterBase::operator=
PrinterBase & operator=(const PrinterBase &)=delete
Halide::Runtime::Internal::PrinterBase::PrinterBase
PrinterBase(PrinterBase &&)=delete
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(const char *arg)
Definition printer.h:114
Halide::Runtime::Internal::PrinterBase::PrinterBase
PrinterBase(const PrinterBase &copy)=delete
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(const halide_type_t &t)
Definition printer.h:160
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(const void *arg)
Definition printer.h:155
Halide::Runtime::Internal::PrinterBase::end
char *const end
Definition printer.h:47
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(const halide_buffer_t &buf)
Definition printer.h:165
Halide::Runtime::Internal::PrinterBase::str
NEVER_INLINE const char * str()
Definition printer.h:74
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(Float16Bits arg)
Definition printer.h:149
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(int64_t arg)
Definition printer.h:119
Halide::Runtime::Internal::PrinterBase::dst
char * dst
Definition printer.h:46
Halide::Runtime::Internal::PrinterBase::start
char *const start
Definition printer.h:48
Halide::Runtime::Internal::PrinterBase::operator<<
NEVER_INLINE PrinterBase & operator<<(double arg)
Definition printer.h:139
Halide::Runtime::Internal::PrinterBase::user_context
void *const user_context
Definition printer.h:49
Halide::Runtime::Internal::PrinterType
PrinterType
Definition printer.h:21
Halide::Runtime::Internal::StringStreamPrinterType
@ StringStreamPrinterType
Definition printer.h:23
Halide::Runtime::Internal::BasicPrinterType
@ BasicPrinterType
Definition printer.h:21
Halide::Runtime::Internal::ErrorPrinterType
@ ErrorPrinterType
Definition printer.h:22
Halide::Runtime::Internal::default_printer_buffer_length
constexpr uint64_t default_printer_buffer_length
Definition printer.h:25
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
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::print
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition runtime_internal.h:23
int64_t
signed __INT64_TYPE__ int64_t
Definition runtime_internal.h:22
halide_uint64_to_string
WEAK char * halide_uint64_to_string(char *dst, char *end, uint64_t arg, int digits)
halide_debug_assert
#define halide_debug_assert(user_context, cond)
halide_debug_assert() is like halide_assert(), but only expands into a check when DEBUG_RUNTIME is de...
Definition runtime_internal.h:290
NEVER_INLINE
#define NEVER_INLINE
Definition runtime_internal.h:54
malloc
void * malloc(size_t)
halide_type_to_string
WEAK char * halide_type_to_string(char *dst, char *end, const halide_type_t *arg)
int32_t
signed __INT32_TYPE__ int32_t
Definition runtime_internal.h:24
uint16_t
unsigned __INT16_TYPE__ uint16_t
Definition runtime_internal.h:27
halide_int64_to_string
WEAK char * halide_int64_to_string(char *dst, char *end, int64_t arg, int digits)
halide_pointer_to_string
WEAK char * halide_pointer_to_string(char *dst, char *end, const void *arg)
ALWAYS_INLINE
#define ALWAYS_INLINE
Definition runtime_internal.h:57
uint32_t
unsigned __INT32_TYPE__ uint32_t
Definition runtime_internal.h:25
halide_double_to_string
WEAK char * halide_double_to_string(char *dst, char *end, double arg, int scientific)
halide_buffer_to_string
WEAK char * halide_buffer_to_string(char *dst, char *end, const halide_buffer_t *arg)
free
void free(void *)
halide_string_to_string
WEAK char * halide_string_to_string(char *dst, char *end, const char *arg)
Halide::Runtime::Internal::PrinterBase::Float16Bits
Definition printer.h:106
Halide::Runtime::Internal::PrinterBase::Float16Bits::bits
uint16_t bits
Definition printer.h:107
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition HalideRuntime.h:1569
halide_type_t
A runtime tag for a type in the halide type system.
Definition HalideRuntime.h:503