1 //=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains support for computing profile summary data.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Attributes.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Metadata.h"
16 #include "llvm/IR/Type.h"
17 #include "llvm/ProfileData/InstrProf.h"
18 #include "llvm/ProfileData/ProfileCommon.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/Casting.h"
24 // A set of cutoff values. Each value, when divided by ProfileSummary::Scale
25 // (which is 1000000) is a desired percentile of total counts.
26 static const uint32_t DefaultCutoffsData
[] = {
29 200000, 300000, 400000, 500000, 600000, 700000, 800000,
30 900000, 950000, 990000, 999000, 999900, 999990, 999999};
31 const ArrayRef
<uint32_t> ProfileSummaryBuilder::DefaultCutoffs
=
34 void InstrProfSummaryBuilder::addRecord(const InstrProfRecord
&R
) {
35 // The first counter is not necessarily an entry count for IR
36 // instrumentation profiles.
37 // Eventually MaxFunctionCount will become obsolete and this can be
39 addEntryCount(R
.Counts
[0]);
40 for (size_t I
= 1, E
= R
.Counts
.size(); I
< E
; ++I
)
41 addInternalCount(R
.Counts
[I
]);
44 // To compute the detailed summary, we consider each line containing samples as
45 // equivalent to a block with a count in the instrumented profile.
46 void SampleProfileSummaryBuilder::addRecord(
47 const sampleprof::FunctionSamples
&FS
, bool isCallsiteSample
) {
48 if (!isCallsiteSample
) {
50 if (FS
.getHeadSamples() > MaxFunctionCount
)
51 MaxFunctionCount
= FS
.getHeadSamples();
53 for (const auto &I
: FS
.getBodySamples())
54 addCount(I
.second
.getSamples());
55 for (const auto &I
: FS
.getCallsiteSamples())
56 for (const auto &CS
: I
.second
)
57 addRecord(CS
.second
, true);
60 // The argument to this method is a vector of cutoff percentages and the return
61 // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
62 void ProfileSummaryBuilder::computeDetailedSummary() {
63 if (DetailedSummaryCutoffs
.empty())
65 llvm::sort(DetailedSummaryCutoffs
);
66 auto Iter
= CountFrequencies
.begin();
67 const auto End
= CountFrequencies
.end();
69 uint32_t CountsSeen
= 0;
70 uint64_t CurrSum
= 0, Count
= 0;
72 for (const uint32_t Cutoff
: DetailedSummaryCutoffs
) {
73 assert(Cutoff
<= 999999);
74 APInt
Temp(128, TotalCount
);
76 APInt
D(128, ProfileSummary::Scale
);
79 uint64_t DesiredCount
= Temp
.getZExtValue();
80 assert(DesiredCount
<= TotalCount
);
81 while (CurrSum
< DesiredCount
&& Iter
!= End
) {
83 uint32_t Freq
= Iter
->second
;
84 CurrSum
+= (Count
* Freq
);
88 assert(CurrSum
>= DesiredCount
);
89 ProfileSummaryEntry PSE
= {Cutoff
, Count
, CountsSeen
};
90 DetailedSummary
.push_back(PSE
);
94 std::unique_ptr
<ProfileSummary
> SampleProfileSummaryBuilder::getSummary() {
95 computeDetailedSummary();
96 return std::make_unique
<ProfileSummary
>(
97 ProfileSummary::PSK_Sample
, DetailedSummary
, TotalCount
, MaxCount
, 0,
98 MaxFunctionCount
, NumCounts
, NumFunctions
);
101 std::unique_ptr
<ProfileSummary
> InstrProfSummaryBuilder::getSummary() {
102 computeDetailedSummary();
103 return std::make_unique
<ProfileSummary
>(
104 ProfileSummary::PSK_Instr
, DetailedSummary
, TotalCount
, MaxCount
,
105 MaxInternalBlockCount
, MaxFunctionCount
, NumCounts
, NumFunctions
);
108 void InstrProfSummaryBuilder::addEntryCount(uint64_t Count
) {
111 if (Count
> MaxFunctionCount
)
112 MaxFunctionCount
= Count
;
115 void InstrProfSummaryBuilder::addInternalCount(uint64_t Count
) {
117 if (Count
> MaxInternalBlockCount
)
118 MaxInternalBlockCount
= Count
;