MLIR  19.0.0git
PassManagerOptions.cpp
Go to the documentation of this file.
1 //===- PassManagerOptions.cpp - PassManager Command Line Options ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Pass/Pass.h"
10 #include "mlir/Pass/PassManager.h"
11 #include "mlir/Pass/PassRegistry.h"
12 #include "mlir/Support/Timing.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/ManagedStatic.h"
15 
16 using namespace mlir;
17 
18 namespace {
19 struct PassManagerOptions {
20  //===--------------------------------------------------------------------===//
21  // Crash Reproducer Generator
22  //===--------------------------------------------------------------------===//
23  llvm::cl::opt<std::string> reproducerFile{
24  "mlir-pass-pipeline-crash-reproducer",
25  llvm::cl::desc("Generate a .mlir reproducer file at the given output path"
26  " if the pass manager crashes or fails")};
27  llvm::cl::opt<bool> localReproducer{
28  "mlir-pass-pipeline-local-reproducer",
29  llvm::cl::desc("When generating a crash reproducer, attempt to generated "
30  "a reproducer with the smallest pipeline."),
31  llvm::cl::init(false)};
32 
33  //===--------------------------------------------------------------------===//
34  // IR Printing
35  //===--------------------------------------------------------------------===//
36  PassNameCLParser printBefore{"mlir-print-ir-before",
37  "Print IR before specified passes"};
38  PassNameCLParser printAfter{"mlir-print-ir-after",
39  "Print IR after specified passes"};
40  llvm::cl::opt<bool> printBeforeAll{
41  "mlir-print-ir-before-all", llvm::cl::desc("Print IR before each pass"),
42  llvm::cl::init(false)};
43  llvm::cl::opt<bool> printAfterAll{"mlir-print-ir-after-all",
44  llvm::cl::desc("Print IR after each pass"),
45  llvm::cl::init(false)};
46  llvm::cl::opt<bool> printAfterChange{
47  "mlir-print-ir-after-change",
48  llvm::cl::desc(
49  "When printing the IR after a pass, only print if the IR changed"),
50  llvm::cl::init(false)};
51  llvm::cl::opt<bool> printAfterFailure{
52  "mlir-print-ir-after-failure",
53  llvm::cl::desc(
54  "When printing the IR after a pass, only print if the pass failed"),
55  llvm::cl::init(false)};
56  llvm::cl::opt<bool> printModuleScope{
57  "mlir-print-ir-module-scope",
58  llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} "
59  "always print the top-level operation"),
60  llvm::cl::init(false)};
61  llvm::cl::opt<std::string> printTreeDir{
62  "mlir-print-ir-tree-dir",
63  llvm::cl::desc("When printing the IR before/after a pass, print file "
64  "tree rooted at this directory")};
65 
66  /// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
67  void addPrinterInstrumentation(PassManager &pm);
68 
69  //===--------------------------------------------------------------------===//
70  // Pass Statistics
71  //===--------------------------------------------------------------------===//
72  llvm::cl::opt<bool> passStatistics{
73  "mlir-pass-statistics",
74  llvm::cl::desc("Display the statistics of each pass")};
75  llvm::cl::opt<PassDisplayMode> passStatisticsDisplayMode{
76  "mlir-pass-statistics-display",
77  llvm::cl::desc("Display method for pass statistics"),
78  llvm::cl::init(PassDisplayMode::Pipeline),
79  llvm::cl::values(
80  clEnumValN(
81  PassDisplayMode::List, "list",
82  "display the results in a merged list sorted by pass name"),
83  clEnumValN(PassDisplayMode::Pipeline, "pipeline",
84  "display the results with a nested pipeline view"))};
85 };
86 } // namespace
87 
88 static llvm::ManagedStatic<PassManagerOptions> options;
89 
90 /// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
91 void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) {
92  std::function<bool(Pass *, Operation *)> shouldPrintBeforePass;
93  std::function<bool(Pass *, Operation *)> shouldPrintAfterPass;
94 
95  // Handle print-before.
96  if (printBeforeAll) {
97  // If we are printing before all, then just return true for the filter.
98  shouldPrintBeforePass = [](Pass *, Operation *) { return true; };
99  } else if (printBefore.hasAnyOccurrences()) {
100  // Otherwise if there are specific passes to print before, then check to see
101  // if the pass info for the current pass is included in the list.
102  shouldPrintBeforePass = [&](Pass *pass, Operation *) {
103  auto *passInfo = pass->lookupPassInfo();
104  return passInfo && printBefore.contains(passInfo);
105  };
106  }
107 
108  // Handle print-after.
109  if (printAfterAll || printAfterFailure) {
110  // If we are printing after all or failure, then just return true for the
111  // filter.
112  shouldPrintAfterPass = [](Pass *, Operation *) { return true; };
113  } else if (printAfter.hasAnyOccurrences()) {
114  // Otherwise if there are specific passes to print after, then check to see
115  // if the pass info for the current pass is included in the list.
116  shouldPrintAfterPass = [&](Pass *pass, Operation *) {
117  auto *passInfo = pass->lookupPassInfo();
118  return passInfo && printAfter.contains(passInfo);
119  };
120  }
121 
122  // If there are no valid printing filters, then just return.
123  if (!shouldPrintBeforePass && !shouldPrintAfterPass)
124  return;
125 
126  // Otherwise, add the IR printing instrumentation.
127  if (!printTreeDir.empty()) {
128  pm.enableIRPrintingToFileTree(shouldPrintBeforePass, shouldPrintAfterPass,
129  printModuleScope, printAfterChange,
130  printAfterFailure, printTreeDir);
131  return;
132  }
133 
134  pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
135  printModuleScope, printAfterChange, printAfterFailure,
136  llvm::errs());
137 }
138 
140  // Make sure that the options struct has been constructed.
141  *options;
142 }
143 
145  if (!options.isConstructed())
146  return failure();
147 
148  // Generate a reproducer on crash/failure.
149  if (options->reproducerFile.getNumOccurrences())
150  pm.enableCrashReproducerGeneration(options->reproducerFile,
151  options->localReproducer);
152 
153  // Enable statistics dumping.
154  if (options->passStatistics)
155  pm.enableStatistics(options->passStatisticsDisplayMode);
156 
157  if (options->printModuleScope && pm.getContext()->isMultithreadingEnabled()) {
159  << "IR print for module scope can't be setup on a pass-manager "
160  "without disabling multi-threading first.\n";
161  return failure();
162  }
163 
164  // Add the IR printing instrumentation.
165  options->addPrinterInstrumentation(pm);
166  return success();
167 }
168 
170  // Create a temporary timing manager for the PM to own, apply its CL options,
171  // and pass it to the PM.
172  auto tm = std::make_unique<DefaultTimingManager>();
174  pm.enableTiming(std::move(tm));
175 }
static llvm::ManagedStatic< PassManagerOptions > options
bool isMultithreadingEnabled()
Return true if multi-threading is enabled by the context.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
The main pass manager and pipeline builder.
Definition: PassManager.h:232
void enableStatistics(PassDisplayMode displayMode=PassDisplayMode::Pipeline)
Prompts the pass manager to print the statistics collected for each of the held passes after each cal...
MLIRContext * getContext() const
Return an instance of the context.
Definition: PassManager.h:257
void enableIRPrinting(std::unique_ptr< IRPrinterConfig > config)
Add an instrumentation to print the IR before and after pass execution, using the provided configurat...
Definition: IRPrinting.cpp:352
void enableIRPrintingToFileTree(std::function< bool(Pass *, Operation *)> shouldPrintBeforePass=[](Pass *, Operation *) { return true;}, std::function< bool(Pass *, Operation *)> shouldPrintAfterPass=[](Pass *, Operation *) { return true;}, bool printModuleScope=true, bool printAfterOnlyOnChange=true, bool printAfterOnlyOnFailure=false, llvm::StringRef printTreeDir=".pass_manager_output", OpPrintingFlags opPrintingFlags=OpPrintingFlags())
Similar to enableIRPrinting above, except that instead of printing the IR to a single output stream,...
Definition: IRPrinting.cpp:375
void enableCrashReproducerGeneration(StringRef outputFile, bool genLocalReproducer=false)
Enable support for the pass manager to generate a reproducer on the event of a crash or a pass failur...
void enableTiming(TimingScope &timingScope)
Add an instrumentation to time the execution of passes and the computation of analyses.
Definition: PassTiming.cpp:148
This class implements a command-line parser specifically for MLIR pass names.
Definition: PassRegistry.h:276
The abstract base pass class.
Definition: Pass.h:52
const PassInfo * lookupPassInfo() const
Returns the pass info for this pass, or null if unknown.
Definition: Pass.h:60
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
LogicalResult applyPassManagerCLOptions(PassManager &pm)
Apply any values provided to the pass manager options that were registered with 'registerPassManagerO...
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
void registerPassManagerCLOptions()
Register a set of useful command-line options that can be used to configure a pass manager.
void applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm)
Apply any values that were registered with 'registerDefaultTimingManagerOptions' to a DefaultTimingMa...
Definition: Timing.cpp:619
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void applyDefaultTimingPassManagerCLOptions(PassManager &pm)
Apply any values provided to the timing manager options that were registered with registerDefaultTimi...
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26