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