Halide 22.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
52#define debug(n) \
53 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
54 if (debug_is_active((n))) std::cerr
55
56/** Allow easily printing the contents of containers, or std::vector-like containers,
57 * in debug output. Used like so:
58 * std::vector<Type> arg_types;
59 * debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
60 * Which results in output like "arg_types: { uint8x8, uint8x8 }" on one line. */
61template<typename T>
62struct PrintSpan {
63 const T &span;
64 PrintSpan(const T &span)
65 : span(span) {
66 }
67};
68// Class template argument deduction (CTAD) guide to prevent warnings.
69template<typename T>
70PrintSpan(const T &) -> PrintSpan<T>;
71
72template<typename StreamT, typename T>
73inline StreamT &operator<<(StreamT &stream, const PrintSpan<T> &wrapper) {
74 stream << "{ ";
75 const char *sep = "";
76 for (const auto &e : wrapper.span) {
77 stream << sep << e;
78 sep = ", ";
79 }
80 stream << " }";
81 return stream;
82}
83
84/** Allow easily printing the contents of spans, or std::vector-like spans,
85 * in debug output. Used like so:
86 * std::vector<Type> arg_types;
87 * debug(4) << "arg_types: " << PrintSpan(arg_types) << "\n";
88 * Which results in output like:
89 * arg_types:
90 * {
91 * uint8x8,
92 * uint8x8,
93 * }
94 * Indentation uses a tab character. */
95template<typename T>
97 const T &span;
98 PrintSpanLn(const T &span)
99 : span(span) {
100 }
101};
102// Class template argument deduction (CTAD) guide to prevent warnings.
103template<typename T>
105
106template<typename StreamT, typename T>
108 stream << "\n{\n";
109 for (const auto &e : wrapper.span) {
110 stream << "\t" << e << ",\n";
111 }
112 stream << "}\n";
113 return stream;
114}
115
116} // namespace Internal
117} // namespace Halide
118
119#endif
A halide module.
Definition Module.h:142
ConstantInterval operator<<(const ConstantInterval &a, const ConstantInterval &b)
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,...
@ 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.
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
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:62
PrintSpan(const T &span)
Definition Debug.h:64
Allow easily printing the contents of spans, or std::vector-like spans, in debug output.
Definition Debug.h:96
PrintSpanLn(const T &span)
Definition Debug.h:98
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:281