Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an easy way to print vectors in debug output. #8072

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ class debug {
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) {
}
};

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) {
}
};

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

Expand Down
Loading