summaryrefslogtreecommitdiff
path: root/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
diff options
context:
space:
mode:
authorMehdi Amini <joker.eph@gmail.com>2023-03-20 12:56:30 +0100
committerMehdi Amini <joker.eph@gmail.com>2023-03-20 13:39:01 +0100
commit930744fcdad7b326dd0337622b6e8dc99efcfa60 (patch)
tree567f06405a951567632e84f756ef9e97883421b7 /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
parentba7f3e1d1e50212bdc8cc438185519fd7257aa44 (diff)
Add an Observer for logging actions application to a stream
Integrate the `tracing::ExecutionContext()` into mlir-opt with a new --log-action-to=<file> option to demonstrate the feature. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D144813
Diffstat (limited to 'mlir/lib/Tools/mlir-opt/MlirOptMain.cpp')
-rw-r--r--mlir/lib/Tools/mlir-opt/MlirOptMain.cpp46
1 files changed, 45 insertions, 1 deletions
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index c9b1ae09ccbc..14f702fc999c 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -14,6 +14,8 @@
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/Debug/Counter.h"
+#include "mlir/Debug/ExecutionContext.h"
+#include "mlir/Debug/Observers/ActionLogging.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinOps.h"
@@ -71,6 +73,12 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
"parsing"),
cl::location(useExplicitModuleFlag), cl::init(false));
+ static cl::opt<std::string, /*ExternalStorage=*/true> logActionsTo{
+ "log-actions-to",
+ cl::desc("Log action execution to a file, or stderr if "
+ " '-' is passed"),
+ cl::location(logActionsToFlag)};
+
static cl::opt<bool, /*ExternalStorage=*/true> showDialects(
"show-dialects",
cl::desc("Print the list of registered dialects and exit"),
@@ -126,6 +134,41 @@ MlirOptMainConfig &MlirOptMainConfig::setPassPipelineParser(
return *this;
}
+/// Set the ExecutionContext on the context and handle the observers.
+class InstallDebugHandler {
+public:
+ InstallDebugHandler(MLIRContext &context, const MlirOptMainConfig &config) {
+ if (config.getLogActionsTo().empty()) {
+ if (tracing::DebugCounter::isActivated())
+ context.registerActionHandler(tracing::DebugCounter());
+ return;
+ }
+ if (tracing::DebugCounter::isActivated())
+ emitError(UnknownLoc::get(&context),
+ "Debug counters are incompatible with --log-actions-to option "
+ "and are disabled");
+ std::string errorMessage;
+ logActionsFile = openOutputFile(config.getLogActionsTo(), &errorMessage);
+ if (!logActionsFile) {
+ emitError(UnknownLoc::get(&context),
+ "Opening file for --log-actions-to failed: ")
+ << errorMessage << "\n";
+ return;
+ }
+ logActionsFile->keep();
+ raw_fd_ostream &logActionsStream = logActionsFile->os();
+ actionLogger = std::make_unique<tracing::ActionLogger>(logActionsStream);
+
+ executionContext.registerObserver(actionLogger.get());
+ context.registerActionHandler(executionContext);
+ }
+
+private:
+ std::unique_ptr<llvm::ToolOutputFile> logActionsFile;
+ std::unique_ptr<tracing::ActionLogger> actionLogger;
+ tracing::ExecutionContext executionContext;
+};
+
/// Perform the actions on the input file indicated by the command line flags
/// within the specified context.
///
@@ -213,7 +256,8 @@ static LogicalResult processBuffer(raw_ostream &os,
context.allowUnregisteredDialects(config.shouldAllowUnregisteredDialects());
if (config.shouldVerifyDiagnostics())
context.printOpOnDiagnostic(false);
- context.registerActionHandler(tracing::DebugCounter());
+
+ InstallDebugHandler installDebugHandler(context, config);
// If we are in verify diagnostics mode then we have a lot of work to do,
// otherwise just perform the actions without worrying about it.