River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 1 | //===- PassManagerOptions.cpp - PassManager Command Line Options ----------===// |
| 2 | // |
Mehdi Amini | 3085710 | 2020-01-26 03:58:30 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
Mehdi Amini | 56222a0 | 2019-12-23 09:35:36 -0800 | [diff] [blame] | 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 6 | // |
Mehdi Amini | 56222a0 | 2019-12-23 09:35:36 -0800 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 8 | |
| 9 | #include "mlir/Pass/Pass.h" |
| 10 | #include "mlir/Pass/PassManager.h" |
| 11 | #include "mlir/Pass/PassRegistry.h" |
Fabian Schuiki | 33f908c | 2021-04-21 10:57:29 +0200 | [diff] [blame] | 12 | #include "mlir/Support/Timing.h" |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 13 | #include "llvm/Support/CommandLine.h" |
| 14 | #include "llvm/Support/ManagedStatic.h" |
| 15 | |
| 16 | using namespace mlir; |
| 17 | |
| 18 | namespace { |
| 19 | struct PassManagerOptions { |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 20 | //===--------------------------------------------------------------------===// |
River Riddle | 7a7dcc1 | 2019-10-10 19:19:11 -0700 | [diff] [blame] | 21 | // Crash Reproducer Generator |
| 22 | //===--------------------------------------------------------------------===// |
| 23 | llvm::cl::opt<std::string> reproducerFile{ |
| 24 | "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")}; |
River Riddle | 983382f | 2020-04-29 15:08:15 -0700 | [diff] [blame] | 27 | llvm::cl::opt<bool> localReproducer{ |
| 28 | "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)}; |
River Riddle | 7a7dcc1 | 2019-10-10 19:19:11 -0700 | [diff] [blame] | 32 | |
| 33 | //===--------------------------------------------------------------------===// |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 34 | // IR Printing |
| 35 | //===--------------------------------------------------------------------===// |
River Riddle | e9cda7c | 2020-12-15 14:42:16 -0800 | [diff] [blame] | 36 | PassNameCLParser printBefore{"print-ir-before", |
| 37 | "Print IR before specified passes"}; |
| 38 | PassNameCLParser printAfter{"print-ir-after", |
| 39 | "Print IR after specified passes"}; |
River Riddle | b245e95 | 2019-10-10 19:13:44 -0700 | [diff] [blame] | 40 | llvm::cl::opt<bool> printBeforeAll{ |
| 41 | "print-ir-before-all", llvm::cl::desc("Print IR before each pass"), |
| 42 | llvm::cl::init(false)}; |
| 43 | llvm::cl::opt<bool> printAfterAll{"print-ir-after-all", |
| 44 | llvm::cl::desc("Print IR after each pass"), |
| 45 | llvm::cl::init(false)}; |
River Riddle | 8904e91 | 2019-12-06 17:04:24 -0800 | [diff] [blame] | 46 | llvm::cl::opt<bool> printAfterChange{ |
| 47 | "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)}; |
River Riddle | 64ce90e | 2021-05-19 16:53:23 -0700 | [diff] [blame] | 51 | llvm::cl::opt<bool> printAfterFailure{ |
| 52 | "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)}; |
River Riddle | b245e95 | 2019-10-10 19:13:44 -0700 | [diff] [blame] | 56 | llvm::cl::opt<bool> printModuleScope{ |
| 57 | "print-ir-module-scope", |
| 58 | llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} " |
River Riddle | 00c6ef8 | 2020-12-03 15:46:32 -0800 | [diff] [blame] | 59 | "always print the top-level operation"), |
River Riddle | b245e95 | 2019-10-10 19:13:44 -0700 | [diff] [blame] | 60 | llvm::cl::init(false)}; |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 61 | |
| 62 | /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. |
| 63 | void addPrinterInstrumentation(PassManager &pm); |
| 64 | |
| 65 | //===--------------------------------------------------------------------===// |
River Riddle | 33a6454 | 2019-12-05 11:52:58 -0800 | [diff] [blame] | 66 | // Pass Statistics |
| 67 | //===--------------------------------------------------------------------===// |
| 68 | llvm::cl::opt<bool> passStatistics{ |
| 69 | "pass-statistics", llvm::cl::desc("Display the statistics of each pass")}; |
| 70 | llvm::cl::opt<PassDisplayMode> passStatisticsDisplayMode{ |
| 71 | "pass-statistics-display", |
| 72 | llvm::cl::desc("Display method for pass statistics"), |
| 73 | llvm::cl::init(PassDisplayMode::Pipeline), |
| 74 | llvm::cl::values( |
| 75 | clEnumValN( |
| 76 | PassDisplayMode::List, "list", |
| 77 | "display the results in a merged list sorted by pass name"), |
| 78 | clEnumValN(PassDisplayMode::Pipeline, "pipeline", |
River Riddle | b245e95 | 2019-10-10 19:13:44 -0700 | [diff] [blame] | 79 | "display the results with a nested pipeline view"))}; |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 80 | }; |
| 81 | } // end anonymous namespace |
| 82 | |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 83 | static llvm::ManagedStatic<PassManagerOptions> options; |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 84 | |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 85 | /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. |
| 86 | void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) { |
River Riddle | da53000 | 2019-12-05 14:52:28 -0800 | [diff] [blame] | 87 | std::function<bool(Pass *, Operation *)> shouldPrintBeforePass; |
| 88 | std::function<bool(Pass *, Operation *)> shouldPrintAfterPass; |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 89 | |
| 90 | // Handle print-before. |
| 91 | if (printBeforeAll) { |
| 92 | // If we are printing before all, then just return true for the filter. |
River Riddle | da53000 | 2019-12-05 14:52:28 -0800 | [diff] [blame] | 93 | shouldPrintBeforePass = [](Pass *, Operation *) { return true; }; |
River Riddle | 9274ed6 | 2019-09-13 12:09:50 -0700 | [diff] [blame] | 94 | } else if (printBefore.hasAnyOccurrences()) { |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 95 | // Otherwise if there are specific passes to print before, then check to see |
| 96 | // if the pass info for the current pass is included in the list. |
River Riddle | da53000 | 2019-12-05 14:52:28 -0800 | [diff] [blame] | 97 | shouldPrintBeforePass = [&](Pass *pass, Operation *) { |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 98 | auto *passInfo = pass->lookupPassInfo(); |
River Riddle | 9274ed6 | 2019-09-13 12:09:50 -0700 | [diff] [blame] | 99 | return passInfo && printBefore.contains(passInfo); |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | |
| 103 | // Handle print-after. |
River Riddle | 64ce90e | 2021-05-19 16:53:23 -0700 | [diff] [blame] | 104 | if (printAfterAll || printAfterFailure) { |
| 105 | // If we are printing after all or failure, then just return true for the |
| 106 | // filter. |
River Riddle | da53000 | 2019-12-05 14:52:28 -0800 | [diff] [blame] | 107 | shouldPrintAfterPass = [](Pass *, Operation *) { return true; }; |
River Riddle | 9274ed6 | 2019-09-13 12:09:50 -0700 | [diff] [blame] | 108 | } else if (printAfter.hasAnyOccurrences()) { |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 109 | // Otherwise if there are specific passes to print after, then check to see |
| 110 | // if the pass info for the current pass is included in the list. |
River Riddle | da53000 | 2019-12-05 14:52:28 -0800 | [diff] [blame] | 111 | shouldPrintAfterPass = [&](Pass *pass, Operation *) { |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 112 | auto *passInfo = pass->lookupPassInfo(); |
River Riddle | 9274ed6 | 2019-09-13 12:09:50 -0700 | [diff] [blame] | 113 | return passInfo && printAfter.contains(passInfo); |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 114 | }; |
| 115 | } |
| 116 | |
| 117 | // If there are no valid printing filters, then just return. |
| 118 | if (!shouldPrintBeforePass && !shouldPrintAfterPass) |
| 119 | return; |
| 120 | |
| 121 | // Otherwise, add the IR printing instrumentation. |
| 122 | pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass, |
River Riddle | 64ce90e | 2021-05-19 16:53:23 -0700 | [diff] [blame] | 123 | printModuleScope, printAfterChange, printAfterFailure, |
| 124 | llvm::errs()); |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 125 | } |
| 126 | |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 127 | void mlir::registerPassManagerCLOptions() { |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 128 | // Make sure that the options struct has been constructed. |
| 129 | *options; |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void mlir::applyPassManagerCLOptions(PassManager &pm) { |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 133 | if (!options.isConstructed()) |
| 134 | return; |
| 135 | |
River Riddle | 7a7dcc1 | 2019-10-10 19:19:11 -0700 | [diff] [blame] | 136 | // Generate a reproducer on crash/failure. |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 137 | if (options->reproducerFile.getNumOccurrences()) |
River Riddle | 983382f | 2020-04-29 15:08:15 -0700 | [diff] [blame] | 138 | pm.enableCrashReproducerGeneration(options->reproducerFile, |
| 139 | options->localReproducer); |
River Riddle | 7a7dcc1 | 2019-10-10 19:19:11 -0700 | [diff] [blame] | 140 | |
River Riddle | 33a6454 | 2019-12-05 11:52:58 -0800 | [diff] [blame] | 141 | // Enable statistics dumping. |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 142 | if (options->passStatistics) |
| 143 | pm.enableStatistics(options->passStatisticsDisplayMode); |
River Riddle | 33a6454 | 2019-12-05 11:52:58 -0800 | [diff] [blame] | 144 | |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 145 | // Add the IR printing instrumentation. |
River Riddle | 8938dea | 2020-04-11 23:11:51 -0700 | [diff] [blame] | 146 | options->addPrinterInstrumentation(pm); |
Fabian Schuiki | 33f908c | 2021-04-21 10:57:29 +0200 | [diff] [blame] | 147 | } |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 148 | |
Fabian Schuiki | 33f908c | 2021-04-21 10:57:29 +0200 | [diff] [blame] | 149 | void mlir::applyDefaultTimingPassManagerCLOptions(PassManager &pm) { |
| 150 | // Create a temporary timing manager for the PM to own, apply its CL options, |
| 151 | // and pass it to the PM. |
| 152 | auto tm = std::make_unique<DefaultTimingManager>(); |
| 153 | applyDefaultTimingManagerCLOptions(*tm); |
| 154 | pm.enableTiming(std::move(tm)); |
River Riddle | 6810c8b | 2019-03-16 20:34:23 -0700 | [diff] [blame] | 155 | } |