-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add filtering capabilities to HL_DEBUG_CODEGEN #8627
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5226ae4
Add filtering capabilities to HL_DEBUG_CODEGEN
alexreinking c335953
Fix handling of empty strings
alexreinking e046b6e
Fix Mullapudi2016
alexreinking a84a9e1
Add <optional> to Debug.cpp
alexreinking 96adf09
Update syntax to friendlier form
alexreinking 58f3c69
fixup! Update syntax to friendlier form
alexreinking 64394b4
Add <algorithm> for MSVC
alexreinking 0e72940
Clean up complexity handling
alexreinking f05f49a
Use Voidifier trick to defer debug string evaluation.
alexreinking 7fc1f6d
Use Voidifier trick to defer debug string evaluation.
alexreinking a581205
Fix debug_is_active check in error handling
alexreinking bd51200
Oops
alexreinking 626b2c2
Appease clang-tidy
alexreinking 4e53db2
Hide debug, debug_is_active macros
alexreinking 73b3a2c
Enable debug() macro in tests
alexreinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,117 @@ | ||
#include "Debug.h" | ||
#include "Error.h" | ||
#include "Util.h" | ||
|
||
namespace Halide { | ||
namespace Internal { | ||
#include <algorithm> | ||
#include <climits> | ||
#include <optional> | ||
|
||
int debug::debug_level() { | ||
static int cached_debug_level = ([]() -> int { | ||
std::string lvl = get_env_variable("HL_DEBUG_CODEGEN"); | ||
return !lvl.empty() ? atoi(lvl.c_str()) : 0; | ||
})(); | ||
return cached_debug_level; | ||
namespace Halide::Internal { | ||
|
||
namespace { | ||
|
||
std::string read_until(const char *&str, const char *delims) { | ||
const char *start = str; | ||
for (; *str; ++str) { | ||
for (const char *ch = delims; *ch; ++ch) { | ||
if (*str == *ch) { | ||
return {start, str}; | ||
} | ||
} | ||
} | ||
return {start, str}; | ||
} | ||
|
||
bool parse_int(const std::string &number, int &value) { | ||
const char *start = number.c_str(); | ||
char *end; | ||
value = static_cast<int>(strtol(start, &end, 10)); | ||
return start < end && *end == '\0'; | ||
} | ||
|
||
class DebugRule { | ||
int verbosity = 0; | ||
std::string file_suffix = ""; | ||
int line_low = -1; | ||
int line_high = INT_MAX; | ||
std::string function_suffix = ""; | ||
enum Complexity { VerbosityOnly, | ||
NeedsMatching } complexity = VerbosityOnly; | ||
|
||
public: | ||
static std::optional<DebugRule> parse(const std::string &spec) { | ||
DebugRule rule; | ||
const char *ptr = spec.c_str(); | ||
|
||
if (!parse_int(read_until(ptr, ",@"), rule.verbosity)) { | ||
return std::nullopt; | ||
} | ||
|
||
if (*ptr == '\0') { | ||
return rule; | ||
} | ||
|
||
if (*ptr == ',') { | ||
rule.file_suffix = read_until(++ptr, ":@"); | ||
if (*ptr == ':') { | ||
if (!parse_int(read_until(++ptr, "-@"), rule.line_low)) { | ||
return std::nullopt; | ||
} | ||
rule.line_high = rule.line_low; | ||
if (*ptr == '-') { | ||
if (!parse_int(read_until(++ptr, "@"), rule.line_high)) { | ||
return std::nullopt; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (*ptr == '@') { | ||
rule.function_suffix = std::string{ptr + 1}; | ||
} | ||
|
||
rule.complexity = NeedsMatching; | ||
return rule; | ||
} | ||
|
||
bool accepts(const int verbosity, const char *file, const char *function, | ||
const int line) const { | ||
switch (complexity) { | ||
case VerbosityOnly: | ||
return verbosity <= this->verbosity; | ||
case NeedsMatching: | ||
return verbosity <= this->verbosity && | ||
ends_with(file, file_suffix) && | ||
ends_with(function, function_suffix) && | ||
line_low <= line && line <= line_high; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
std::vector<DebugRule> parse_rules(const std::string &env) { | ||
std::vector<DebugRule> rules; | ||
for (const std::string &spec : split_string(env, ";")) { | ||
if (auto rule = DebugRule::parse(spec)) { | ||
rules.push_back(*rule); | ||
} else if (!spec.empty()) { | ||
user_warning | ||
<< "Ignoring malformed HL_DEBUG_CODEGEN entry: [" << spec << "]\n" | ||
<< "The expected format is:\n " | ||
<< "verbosity[,filename[:line_low[-line_high]]][@func]"; | ||
} | ||
} | ||
return rules; | ||
} | ||
|
||
} // namespace | ||
|
||
bool debug_is_active_impl(const int verbosity, const char *file, const char *function, | ||
const int line) { | ||
static const std::vector<DebugRule> rules = parse_rules(get_env_variable("HL_DEBUG_CODEGEN")); | ||
return std::any_of(rules.begin(), rules.end(), [&](const auto &rule) { | ||
return rule.accepts(verbosity, file, function, line); | ||
}); | ||
} | ||
|
||
} // namespace Internal | ||
} // namespace Halide | ||
} // namespace Halide::Internal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.