Skip to content

Commit

Permalink
[PGO] Enable preinline and cleanup when optimize for size
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D91673
  • Loading branch information
ZequanWu committed Dec 10, 2020
1 parent fa4bd4b commit b5216b2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
14 changes: 5 additions & 9 deletions llvm/lib/Passes/PassBuilder.cpp
Expand Up @@ -838,20 +838,16 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
std::string ProfileFile,
std::string ProfileRemappingFile) {
assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!");
// Generally running simplification passes and the inliner with an high
// threshold results in smaller executables, but there may be cases where
// the size grows, so let's be conservative here and skip this simplification
// at -Os/Oz. We will not do this inline for context sensistive PGO (when
// IsCS is true).
if (!Level.isOptimizingForSize() && !IsCS && !DisablePreInliner) {
if (!IsCS && !DisablePreInliner) {
InlineParams IP;

IP.DefaultThreshold = PreInlineThreshold;

// FIXME: The hint threshold has the same value used by the regular inliner.
// This should probably be lowered after performance testing.
// FIXME: The hint threshold has the same value used by the regular inliner
// when not optimzing for size. This should probably be lowered after
// performance testing.
// FIXME: this comment is cargo culted from the old pass manager, revisit).
IP.HintThreshold = 325;
IP.HintThreshold = Level.isOptimizingForSize() ? PreInlineThreshold : 325;
ModuleInlinerWrapperPass MIWP(IP, DebugLogging);
CGSCCPassManager &CGPipeline = MIWP.getPM();

Expand Down
14 changes: 8 additions & 6 deletions llvm/lib/Transforms/IPO/PassManagerBuilder.cpp
Expand Up @@ -330,19 +330,21 @@ void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM,
return;

// Perform the preinline and cleanup passes for O1 and above.
// And avoid doing them if optimizing for size.
// We will not do this inline for context sensitive PGO (when IsCS is true).
if (OptLevel > 0 && SizeLevel == 0 && !DisablePreInliner &&
PGOSampleUse.empty() && !IsCS) {
if (OptLevel > 0 && !DisablePreInliner && PGOSampleUse.empty() && !IsCS) {
// Create preinline pass. We construct an InlineParams object and specify
// the threshold here to avoid the command line options of the regular
// inliner to influence pre-inlining. The only fields of InlineParams we
// care about are DefaultThreshold and HintThreshold.
InlineParams IP;
IP.DefaultThreshold = PreInlineThreshold;
// FIXME: The hint threshold has the same value used by the regular inliner.
// This should probably be lowered after performance testing.
IP.HintThreshold = 325;
// FIXME: The hint threshold has the same value used by the regular inliner
// when not optimzing for size. This should probably be lowered after
// performance testing.
// Use PreInlineThreshold for both -Os and -Oz. Not running preinliner makes
// the instrumented binary unusably large. Even if PreInlineThreshold is not
// correct thresold for -Oz, it is better than not running preinliner.
IP.HintThreshold = SizeLevel > 0 ? PreInlineThreshold : 325;

MPM.add(createFunctionInliningPass(IP));
MPM.add(createSROAPass());
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Other/new-pm-pgo-preinline.ll
@@ -0,0 +1,24 @@
; RUN: opt -disable-verify -debug-pass-manager -pgo-kind=pgo-instr-gen-pipeline -passes='default<Os>' -S %s 2>&1 | FileCheck %s --check-prefixes=CHECK-Osz
; RUN: opt -disable-verify -debug-pass-manager -pgo-kind=pgo-instr-gen-pipeline -passes='default<Oz>' -S %s 2>&1 | FileCheck %s --check-prefixes=CHECK-Osz

; CHECK-Osz: Running pass: ModuleInlinerWrapperPass
; CHECK-Osz-NEXT: Running analysis: InlineAdvisorAnalysis
; CHECK-Osz-NEXT: Starting {{.*}}Module pass manager run.
; CHECK-Osz-NEXT: Running analysis: InnerAnalysisManagerProxy
; CHECK-Osz-NEXT: Running analysis: LazyCallGraphAnalysis
; CHECK-Osz-NEXT: Running analysis: FunctionAnalysisManagerCGSCCProxy on (foo)
; CHECK-Osz-NEXT: Running analysis: OuterAnalysisManagerProxy
; CHECK-Osz-NEXT: Starting CGSCC pass manager run.
; CHECK-Osz-NEXT: Running pass: InlinerPass on (foo)
; CHECK-Osz-NEXT: Running pass: SROA on foo
; CHECK-Osz-NEXT: Running pass: EarlyCSEPass on foo
; CHECK-Osz-NEXT: Running pass: SimplifyCFGPass on foo
; CHECK-Osz-NEXT: Running pass: InstCombinePass on foo
; CHECK-Osz-NEXT: Finished CGSCC pass manager run.
; CHECK-Osz-NEXT: Finished {{.*}}Module pass manager run.
; CHECK-Osz-NEXT: Running pass: GlobalDCEPass
; CHECK-Osz-NEXT: Running pass: PGOInstrumentationGen

define void @foo() {
ret void
}
50 changes: 24 additions & 26 deletions llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll
Expand Up @@ -5,27 +5,27 @@
; RUN: opt -disable-verify -debug-pass-manager \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<O1>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O1,CHECK-O123
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O1,CHECK-O123SZ
; RUN: opt -disable-verify -debug-pass-manager \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<O2>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O2,CHECK-O23SZ,CHECK-O123
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O2,CHECK-O23SZ,CHECK-O123SZ
; RUN: opt -disable-verify -debug-pass-manager \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<O3>' -S -passes-ep-pipeline-start='no-op-module' %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O3,CHECK-O23SZ,CHECK-O123,CHECK-EP-PIPELINE-START
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O3,CHECK-O23SZ,CHECK-O123SZ,CHECK-EP-PIPELINE-START
; RUN: opt -disable-verify -debug-pass-manager \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<Os>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-Os,CHECK-O23SZ
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O123SZ,CHECK-Os,CHECK-O23SZ
; RUN: opt -disable-verify -debug-pass-manager \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<Oz>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-Oz,CHECK-O23SZ
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O123SZ,CHECK-Oz,CHECK-O23SZ
; RUN: opt -disable-verify -debug-pass-manager -new-pm-debug-info-for-profiling \
; RUN: -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' \
; RUN: -passes='thinlto-pre-link<O2>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O2,CHECK-O23SZ,CHECK-O123
; RUN: | FileCheck %s --check-prefixes=CHECK-O,CHECK-O2,CHECK-O23SZ,CHECK-O123SZ
;
; CHECK-O: Starting {{.*}}Module pass manager run.
; CHECK-O-NEXT: Running pass: Annotation2Metadata
Expand Down Expand Up @@ -57,22 +57,22 @@
; CHECK-O-NEXT: Running analysis: OuterAnalysisManagerProxy
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
; CHECK-O-NEXT: Finished {{.*}}Function pass manager run.
; CHECK-O123-NEXT: Running pass: ModuleInlinerWrapperPass
; CHECK-O123-NEXT: Running analysis: InlineAdvisorAnalysis
; CHECK-O123-NEXT: Starting {{.*}}Module pass manager run.
; CHECK-O123-NEXT: Running analysis: InnerAnalysisManagerProxy
; CHECK-O123-NEXT: Running analysis: LazyCallGraphAnalysis
; CHECK-O123-NEXT: Running analysis: FunctionAnalysisManagerCGSCCProxy on (foo)
; CHECK-O123-NEXT: Running analysis: OuterAnalysisManagerProxy
; CHECK-O123-NEXT: Starting CGSCC pass manager run.
; CHECK-O123-NEXT: Running pass: InlinerPass on (foo)
; CHECK-O123-NEXT: Running pass: SROA on foo
; CHECK-O123-NEXT: Running pass: EarlyCSEPass on foo
; CHECK-O123-NEXT: Running pass: SimplifyCFGPass on foo
; CHECK-O123-NEXT: Running pass: InstCombinePass on foo
; CHECK-O123-NEXT: Finished CGSCC pass manager run.
; CHECK-O123-NEXT: Finished {{.*}}Module pass manager run.
; CHECK-O123-NEXT: Running pass: GlobalDCEPass
; CHECK-O123SZ-NEXT: Running pass: ModuleInlinerWrapperPass
; CHECK-O123SZ-NEXT: Running analysis: InlineAdvisorAnalysis
; CHECK-O123SZ-NEXT: Starting {{.*}}Module pass manager run.
; CHECK-O123SZ-NEXT: Running analysis: InnerAnalysisManagerProxy
; CHECK-O123SZ-NEXT: Running analysis: LazyCallGraphAnalysis
; CHECK-O123SZ-NEXT: Running analysis: FunctionAnalysisManagerCGSCCProxy on (foo)
; CHECK-O123SZ-NEXT: Running analysis: OuterAnalysisManagerProxy
; CHECK-O123SZ-NEXT: Starting CGSCC pass manager run.
; CHECK-O123SZ-NEXT: Running pass: InlinerPass on (foo)
; CHECK-O123SZ-NEXT: Running pass: SROA on foo
; CHECK-O123SZ-NEXT: Running pass: EarlyCSEPass on foo
; CHECK-O123SZ-NEXT: Running pass: SimplifyCFGPass on foo
; CHECK-O123SZ-NEXT: Running pass: InstCombinePass on foo
; CHECK-O123SZ-NEXT: Finished CGSCC pass manager run.
; CHECK-O123SZ-NEXT: Finished {{.*}}Module pass manager run.
; CHECK-O123SZ-NEXT: Running pass: GlobalDCEPass
; CHECK-O-NEXT: Running pass: PGOInstrumentationUse
; These next two can appear in any order since they are accessed as parameters
; on the same call to BlockFrequencyInfo::calculate.
Expand All @@ -81,15 +81,13 @@
; CHECK-O-DAG: Running analysis: LoopAnalysis on foo
; CHECK-O-NEXT: Running analysis: BlockFrequencyAnalysis on foo
; CHECK-O-NEXT: Invalidating analysis: InnerAnalysisManagerProxy
; CHECK-O123-NEXT: Invalidating analysis: LazyCallGraphAnalysis on
; CHECK-O123-NEXT: Invalidating analysis: InnerAnalysisManagerProxy
; CHECK-O123SZ-NEXT: Invalidating analysis: LazyCallGraphAnalysis on
; CHECK-O123SZ-NEXT: Invalidating analysis: InnerAnalysisManagerProxy
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}ProfileSummaryAnalysis
; CHECK-O-NEXT: Running pass: PGOIndirectCallPromotion on
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis on foo
; CHECK-O-NEXT: Running pass: ModuleInlinerWrapperPass
; CHECK-Os-NEXT: Running analysis: InlineAdvisorAnalysis
; CHECK-Oz-NEXT: Running analysis: InlineAdvisorAnalysis
; CHECK-O-NEXT: Starting {{.*}}Module pass manager run.
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
; CHECK-O-NEXT: Running analysis: LazyCallGraphAnalysis
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Other/pm-pgo-preinline.ll
@@ -0,0 +1,24 @@
; RUN: opt -disable-verify -enable-new-pm=0 -pgo-kind=pgo-instr-gen-pipeline -mtriple=x86_64-- -Os -debug-pass=Structure < %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-Osz
; RUN: opt -disable-verify -enable-new-pm=0 -pgo-kind=pgo-instr-gen-pipeline -mtriple=x86_64-- -Oz -debug-pass=Structure < %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-Osz


; CHECK-Osz: CallGraph Construction
; CHECK-Osz-NEXT: Call Graph SCC Pass Manager
; CHECK-Osz-NEXT: Function Integration/Inlining
; CHECK-Osz-NEXT: FunctionPass Manager
; CHECK-Osz-NEXT: Dominator Tree Construction
; CHECK-Osz-NEXT: SROA
; CHECK-Osz-NEXT: Early CSE
; CHECK-Osz-NEXT: Simplify the CFG
; CHECK-Osz-NEXT: Dominator Tree Construction
; CHECK-Osz-NEXT: Basic Alias Analysis (stateless AA impl)
; CHECK-Osz-NEXT: Function Alias Analysis Results
; CHECK-Osz-NEXT: Natural Loop Information
; CHECK-Osz-NEXT: Lazy Branch Probability Analysis
; CHECK-Osz-NEXT: Lazy Block Frequency Analysis
; CHECK-Osz-NEXT: Optimization Remark Emitter
; CHECK-Osz-NEXT: Combine redundant instructions

define void @foo() {
ret void
}

0 comments on commit b5216b2

Please sign in to comment.