Halide 21.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Debug.h
Go to the documentation of this file.
1#ifndef HALIDE_DEBUG_H
2#define HALIDE_DEBUG_H
3
4/** \file
5 * Defines functions for debug logging during code generation.
6 */
7
8#include <cstdlib>
9#include <iostream>
10#include <string>
11
12namespace Halide {
13
14struct Expr;
15struct Type;
16// Forward declare some things from IRPrinter, which we can't include yet.
17std::ostream &operator<<(std::ostream &stream, const Expr &);
18std::ostream &operator<<(std::ostream &stream, const Type &);
19
20class Module;
21std::ostream &operator<<(std::ostream &stream, const Module &);
22
23struct Target;
24/** Emit a halide Target in a human readable form */
25std::ostream &operator<<(std::ostream &stream, const Target &);
26
27namespace Internal {
28
29struct Stmt;
30std::ostream &operator<<(std::ostream &stream, const Stmt &);
31
32struct LoweredFunc;
33std::ostream &operator<<(std::ostream &, const LoweredFunc &);
34
35bool debug_is_active_impl(int verbosity, const char *file, const char *function, int line);
36#define debug_is_active(n) (::Halide::Internal::debug_is_active_impl((n), __FILE__, __FUNCTION__, __LINE__))
37
38/** For optional debugging during codegen, use the debug macro as
39 * follows:
40 *
41 * \code
42 * debug(verbosity) << "The expression is " << expr << "\n";
43 * \endcode
44 *
45 * verbosity of 0 always prints, 1 should print after every major
46 * stage, 2 should be used for more detail, and 3 should be used for
47 * tracing everything that occurs. The verbosity with which to print
48 * is determined by the value of the environment variable
49 * HL_DEBUG_CODEGEN
50 */
51// clang-format off
52#define debug(n) \
53 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
54 if (debug_is_active((n))) std::cerr
55// clang-format on
56
57/** Allow easily printing the contents of containers, or std::vector-like containers,
58 * in debug output. Used like so:
59 * std::vector<Type> arg_types;
60 * debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
61 * Which results in output like "arg_types: { uint8x8, uint8x8 }" on one line. */
62template<typename T>
63struct PrintSpan {
64 const T &span;
65 PrintSpan(const T &span)
66 : span(span) {
67 }
68};
69// Class template argument deduction (CTAD) guide to prevent warnings.
70template<typename T>
71PrintSpan(const T &) -> PrintSpan<T>;
72
73template<typename StreamT, typename T>
74inline StreamT &operator<<(StreamT &stream, const PrintSpan<T> &wrapper) {
75 stream << "{ ";
76 const char *sep = "";
77 for (const auto &e : wrapper.span) {
78 stream << sep << e;
79 sep = ", ";
80 }
81 stream << " }";
82 return stream;
83}
84
85/** Allow easily printing the contents of spans, or std::vector-like spans,
86 * in debug output. Used like so:
87 * std::vector<Type> arg_types;
88 * debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
89 * Which results in output like:
90 * arg_types:
91 * {
92 * uint8x8,
93 * uint8x8,
94 * }
95 * Indentation uses a tab character. */
96template<typename T>
98 const T &span;
99 PrintSpanLn(const T &span)
100 : span(span) {
101 }
102};
103// Class template argument deduction (CTAD) guide to prevent warnings.
104template<typename T>
106
107template<typename StreamT, typename T>
108inline StreamT &operator<<(StreamT &stream, const PrintSpanLn<T> &wrapper) {
109 stream << "\n{\n";
110 for (const auto &e : wrapper.span) {
111 stream << "\t" << e << ",\n";
112 }
113 stream << "}\n";
114 return stream;
115}
116
117} // namespace Internal
118} // namespace Halide
119
120#endif
A halide module.
Definition Module.h:142
PrintSpanLn(const T &) -> PrintSpanLn< T >
ConstantInterval operator<<(const ConstantInterval &a, const ConstantInterval &b)
PrintSpan(const T &) -> PrintSpan< T >
bool debug_is_active_impl(int verbosity, const char *file, const char *function, int line)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
std::ostream & operator<<(std::ostream &stream, const Expr &)
Emit an expression on an output stream (such as std::cout) in human-readable form.
A fragment of Halide syntax.
Definition Expr.h:258
Definition of a lowered function.
Definition Module.h:101
Allow easily printing the contents of containers, or std::vector-like containers, in debug output.
Definition Debug.h:63
PrintSpan(const T &span)
Definition Debug.h:65
Allow easily printing the contents of spans, or std::vector-like spans, in debug output.
Definition Debug.h:97
PrintSpanLn(const T &span)
Definition Debug.h:99
A reference-counted handle to a statement node.
Definition Expr.h:427
A struct representing a target machine and os to generate code for.
Definition Target.h:19
Types in the halide type system.
Definition Type.h:283