Moving the IR printing and execution timing options out of mlir-opt and into lib/Pass. We now expose two methods: registerPassManagerCLOptions and applyPassManagerCLOptions; to allow for multiple different users (mlir-opt, etc.) to opt-in to this common functionality.

PiperOrigin-RevId: 238836911
This commit is contained in:
River Riddle 2019-03-16 20:34:23 -07:00 committed by jpienaar
parent 8e7b683d1f
commit 6810c8bdc1
3 changed files with 169 additions and 89 deletions

View File

@ -123,6 +123,14 @@ private:
std::unique_ptr<PassInstrumentor> instrumentor;
};
/// Register a set of useful command-line options that can be used to configure
/// a pass manager. The values of these options can be applied via the
/// 'applyPassManagerCLOptions' method below.
void registerPassManagerCLOptions();
/// Apply any values provided to the pass manager options that were registered
/// with 'registerPassManagerOptions'.
void applyPassManagerCLOptions(PassManager &pm);
} // end namespace mlir
#endif // MLIR_PASS_PASSMANAGER_H

View File

@ -0,0 +1,153 @@
//===- PassManagerOptions.cpp - PassManager Command Line Options ----------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
using namespace mlir;
namespace {
struct PassManagerOptions {
typedef llvm::cl::list<const mlir::PassRegistryEntry *, bool, PassNameParser>
PassOptionList;
PassManagerOptions();
//===--------------------------------------------------------------------===//
// IR Printing
//===--------------------------------------------------------------------===//
PassOptionList printBefore;
PassOptionList printAfter;
llvm::cl::opt<bool> printBeforeAll;
llvm::cl::opt<bool> printAfterAll;
llvm::cl::opt<bool> printModuleScope;
/// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
void addPrinterInstrumentation(PassManager &pm);
//===--------------------------------------------------------------------===//
// Pass Timing
//===--------------------------------------------------------------------===//
llvm::cl::opt<bool> passTiming;
llvm::cl::opt<PassTimingDisplayMode> passTimingDisplayMode;
/// Add a pass timing instrumentation if enabled by 'pass-timing' flags.
void addTimingInstrumentation(PassManager &pm);
};
} // end anonymous namespace
static llvm::ManagedStatic<llvm::Optional<PassManagerOptions>> options;
PassManagerOptions::PassManagerOptions()
//===------------------------------------------------------------------===//
// IR Printing
//===------------------------------------------------------------------===//
: printBefore("print-ir-before",
llvm::cl::desc("Print IR before specified passes")),
printAfter("print-ir-after",
llvm::cl::desc("Print IR after specified passes")),
printBeforeAll("print-ir-before-all",
llvm::cl::desc("Print IR before each pass"),
llvm::cl::init(false)),
printAfterAll("print-ir-after-all",
llvm::cl::desc("Print IR after each pass"),
llvm::cl::init(false)),
printModuleScope(
"print-ir-module-scope",
llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} "
"always print "
"a module IR"),
llvm::cl::init(false)),
//===----------------------------------------------------------------===//
// Pass Timing
//===----------------------------------------------------------------===//
passTiming("pass-timing",
llvm::cl::desc("Display the execution times of each pass")),
passTimingDisplayMode(
"pass-timing-display",
llvm::cl::desc("Display method for pass timing data"),
llvm::cl::init(PassTimingDisplayMode::Pipeline),
llvm::cl::values(
clEnumValN(PassTimingDisplayMode::List, "list",
"display the results in a list sorted by total time"),
clEnumValN(PassTimingDisplayMode::Pipeline, "pipeline",
"display the results with a nested pipeline view"))) {}
/// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) {
std::function<bool(Pass *)> shouldPrintBeforePass, shouldPrintAfterPass;
// Handle print-before.
if (printBeforeAll) {
// If we are printing before all, then just return true for the filter.
shouldPrintBeforePass = [](Pass *) { return true; };
} else if (printBefore.getNumOccurrences() != 0) {
// Otherwise if there are specific passes to print before, then check to see
// if the pass info for the current pass is included in the list.
shouldPrintBeforePass = [&](Pass *pass) {
auto *passInfo = pass->lookupPassInfo();
return passInfo && llvm::is_contained(printBefore, passInfo);
};
}
// Handle print-after.
if (printAfterAll) {
// If we are printing after all, then just return true for the filter.
shouldPrintAfterPass = [](Pass *) { return true; };
} else if (printAfter.getNumOccurrences() != 0) {
// Otherwise if there are specific passes to print after, then check to see
// if the pass info for the current pass is included in the list.
shouldPrintAfterPass = [&](Pass *pass) {
auto *passInfo = pass->lookupPassInfo();
return passInfo && llvm::is_contained(printAfter, passInfo);
};
}
// If there are no valid printing filters, then just return.
if (!shouldPrintBeforePass && !shouldPrintAfterPass)
return;
// Otherwise, add the IR printing instrumentation.
pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
printModuleScope, llvm::errs());
}
/// Add a pass timing instrumentation if enabled by 'pass-timing' flags.
void PassManagerOptions::addTimingInstrumentation(PassManager &pm) {
if (passTiming)
pm.enableTiming(passTimingDisplayMode);
}
void mlir::registerPassManagerCLOptions() {
// Reset the options instance if it hasn't been enabled yet.
if (!options->hasValue())
options->emplace();
}
void mlir::applyPassManagerCLOptions(PassManager &pm) {
// Add the IR printing instrumentation.
(*options)->addPrinterInstrumentation(pm);
// Note: The pass timing instrumentation should be added last to avoid any
// potential "ghost" timing from other instrumentations being unintentionally
// included in the timing results.
(*options)->addTimingInstrumentation(pm);
}

View File

@ -68,88 +68,10 @@ static cl::opt<bool>
cl::desc("Run the verifier after each transformation pass"),
cl::init(true));
static cl::opt<bool>
passTiming("pass-timing",
cl::desc("Display the execution times of each pass"));
static cl::opt<PassTimingDisplayMode> passTimingDisplayMode(
"pass-timing-display", cl::desc("Display method for pass timing data"),
cl::init(PassTimingDisplayMode::Pipeline),
cl::values(clEnumValN(PassTimingDisplayMode::List, "list",
"display the results in a list sorted by total time"),
clEnumValN(PassTimingDisplayMode::Pipeline, "pipeline",
"display the results with a nested pipeline view")));
namespace {
typedef llvm::cl::list<const mlir::PassRegistryEntry *, bool, PassNameParser>
PassOptionList;
}
// Print IR out before/after specified passes.
static PassOptionList
printBefore("print-ir-before",
llvm::cl::desc("Print IR before specified passes"));
static PassOptionList
printAfter("print-ir-after",
llvm::cl::desc("Print IR after specified passes"));
static cl::opt<bool> printBeforeAll("print-ir-before-all",
llvm::cl::desc("Print IR before each pass"),
cl::init(false));
static cl::opt<bool> printAfterAll("print-ir-after-all",
llvm::cl::desc("Print IR after each pass"),
cl::init(false));
static cl::opt<bool> printModuleScope(
"print-ir-module-scope",
cl::desc("When printing IR for print-ir-[before|after]{-all} always print "
"a module IR"),
cl::init(false));
static std::vector<const mlir::PassRegistryEntry *> *passList;
enum OptResult { OptSuccess, OptFailure };
/// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
static void addPrinterInstrumentation(PassManager &pm) {
std::function<bool(Pass *)> shouldPrintBeforePass, shouldPrintAfterPass;
// Handle print-before.
if (printBeforeAll) {
// If we are printing before all, then just return true for the filter.
shouldPrintBeforePass = [](Pass *) { return true; };
} else if (printBefore.getNumOccurrences() != 0) {
// Otherwise if there are specific passes to print before, then check to see
// if the pass info for the current pass is included in the list.
shouldPrintBeforePass = [&](Pass *pass) {
auto *passInfo = pass->lookupPassInfo();
return passInfo && llvm::is_contained(printBefore, passInfo);
};
}
// Handle print-after.
if (printAfterAll) {
// If we are printing after all, then just return true for the filter.
shouldPrintAfterPass = [](Pass *) { return true; };
} else if (printAfter.getNumOccurrences() != 0) {
// Otherwise if there are specific passes to print after, then check to see
// if the pass info for the current pass is included in the list.
shouldPrintAfterPass = [&](Pass *pass) {
auto *passInfo = pass->lookupPassInfo();
return passInfo && llvm::is_contained(printAfter, passInfo);
};
}
// If there are no valid printing filters, then just return.
if (!shouldPrintBeforePass && !shouldPrintAfterPass)
return;
// Otherwise, add the IR printing instrumentation.
pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
printModuleScope, llvm::dbgs());
}
/// Given a MemoryBuffer along with a line and column within it, return the
/// location being referenced.
static SMLoc getLocFromLineAndCol(MemoryBuffer &membuf, unsigned lineNo,
@ -208,17 +130,10 @@ static OptResult performActions(SourceMgr &sourceMgr, MLIRContext *context) {
for (const auto *passEntry : *passList)
passEntry->addToPipeline(pm);
// Add any necessary instrumentations.
// Add the IR printing instrumentation.
addPrinterInstrumentation(pm);
// Note: The pass timing instrumentation should be added last to avoid any
// potential "ghost" timing from other instrumentations being unintentionally
// included in the timing results.
if (passTiming)
pm.enableTiming(passTimingDisplayMode);
// Apply any pass manager command line options.
applyPassManagerCLOptions(pm);
// Run the pipeline.
if (failed(pm.run(module.get())))
return OptFailure;
@ -452,8 +367,12 @@ int main(int argc, char **argv) {
llvm::PrettyStackTraceProgram x(argc, argv);
InitLLVM y(argc, argv);
// Register any pass manager command line options.
registerPassManagerCLOptions();
// Parse pass names in main to ensure static initialization completed.
PassOptionList passList("", llvm::cl::desc("Compiler passes to run"));
llvm::cl::list<const mlir::PassRegistryEntry *, bool, PassNameParser>
passList("", llvm::cl::desc("Compiler passes to run"));
::passList = &passList;
cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");