llvm.org GIT mirror llvm / 8a7a8e6
Make ICP uses PSI to check for hotness. Summary: Currently, ICP checks the count against a fixed value to see if it is hot enough to be promoted. This does not work for SamplePGO because sampled count may be much smaller. This patch uses PSI to check if the count is hot enough to be promoted. Reviewers: davidxl, tejohnson, eraman Reviewed By: davidxl Subscribers: sanjoy, llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D36341 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310416 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 2 years ago
7 changed file(s) with 33 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
3131
3232 #define DEBUG_TYPE "pgo-icall-prom-analysis"
3333
34 // The minimum call count for the direct-call target to be considered as the
35 // promotion candidate.
36 static cl::opt
37 ICPCountThreshold("icp-count-threshold", cl::Hidden, cl::ZeroOrMore,
38 cl::init(1000),
39 cl::desc("The minimum count to the direct call target "
40 "for the promotion"));
41
4234 // The percent threshold for the direct-call target (this call site vs the
4335 // remaining call count) for it to be considered as the promotion target.
4436 static cl::opt ICPRemainingPercentThreshold(
6860 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
6961 uint64_t TotalCount,
7062 uint64_t RemainingCount) {
71 return Count >= ICPCountThreshold &&
72 Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
63 return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
7364 Count * 100 >= ICPTotalPercentThreshold * TotalCount;
7465 }
7566
2121 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
2222 #include "llvm/Analysis/IndirectCallSiteVisitor.h"
2323 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
24 #include "llvm/Analysis/ProfileSummaryInfo.h"
2425 #include "llvm/IR/BasicBlock.h"
2526 #include "llvm/IR/CallSite.h"
2627 #include "llvm/IR/DerivedTypes.h"
120121 *PassRegistry::getPassRegistry());
121122 }
122123
124 void getAnalysisUsage(AnalysisUsage &AU) const override {
125 AU.addRequired();
126 }
127
123128 StringRef getPassName() const override { return "PGOIndirectCallPromotion"; }
124129
125130 private:
193198 bool SamplePGO, OptimizationRemarkEmitter &ORE)
194199 : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
195200
196 bool processFunction();
201 bool processFunction(ProfileSummaryInfo *PSI);
197202 };
198203 } // end anonymous namespace
199204
612617
613618 // Traverse all the indirect-call callsite and get the value profile
614619 // annotation to perform indirect-call promotion.
615 bool ICallPromotionFunc::processFunction() {
620 bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
616621 bool Changed = false;
617622 ICallPromotionAnalysis ICallAnalysis;
618623 for (auto &I : findIndirectCallSites(F)) {
620625 uint64_t TotalCount;
621626 auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
622627 I, NumVals, TotalCount, NumCandidates);
623 if (!NumCandidates)
628 if (!NumCandidates ||
629 (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
624630 continue;
625631 auto PromotionCandidates = getPromotionCandidatesForCallSite(
626632 I, ICallProfDataRef, TotalCount, NumCandidates);
642648 }
643649
644650 // A wrapper function that does the actual work.
645 static bool promoteIndirectCalls(Module &M, bool InLTO, bool SamplePGO,
651 static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
652 bool InLTO, bool SamplePGO,
646653 ModuleAnalysisManager *AM = nullptr) {
647654 if (DisableICP)
648655 return false;
672679 }
673680
674681 ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
675 bool FuncChanged = ICallPromotion.processFunction();
682 bool FuncChanged = ICallPromotion.processFunction(PSI);
676683 if (ICPDUMPAFTER && FuncChanged) {
677684 DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
678685 DEBUG(dbgs() << "\n");
687694 }
688695
689696 bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
697 ProfileSummaryInfo *PSI =
698 getAnalysis().getPSI();
699
690700 // Command-line option has the priority for InLTO.
691 return promoteIndirectCalls(M, InLTO | ICPLTOMode,
701 return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
692702 SamplePGO | ICPSamplePGOMode);
693703 }
694704
695705 PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
696706 ModuleAnalysisManager &AM) {
697 if (!promoteIndirectCalls(M, InLTO | ICPLTOMode, SamplePGO | ICPSamplePGOMode,
698 &AM))
707 ProfileSummaryInfo *PSI = &AM.getResult(M);
708
709 if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
710 SamplePGO | ICPSamplePGOMode, &AM))
699711 return PreservedAnalyses::all();
700712
701713 return PreservedAnalyses::none();
2929 ; CHECK-O-NEXT: Running pass: InferFunctionAttrsPass
3030 ; CHECK-O-NEXT: Running analysis: TargetLibraryAnalysis
3131 ; CHECK-O2-NEXT: PGOIndirectCallPromotion
32 ; CHECK-O2-NEXT: Running analysis: ProfileSummaryAnalysis
3233 ; CHECK-O2-NEXT: Running analysis: InnerAnalysisManagerProxy<{{.*}}Function
3334 ; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
3435 ; CHECK-O2-NEXT: Running pass: IPSCCPPass
None ; RUN: opt < %s -icp-lto -pgo-icall-prom -S -icp-count-threshold=0 | FileCheck %s --check-prefix=ICP
1 ; RUN: opt < %s -icp-lto -passes=pgo-icall-prom -S -icp-count-threshold=0 | FileCheck %s --check-prefix=ICP
0 ; RUN: opt < %s -icp-lto -pgo-icall-prom -S | FileCheck %s --check-prefix=ICP
1 ; RUN: opt < %s -icp-lto -passes=pgo-icall-prom -S | FileCheck %s --check-prefix=ICP
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
33 target triple = "x86_64-unknown-linux-gnu"
44
None ; RUN: opt < %s -pgo-icall-prom -S | FileCheck %s --check-prefix=ICALL-PROM
1 ; RUN: opt < %s -pgo-icall-prom -S -icp-samplepgo | FileCheck %s --check-prefix=ICALL-PROM
2 ; RUN: opt < %s -pgo-icall-prom -S -icp-samplepgo | FileCheck %s --check-prefix=ICALL-PROM-SAMPLEPGO
3 ; RUN: opt < %s -passes=pgo-icall-prom -S | FileCheck %s --check-prefix=ICALL-PROM
4 ; RUN: opt < %s -pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=0 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
5 ; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=0 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
6 ; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=0 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=20 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS2-REMARK
0 ; RUN: opt < %s -pgo-icall-prom -S -icp-total-percent-threshold=50 | FileCheck %s --check-prefix=ICALL-PROM
1 ; RUN: opt < %s -pgo-icall-prom -S -icp-samplepgo -icp-total-percent-threshold=50 | FileCheck %s --check-prefix=ICALL-PROM
2 ; RUN: opt < %s -passes=pgo-icall-prom -S -icp-total-percent-threshold=50 | FileCheck %s --check-prefix=ICALL-PROM
3 ; RUN: opt < %s -pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
4 ; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
5 ; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=20 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS2-REMARK
76
87 ; PASS-REMARK: remark: :0:0: Promote indirect call to func4 with count 1030 out of 1600
98 ; PASS-REMARK: remark: :0:0: Promote indirect call to func2 with count 410 out of 570
66 ; IMPORTS-DAG: Import a
77 ; IMPORTS-DAG: Import c
88
9 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -icp-count-threshold=1 | FileCheck %s --check-prefix=ICALL-PROM
10 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=1 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
9 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S | FileCheck %s --check-prefix=ICALL-PROM
10 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -pass-remarks=pgo-icall-prom 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
1111 ; PASS-REMARK: Promote indirect call to a with count 1 out of 1
1212 ; PASS-REMARK: Promote indirect call to c.llvm.0 with count 1 out of 1
1313
66 ; by ICP. Note that the GUID in the profile is from the oroginal name.
77 ; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -o %t4.bc -print-imports 2>&1 | FileCheck %s --check-prefix=IMPORTS
88 ; IMPORTS: Import _ZL3foov.llvm.0
9 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -icp-count-threshold=1 | FileCheck %s --check-prefix=ICALL-PROM
9 ; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S | FileCheck %s --check-prefix=ICALL-PROM
1010
1111 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1212 target triple = "x86_64-unknown-linux-gnu"