2019-03-17 11:34:23 +08:00
|
|
|
//===- PassManagerOptions.cpp - PassManager Command Line Options ----------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-17 11:34:23 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
#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 {
|
2019-10-11 10:19:11 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Crash Reproducer Generator
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
llvm::cl::opt<std::string> reproducerFile{
|
|
|
|
"pass-pipeline-crash-reproducer",
|
|
|
|
llvm::cl::desc("Generate a .mlir reproducer file at the given output path"
|
|
|
|
" if the pass manager crashes or fails")};
|
|
|
|
|
2019-07-09 05:05:37 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Multi-threading
|
|
|
|
//===--------------------------------------------------------------------===//
|
2019-10-11 10:13:44 +08:00
|
|
|
llvm::cl::opt<bool> disableThreads{
|
|
|
|
"disable-pass-threading",
|
|
|
|
llvm::cl::desc("Disable multithreading in the pass manager"),
|
|
|
|
llvm::cl::init(false)};
|
2019-07-09 05:05:37 +08:00
|
|
|
|
2019-03-17 11:34:23 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// IR Printing
|
|
|
|
//===--------------------------------------------------------------------===//
|
2019-10-11 10:13:44 +08:00
|
|
|
PassPipelineCLParser printBefore{"print-ir-before",
|
|
|
|
"Print IR before specified passes"};
|
|
|
|
PassPipelineCLParser printAfter{"print-ir-after",
|
|
|
|
"Print IR after specified passes"};
|
|
|
|
llvm::cl::opt<bool> printBeforeAll{
|
|
|
|
"print-ir-before-all", llvm::cl::desc("Print IR before each pass"),
|
|
|
|
llvm::cl::init(false)};
|
|
|
|
llvm::cl::opt<bool> printAfterAll{"print-ir-after-all",
|
|
|
|
llvm::cl::desc("Print IR after each pass"),
|
|
|
|
llvm::cl::init(false)};
|
2019-12-07 09:04:24 +08:00
|
|
|
llvm::cl::opt<bool> printAfterChange{
|
|
|
|
"print-ir-after-change",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"When printing the IR after a pass, only print if the IR changed"),
|
|
|
|
llvm::cl::init(false)};
|
2019-10-11 10:13:44 +08:00
|
|
|
llvm::cl::opt<bool> printModuleScope{
|
|
|
|
"print-ir-module-scope",
|
|
|
|
llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} "
|
|
|
|
"always print the top-level module operation"),
|
|
|
|
llvm::cl::init(false)};
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
/// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
|
|
|
|
void addPrinterInstrumentation(PassManager &pm);
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Pass Timing
|
|
|
|
//===--------------------------------------------------------------------===//
|
2019-10-11 10:13:44 +08:00
|
|
|
llvm::cl::opt<bool> passTiming{
|
|
|
|
"pass-timing",
|
|
|
|
llvm::cl::desc("Display the execution times of each pass")};
|
2019-12-06 03:52:58 +08:00
|
|
|
llvm::cl::opt<PassDisplayMode> passTimingDisplayMode{
|
2019-10-11 10:13:44 +08:00
|
|
|
"pass-timing-display",
|
|
|
|
llvm::cl::desc("Display method for pass timing data"),
|
2019-12-06 03:52:58 +08:00
|
|
|
llvm::cl::init(PassDisplayMode::Pipeline),
|
2019-10-11 10:13:44 +08:00
|
|
|
llvm::cl::values(
|
2019-12-06 03:52:58 +08:00
|
|
|
clEnumValN(PassDisplayMode::List, "list",
|
2019-10-11 10:13:44 +08:00
|
|
|
"display the results in a list sorted by total time"),
|
2019-12-06 03:52:58 +08:00
|
|
|
clEnumValN(PassDisplayMode::Pipeline, "pipeline",
|
|
|
|
"display the results with a nested pipeline view"))};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Pass Statistics
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
llvm::cl::opt<bool> passStatistics{
|
|
|
|
"pass-statistics", llvm::cl::desc("Display the statistics of each pass")};
|
|
|
|
llvm::cl::opt<PassDisplayMode> passStatisticsDisplayMode{
|
|
|
|
"pass-statistics-display",
|
|
|
|
llvm::cl::desc("Display method for pass statistics"),
|
|
|
|
llvm::cl::init(PassDisplayMode::Pipeline),
|
|
|
|
llvm::cl::values(
|
|
|
|
clEnumValN(
|
|
|
|
PassDisplayMode::List, "list",
|
|
|
|
"display the results in a merged list sorted by pass name"),
|
|
|
|
clEnumValN(PassDisplayMode::Pipeline, "pipeline",
|
2019-10-11 10:13:44 +08:00
|
|
|
"display the results with a nested pipeline view"))};
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
/// Add a pass timing instrumentation if enabled by 'pass-timing' flags.
|
|
|
|
void addTimingInstrumentation(PassManager &pm);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-04-12 14:11:51 +08:00
|
|
|
static llvm::ManagedStatic<PassManagerOptions> options;
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
/// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
|
|
|
|
void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) {
|
2019-12-06 06:52:28 +08:00
|
|
|
std::function<bool(Pass *, Operation *)> shouldPrintBeforePass;
|
|
|
|
std::function<bool(Pass *, Operation *)> shouldPrintAfterPass;
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
// Handle print-before.
|
|
|
|
if (printBeforeAll) {
|
|
|
|
// If we are printing before all, then just return true for the filter.
|
2019-12-06 06:52:28 +08:00
|
|
|
shouldPrintBeforePass = [](Pass *, Operation *) { return true; };
|
2019-09-14 03:09:50 +08:00
|
|
|
} else if (printBefore.hasAnyOccurrences()) {
|
2019-03-17 11:34:23 +08:00
|
|
|
// 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.
|
2019-12-06 06:52:28 +08:00
|
|
|
shouldPrintBeforePass = [&](Pass *pass, Operation *) {
|
2019-03-17 11:34:23 +08:00
|
|
|
auto *passInfo = pass->lookupPassInfo();
|
2019-09-14 03:09:50 +08:00
|
|
|
return passInfo && printBefore.contains(passInfo);
|
2019-03-17 11:34:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle print-after.
|
|
|
|
if (printAfterAll) {
|
|
|
|
// If we are printing after all, then just return true for the filter.
|
2019-12-06 06:52:28 +08:00
|
|
|
shouldPrintAfterPass = [](Pass *, Operation *) { return true; };
|
2019-09-14 03:09:50 +08:00
|
|
|
} else if (printAfter.hasAnyOccurrences()) {
|
2019-03-17 11:34:23 +08:00
|
|
|
// 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.
|
2019-12-06 06:52:28 +08:00
|
|
|
shouldPrintAfterPass = [&](Pass *pass, Operation *) {
|
2019-03-17 11:34:23 +08:00
|
|
|
auto *passInfo = pass->lookupPassInfo();
|
2019-09-14 03:09:50 +08:00
|
|
|
return passInfo && printAfter.contains(passInfo);
|
2019-03-17 11:34:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no valid printing filters, then just return.
|
|
|
|
if (!shouldPrintBeforePass && !shouldPrintAfterPass)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, add the IR printing instrumentation.
|
|
|
|
pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
|
2019-12-07 09:04:24 +08:00
|
|
|
printModuleScope, printAfterChange, llvm::errs());
|
2019-03-17 11:34:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a pass timing instrumentation if enabled by 'pass-timing' flags.
|
|
|
|
void PassManagerOptions::addTimingInstrumentation(PassManager &pm) {
|
|
|
|
if (passTiming)
|
2020-04-28 07:38:17 +08:00
|
|
|
pm.enableTiming(
|
|
|
|
std::make_unique<PassManager::PassTimingConfig>(passTimingDisplayMode));
|
2019-03-17 11:34:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mlir::registerPassManagerCLOptions() {
|
2020-04-12 14:11:51 +08:00
|
|
|
// Make sure that the options struct has been constructed.
|
|
|
|
*options;
|
2019-03-17 11:34:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mlir::applyPassManagerCLOptions(PassManager &pm) {
|
2020-04-12 14:11:51 +08:00
|
|
|
if (!options.isConstructed())
|
|
|
|
return;
|
|
|
|
|
2019-10-11 10:19:11 +08:00
|
|
|
// Generate a reproducer on crash/failure.
|
2020-04-12 14:11:51 +08:00
|
|
|
if (options->reproducerFile.getNumOccurrences())
|
|
|
|
pm.enableCrashReproducerGeneration(options->reproducerFile);
|
2019-10-11 10:19:11 +08:00
|
|
|
|
2019-07-09 05:05:37 +08:00
|
|
|
// Disable multi-threading.
|
2020-04-12 14:11:51 +08:00
|
|
|
if (options->disableThreads)
|
2019-07-09 05:05:37 +08:00
|
|
|
pm.disableMultithreading();
|
|
|
|
|
2019-12-06 03:52:58 +08:00
|
|
|
// Enable statistics dumping.
|
2020-04-12 14:11:51 +08:00
|
|
|
if (options->passStatistics)
|
|
|
|
pm.enableStatistics(options->passStatisticsDisplayMode);
|
2019-12-06 03:52:58 +08:00
|
|
|
|
2019-03-17 11:34:23 +08:00
|
|
|
// Add the IR printing instrumentation.
|
2020-04-12 14:11:51 +08:00
|
|
|
options->addPrinterInstrumentation(pm);
|
2019-03-17 11:34:23 +08:00
|
|
|
|
|
|
|
// 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.
|
2020-04-12 14:11:51 +08:00
|
|
|
options->addTimingInstrumentation(pm);
|
2019-03-17 11:34:23 +08:00
|
|
|
}
|