Index: llvm/include/llvm/IR/PassManager.h =================================================================== --- llvm/include/llvm/IR/PassManager.h +++ llvm/include/llvm/IR/PassManager.h @@ -381,6 +381,21 @@ Name = Name.drop_front(strlen("llvm::")); return Name; } + + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + auto ClassName = name(); + auto PassName = MapClassName2PassName(ClassName); + if (!PassName.empty()) { + OS << PassName; + static_cast(this)->printPipelineParams(OS); + } else { + OS << "NO-PASSNAME-FOR(" << ClassName << ")"; + } + } + + void printPipelineParams(raw_ostream &OS) {} }; /// A CRTP mix-in that provides informational APIs needed for analysis passes. @@ -480,6 +495,17 @@ return *this; } + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { + auto *P = Passes[Idx].get(); + P->printPipeline(OS, MapClassName2PassName); + if (Idx + 1 < Size) + OS << ","; + } + } + /// Run all of the passes in this manager over the given unit of IR. /// ExtraArgs are passed to each pass. PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, @@ -1195,6 +1221,8 @@ /// Runs the function pass across every function in the module. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); + void printPipeline(raw_ostream &OS, + std::function MapClassName2PassName); static bool isRequired() { return true; } @@ -1243,6 +1271,19 @@ return PreservedAnalyses::all(); } + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + auto ClassName = AnalysisT::name(); + auto PassName = MapClassName2PassName(ClassName); + OS << "require<"; + if (!PassName.empty()) { + OS << PassName; + } else { + OS << "NO-PASSNAME-FOR(" << ClassName << ")"; + } + OS << ">"; + } static bool isRequired() { return true; } }; @@ -1263,6 +1304,19 @@ PA.abandon(); return PA; } + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + auto ClassName = AnalysisT::name(); + auto PassName = MapClassName2PassName(ClassName); + OS << "invalidate<"; + if (!PassName.empty()) { + OS << PassName; + } else { + OS << "NO-PASSNAME-FOR(" << ClassName << ")"; + } + OS << ">"; + } }; /// A utility pass that does nothing, but preserves no analyses. @@ -1275,6 +1329,11 @@ PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) { return PreservedAnalyses::none(); } + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + OS << "invalidate"; + } }; /// A utility pass template that simply runs another pass multiple times. @@ -1312,6 +1371,14 @@ return PA; } + void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + OS << "repeat<" << Count << ">("; + P.printPipeline(OS, MapClassName2PassName); + OS << ")"; + } + private: int Count; PassT P; Index: llvm/include/llvm/IR/PassManagerInternal.h =================================================================== --- llvm/include/llvm/IR/PassManagerInternal.h +++ llvm/include/llvm/IR/PassManagerInternal.h @@ -46,6 +46,10 @@ virtual PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) = 0; + virtual void + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) = 0; + virtual void printPipelineParams(raw_ostream &OS) = 0; /// Polymorphic method to access the name of a pass. virtual StringRef name() const = 0; @@ -85,6 +89,16 @@ return Pass.run(IR, AM, ExtraArgs...); } + void printPipeline( + raw_ostream &OS, + std::function MapClassName2PassName) override { + Pass.printPipeline(OS, MapClassName2PassName); + } + + void printPipelineParams(raw_ostream &OS) override { + Pass.printPipelineParams(OS); + } + StringRef name() const override { return PassT::name(); } template Index: llvm/include/llvm/Transforms/Scalar/LoopPassManager.h =================================================================== --- llvm/include/llvm/Transforms/Scalar/LoopPassManager.h +++ llvm/include/llvm/Transforms/Scalar/LoopPassManager.h @@ -94,6 +94,8 @@ PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U); + void printPipeline(raw_ostream &OS, + std::function MapClassName2PassName); /// Add either a loop pass or a loop-nest pass to the pass manager. Append \p /// Pass to the list of loop passes if it has a dedicated \fn run() method for /// loops and to the list of loop-nest passes if the \fn run() method is for @@ -424,6 +426,8 @@ /// Runs the loop passes across every loop in the function. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); + void printPipeline(raw_ostream &OS, + std::function MapClassName2PassName); static bool isRequired() { return true; } Index: llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h =================================================================== --- llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h +++ llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h @@ -39,6 +39,7 @@ /// Construct a pass with optional optimizations. SimplifyCFGPass(const SimplifyCFGOptions &PassOptions); + void printPipelineParams(raw_ostream &OS); /// Run the pass over the function. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); }; Index: llvm/lib/IR/PassManager.cpp =================================================================== --- llvm/lib/IR/PassManager.cpp +++ llvm/lib/IR/PassManager.cpp @@ -91,6 +91,14 @@ } } // namespace llvm +void ModuleToFunctionPassAdaptor::printPipeline( + raw_ostream &OS, + std::function MapClassName2PassName) { + OS << "function("; + Pass->printPipeline(OS, MapClassName2PassName); + OS << ")"; +} + PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M, ModuleAnalysisManager &AM) { FunctionAnalysisManager &FAM = Index: llvm/lib/Passes/PassBuilder.cpp =================================================================== --- llvm/lib/Passes/PassBuilder.cpp +++ llvm/lib/Passes/PassBuilder.cpp @@ -291,6 +291,10 @@ } namespace llvm { +cl::opt PrintPipelinePasses( + "print-pipeline-passes", + cl::desc("Print a '-passes' compatible string describing the pipeline.")); + extern cl::opt MaxDevirtIterations; extern cl::opt EnableConstraintElimination; extern cl::opt EnableFunctionSpecialization; @@ -439,7 +443,8 @@ /// it. This should be updated if new pass instrumentation wants to use the map. /// We currently only use this for --print-before/after. bool shouldPopulateClassToPassNames() { - return !printBeforePasses().empty() || !printAfterPasses().empty(); + return PrintPipelinePasses || !printBeforePasses().empty() || + !printAfterPasses().empty(); } } // namespace Index: llvm/lib/Transforms/Scalar/LoopPassManager.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopPassManager.cpp +++ llvm/lib/Transforms/Scalar/LoopPassManager.cpp @@ -44,6 +44,18 @@ return PA; } +void PassManager:: + printPipeline(raw_ostream &OS, + std::function MapClassName2PassName) { + for (unsigned Idx = 0, Size = LoopPasses.size(); Idx != Size; ++Idx) { + auto *P = LoopPasses[Idx].get(); + P->printPipeline(OS, MapClassName2PassName); + if (Idx + 1 < Size) + OS << ","; + } +} + // Run both loop passes and loop-nest passes on top-level loop \p L. PreservedAnalyses LoopPassManager::runWithLoopNestPasses(Loop &L, LoopAnalysisManager &AM, @@ -172,6 +184,13 @@ } } // namespace llvm +void FunctionToLoopPassAdaptor::printPipeline( + raw_ostream &OS, + std::function MapClassName2PassName) { + OS << (UseMemorySSA ? "loop-mssa(" : "loop("); + Pass->printPipeline(OS, MapClassName2PassName); + OS << ")"; +} PreservedAnalyses FunctionToLoopPassAdaptor::run(Function &F, FunctionAnalysisManager &AM) { // Before we even compute any loop analyses, first run a miniature function Index: llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp +++ llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp @@ -319,6 +319,18 @@ applyCommandLineOverridesToOptions(Options); } +void SimplifyCFGPass::printPipelineParams(raw_ostream &OS) { + OS << "<"; + OS << "bonus-inst-threshold=" << Options.BonusInstThreshold << ";"; + OS << (Options.ForwardSwitchCondToPhi ? "" : "no-") << "forward-switch-cond;"; + OS << (Options.ConvertSwitchToLookupTable ? "" : "no-") + << "switch-to-lookup;"; + OS << (Options.NeedCanonicalLoop ? "" : "no-") << "keep-loops;"; + OS << (Options.HoistCommonInsts ? "" : "no-") << "hoist-common-insts;"; + OS << (Options.SinkCommonInsts ? "" : "no-") << "sink-common-insts"; + OS << ">"; +} + PreservedAnalyses SimplifyCFGPass::run(Function &F, FunctionAnalysisManager &AM) { auto &TTI = AM.getResult(F); Index: llvm/tools/opt/NewPMDriver.cpp =================================================================== --- llvm/tools/opt/NewPMDriver.cpp +++ llvm/tools/opt/NewPMDriver.cpp @@ -137,6 +137,7 @@ extern cl::opt CSPGOKindFlag; extern cl::opt CSProfileGenFile; extern cl::opt DisableBasicAA; +extern cl::opt PrintPipelinePasses; } // namespace llvm static cl::opt @@ -473,6 +474,17 @@ // Before executing passes, print the final values of the LLVM options. cl::PrintOptionValues(); + // Print a textual, '-passes=' compatible, representation of pipeline if + // requested. + if (PrintPipelinePasses) { + outs() << "-passes='"; + MPM.printPipeline(outs(), [&PIC](StringRef ClassName) { + return PIC.getPassNameForClassName(ClassName); + }); + outs() << "'\n"; + return true; + } + // Now that we have all of the passes ready, run them. MPM.run(M, MAM);