-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This builds on top of #6472 to de-inline as much of Printer as possible, moving things into a new 'printer' runtime module using a PrinterBase class. This is, admittedly, a pretty small improvement: comparing before-and-after on OSX for target=host shows only a ~4k reduction in object size (115k -> 111k for `runtime.o`) but adding targets with more verbose error reporting and such increases the benefit (eg host-opencl gives a 179k -> 164k reduction for runtime.o). Since ~all of the usages of Printer in runtime are for error handling, debugging, profiling, or tracing, any possible reduction in performance seems unlikely to be significant.
- Loading branch information
1 parent
319fb63
commit 3c5e2d2
Showing
7 changed files
with
228 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include "printer.h" | ||
#include "HalideRuntime.h" | ||
|
||
namespace Halide { | ||
namespace Runtime { | ||
namespace Internal { | ||
|
||
static const char *const kAllocationError = "Printer buffer allocation failed.\n"; | ||
|
||
WEAK PrinterBase::PrinterBase(void *ctx, char *mem, uint64_t length) | ||
: user_context(ctx), length(length) { | ||
|
||
if (mem == nullptr) { | ||
mem = (char *)malloc(length); | ||
mem_to_free = mem; | ||
} else { | ||
mem_to_free = nullptr; | ||
} | ||
|
||
dst = buf = mem; | ||
if (dst) { | ||
end = buf + length - 1; | ||
*end = 0; | ||
} else { | ||
end = dst; | ||
} | ||
} | ||
|
||
WEAK PrinterBase::PrinterBase(void *ctx) | ||
: PrinterBase(ctx, nullptr, default_printer_buffer_length) { | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(const char *arg) { | ||
dst = halide_string_to_string(dst, end, arg); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(int64_t arg) { | ||
dst = halide_int64_to_string(dst, end, arg, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(int32_t arg) { | ||
dst = halide_int64_to_string(dst, end, arg, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(uint64_t arg) { | ||
dst = halide_uint64_to_string(dst, end, arg, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(uint32_t arg) { | ||
dst = halide_uint64_to_string(dst, end, arg, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(double arg) { | ||
dst = halide_double_to_string(dst, end, arg, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(float arg) { | ||
dst = halide_double_to_string(dst, end, arg, 0); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(const void *arg) { | ||
dst = halide_pointer_to_string(dst, end, arg); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::write_float16_from_bits(const uint16_t arg) { | ||
double value = halide_float16_bits_to_double(arg); | ||
dst = halide_double_to_string(dst, end, value, 1); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(const halide_type_t &t) { | ||
dst = halide_type_to_string(dst, end, &t); | ||
return *this; | ||
} | ||
|
||
WEAK PrinterBase &PrinterBase::operator<<(const halide_buffer_t &buf) { | ||
dst = halide_buffer_to_string(dst, end, &buf); | ||
return *this; | ||
} | ||
|
||
// Use it like a stringstream. | ||
WEAK const char *PrinterBase::str() { | ||
if (!buf) { | ||
return kAllocationError; | ||
} | ||
|
||
// This is really only needed for StringStreamPrinter, but is easier to do unconditionally | ||
halide_msan_annotate_memory_is_initialized(user_context, buf, dst - buf + 1); | ||
return buf; | ||
} | ||
|
||
// Clear it. Useful for reusing a stringstream. | ||
WEAK void PrinterBase::clear() { | ||
dst = buf; | ||
if (dst) { | ||
dst[0] = 0; | ||
} | ||
} | ||
|
||
// Delete the last N characters | ||
WEAK void PrinterBase::erase(int n) { | ||
if (dst) { | ||
dst -= n; | ||
if (dst < buf) { | ||
dst = buf; | ||
} | ||
dst[0] = 0; | ||
} | ||
} | ||
|
||
WEAK void PrinterBase::finish_error() { | ||
halide_error(user_context, str()); | ||
} | ||
|
||
WEAK void PrinterBase::finish_print() { | ||
halide_print(user_context, str()); | ||
} | ||
|
||
} // namespace Internal | ||
} // namespace Runtime | ||
} // namespace Halide |
Oops, something went wrong.