Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -533,26 +533,8 @@ // libraries and other oracles. MPM.addPass(InferFunctionAttrsPass()); - // Create an early function pass manager to cleanup the output of the - // frontend. - FunctionPassManager EarlyFPM(DebugLogging); - EarlyFPM.addPass(SimplifyCFGPass()); - EarlyFPM.addPass(SROA()); - EarlyFPM.addPass(EarlyCSEPass()); - EarlyFPM.addPass(LowerExpectIntrinsicPass()); - // In SamplePGO ThinLTO backend, we need instcombine before profile annotation - // to convert bitcast to direct calls so that they can be inlined during the - // profile annotation prepration step. - // More details about SamplePGO design can be found in: - // https://research.google.com/pubs/pub45290.html - // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. - if (PGOOpt && !PGOOpt->SampleProfileFile.empty() && - Phase == ThinLTOPhase::PostLink) - EarlyFPM.addPass(InstCombinePass()); - MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); - if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) { - // Annotate sample profile right after early FPM to ensure freshness of + // Annotate sample profile before early FPM to ensure freshness of // the debug info. MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile)); // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard @@ -566,6 +548,15 @@ true)); } + // Create an early function pass manager to cleanup the output of the + // frontend. + FunctionPassManager EarlyFPM(DebugLogging); + EarlyFPM.addPass(SimplifyCFGPass()); + EarlyFPM.addPass(SROA()); + EarlyFPM.addPass(EarlyCSEPass()); + EarlyFPM.addPass(LowerExpectIntrinsicPass()); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); + // Interprocedural constant propagation now that basic cleanup has occured // and prior to optimizing globals. // FIXME: This position in the pipeline hasn't been carefully considered in @@ -861,6 +852,15 @@ MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, false /* SamplePGO */)); + // In SamplePGO ThinLTO backend, we need instcombine before profile + // annotation to convert bitcast to direct calls so that they can be + // inlined during the profile annotation prepration step. + // More details about SamplePGO design can be found in: + // https://research.google.com/pubs/pub45290.html + // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. + if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) + MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass())); + // Add the core simplification pipeline. MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink, DebugLogging)); Index: test/Other/new-pm-pgo.ll =================================================================== --- test/Other/new-pm-pgo.ll +++ test/Other/new-pm-pgo.ll @@ -13,11 +13,11 @@ ; USE: Running pass: PGOInstrumentationUse ; SAMPLE_USE_O: Running pass: ModuleToFunctionPassAdaptor<{{.*}}AddDiscriminatorsPass{{.*}}> ; SAMPLE_USE_PRE_LINK: Running pass: ModuleToFunctionPassAdaptor<{{.*}}AddDiscriminatorsPass{{.*}}> -; SAMPLE_USE: Running pass: SimplifyCFGPass -; SAMPLE_USE: Running pass: SROA -; SAMPLE_USE: Running pass: EarlyCSEPass -; SAMPLE_USE: Running pass: LowerExpectIntrinsicPass -; SAMPLE_USE_POST_LINK: Running pass: InstCombinePass +; SAMPLE_USE-NOT: Running pass: SimplifyCFGPass +; SAMPLE_USE-NOT: Running pass: SROA +; SAMPLE_USE-NOT: Running pass: EarlyCSEPass +; SAMPLE_USE-NOT: Running pass: LowerExpectIntrinsicPass +; SAMPLE_USE_POST_LINK: Running pass: ModuleToFunctionPassAdaptor<{{.*}}InstCombinePass{{.*}}> ; SAMPLE_USE: Running pass: SampleProfileLoaderPass ; SAMPLE_USE_O: Running pass: PGOIndirectCallPromotion ; SAMPLE_USE_POST_LINK-NOT: Running pass: GlobalOptPass