2016-06-04 00:54:26 +02:00
|
|
|
//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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-06-04 00:54:26 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that provides access to the global profile summary
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2017-01-13 02:34:00 +01:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
2016-11-10 00:36:02 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2020-04-22 09:03:59 +02:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2016-06-04 00:54:26 +02:00
|
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/ProfileSummary.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-07-08 20:19:59 +02:00
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
2019-11-15 00:15:48 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-06-04 00:54:26 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2021-04-15 07:53:40 +02:00
|
|
|
// Knobs for profile summary based thresholds.
|
|
|
|
extern cl::opt<int> ProfileSummaryCutoffHot;
|
|
|
|
extern cl::opt<int> ProfileSummaryCutoffCold;
|
|
|
|
extern cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold;
|
|
|
|
extern cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold;
|
|
|
|
extern cl::opt<int> ProfileSummaryHotCount;
|
|
|
|
extern cl::opt<int> ProfileSummaryColdCount;
|
2018-11-02 18:39:31 +01:00
|
|
|
|
[SampleFDO] For functions without profiles, provide an option to put
them in a special text section.
For sampleFDO, because the optimized build uses profile generated from
previous release, previously we couldn't tell a function without profile
was truely cold or just newly created so we had to treat them conservatively
and put them in .text section instead of .text.unlikely. The result was when
we persuing the best performance by locking .text.hot and .text in memory,
we wasted a lot of memory to keep cold functions inside.
In https://reviews.llvm.org/D66374, we introduced profile symbol list to
discriminate functions being cold versus functions being newly added.
This mechanism works quite well for regular use cases in AutoFDO. However,
in some case, we can only have a partial profile when optimizing a target.
The partial profile may be an aggregated profile collected from many targets.
The profile symbol list method used for regular sampleFDO profile is not
applicable to partial profile use case because it may be too large and
introduce many false positives.
To solve the problem for partial profile use case, we provide an option called
--profile-unknown-in-special-section. For functions without profile, we will
still treat them conservatively in compiler optimizations -- for example,
treat them as warm instead of cold in inliner. When we use profile info to
add section prefix for functions, we will discriminate functions known to be
not cold versus functions without profile (being unknown), and we will put
functions being unknown in a special text section called .text.unknown.
Runtime system will have the flexibility to decide where to put the special
section in order to achieve a balance between performance and memory saving.
Differential Revision: https://reviews.llvm.org/D62540
2020-05-05 02:17:34 +02:00
|
|
|
static cl::opt<bool> PartialProfile(
|
|
|
|
"partial-profile", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("Specify the current profile is used as a partial profile."));
|
|
|
|
|
2020-04-09 01:06:25 +02:00
|
|
|
cl::opt<bool> ScalePartialSampleProfileWorkingSetSize(
|
2020-06-01 19:45:28 +02:00
|
|
|
"scale-partial-sample-profile-working-set-size", cl::Hidden, cl::init(true),
|
2020-04-09 01:06:25 +02:00
|
|
|
cl::desc(
|
|
|
|
"If true, scale the working set size of the partial sample profile "
|
|
|
|
"by the partial profile ratio to reflect the size of the program "
|
|
|
|
"being compiled."));
|
|
|
|
|
|
|
|
static cl::opt<double> PartialSampleProfileWorkingSetSizeScaleFactor(
|
|
|
|
"partial-sample-profile-working-set-size-scale-factor", cl::Hidden,
|
|
|
|
cl::init(0.008),
|
|
|
|
cl::desc("The scale factor used to scale the working set size of the "
|
|
|
|
"partial sample profile along with the partial profile ratio. "
|
|
|
|
"This includes the factor of the profile counter per block "
|
|
|
|
"and the factor to scale the working set size to use the same "
|
|
|
|
"shared thresholds as PGO."));
|
|
|
|
|
2016-06-04 00:54:26 +02:00
|
|
|
// The profile summary metadata may be attached either by the frontend or by
|
|
|
|
// any backend passes (IR level instrumentation, for example). This method
|
|
|
|
// checks if the Summary is null and if so checks if the summary metadata is now
|
2020-05-14 04:32:40 +02:00
|
|
|
// available in the module and parses it to get the Summary object.
|
|
|
|
void ProfileSummaryInfo::refresh() {
|
|
|
|
if (hasProfileSummary())
|
|
|
|
return;
|
2019-02-28 20:55:07 +01:00
|
|
|
// First try to get context sensitive ProfileSummary.
|
2021-03-18 17:45:07 +01:00
|
|
|
auto *SummaryMD = M->getProfileSummary(/* IsCS */ true);
|
2020-05-14 04:32:40 +02:00
|
|
|
if (SummaryMD)
|
2019-02-28 20:55:07 +01:00
|
|
|
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
|
2020-05-14 04:32:40 +02:00
|
|
|
|
|
|
|
if (!hasProfileSummary()) {
|
|
|
|
// This will actually return PSK_Instr or PSK_Sample summary.
|
2021-03-18 17:45:07 +01:00
|
|
|
SummaryMD = M->getProfileSummary(/* IsCS */ false);
|
2020-05-14 04:32:40 +02:00
|
|
|
if (SummaryMD)
|
|
|
|
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
|
2019-02-28 20:55:07 +01:00
|
|
|
}
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!hasProfileSummary())
|
|
|
|
return;
|
|
|
|
computeThresholds();
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
Optional<uint64_t> ProfileSummaryInfo::getProfileCount(
|
|
|
|
const CallBase &Call, BlockFrequencyInfo *BFI, bool AllowSynthetic) const {
|
2020-04-27 18:07:18 +02:00
|
|
|
assert((isa<CallInst>(Call) || isa<InvokeInst>(Call)) &&
|
2017-03-10 20:45:16 +01:00
|
|
|
"We can only get profile count for call/invoke instruction.");
|
2017-05-16 22:14:39 +02:00
|
|
|
if (hasSampleProfile()) {
|
2017-05-12 01:18:05 +02:00
|
|
|
// In sample PGO mode, check if there is a profile metadata on the
|
|
|
|
// instruction. If it is present, determine hotness solely based on that,
|
2017-08-03 19:11:41 +02:00
|
|
|
// since the sampled entry count may not be accurate. If there is no
|
|
|
|
// annotated on the instruction, return None.
|
2017-05-12 01:18:05 +02:00
|
|
|
uint64_t TotalCount;
|
2020-04-27 18:07:18 +02:00
|
|
|
if (Call.extractProfTotalWeight(TotalCount))
|
2017-05-12 01:18:05 +02:00
|
|
|
return TotalCount;
|
2017-08-03 19:11:41 +02:00
|
|
|
return None;
|
2017-05-12 01:18:05 +02:00
|
|
|
}
|
2017-03-10 20:45:16 +01:00
|
|
|
if (BFI)
|
2020-04-27 18:07:18 +02:00
|
|
|
return BFI->getBlockProfileCount(Call.getParent(), AllowSynthetic);
|
2017-03-10 20:45:16 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-10-10 23:47:28 +02:00
|
|
|
/// Returns true if the function's entry is hot. If it returns false, it
|
|
|
|
/// either means it is not hot or it is unknown whether it is hot or not (for
|
2016-09-30 23:05:49 +02:00
|
|
|
/// example, no profile data is available).
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) const {
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!F || !hasProfileSummary())
|
2016-06-04 00:54:26 +02:00
|
|
|
return false;
|
|
|
|
auto FunctionCount = F->getEntryCount();
|
|
|
|
// FIXME: The heuristic used below for determining hotness is based on
|
|
|
|
// preliminary SPEC tuning for inliner. This will eventually be a
|
|
|
|
// convenience method that calls isHotCount.
|
2018-01-17 23:24:23 +01:00
|
|
|
return FunctionCount && isHotCount(FunctionCount.getCount());
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 18:53:10 +01:00
|
|
|
/// Returns true if the function contains hot code. This can include a hot
|
|
|
|
/// function entry count, hot basic block, or (in the case of Sample PGO)
|
|
|
|
/// hot total call edge count.
|
2017-03-24 00:14:11 +01:00
|
|
|
/// If it returns false, it either means it is not hot or it is unknown
|
2017-12-20 18:53:10 +01:00
|
|
|
/// (for example, no profile data is available).
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionHotInCallGraph(
|
|
|
|
const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!F || !hasProfileSummary())
|
2017-03-24 00:14:11 +01:00
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
2018-01-17 23:24:23 +01:00
|
|
|
if (isHotCount(FunctionCount.getCount()))
|
2017-03-24 00:14:11 +01:00
|
|
|
return true;
|
|
|
|
|
2017-12-20 18:53:10 +01:00
|
|
|
if (hasSampleProfile()) {
|
|
|
|
uint64_t TotalCallCount = 0;
|
|
|
|
for (const auto &BB : *F)
|
|
|
|
for (const auto &I : BB)
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I))
|
2020-04-27 18:07:18 +02:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2017-12-20 18:53:10 +01:00
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
if (isHotCount(TotalCallCount))
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-24 00:14:11 +01:00
|
|
|
for (const auto &BB : *F)
|
2018-11-19 06:23:16 +01:00
|
|
|
if (isHotBlock(&BB, &BFI))
|
2017-12-20 18:53:10 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
2017-03-24 00:14:11 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 18:53:10 +01:00
|
|
|
/// Returns true if the function only contains cold code. This means that
|
|
|
|
/// the function entry and blocks are all cold, and (in the case of Sample PGO)
|
|
|
|
/// the total call edge count is cold.
|
2017-03-24 00:14:11 +01:00
|
|
|
/// If it returns false, it either means it is not cold or it is unknown
|
2017-12-20 18:53:10 +01:00
|
|
|
/// (for example, no profile data is available).
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionColdInCallGraph(
|
|
|
|
const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!F || !hasProfileSummary())
|
2017-03-24 00:14:11 +01:00
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
2018-01-17 23:24:23 +01:00
|
|
|
if (!isColdCount(FunctionCount.getCount()))
|
2017-03-24 00:14:11 +01:00
|
|
|
return false;
|
2017-12-20 18:53:10 +01:00
|
|
|
|
|
|
|
if (hasSampleProfile()) {
|
|
|
|
uint64_t TotalCallCount = 0;
|
|
|
|
for (const auto &BB : *F)
|
|
|
|
for (const auto &I : BB)
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I))
|
2020-04-27 18:07:18 +02:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2017-12-20 18:53:10 +01:00
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
if (!isColdCount(TotalCallCount))
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-24 00:14:11 +01:00
|
|
|
for (const auto &BB : *F)
|
2018-11-19 06:23:16 +01:00
|
|
|
if (!isColdBlock(&BB, &BFI))
|
2017-12-20 18:53:10 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
2017-03-24 00:14:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) const {
|
[SampleFDO] For functions without profiles, provide an option to put
them in a special text section.
For sampleFDO, because the optimized build uses profile generated from
previous release, previously we couldn't tell a function without profile
was truely cold or just newly created so we had to treat them conservatively
and put them in .text section instead of .text.unlikely. The result was when
we persuing the best performance by locking .text.hot and .text in memory,
we wasted a lot of memory to keep cold functions inside.
In https://reviews.llvm.org/D66374, we introduced profile symbol list to
discriminate functions being cold versus functions being newly added.
This mechanism works quite well for regular use cases in AutoFDO. However,
in some case, we can only have a partial profile when optimizing a target.
The partial profile may be an aggregated profile collected from many targets.
The profile symbol list method used for regular sampleFDO profile is not
applicable to partial profile use case because it may be too large and
introduce many false positives.
To solve the problem for partial profile use case, we provide an option called
--profile-unknown-in-special-section. For functions without profile, we will
still treat them conservatively in compiler optimizations -- for example,
treat them as warm instead of cold in inliner. When we use profile info to
add section prefix for functions, we will discriminate functions known to be
not cold versus functions without profile (being unknown), and we will put
functions being unknown in a special text section called .text.unknown.
Runtime system will have the flexibility to decide where to put the special
section in order to achieve a balance between performance and memory saving.
Differential Revision: https://reviews.llvm.org/D62540
2020-05-05 02:17:34 +02:00
|
|
|
assert(hasPartialSampleProfile() && "Expect partial sample profile");
|
|
|
|
return !F.getEntryCount().hasValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
template <bool isHot>
|
2020-02-27 19:49:04 +01:00
|
|
|
bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile(
|
2020-05-28 02:04:28 +02:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!F || !hasProfileSummary())
|
2019-09-25 00:17:51 +02:00
|
|
|
return false;
|
2020-02-27 19:49:04 +01:00
|
|
|
if (auto FunctionCount = F->getEntryCount()) {
|
|
|
|
if (isHot &&
|
|
|
|
isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
|
2019-09-25 00:17:51 +02:00
|
|
|
return true;
|
2020-02-27 19:49:04 +01:00
|
|
|
if (!isHot &&
|
|
|
|
!isColdCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-25 00:17:51 +02:00
|
|
|
if (hasSampleProfile()) {
|
|
|
|
uint64_t TotalCallCount = 0;
|
|
|
|
for (const auto &BB : *F)
|
|
|
|
for (const auto &I : BB)
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I))
|
2020-04-27 18:07:18 +02:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2019-09-25 00:17:51 +02:00
|
|
|
TotalCallCount += CallCount.getValue();
|
2020-02-27 19:49:04 +01:00
|
|
|
if (isHot && isHotCountNthPercentile(PercentileCutoff, TotalCallCount))
|
2019-09-25 00:17:51 +02:00
|
|
|
return true;
|
2020-02-27 19:49:04 +01:00
|
|
|
if (!isHot && !isColdCountNthPercentile(PercentileCutoff, TotalCallCount))
|
|
|
|
return false;
|
2019-09-25 00:17:51 +02:00
|
|
|
}
|
2020-02-27 19:49:04 +01:00
|
|
|
for (const auto &BB : *F) {
|
|
|
|
if (isHot && isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI))
|
2019-09-25 00:17:51 +02:00
|
|
|
return true;
|
2020-02-27 19:49:04 +01:00
|
|
|
if (!isHot && !isColdBlockNthPercentile(PercentileCutoff, &BB, &BFI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !isHot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like isFunctionHotInCallGraph but for a given cutoff.
|
|
|
|
bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile(
|
2020-05-28 02:04:28 +02:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isFunctionHotOrColdInCallGraphNthPercentile<true>(
|
|
|
|
PercentileCutoff, F, BFI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile(
|
2020-05-28 02:04:28 +02:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isFunctionHotOrColdInCallGraphNthPercentile<false>(
|
|
|
|
PercentileCutoff, F, BFI);
|
2019-09-25 00:17:51 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 23:47:28 +02:00
|
|
|
/// Returns true if the function's entry is a cold. If it returns false, it
|
|
|
|
/// either means it is not cold or it is unknown whether it is cold or not (for
|
2016-09-30 23:05:49 +02:00
|
|
|
/// example, no profile data is available).
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) const {
|
2016-06-10 03:42:05 +02:00
|
|
|
if (!F)
|
|
|
|
return false;
|
2017-03-10 21:50:51 +01:00
|
|
|
if (F->hasFnAttribute(Attribute::Cold))
|
2016-06-04 00:54:26 +02:00
|
|
|
return true;
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!hasProfileSummary())
|
2016-06-10 03:42:05 +02:00
|
|
|
return false;
|
2016-06-04 00:54:26 +02:00
|
|
|
auto FunctionCount = F->getEntryCount();
|
|
|
|
// FIXME: The heuristic used below for determining coldness is based on
|
|
|
|
// preliminary SPEC tuning for inliner. This will eventually be a
|
|
|
|
// convenience method that calls isHotCount.
|
2018-01-17 23:24:23 +01:00
|
|
|
return FunctionCount && isColdCount(FunctionCount.getCount());
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:05:49 +02:00
|
|
|
/// Compute the hot and cold thresholds.
|
2016-06-04 00:54:26 +02:00
|
|
|
void ProfileSummaryInfo::computeThresholds() {
|
|
|
|
auto &DetailedSummary = Summary->getDetailedSummary();
|
2020-07-08 20:19:59 +02:00
|
|
|
auto &HotEntry = ProfileSummaryBuilder::getEntryForPercentile(
|
|
|
|
DetailedSummary, ProfileSummaryCutoffHot);
|
2021-04-15 07:53:40 +02:00
|
|
|
HotCountThreshold =
|
|
|
|
ProfileSummaryBuilder::getHotCountThreshold(DetailedSummary);
|
|
|
|
ColdCountThreshold =
|
|
|
|
ProfileSummaryBuilder::getColdCountThreshold(DetailedSummary);
|
2018-11-02 18:39:31 +01:00
|
|
|
assert(ColdCountThreshold <= HotCountThreshold &&
|
|
|
|
"Cold count threshold cannot exceed hot count threshold!");
|
2020-04-09 01:06:25 +02:00
|
|
|
if (!hasPartialSampleProfile() || !ScalePartialSampleProfileWorkingSetSize) {
|
|
|
|
HasHugeWorkingSetSize =
|
|
|
|
HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
|
|
|
|
HasLargeWorkingSetSize =
|
|
|
|
HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
|
|
|
|
} else {
|
|
|
|
// Scale the working set size of the partial sample profile to reflect the
|
|
|
|
// size of the program being compiled.
|
|
|
|
double PartialProfileRatio = Summary->getPartialProfileRatio();
|
|
|
|
uint64_t ScaledHotEntryNumCounts =
|
|
|
|
static_cast<uint64_t>(HotEntry.NumCounts * PartialProfileRatio *
|
|
|
|
PartialSampleProfileWorkingSetSizeScaleFactor);
|
|
|
|
HasHugeWorkingSetSize =
|
|
|
|
ScaledHotEntryNumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
|
|
|
|
HasLargeWorkingSetSize =
|
|
|
|
ScaledHotEntryNumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
|
|
|
|
}
|
2019-09-25 00:17:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
Optional<uint64_t>
|
|
|
|
ProfileSummaryInfo::computeThreshold(int PercentileCutoff) const {
|
2020-05-14 04:32:40 +02:00
|
|
|
if (!hasProfileSummary())
|
2019-09-25 00:17:51 +02:00
|
|
|
return None;
|
|
|
|
auto iter = ThresholdCache.find(PercentileCutoff);
|
|
|
|
if (iter != ThresholdCache.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
auto &DetailedSummary = Summary->getDetailedSummary();
|
2020-07-08 20:19:59 +02:00
|
|
|
auto &Entry = ProfileSummaryBuilder::getEntryForPercentile(DetailedSummary,
|
|
|
|
PercentileCutoff);
|
2019-09-25 00:17:51 +02:00
|
|
|
uint64_t CountThreshold = Entry.MinCount;
|
|
|
|
ThresholdCache[PercentileCutoff] = CountThreshold;
|
|
|
|
return CountThreshold;
|
2017-08-04 01:42:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::hasHugeWorkingSetSize() const {
|
2017-08-04 01:42:58 +02:00
|
|
|
return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::hasLargeWorkingSetSize() const {
|
2019-09-25 00:17:51 +02:00
|
|
|
return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isHotCount(uint64_t C) const {
|
2016-06-04 00:54:26 +02:00
|
|
|
return HotCountThreshold && C >= HotCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isColdCount(uint64_t C) const {
|
2016-06-04 00:54:26 +02:00
|
|
|
return ColdCountThreshold && C <= ColdCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
template <bool isHot>
|
2020-02-27 19:49:04 +01:00
|
|
|
bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff,
|
2020-05-28 02:04:28 +02:00
|
|
|
uint64_t C) const {
|
2019-09-25 00:17:51 +02:00
|
|
|
auto CountThreshold = computeThreshold(PercentileCutoff);
|
2020-02-27 19:49:04 +01:00
|
|
|
if (isHot)
|
|
|
|
return CountThreshold && C >= CountThreshold.getValue();
|
|
|
|
else
|
|
|
|
return CountThreshold && C <= CountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff,
|
|
|
|
uint64_t C) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isHotOrColdCountNthPercentile<true>(PercentileCutoff, C);
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff,
|
|
|
|
uint64_t C) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isHotOrColdCountNthPercentile<false>(PercentileCutoff, C);
|
2019-09-25 00:17:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() const {
|
2018-08-07 20:13:10 +02:00
|
|
|
return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
|
2018-05-11 01:02:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() const {
|
2018-08-07 20:13:10 +02:00
|
|
|
return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
|
2018-05-11 01:02:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB,
|
|
|
|
BlockFrequencyInfo *BFI) const {
|
2018-11-19 06:23:16 +01:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
2017-03-10 02:44:37 +01:00
|
|
|
return Count && isHotCount(*Count);
|
2017-01-13 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 06:23:16 +01:00
|
|
|
bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
|
2020-05-28 02:04:28 +02:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2018-11-19 06:23:16 +01:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
2018-12-13 22:51:42 +01:00
|
|
|
return Count && isColdCount(*Count);
|
2017-01-13 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
template <bool isHot>
|
|
|
|
bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
|
|
|
if (isHot)
|
|
|
|
return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
else
|
|
|
|
return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isHotBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isHotOrColdBlockNthPercentile<true>(PercentileCutoff, BB, BFI);
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::isColdBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-27 19:49:04 +01:00
|
|
|
return isHotOrColdBlockNthPercentile<false>(PercentileCutoff, BB, BFI);
|
2019-09-25 00:17:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-18 04:35:05 +02:00
|
|
|
bool ProfileSummaryInfo::isHotCallSite(const CallBase &CB,
|
2020-05-28 02:04:28 +02:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2020-04-27 18:07:18 +02:00
|
|
|
auto C = getProfileCount(CB, BFI);
|
2017-03-10 20:45:16 +01:00
|
|
|
return C && isHotCount(*C);
|
2017-01-13 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 04:35:05 +02:00
|
|
|
bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB,
|
2020-05-28 02:04:28 +02:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2020-04-27 18:07:18 +02:00
|
|
|
auto C = getProfileCount(CB, BFI);
|
2017-08-03 19:11:41 +02:00
|
|
|
if (C)
|
|
|
|
return isColdCount(*C);
|
|
|
|
|
|
|
|
// In SamplePGO, if the caller has been sampled, and there is no profile
|
2018-04-12 20:36:01 +02:00
|
|
|
// annotated on the callsite, we consider the callsite as cold.
|
2020-04-18 04:35:05 +02:00
|
|
|
return hasSampleProfile() && CB.getCaller()->hasProfileData();
|
2016-11-10 00:36:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:04:28 +02:00
|
|
|
bool ProfileSummaryInfo::hasPartialSampleProfile() const {
|
[SampleFDO] For functions without profiles, provide an option to put
them in a special text section.
For sampleFDO, because the optimized build uses profile generated from
previous release, previously we couldn't tell a function without profile
was truely cold or just newly created so we had to treat them conservatively
and put them in .text section instead of .text.unlikely. The result was when
we persuing the best performance by locking .text.hot and .text in memory,
we wasted a lot of memory to keep cold functions inside.
In https://reviews.llvm.org/D66374, we introduced profile symbol list to
discriminate functions being cold versus functions being newly added.
This mechanism works quite well for regular use cases in AutoFDO. However,
in some case, we can only have a partial profile when optimizing a target.
The partial profile may be an aggregated profile collected from many targets.
The profile symbol list method used for regular sampleFDO profile is not
applicable to partial profile use case because it may be too large and
introduce many false positives.
To solve the problem for partial profile use case, we provide an option called
--profile-unknown-in-special-section. For functions without profile, we will
still treat them conservatively in compiler optimizations -- for example,
treat them as warm instead of cold in inliner. When we use profile info to
add section prefix for functions, we will discriminate functions known to be
not cold versus functions without profile (being unknown), and we will put
functions being unknown in a special text section called .text.unknown.
Runtime system will have the flexibility to decide where to put the special
section in order to achieve a balance between performance and memory saving.
Differential Revision: https://reviews.llvm.org/D62540
2020-05-05 02:17:34 +02:00
|
|
|
return hasProfileSummary() &&
|
|
|
|
Summary->getKind() == ProfileSummary::PSK_Sample &&
|
|
|
|
(PartialProfile || Summary->isPartialProfile());
|
|
|
|
}
|
|
|
|
|
2016-06-04 00:54:26 +02:00
|
|
|
INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
|
|
|
|
"Profile summary info", false, true)
|
|
|
|
|
|
|
|
ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
|
|
|
|
: ImmutablePass(ID) {
|
|
|
|
initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:00:58 +02:00
|
|
|
bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
|
|
|
|
PSI.reset(new ProfileSummaryInfo(M));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
|
|
|
|
PSI.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:53:26 +01:00
|
|
|
AnalysisKey ProfileSummaryAnalysis::Key;
|
2016-06-17 02:11:01 +02:00
|
|
|
ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
|
|
|
|
ModuleAnalysisManager &) {
|
2016-09-28 23:00:58 +02:00
|
|
|
return ProfileSummaryInfo(M);
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
|
2016-08-09 02:28:38 +02:00
|
|
|
ModuleAnalysisManager &AM) {
|
2016-06-04 00:54:26 +02:00
|
|
|
ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
|
|
|
|
|
|
|
|
OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
|
|
|
|
for (auto &F : M) {
|
|
|
|
OS << F.getName();
|
2016-10-10 23:47:28 +02:00
|
|
|
if (PSI.isFunctionEntryHot(&F))
|
|
|
|
OS << " :hot entry ";
|
|
|
|
else if (PSI.isFunctionEntryCold(&F))
|
|
|
|
OS << " :cold entry ";
|
2016-06-04 00:54:26 +02:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char ProfileSummaryInfoWrapperPass::ID = 0;
|