|
1 | 1 | #include "Debug.h" |
| 2 | +#include "Error.h" |
2 | 3 | #include "Util.h" |
3 | 4 |
|
4 | | -namespace Halide { |
5 | | -namespace Internal { |
| 5 | +#include <algorithm> |
| 6 | +#include <climits> |
| 7 | +#include <optional> |
6 | 8 |
|
7 | | -int debug::debug_level() { |
8 | | - static int cached_debug_level = ([]() -> int { |
9 | | - std::string lvl = get_env_variable("HL_DEBUG_CODEGEN"); |
10 | | - return !lvl.empty() ? atoi(lvl.c_str()) : 0; |
11 | | - })(); |
12 | | - return cached_debug_level; |
| 9 | +namespace Halide::Internal { |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +std::string read_until(const char *&str, const char *delims) { |
| 14 | + const char *start = str; |
| 15 | + for (; *str; ++str) { |
| 16 | + for (const char *ch = delims; *ch; ++ch) { |
| 17 | + if (*str == *ch) { |
| 18 | + return {start, str}; |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + return {start, str}; |
| 23 | +} |
| 24 | + |
| 25 | +bool parse_int(const std::string &number, int &value) { |
| 26 | + const char *start = number.c_str(); |
| 27 | + char *end; |
| 28 | + value = static_cast<int>(strtol(start, &end, 10)); |
| 29 | + return start < end && *end == '\0'; |
| 30 | +} |
| 31 | + |
| 32 | +class DebugRule { |
| 33 | + int verbosity = 0; |
| 34 | + std::string file_suffix = ""; |
| 35 | + int line_low = -1; |
| 36 | + int line_high = INT_MAX; |
| 37 | + std::string function_suffix = ""; |
| 38 | + enum Complexity { VerbosityOnly, |
| 39 | + NeedsMatching } complexity = VerbosityOnly; |
| 40 | + |
| 41 | +public: |
| 42 | + static std::optional<DebugRule> parse(const std::string &spec) { |
| 43 | + DebugRule rule; |
| 44 | + const char *ptr = spec.c_str(); |
| 45 | + |
| 46 | + if (!parse_int(read_until(ptr, ",@"), rule.verbosity)) { |
| 47 | + return std::nullopt; |
| 48 | + } |
| 49 | + |
| 50 | + if (*ptr == '\0') { |
| 51 | + return rule; |
| 52 | + } |
| 53 | + |
| 54 | + if (*ptr == ',') { |
| 55 | + rule.file_suffix = read_until(++ptr, ":@"); |
| 56 | + if (*ptr == ':') { |
| 57 | + if (!parse_int(read_until(++ptr, "-@"), rule.line_low)) { |
| 58 | + return std::nullopt; |
| 59 | + } |
| 60 | + rule.line_high = rule.line_low; |
| 61 | + if (*ptr == '-') { |
| 62 | + if (!parse_int(read_until(++ptr, "@"), rule.line_high)) { |
| 63 | + return std::nullopt; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (*ptr == '@') { |
| 70 | + rule.function_suffix = std::string{ptr + 1}; |
| 71 | + } |
| 72 | + |
| 73 | + rule.complexity = NeedsMatching; |
| 74 | + return rule; |
| 75 | + } |
| 76 | + |
| 77 | + bool accepts(const int verbosity, const char *file, const char *function, |
| 78 | + const int line) const { |
| 79 | + switch (complexity) { |
| 80 | + case VerbosityOnly: |
| 81 | + return verbosity <= this->verbosity; |
| 82 | + case NeedsMatching: |
| 83 | + return verbosity <= this->verbosity && |
| 84 | + ends_with(file, file_suffix) && |
| 85 | + ends_with(function, function_suffix) && |
| 86 | + line_low <= line && line <= line_high; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +std::vector<DebugRule> parse_rules(const std::string &env) { |
| 93 | + std::vector<DebugRule> rules; |
| 94 | + for (const std::string &spec : split_string(env, ";")) { |
| 95 | + if (auto rule = DebugRule::parse(spec)) { |
| 96 | + rules.push_back(*rule); |
| 97 | + } else if (!spec.empty()) { |
| 98 | + user_warning |
| 99 | + << "Ignoring malformed HL_DEBUG_CODEGEN entry: [" << spec << "]\n" |
| 100 | + << "The expected format is:\n " |
| 101 | + << "verbosity[,filename[:line_low[-line_high]]][@func]"; |
| 102 | + } |
| 103 | + } |
| 104 | + return rules; |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace |
| 108 | + |
| 109 | +bool debug_is_active_impl(const int verbosity, const char *file, const char *function, |
| 110 | + const int line) { |
| 111 | + static const std::vector<DebugRule> rules = parse_rules(get_env_variable("HL_DEBUG_CODEGEN")); |
| 112 | + return std::any_of(rules.begin(), rules.end(), [&](const auto &rule) { |
| 113 | + return rule.accepts(verbosity, file, function, line); |
| 114 | + }); |
13 | 115 | } |
14 | 116 |
|
15 | | -} // namespace Internal |
16 | | -} // namespace Halide |
| 117 | +} // namespace Halide::Internal |
0 commit comments