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