18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/ToolOutputFile.h"
28 DebugConfigCLOptions() {
29 static cl::opt<std::string,
true> logActionsTo{
31 cl::desc(
"Log action execution to a file, or stderr if "
33 cl::location(logActionsToFlag)};
35 static cl::opt<std::string,
true> profileActionsTo{
37 cl::desc(
"Profile action execution to a file, or stderr if "
39 cl::location(profileActionsToFlag)};
41 static cl::list<std::string> logActionLocationFilter(
42 "log-mlir-actions-filter",
44 "Comma separated list of locations to filter actions from logging"),
46 cl::cb<void, std::string>([&](
const std::string &location) {
47 static bool registerOnce = [&] {
48 addLogActionLocFilter(&locBreakpointManager);
52 static std::vector<std::string> locations;
53 locations.push_back(location);
54 StringRef locStr = locations.back();
57 auto diag = [](Twine msg) { llvm::errs() << msg <<
"\n"; };
60 if (
failed(locBreakpoint)) {
61 llvm::errs() <<
"Invalid location filter: " << locStr <<
"\n";
64 auto [file, line, col] = *locBreakpoint;
65 locBreakpointManager.addBreakpoint(file, line, col);
68 static cl::opt<bool,
true> enableDebuggerHook(
69 "mlir-enable-debugger-hook",
70 cl::desc(
"Enable Debugger hook for debugging MLIR Actions"),
71 cl::location(enableDebuggerActionHookFlag), cl::init(
false));
73 tracing::FileLineColLocBreakpointManager locBreakpointManager;
93 errs() <<
"ExecutionContext registered on the context";
96 "Debug counters are incompatible with --log-actions-to and "
97 "--mlir-enable-debugger-hook options and are disabled");
99 std::string errorMessage;
101 if (!logActionsFile) {
103 "Opening file for --log-actions-to failed: ")
104 << errorMessage <<
"\n";
107 logActionsFile->keep();
108 raw_fd_ostream &logActionsStream = logActionsFile->os();
109 actionLogger = std::make_unique<tracing::ActionLogger>(logActionsStream);
111 actionLogger->addBreakpointManager(locationBreakpoint);
112 executionContext.registerObserver(actionLogger.get());
116 std::string errorMessage;
119 if (!profileActionsFile) {
121 "Opening file for --profile-actions-to failed: ")
122 << errorMessage <<
"\n";
125 profileActionsFile->keep();
126 raw_fd_ostream &profileActionsStream = profileActionsFile->os();
128 std::make_unique<tracing::ActionProfiler>(profileActionsStream);
129 executionContext.registerObserver(actionProfiler.get());
133 errs() <<
" (with Debugger hook)";
141 std::unique_ptr<ToolOutputFile> logActionsFile;
143 std::unique_ptr<tracing::ActionLogger> actionLogger;
144 std::vector<std::unique_ptr<tracing::FileLineColLocBreakpoint>>
146 std::unique_ptr<ToolOutputFile> profileActionsFile;
147 std::unique_ptr<tracing::ActionProfiler> actionProfiler;
152 :
impl(std::make_unique<
Impl>(context, config)) {}
static ManagedStatic< DebugConfigCLOptions > clOptionsConfig
static std::string diag(const llvm::Value &value)
Impl(MLIRContext &context, const DebugConfig &config)
MLIRContext is the top-level object for a collection of MLIR operations.
void registerActionHandler(HandlerTy handler)
Register a handler for handling actions that are dispatched through this context.
ArrayRef< tracing::BreakpointManager * > getLogActionsLocFilters() const
Get the location breakpoint managers to use to filter out action logging.
static DebugConfig createFromCLOptions()
Create a new config with the default set from the CL options.
StringRef getLogActionsTo() const
Get the filename to use for logging actions.
StringRef getProfileActionsTo() const
Get the filename to use for profiling actions.
static void registerCLOptions()
Register the options as global LLVM command line options.
bool isDebuggerActionHookEnabled() const
Return true if the debugger action hook is enabled.
This class implements an action handler that attaches a counter value to debug actions and enables/di...
static bool isActivated()
Returns true if any of the CL options are activated.
The ExecutionContext is the main orchestration of the infrastructure, it acts as a handler in the MLI...
static FailureOr< std::tuple< StringRef, int64_t, int64_t > > parseFromString(StringRef str, llvm::function_ref< void(Twine)> diag=[](Twine) {})
Parse a string representation in the form of "<file>:<line>:<col>".
InstallDebugHandler(MLIRContext &context, const DebugConfig &config)
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
std::unique_ptr< llvm::ToolOutputFile > openOutputFile(llvm::StringRef outputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for writing.
void setupDebuggerExecutionContextHook(tracing::ExecutionContext &executionContext)
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.