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 runPasses(llvm::legacy::PassManager & modulePM,llvm::legacy::FunctionPassManager & funcPM,llvm::Module & m)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{ initializeLLVMPasses()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> 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)}; 56 llvm::cl::opt<bool> printModuleScope{ 57 "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 62 /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. 63 void addPrinterInstrumentation(PassManager &pm); 64 populatePassManagers(llvm::legacy::PassManager & modulePM,llvm::legacy::FunctionPassManager & funcPM,unsigned optLevel,unsigned sizeLevel,llvm::TargetMachine * targetMachine)65 //===--------------------------------------------------------------------===// 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", 79 "display the results with a nested pipeline view"))}; 80 }; 81 } // end anonymous namespace 82 83 static llvm::ManagedStatic<PassManagerOptions> options; 84 85 /// Add an IR printing instrumentation if enabled by any 'print-ir' flags. 86 void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) { 87 std::function<bool(Pass *, Operation *)> shouldPrintBeforePass; 88 std::function<bool(Pass *, Operation *)> shouldPrintAfterPass; 89 90 // Handle print-before. 91 if (printBeforeAll) { 92 // If we are printing before all, then just return true for the filter. 93 shouldPrintBeforePass = [](Pass *, Operation *) { return true; }; 94 } else if (printBefore.hasAnyOccurrences()) { 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. makeOptimizingTransformer(unsigned optLevel,unsigned sizeLevel,llvm::TargetMachine * targetMachine)97 shouldPrintBeforePass = [&](Pass *pass, Operation *) { 98 auto *passInfo = pass->lookupPassInfo(); 99 return passInfo && printBefore.contains(passInfo); 100 }; 101 } 102 103 // Handle print-after. 104 if (printAfterAll || printAfterFailure) { 105 // If we are printing after all or failure, then just return true for the 106 // filter. 107 shouldPrintAfterPass = [](Pass *, Operation *) { return true; }; 108 } else if (printAfter.hasAnyOccurrences()) { 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. makeLLVMPassesTransformer(llvm::ArrayRef<const llvm::PassInfo * > llvmPasses,llvm::Optional<unsigned> mbOptLevel,llvm::TargetMachine * targetMachine,unsigned optPassesInsertPos)111 shouldPrintAfterPass = [&](Pass *pass, Operation *) { 112 auto *passInfo = pass->lookupPassInfo(); 113 return passInfo && printAfter.contains(passInfo); 114 }; 115 } __anon64f0cf600202(llvm::Module *m) 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, 123 printModuleScope, printAfterChange, printAfterFailure, 124 llvm::errs()); 125 } 126 127 void mlir::registerPassManagerCLOptions() { 128 // Make sure that the options struct has been constructed. 129 *options; 130 } 131 132 void mlir::applyPassManagerCLOptions(PassManager &pm) { 133 if (!options.isConstructed()) 134 return; 135 136 // Generate a reproducer on crash/failure. 137 if (options->reproducerFile.getNumOccurrences()) 138 pm.enableCrashReproducerGeneration(options->reproducerFile, 139 options->localReproducer); 140 141 // Enable statistics dumping. 142 if (options->passStatistics) 143 pm.enableStatistics(options->passStatisticsDisplayMode); 144 145 // Add the IR printing instrumentation. 146 options->addPrinterInstrumentation(pm); 147 } 148 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)); 155 } 156