-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathDebug.h
131 lines (111 loc) · 3.33 KB
/
Debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef HALIDE_DEBUG_H
#define HALIDE_DEBUG_H
/** \file
* Defines functions for debug logging during code generation.
*/
#include <cstdlib>
#include <iostream>
#include <string>
namespace Halide {
struct Expr;
struct Type;
// Forward declare some things from IRPrinter, which we can't include yet.
std::ostream &operator<<(std::ostream &stream, const Expr &);
std::ostream &operator<<(std::ostream &stream, const Type &);
class Module;
std::ostream &operator<<(std::ostream &stream, const Module &);
struct Target;
/** Emit a halide Target in a human readable form */
std::ostream &operator<<(std::ostream &stream, const Target &);
namespace Internal {
struct Stmt;
std::ostream &operator<<(std::ostream &stream, const Stmt &);
struct LoweredFunc;
std::ostream &operator<<(std::ostream &, const LoweredFunc &);
/** For optional debugging during codegen, use the debug class as
* follows:
*
\code
debug(verbosity) << "The expression is " << expr << "\n";
\endcode
*
* verbosity of 0 always prints, 1 should print after every major
* stage, 2 should be used for more detail, and 3 should be used for
* tracing everything that occurs. The verbosity with which to print
* is determined by the value of the environment variable
* HL_DEBUG_CODEGEN
*/
class debug {
const bool logging;
public:
debug(int verbosity)
: logging(verbosity <= debug_level()) {
}
template<typename T>
debug &operator<<(T &&x) {
if (logging) {
std::cerr << std::forward<T>(x);
}
return *this;
}
static int debug_level();
};
/** Allow easily printing the contents of containers, or std::vector-like containers,
* in debug output. Used like so:
* std::vector<Type> arg_types;
* debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
* Which results in output like "arg_types: { uint8x8, uint8x8 }" on one line. */
template<typename T>
struct PrintSpan {
const T &span;
PrintSpan(const T &span)
: span(span) {
}
};
// Class template argument deduction (CTAD) guide to prevent warnings.
template<typename T>
PrintSpan(const T &) -> PrintSpan<T>;
template<typename StreamT, typename T>
inline StreamT &operator<<(StreamT &stream, const PrintSpan<T> &wrapper) {
stream << "{ ";
const char *sep = "";
for (const auto &e : wrapper.span) {
stream << sep << e;
sep = ", ";
}
stream << " }";
return stream;
}
/** Allow easily printing the contents of spans, or std::vector-like spans,
* in debug output. Used like so:
* std::vector<Type> arg_types;
* debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
* Which results in output like:
* arg_types:
* {
* uint8x8,
* uint8x8,
* }
* Indentation uses a tab character. */
template<typename T>
struct PrintSpanLn {
const T &span;
PrintSpanLn(const T &span)
: span(span) {
}
};
// Class template argument deduction (CTAD) guide to prevent warnings.
template<typename T>
PrintSpanLn(const T &) -> PrintSpanLn<T>;
template<typename StreamT, typename T>
inline StreamT &operator<<(StreamT &stream, const PrintSpanLn<T> &wrapper) {
stream << "\n{\n";
for (const auto &e : wrapper.span) {
stream << "\t" << e << ",\n";
}
stream << "}\n";
return stream;
}
} // namespace Internal
} // namespace Halide
#endif