Index: include/llvm/Passes/PassBuilder.h =================================================================== --- include/llvm/Passes/PassBuilder.h +++ include/llvm/Passes/PassBuilder.h @@ -234,10 +234,14 @@ /// \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 ThinLTOBackend indicates whether this is invoked in ThinLTOBackend. + /// We should not invoke AddDiscriminator pass in the ThinLTOBackend. ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level, bool DebugLogging = false, - bool PrepareForThinLTO = false); + bool PrepareForThinLTO = false, + bool ThinLTOBackend = false); /// Construct the core LLVM module optimization pipeline. /// Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -525,7 +525,8 @@ ModulePassManager PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, bool DebugLogging, - bool PrepareForThinLTO) { + bool PrepareForThinLTO, + bool ThinLTOBackend) { ModulePassManager MPM(DebugLogging); // Do basic inference of function attributes from known properties of system @@ -535,7 +536,7 @@ // Create an early function pass manager to cleanup the output of the // frontend. FunctionPassManager EarlyFPM(DebugLogging); - if (PGOOpt && PGOOpt->SamplePGOSupport) + if (PGOOpt && PGOOpt->SamplePGOSupport && !ThinLTOBackend) EarlyFPM.addPass(AddDiscriminatorsPass()); EarlyFPM.addPass(SimplifyCFGPass()); EarlyFPM.addPass(SROA()); @@ -576,16 +577,17 @@ if (PGOOpt) { assert(PGOOpt->RunProfileGen || !PGOOpt->SampleProfileFile.empty() || !PGOOpt->ProfileUseFile.empty() || PGOOpt->SamplePGOSupport); - if (PGOOpt->SampleProfileFile.empty()) + if (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty()) addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen, PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile); - else + else if (!PGOOpt->SampleProfileFile.empty()) 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 // it changes IR to makes profile annotation in back compile inaccurate. - if (!PrepareForThinLTO || PGOOpt->SampleProfileFile.empty()) + if ((!PrepareForThinLTO && !PGOOpt->SampleProfileFile.empty()) + || !PGOOpt->ProfileUseFile.empty()) MPM.addPass(PGOIndirectCallPromotion( false, PGOOpt && !PGOOpt->SampleProfileFile.empty())); } @@ -781,7 +783,8 @@ // Add the core simplification pipeline. MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/false)); + /*PrepareForThinLTO=*/false, + /*ThinLTOBackend=*/false)); // Now add the optimization pipeline. MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging)); @@ -803,7 +806,8 @@ // unrolling/vectorization/... now. Just simplify the module as much as we // can. MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/true)); + /*PrepareForThinLTO=*/true, + /*ThinLTOBackend=*/false)); // Run partial inlining pass to partially inline functions that have // large bodies. @@ -842,7 +846,8 @@ // Add the core simplification pipeline. MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging, - /*PrepareForThinLTO=*/false)); + /*PrepareForThinLTO=*/false, + /*ThinLTOBackend=*/true)); // Now add the optimization pipeline. MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging)); Index: test/Other/new-pm-thinlto-defaults.ll =================================================================== --- test/Other/new-pm-thinlto-defaults.ll +++ test/Other/new-pm-thinlto-defaults.ll @@ -23,6 +23,9 @@ ; RUN: opt -disable-verify -debug-pass-manager \ ; RUN: -passes='thinlto-pre-link,name-anon-globals' -S %s 2>&1 \ ; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-Oz,CHECK-PRELINK-O,CHECK-PRELINK-Oz +; RUN: opt -disable-verify -debug-pass-manager -new-pm-debug-info-for-profiling \ +; RUN: -passes='thinlto-pre-link,name-anon-globals' -S %s 2>&1 \ +; RUN: | FileCheck %s --check-prefixes=CHECK-DIS,CHECK-O,CHECK-O2,CHECK-PRELINK-O,CHECK-PRELINK-O2 ; ; Postlink pipelines: ; RUN: opt -disable-verify -debug-pass-manager \ @@ -40,6 +43,9 @@ ; RUN: opt -disable-verify -debug-pass-manager \ ; RUN: -passes='thinlto' -S %s 2>&1 \ ; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-Oz,CHECK-POSTLINK-O,CHECK-POSTLINK-Oz +; RUN: opt -disable-verify -debug-pass-manager -new-pm-debug-info-for-profiling \ +; RUN: -passes='thinlto' -S %s 2>&1 \ +; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O2,CHECK-POSTLINK-O,CHECK-POSTLINK-O2 ; ; CHECK-O: Starting llvm::Module pass manager run. ; CHECK-O-NEXT: Running pass: PassManager<{{.*}}Module{{.*}}> @@ -55,6 +61,7 @@ ; CHECK-O-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}> ; CHECK-PRELINK-O-NEXT: Running analysis: InnerAnalysisManagerProxy ; CHECK-O-NEXT: Starting llvm::Function pass manager run. +; CHECK-DIS: Running pass: AddDiscriminatorsPass ; CHECK-O-NEXT: Running pass: SimplifyCFGPass ; CHECK-O-NEXT: Running analysis: TargetIRAnalysis ; CHECK-O-NEXT: Running analysis: AssumptionAnalysis