Index: include/llvm/Passes/PassBuilder.h =================================================================== --- include/llvm/Passes/PassBuilder.h +++ include/llvm/Passes/PassBuilder.h @@ -71,6 +71,18 @@ std::vector InnerPipeline; }; + /// \brief ThinLTO optimization phase. + /// + /// This enumerates the LLVM ThinLTO optimization phases. + enum class ThinLTOPhase { + /// No ThinLTO behavior needed. + None, + // ThinLTO prelink (summary) phase. + PreLink, + // ThinLTO postlink (backend compile) phase. + PostLink + }; + /// \brief LLVM-provided high-level optimization levels. /// /// This enumerates the LLVM-provided high-level optimization levels. Each @@ -214,13 +226,11 @@ /// require some transformations for semantic reasons, they should explicitly /// build them. /// - /// \p PrepareForThinLTO indicates whether this is invoked in - /// PrepareForThinLTO phase. Special handling is needed for sample PGO to - /// ensure profile accurate in the backend profile annotation phase. + /// \p Phase indicates the current optimization phase. FunctionPassManager buildFunctionSimplificationPipeline(OptimizationLevel Level, - bool DebugLogging = false, - bool PrepareForThinLTO = false); + ThinLTOPhase Phase, + bool DebugLogging = false); /// Construct the core LLVM module canonicalization and simplification /// pipeline. @@ -236,13 +246,11 @@ /// require some transformations for semantic reasons, they should explicitly /// build them. /// - /// \p PrepareForThinLTO indicates whether this is invoked in - /// PrepareForThinLTO phase. Special handling is needed for sample PGO to - /// ensure profile accurate in the backend profile annotation phase. + /// \p Phase indicates the current optimization phase. ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level, - bool DebugLogging = false, - bool PrepareForThinLTO = false); + ThinLTOPhase Phase, + bool DebugLogging = false); /// Construct the core LLVM module optimization pipeline. /// Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -325,8 +325,8 @@ FunctionPassManager PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, - bool DebugLogging, - bool PrepareForThinLTO) { + ThinLTOPhase Phase, + bool DebugLogging) { assert(Level != O0 && "Must request optimizations!"); FunctionPassManager FPM(DebugLogging); @@ -389,10 +389,11 @@ C(LPM2, Level); LPM2.addPass(LoopDeletionPass()); - // Do not enable unrolling in PrepareForThinLTO phase during sample PGO + // Do not enable unrolling in PreLinkThinLTO phase during sample PGO // because it changes IR to makes profile annotation in back compile // inaccurate. - if (!PrepareForThinLTO || !PGOOpt || PGOOpt->SampleProfileFile.empty()) + if (Phase != ThinLTOPhase::PreLink || + !PGOOpt || PGOOpt->SampleProfileFile.empty()) LPM2.addPass(LoopUnrollPass::createFull(Level)); for (auto &C : LoopOptimizerEndEPCallbacks) @@ -524,8 +525,8 @@ ModulePassManager PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, - bool DebugLogging, - bool PrepareForThinLTO) { + ThinLTOPhase Phase, + bool DebugLogging) { ModulePassManager MPM(DebugLogging); // Do basic inference of function attributes from known properties of system @@ -581,9 +582,9 @@ MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile)); // Indirect call promotion that promotes intra-module targes only. - // Do not enable it in PrepareForThinLTO phase during sample PGO because + // Do not enable it in PreLinkThinLTO phase during sample PGO because // it changes IR to makes profile annotation in back compile inaccurate. - if ((!PrepareForThinLTO && !PGOOpt->SampleProfileFile.empty()) + if ((Phase != ThinLTOPhase::PreLink && !PGOOpt->SampleProfileFile.empty()) || !PGOOpt->ProfileUseFile.empty()) MPM.addPass(PGOIndirectCallPromotion( false, PGOOpt && !PGOOpt->SampleProfileFile.empty())); @@ -611,10 +612,11 @@ // Run the inliner first. The theory is that we are walking bottom-up and so // the callees have already been fully optimized, and we want to inline them // into the callers so that our optimizations can reflect that. - // For PrepareForThinLTO pass, we disable hot-caller heuristic for sample PGO + // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO // because it makes profile annotation in the backend inaccurate. InlineParams IP = getInlineParamsFromOptLevel(Level); - if (PrepareForThinLTO && PGOOpt && !PGOOpt->SampleProfileFile.empty()) + if (Phase == ThinLTOPhase::PreLink && + PGOOpt && !PGOOpt->SampleProfileFile.empty()) IP.HotCallSiteThreshold = 0; MainCGPipeline.addPass(InlinerPass(IP)); @@ -629,8 +631,7 @@ // Lastly, add the core function simplification pipeline nested inside the // CGSCC walk. MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( - buildFunctionSimplificationPipeline(Level, DebugLogging, - PrepareForThinLTO))); + buildFunctionSimplificationPipeline(Level, Phase, DebugLogging))); for (auto &C : CGSCCOptimizerLateEPCallbacks) C(MainCGPipeline, Level); @@ -782,8 +783,8 @@ MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); // Add the core simplification pipeline. - MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/false)); + MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None, + DebugLogging)); // Now add the optimization pipeline. MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging)); @@ -807,8 +808,8 @@ // If we are planning to perform ThinLTO later, we don't bloat the code with // unrolling/vectorization/... now. Just simplify the module as much as we // can. - MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/true)); + MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink, + DebugLogging)); // Run partial inlining pass to partially inline functions that have // large bodies. @@ -846,8 +847,8 @@ !PGOOpt->ProfileUseFile.empty())); // Add the core simplification pipeline. - MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/false)); + MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink, + DebugLogging)); // Now add the optimization pipeline. MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));