Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
//===- MlirOptMain.cpp - MLIR Optimizer Driver ----------------------------===// | //===- MlirOptMain.cpp - MLIR Optimizer Driver ----------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// | // | ||||
// This is a utility that runs an optimization pass and prints the result back | // This is a utility that runs an optimization pass and prints the result back | ||||
// out. It is designed to support unit testing. | // out. It is designed to support unit testing. | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "mlir/Tools/mlir-opt/MlirOptMain.h" | #include "mlir/Tools/mlir-opt/MlirOptMain.h" | ||||
#include "mlir/Bytecode/BytecodeWriter.h" | #include "mlir/Bytecode/BytecodeWriter.h" | ||||
#include "mlir/Debug/Counter.h" | #include "mlir/Debug/Counter.h" | ||||
#include "mlir/Debug/DebuggerExecutionContextHook.h" | |||||
#include "mlir/Debug/ExecutionContext.h" | #include "mlir/Debug/ExecutionContext.h" | ||||
#include "mlir/Debug/Observers/ActionLogging.h" | #include "mlir/Debug/Observers/ActionLogging.h" | ||||
#include "mlir/Dialect/IRDL/IR/IRDL.h" | #include "mlir/Dialect/IRDL/IR/IRDL.h" | ||||
#include "mlir/Dialect/IRDL/IRDLLoading.h" | #include "mlir/Dialect/IRDL/IRDLLoading.h" | ||||
#include "mlir/IR/AsmState.h" | #include "mlir/IR/AsmState.h" | ||||
#include "mlir/IR/Attributes.h" | #include "mlir/IR/Attributes.h" | ||||
#include "mlir/IR/BuiltinOps.h" | #include "mlir/IR/BuiltinOps.h" | ||||
#include "mlir/IR/Diagnostics.h" | #include "mlir/IR/Diagnostics.h" | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | static cl::opt<bool, /*ExternalStorage=*/true> emitBytecode( | ||||
"emit-bytecode", cl::desc("Emit bytecode when generating output"), | "emit-bytecode", cl::desc("Emit bytecode when generating output"), | ||||
cl::location(emitBytecodeFlag), cl::init(false)); | cl::location(emitBytecodeFlag), cl::init(false)); | ||||
static cl::opt<std::string, /*ExternalStorage=*/true> irdlFile( | static cl::opt<std::string, /*ExternalStorage=*/true> irdlFile( | ||||
"irdl-file", | "irdl-file", | ||||
cl::desc("IRDL file to register before processing the input"), | cl::desc("IRDL file to register before processing the input"), | ||||
cl::location(irdlFileFlag), cl::init(""), cl::value_desc("filename")); | cl::location(irdlFileFlag), cl::init(""), cl::value_desc("filename")); | ||||
static cl::opt<bool, /*ExternalStorage=*/true> enableDebuggerHook( | |||||
"mlir-enable-debugger-hook", | |||||
cl::desc("Enable Debugger hook for debugging MLIR Actions"), | |||||
cl::location(enableDebuggerActionHookFlag), cl::init(false)); | |||||
static cl::opt<bool, /*ExternalStorage=*/true> explicitModule( | static cl::opt<bool, /*ExternalStorage=*/true> explicitModule( | ||||
"no-implicit-module", | "no-implicit-module", | ||||
cl::desc("Disable implicit addition of a top-level module op during " | cl::desc("Disable implicit addition of a top-level module op during " | ||||
"parsing"), | "parsing"), | ||||
cl::location(useExplicitModuleFlag), cl::init(false)); | cl::location(useExplicitModuleFlag), cl::init(false)); | ||||
static cl::opt<std::string, /*ExternalStorage=*/true> logActionsTo{ | static cl::opt<std::string, /*ExternalStorage=*/true> logActionsTo{ | ||||
"log-actions-to", | "log-actions-to", | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | dialectPlugins->setCallback([&](const std::string &pluginPath) { | ||||
plugin.get().registerDialectRegistryCallbacks(registry); | plugin.get().registerDialectRegistryCallbacks(registry); | ||||
}); | }); | ||||
} | } | ||||
/// Set the ExecutionContext on the context and handle the observers. | /// Set the ExecutionContext on the context and handle the observers. | ||||
class InstallDebugHandler { | class InstallDebugHandler { | ||||
public: | public: | ||||
InstallDebugHandler(MLIRContext &context, const MlirOptMainConfig &config) { | InstallDebugHandler(MLIRContext &context, const MlirOptMainConfig &config) { | ||||
if (config.getLogActionsTo().empty()) { | if (config.getLogActionsTo().empty() && | ||||
!config.isDebuggerActionHookEnabled()) { | |||||
if (tracing::DebugCounter::isActivated()) | if (tracing::DebugCounter::isActivated()) | ||||
context.registerActionHandler(tracing::DebugCounter()); | context.registerActionHandler(tracing::DebugCounter()); | ||||
return; | return; | ||||
} | } | ||||
llvm::errs() << "ExecutionContext registered on the context"; | |||||
if (tracing::DebugCounter::isActivated()) | if (tracing::DebugCounter::isActivated()) | ||||
emitError(UnknownLoc::get(&context), | emitError(UnknownLoc::get(&context), | ||||
"Debug counters are incompatible with --log-actions-to option " | "Debug counters are incompatible with --log-actions-to and " | ||||
"and are disabled"); | "--mlir-enable-debugger-hook options and are disabled"); | ||||
if (!config.getLogActionsTo().empty()) { | |||||
std::string errorMessage; | std::string errorMessage; | ||||
logActionsFile = openOutputFile(config.getLogActionsTo(), &errorMessage); | logActionsFile = openOutputFile(config.getLogActionsTo(), &errorMessage); | ||||
if (!logActionsFile) { | if (!logActionsFile) { | ||||
emitError(UnknownLoc::get(&context), | emitError(UnknownLoc::get(&context), | ||||
"Opening file for --log-actions-to failed: ") | "Opening file for --log-actions-to failed: ") | ||||
<< errorMessage << "\n"; | << errorMessage << "\n"; | ||||
return; | return; | ||||
} | } | ||||
logActionsFile->keep(); | logActionsFile->keep(); | ||||
raw_fd_ostream &logActionsStream = logActionsFile->os(); | raw_fd_ostream &logActionsStream = logActionsFile->os(); | ||||
actionLogger = std::make_unique<tracing::ActionLogger>(logActionsStream); | actionLogger = std::make_unique<tracing::ActionLogger>(logActionsStream); | ||||
for (const auto *locationBreakpoint : config.getLogActionsLocFilters()) | for (const auto *locationBreakpoint : config.getLogActionsLocFilters()) | ||||
actionLogger->addBreakpointManager(locationBreakpoint); | actionLogger->addBreakpointManager(locationBreakpoint); | ||||
executionContext.registerObserver(actionLogger.get()); | executionContext.registerObserver(actionLogger.get()); | ||||
} | |||||
if (config.isDebuggerActionHookEnabled()) { | |||||
llvm::errs() << " (with Debugger hook)"; | |||||
setupDebuggerExecutionContextHook(executionContext); | |||||
} | |||||
llvm::errs() << "\n"; | |||||
context.registerActionHandler(executionContext); | context.registerActionHandler(executionContext); | ||||
} | } | ||||
private: | private: | ||||
std::unique_ptr<llvm::ToolOutputFile> logActionsFile; | std::unique_ptr<llvm::ToolOutputFile> logActionsFile; | ||||
std::unique_ptr<tracing::ActionLogger> actionLogger; | std::unique_ptr<tracing::ActionLogger> actionLogger; | ||||
std::vector<std::unique_ptr<tracing::FileLineColLocBreakpoint>> | std::vector<std::unique_ptr<tracing::FileLineColLocBreakpoint>> | ||||
locationBreakpoints; | locationBreakpoints; | ||||
▲ Show 20 Lines • Show All 277 Lines • Show Last 20 Lines |