llvm.org GIT mirror llvm / 7a71e16
[PGO] Indirect-call profile annotation in IR level profiling This patch reads the indirect-call value records in the profile and makes the annotation in the indirect-call instruction. This is for IR level profile instrumentation. Differential Revision: http://reviews.llvm.org/D16935 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260400 91177308-0d34-0410-b5e6-96231b3b80d8 Rong Xu 3 years ago
3 changed file(s) with 124 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
2424 //
2525 // This file contains two passes:
2626 // (1) Pass PGOInstrumentationGen which instruments the IR to generate edge
27 // count profile, and
27 // count profile, and generates the instrumentation for indirect call
28 // profiling.
2829 // (2) Pass PGOInstrumentationUse which reads the edge count profile and
29 // annotates the branch weights.
30 // annotates the branch weights. It also reads the indirect call value
31 // profiling records and annotate the indirect call instructions.
32 //
3033 // To get the precise counter information, These two passes need to invoke at
3134 // the same compilation point (so they see the same IR). For pass
3235 // PGOInstrumentationGen, the real work is done in instrumentOneFunc(). For
8386 STATISTIC(NumOfPGOFunc, "Number of functions having valid profile counts.");
8487 STATISTIC(NumOfPGOMismatch, "Number of functions having mismatch profile.");
8588 STATISTIC(NumOfPGOMissing, "Number of functions without profile.");
86 STATISTIC(NumOfPGOICall, "Number of indirect call value instrumentation.");
89 STATISTIC(NumOfPGOICall, "Number of indirect call value instrumentations.");
8790
8891 // Command line option to specify the file to read profile from. This is
8992 // mainly used for testing.
9497 "mainly for test purpose."));
9598
9699 // Command line options to disable value profiling. The default is false:
97 // i.e. vaule profiling is enabled by default. This is for debug purpose.
100 // i.e. value profiling is enabled by default. This is for debug purpose.
98101 static cl::opt
99102 DisableValueProfiling("disable-vp", cl::init(false),
100103 cl::Hidden,
462465 // compilation.
463466 uint64_t ProgramMaxCount;
464467
468 // ProfileRecord for this function.
469 InstrProfRecord ProfileRecord;
470
465471 // Find the Instrumented BB and set the value.
466472 void setInstrumentedCounts(const std::vector &CountFromProfile);
467473
501507
502508 // Set the branch weights based on the count values.
503509 void setBranchWeights();
510
511 // Annotate the indirect call sites.
512 void annotateIndirectCallSites();
504513 };
505514
506515 // Visit all the edges and assign the count value for the instrumented
573582 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
574583 return false;
575584 }
576 std::vector &CountFromProfile = Result.get().Counts;
585 ProfileRecord = std::move(Result.get());
586 std::vector &CountFromProfile = ProfileRecord.Counts;
577587
578588 NumOfPGOFunc++;
579589 DEBUG(dbgs() << CountFromProfile.size() << " counts\n");
711721 dbgs() << "\n";);
712722 }
713723 }
724
725 // Traverse all the indirect callsites and annotate the instructions.
726 void PGOUseFunc::annotateIndirectCallSites() {
727 if (DisableValueProfiling)
728 return;
729
730 unsigned IndirectCallSiteIndex = 0;
731 PGOIndirectCallSiteVisitor ICV;
732 ICV.visit(F);
733 unsigned NumValueSites=
734 ProfileRecord.getNumValueSites(IPVK_IndirectCallTarget);
735 if (NumValueSites != ICV.IndirectCallInsts.size()) {
736 std::string Msg =
737 std::string("Inconsistent number of indirect call sites: ") +
738 F.getName().str();
739 auto &Ctx = M->getContext();
740 Ctx.diagnose(
741 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
742 return;
743 }
744
745 for (auto &I : ICV.IndirectCallInsts) {
746 DEBUG(dbgs() << "Read one indirect call instrumentation: Index="
747 << IndirectCallSiteIndex << " out of "
748 << NumValueSites<< "\n");
749 annotateValueSite(*M, *I, ProfileRecord, IPVK_IndirectCallTarget,
750 IndirectCallSiteIndex);
751 IndirectCallSiteIndex++;
752 }
753 }
714754 } // end anonymous namespace
715755
716756 // Create a COMDAT variable IR_LEVEL_PROF_VARNAME to make the runtime
750790 if (Func.readCounters(PGOReader)) {
751791 Func.populateCounters();
752792 Func.setBranchWeights();
793 Func.annotateIndirectCallSites();
753794 }
754795 }
755796
0 :ir
1 bar
2 # Func Hash:
3 12884901887
4 # Num Counters:
5 1
6 # Counter Values:
7 140
8 # Num Value Kinds:
9 1
10 # ValueKind = IPVK_IndirectCallTarget:
11 0
12 # NumValueSites:
13 1
14 3
15 func2:80
16 func1:40
17 func3:20
18
19 func1
20 # Func Hash:
21 12884901887
22 # Num Counters:
23 1
24 # Counter Values:
25 40
26
27 func2
28 # Func Hash:
29 12884901887
30 # Num Counters:
31 1
32 # Counter Values:
33 80
34
35 func3
36 # Func Hash:
37 12884901887
38 # Num Counters:
39 1
40 # Counter Values:
41 20
42
0 ; RUN: llvm-profdata merge %S/Inputs/indirect_call.proftext -o %t.profdata
1 ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=VP-ANNOTATION
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @foo = common global i32 (i32)* null, align 8
6
7 define i32 @func1(i32 %x) {
8 entry:
9 ret i32 %x
10 }
11
12 define i32 @func2(i32 %x) {
13 entry:
14 %add = add nsw i32 %x, 1
15 ret i32 %add
16 }
17
18 define i32 @func3(i32 %x) {
19 entry:
20 %add = add nsw i32 %x, 3
21 ret i32 %add
22 }
23
24 define i32 @bar(i32 %i) {
25 entry:
26 %tmp = load i32 (i32)*, i32 (i32)** @foo, align 8
27 %call = call i32 %tmp(i32 %i)
28 ; VP-ANNOTATION: %call = call i32 %tmp(i32 %i)
29 ; VP-ANNOTATION-SAME: !prof ![[VP:[0-9]+]]
30 ; VP-ANNOTATION: ![[VP]] = !{!"VP", i32 0, i64 140, i64 -4377547752858689819, i64 80, i64 -2545542355363006406, i64 40, i64 -6929281286627296573, i64 20}
31 ret i32 %call
32 }
33
34