2016-05-20 05:07:12 +08:00
|
|
|
//=-- ProfilesummaryBuilder.cpp - Profile summary computation ---------------=//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-20 05:07:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for computing profile summary data.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
|
|
|
#include "llvm/ProfileData/SampleProf.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// A set of cutoff values. Each value, when divided by ProfileSummary::Scale
|
|
|
|
// (which is 1000000) is a desired percentile of total counts.
|
2016-05-28 01:38:16 +08:00
|
|
|
static const uint32_t DefaultCutoffsData[] = {
|
|
|
|
10000, /* 1% */
|
|
|
|
100000, /* 10% */
|
2018-04-13 05:38:43 +08:00
|
|
|
200000, 300000, 400000, 500000, 600000, 700000, 800000,
|
|
|
|
900000, 950000, 990000, 999000, 999900, 999990, 999999};
|
2016-05-28 01:38:16 +08:00
|
|
|
const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
|
|
|
|
DefaultCutoffsData;
|
2016-05-20 05:07:12 +08:00
|
|
|
|
|
|
|
void InstrProfSummaryBuilder::addRecord(const InstrProfRecord &R) {
|
|
|
|
// The first counter is not necessarily an entry count for IR
|
|
|
|
// instrumentation profiles.
|
|
|
|
// Eventually MaxFunctionCount will become obsolete and this can be
|
|
|
|
// removed.
|
|
|
|
addEntryCount(R.Counts[0]);
|
|
|
|
for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
|
|
|
|
addInternalCount(R.Counts[I]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// To compute the detailed summary, we consider each line containing samples as
|
|
|
|
// equivalent to a block with a count in the instrumented profile.
|
|
|
|
void SampleProfileSummaryBuilder::addRecord(
|
2019-04-04 03:54:43 +08:00
|
|
|
const sampleprof::FunctionSamples &FS, bool isCallsiteSample) {
|
|
|
|
if (!isCallsiteSample) {
|
|
|
|
NumFunctions++;
|
|
|
|
if (FS.getHeadSamples() > MaxFunctionCount)
|
|
|
|
MaxFunctionCount = FS.getHeadSamples();
|
|
|
|
}
|
2016-05-20 05:07:12 +08:00
|
|
|
for (const auto &I : FS.getBodySamples())
|
|
|
|
addCount(I.second.getSamples());
|
2019-04-04 03:54:43 +08:00
|
|
|
for (const auto &I : FS.getCallsiteSamples())
|
|
|
|
for (const auto &CS : I.second)
|
|
|
|
addRecord(CS.second, true);
|
2016-05-20 05:07:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The argument to this method is a vector of cutoff percentages and the return
|
|
|
|
// value is a vector of (Cutoff, MinCount, NumCounts) triplets.
|
|
|
|
void ProfileSummaryBuilder::computeDetailedSummary() {
|
|
|
|
if (DetailedSummaryCutoffs.empty())
|
|
|
|
return;
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 10:13:45 +08:00
|
|
|
llvm::sort(DetailedSummaryCutoffs);
|
2016-10-01 05:05:55 +08:00
|
|
|
auto Iter = CountFrequencies.begin();
|
|
|
|
const auto End = CountFrequencies.end();
|
2016-05-20 05:07:12 +08:00
|
|
|
|
|
|
|
uint32_t CountsSeen = 0;
|
|
|
|
uint64_t CurrSum = 0, Count = 0;
|
|
|
|
|
2016-10-01 05:05:55 +08:00
|
|
|
for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
|
2016-05-20 05:07:12 +08:00
|
|
|
assert(Cutoff <= 999999);
|
|
|
|
APInt Temp(128, TotalCount);
|
|
|
|
APInt N(128, Cutoff);
|
|
|
|
APInt D(128, ProfileSummary::Scale);
|
|
|
|
Temp *= N;
|
|
|
|
Temp = Temp.sdiv(D);
|
|
|
|
uint64_t DesiredCount = Temp.getZExtValue();
|
|
|
|
assert(DesiredCount <= TotalCount);
|
|
|
|
while (CurrSum < DesiredCount && Iter != End) {
|
|
|
|
Count = Iter->first;
|
|
|
|
uint32_t Freq = Iter->second;
|
|
|
|
CurrSum += (Count * Freq);
|
|
|
|
CountsSeen += Freq;
|
|
|
|
Iter++;
|
|
|
|
}
|
|
|
|
assert(CurrSum >= DesiredCount);
|
|
|
|
ProfileSummaryEntry PSE = {Cutoff, Count, CountsSeen};
|
|
|
|
DetailedSummary.push_back(PSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 17:18:37 +08:00
|
|
|
std::unique_ptr<ProfileSummary> SampleProfileSummaryBuilder::getSummary() {
|
2016-05-20 05:07:12 +08:00
|
|
|
computeDetailedSummary();
|
2016-05-20 17:18:37 +08:00
|
|
|
return llvm::make_unique<ProfileSummary>(
|
|
|
|
ProfileSummary::PSK_Sample, DetailedSummary, TotalCount, MaxCount, 0,
|
|
|
|
MaxFunctionCount, NumCounts, NumFunctions);
|
2016-05-20 05:07:12 +08:00
|
|
|
}
|
|
|
|
|
2016-05-20 17:18:37 +08:00
|
|
|
std::unique_ptr<ProfileSummary> InstrProfSummaryBuilder::getSummary() {
|
2016-05-20 05:07:12 +08:00
|
|
|
computeDetailedSummary();
|
2016-05-20 17:18:37 +08:00
|
|
|
return llvm::make_unique<ProfileSummary>(
|
|
|
|
ProfileSummary::PSK_Instr, DetailedSummary, TotalCount, MaxCount,
|
|
|
|
MaxInternalBlockCount, MaxFunctionCount, NumCounts, NumFunctions);
|
2016-05-20 05:07:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfSummaryBuilder::addEntryCount(uint64_t Count) {
|
|
|
|
addCount(Count);
|
|
|
|
NumFunctions++;
|
|
|
|
if (Count > MaxFunctionCount)
|
|
|
|
MaxFunctionCount = Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
|
|
|
|
addCount(Count);
|
|
|
|
if (Count > MaxInternalBlockCount)
|
|
|
|
MaxInternalBlockCount = Count;
|
|
|
|
}
|