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> printCustomAssemblyAfterFailure{ 57 "mlir-print-custom-assembly-after-failure", 58 llvm::cl::desc( 59 "When printing the IR after a pass failure, print in custom form " 60 "instead of generic (WARNING: this is unsafe and there is no " 61 "guarantee of a crash-free or valid print"), 62 llvm::cl::init(false)}; 63 llvm::cl::opt<bool> printModuleScope{ 64 "mlir-print-ir-module-scope", 65 llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} " 66 "always print the top-level operation"), 67 llvm::cl::init(false)}; 68 69 /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. 70 void addPrinterInstrumentation(PassManager &pm); 71 72 //===--------------------------------------------------------------------===// 73 // Pass Statistics 74 //===--------------------------------------------------------------------===// 75 llvm::cl::opt<bool> passStatistics{ 76 "mlir-pass-statistics", 77 llvm::cl::desc("Display the statistics of each pass")}; 78 llvm::cl::opt<PassDisplayMode> passStatisticsDisplayMode{ 79 "mlir-pass-statistics-display", 80 llvm::cl::desc("Display method for pass statistics"), 81 llvm::cl::init(PassDisplayMode::Pipeline), 82 llvm::cl::values( 83 clEnumValN( 84 PassDisplayMode::List, "list", 85 "display the results in a merged list sorted by pass name"), 86 clEnumValN(PassDisplayMode::Pipeline, "pipeline", 87 "display the results with a nested pipeline view"))}; 88 }; 89 } // namespace 90 91 static llvm::ManagedStatic<PassManagerOptions> options; 92 93 /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. 94 void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) { 95 std::function<bool(Pass *, Operation *)> shouldPrintBeforePass; 96 std::function<bool(Pass *, Operation *)> shouldPrintAfterPass; 97 98 // Handle print-before. 99 if (printBeforeAll) { 100 // If we are printing before all, then just return true for the filter. 101 shouldPrintBeforePass = [](Pass *, Operation *) { return true; }; 102 } else if (printBefore.hasAnyOccurrences()) { 103 // Otherwise if there are specific passes to print before, then check to see 104 // if the pass info for the current pass is included in the list. 105 shouldPrintBeforePass = [&](Pass *pass, Operation *) { 106 auto *passInfo = pass->lookupPassInfo(); 107 return passInfo && printBefore.contains(passInfo); 108 }; 109 } 110 111 // Handle print-after. 112 if (printAfterAll || printAfterFailure) { 113 // If we are printing after all or failure, then just return true for the 114 // filter. 115 shouldPrintAfterPass = [](Pass *, Operation *) { return true; }; 116 } else if (printAfter.hasAnyOccurrences()) { 117 // Otherwise if there are specific passes to print after, then check to see 118 // if the pass info for the current pass is included in the list. 119 shouldPrintAfterPass = [&](Pass *pass, Operation *) { 120 auto *passInfo = pass->lookupPassInfo(); 121 return passInfo && printAfter.contains(passInfo); 122 }; 123 } 124 125 // If there are no valid printing filters, then just return. 126 if (!shouldPrintBeforePass && !shouldPrintAfterPass) 127 return; 128 129 // Otherwise, add the IR printing instrumentation. 130 pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass, 131 printModuleScope, printAfterChange, printAfterFailure, 132 printCustomAssemblyAfterFailure, llvm::errs()); 133 } 134 135 void mlir::registerPassManagerCLOptions() { 136 // Make sure that the options struct has been constructed. 137 *options; 138 } 139 140 void mlir::applyPassManagerCLOptions(PassManager &pm) { 141 if (!options.isConstructed()) 142 return; 143 144 // Generate a reproducer on crash/failure. 145 if (options->reproducerFile.getNumOccurrences()) 146 pm.enableCrashReproducerGeneration(options->reproducerFile, 147 options->localReproducer); 148 149 // Enable statistics dumping. 150 if (options->passStatistics) 151 pm.enableStatistics(options->passStatisticsDisplayMode); 152 153 // Add the IR printing instrumentation. 154 options->addPrinterInstrumentation(pm); 155 } 156 157 void mlir::applyDefaultTimingPassManagerCLOptions(PassManager &pm) { 158 // Create a temporary timing manager for the PM to own, apply its CL options, 159 // and pass it to the PM. 160 auto tm = std::make_unique<DefaultTimingManager>(); 161 applyDefaultTimingManagerCLOptions(*tm); 162 pm.enableTiming(std::move(tm)); 163 } 164