Skip to content

Commit

Permalink
[mlir-opt] Add a '-dump-pass-pipeline' option
Browse files Browse the repository at this point in the history
Add an option to dump the pipeline that will be run to stderr. A
dedicated option is needed since the existing `test-dump-pipeline`
pipeline won't be usable with `-pass-pipeline` after D135745.

Reviewed By: rriddle, mehdi_amini

Differential Revision: https://reviews.llvm.org/D135747
  • Loading branch information
rkayaith committed Oct 20, 2022
1 parent 889911e commit cad61e4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
15 changes: 9 additions & 6 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Expand Up @@ -53,12 +53,15 @@ using PassPipelineFn = llvm::function_ref<LogicalResult(PassManager &pm)>;
/// - emitBytecode will generate bytecode output instead of text.
/// - implicitModule will enable implicit addition of a top-level
/// 'builtin.module' if one doesn't already exist.
LogicalResult MlirOptMain(
llvm::raw_ostream &outputStream, std::unique_ptr<llvm::MemoryBuffer> buffer,
const PassPipelineCLParser &passPipeline, DialectRegistry &registry,
bool splitInputFile, bool verifyDiagnostics, bool verifyPasses,
bool allowUnregisteredDialects, bool preloadDialectsInContext = false,
bool emitBytecode = false, bool implicitModule = false);
/// - dumpPassPipeline will dump the pipeline being run to stderr
LogicalResult
MlirOptMain(llvm::raw_ostream &outputStream,
std::unique_ptr<llvm::MemoryBuffer> buffer,
const PassPipelineCLParser &passPipeline, DialectRegistry &registry,
bool splitInputFile, bool verifyDiagnostics, bool verifyPasses,
bool allowUnregisteredDialects,
bool preloadDialectsInContext = false, bool emitBytecode = false,
bool implicitModule = false, bool dumpPassPipeline = false);

/// Support a callback to setup the pass manager.
/// - passManagerSetupFn is the callback invoked to setup the pass manager to
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Pass/Pass.cpp
Expand Up @@ -365,7 +365,7 @@ void OpPassManager::printAsTextualPipeline(raw_ostream &os) const {
}

void OpPassManager::dump() {
llvm::errs() << "Pass Manager with " << impl->passes.size() << " passes: ";
llvm::errs() << "Pass Manager with " << impl->passes.size() << " passes:\n";
printAsTextualPipeline(llvm::errs());
llvm::errs() << "\n";
}
Expand Down
29 changes: 19 additions & 10 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Expand Up @@ -187,20 +187,24 @@ LogicalResult mlir::MlirOptMain(raw_ostream &outputStream,
splitInputFile, /*insertMarkerInOutput=*/true);
}

LogicalResult mlir::MlirOptMain(raw_ostream &outputStream,
std::unique_ptr<MemoryBuffer> buffer,
const PassPipelineCLParser &passPipeline,
DialectRegistry &registry, bool splitInputFile,
bool verifyDiagnostics, bool verifyPasses,
bool allowUnregisteredDialects,
bool preloadDialectsInContext,
bool emitBytecode, bool implicitModule) {
LogicalResult mlir::MlirOptMain(
raw_ostream &outputStream, std::unique_ptr<MemoryBuffer> buffer,
const PassPipelineCLParser &passPipeline, DialectRegistry &registry,
bool splitInputFile, bool verifyDiagnostics, bool verifyPasses,
bool allowUnregisteredDialects, bool preloadDialectsInContext,
bool emitBytecode, bool implicitModule, bool dumpPassPipeline) {
auto passManagerSetupFn = [&](PassManager &pm) {
auto errorHandler = [&](const Twine &msg) {
emitError(UnknownLoc::get(pm.getContext())) << msg;
return failure();
};
return passPipeline.addToPipeline(pm, errorHandler);
if (failed(passPipeline.addToPipeline(pm, errorHandler)))
return failure();
if (dumpPassPipeline) {
pm.dump();
llvm::errs() << "\n";
}
return success();
};
return MlirOptMain(outputStream, std::move(buffer), passManagerSetupFn,
registry, splitInputFile, verifyDiagnostics, verifyPasses,
Expand Down Expand Up @@ -253,6 +257,10 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
"Disable implicit addition of a top-level module op during parsing"),
cl::init(false)};

static cl::opt<bool> dumpPassPipeline{
"dump-pass-pipeline", cl::desc("Print the pipeline that will be run"),
cl::init(false)};

InitLLVM y(argc, argv);

// Register any command line options.
Expand Down Expand Up @@ -298,7 +306,8 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
if (failed(MlirOptMain(output->os(), std::move(file), passPipeline, registry,
splitInputFile, verifyDiagnostics, verifyPasses,
allowUnregisteredDialects, preloadDialectsInContext,
emitBytecode, /*implicitModule=*/!noImplicitModule)))
emitBytecode, /*implicitModule=*/!noImplicitModule,
dumpPassPipeline)))
return failure();

// Keep the output file if the invocation of MlirOptMain was successful.
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/Pass/pipeline-options-parsing.mlir
Expand Up @@ -3,9 +3,9 @@
// RUN: not mlir-opt %s -pass-pipeline='builtin.module(func.func(test-options-pass{list=3}), test-module-pass{invalid-option=3})' 2>&1 | FileCheck --check-prefix=CHECK_ERROR_3 %s
// RUN: not mlir-opt %s -pass-pipeline='test-options-pass{list=3 list=notaninteger}' 2>&1 | FileCheck --check-prefix=CHECK_ERROR_4 %s
// RUN: mlir-opt %s -pass-pipeline='func.func(test-options-pass{list=1,2,3,4 list=5 string=value1 string=value2})'
// RUN: mlir-opt %s -verify-each=false -pass-pipeline='func.func(test-options-pass{string-list=a list=1,2,3,4 string-list=b,c list=5 string-list=d string=nested_pipeline{arg1=10 arg2=" {} " arg3=true}})' -test-dump-pipeline 2>&1 | FileCheck --check-prefix=CHECK_1 %s
// RUN: mlir-opt %s -verify-each=false -test-options-pass-pipeline='list=1 string-list=a,b' -test-dump-pipeline 2>&1 | FileCheck --check-prefix=CHECK_2 %s
// RUN: mlir-opt %s -verify-each=false -pass-pipeline='builtin.module(func.func(test-options-pass{list=3}), func.func(test-options-pass{list=1,2,3,4}))' -test-dump-pipeline 2>&1 | FileCheck --check-prefix=CHECK_3 %s
// RUN: mlir-opt %s -verify-each=false -pass-pipeline='func.func(test-options-pass{string-list=a list=1,2,3,4 string-list=b,c list=5 string-list=d string=nested_pipeline{arg1=10 arg2=" {} " arg3=true}})' -dump-pass-pipeline 2>&1 | FileCheck --check-prefix=CHECK_1 %s
// RUN: mlir-opt %s -verify-each=false -test-options-pass-pipeline='list=1 string-list=a,b' -dump-pass-pipeline 2>&1 | FileCheck --check-prefix=CHECK_2 %s
// RUN: mlir-opt %s -verify-each=false -pass-pipeline='builtin.module(func.func(test-options-pass{list=3}), func.func(test-options-pass{list=1,2,3,4}))' -dump-pass-pipeline 2>&1 | FileCheck --check-prefix=CHECK_3 %s

// CHECK_ERROR_1: missing closing '}' while processing pass options
// CHECK_ERROR_2: no such option test-option
Expand Down
7 changes: 0 additions & 7 deletions mlir/test/lib/Pass/TestPassManager.cpp
Expand Up @@ -228,13 +228,6 @@ void registerPassManagerTestPass() {
PassPipelineRegistration<>("test-textual-pm-nested-pipeline",
"Test a nested pipeline in the pass manager",
testNestedPipelineTextual);
PassPipelineRegistration<>(
"test-dump-pipeline",
"Dumps the pipeline build so far for debugging purposes",
[](OpPassManager &pm) {
pm.printAsTextualPipeline(llvm::errs());
llvm::errs() << "\n";
});

PassPipelineRegistration<TestOptionsPass::Options>
registerOptionsPassPipeline(
Expand Down

0 comments on commit cad61e4

Please sign in to comment.