Index: include/llvm/LTO/Config.h =================================================================== --- include/llvm/LTO/Config.h +++ include/llvm/LTO/Config.h @@ -47,6 +47,11 @@ /// Disable entirely the optimizer, including importing for ThinLTO bool CodeGenOnly = false; + /// If this field is set, the new pass manager is used to run passes. + /// This will go away once the new pass manager will be the default and + /// the old pass manager will be removed. + bool OptUseNewPM = false; + /// If this field is set, the set of passes run in the middle-end optimizer /// will be the one specified by the string. Only works with the new pass /// manager as the old one doesn't have this ability. Index: lib/LTO/LTOBackend.cpp =================================================================== --- lib/LTO/LTOBackend.cpp +++ lib/LTO/LTOBackend.cpp @@ -124,6 +124,57 @@ Conf.CodeModel, Conf.CGOptLevel)); } +static void runNewPMPasses(Module &Mod, TargetMachine *TM, + unsigned OptLevel) { + PassBuilder PB(TM); + AAManager AA; + + // Parse a custom AA pipeline if asked to. + assert(PB.parseAAPipeline(AA, "default")); + + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; + + // Register the AA manager first so that our version is the one used. + FAM.registerPass([&] { return std::move(AA); }); + + // Register all the basic analyses with the managers. + PB.registerModuleAnalyses(MAM); + PB.registerCGSCCAnalyses(CGAM); + PB.registerFunctionAnalyses(FAM); + PB.registerLoopAnalyses(LAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + + ModulePassManager MPM; + // FIXME (davide): verify the input. + + PassBuilder::OptimizationLevel OL; + + switch (OptLevel) { + default: + llvm_unreachable("Invalid optimization level"); + case 0: + OL = PassBuilder::O0; + break; + case 1: + OL = PassBuilder::O1; + break; + case 2: + OL = PassBuilder::O2; + break; + case 3: + OL = PassBuilder::O3; + break; + } + + MPM = PB.buildLTODefaultPipeline(OL, false /* DebugLogging */); + MPM.run(Mod, MAM); + + // FIXME (davide): verify the output. +} + static void runNewPMCustomPasses(Module &Mod, TargetMachine *TM, std::string PipelineDesc, std::string AAPipelineDesc, @@ -193,12 +244,16 @@ bool opt(Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod, bool IsThinLTO, ModuleSummaryIndex &CombinedIndex) { - if (Conf.OptPipeline.empty()) - runOldPMPasses(Conf, Mod, TM, IsThinLTO, CombinedIndex); - else + if (Conf.OptUseNewPM || !Conf.OptPipeline.empty()) { // FIXME: Plumb the combined index into the new pass manager. - runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline, - Conf.DisableVerify); + if (Conf.OptPipeline.empty()) + runNewPMPasses(Mod, TM, Conf.OptLevel); + else + runNewPMCustomPasses(Mod, TM, Conf.OptPipeline, Conf.AAPipeline, + Conf.DisableVerify); + } + else + runOldPMPasses(Conf, Mod, TM, IsThinLTO, CombinedIndex); return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod); }