2016-06-04 06:54:26 +08:00
|
|
|
//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
|
|
|
|
//
|
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-06-04 06:54:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that provides access to the global profile summary
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2017-01-13 09:34:00 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
2016-11-10 07:36:02 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2020-04-22 15:03:59 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2016-06-04 06:54:26 +08: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-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-07-09 02:19:59 +08:00
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
2019-11-15 07:15:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-06-04 06:54:26 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// The following two parameters determine the threshold for a count to be
|
|
|
|
// considered hot/cold. These two parameters are percentile values (multiplied
|
|
|
|
// by 10000). If the counts are sorted in descending order, the minimum count to
|
|
|
|
// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
|
|
|
|
// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
|
|
|
|
// threshold for determining cold count (everything <= this threshold is
|
|
|
|
// considered cold).
|
|
|
|
|
|
|
|
static cl::opt<int> ProfileSummaryCutoffHot(
|
2017-08-05 00:20:54 +08:00
|
|
|
"profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
|
2016-06-04 06:54:26 +08:00
|
|
|
cl::desc("A count is hot if it exceeds the minimum count to"
|
|
|
|
" reach this percentile of total counts."));
|
|
|
|
|
|
|
|
static cl::opt<int> ProfileSummaryCutoffCold(
|
|
|
|
"profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
|
|
|
|
cl::desc("A count is cold if it is below the minimum count"
|
|
|
|
" to reach this percentile of total counts."));
|
|
|
|
|
2017-08-04 07:42:58 +08:00
|
|
|
static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
|
|
|
|
"profile-summary-huge-working-set-size-threshold", cl::Hidden,
|
|
|
|
cl::init(15000), cl::ZeroOrMore,
|
|
|
|
cl::desc("The code working set size is considered huge if the number of"
|
|
|
|
" blocks required to reach the -profile-summary-cutoff-hot"
|
|
|
|
" percentile exceeds this count."));
|
|
|
|
|
2019-09-25 06:17:51 +08:00
|
|
|
static cl::opt<unsigned> ProfileSummaryLargeWorkingSetSizeThreshold(
|
|
|
|
"profile-summary-large-working-set-size-threshold", cl::Hidden,
|
|
|
|
cl::init(12500), cl::ZeroOrMore,
|
|
|
|
cl::desc("The code working set size is considered large if the number of"
|
|
|
|
" blocks required to reach the -profile-summary-cutoff-hot"
|
|
|
|
" percentile exceeds this count."));
|
|
|
|
|
2018-11-03 01:39:31 +08:00
|
|
|
// The next two options override the counts derived from summary computation and
|
|
|
|
// are useful for debugging purposes.
|
|
|
|
static cl::opt<int> ProfileSummaryHotCount(
|
|
|
|
"profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
|
|
|
|
cl::desc("A fixed hot count that overrides the count derived from"
|
|
|
|
" profile-summary-cutoff-hot"));
|
|
|
|
|
|
|
|
static cl::opt<int> ProfileSummaryColdCount(
|
|
|
|
"profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
|
|
|
|
cl::desc("A fixed cold count that overrides the count derived from"
|
|
|
|
" profile-summary-cutoff-cold"));
|
|
|
|
|
[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 08:17:34 +08: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 07:06:25 +08:00
|
|
|
cl::opt<bool> ScalePartialSampleProfileWorkingSetSize(
|
2020-06-02 01:45:28 +08:00
|
|
|
"scale-partial-sample-profile-working-set-size", cl::Hidden, cl::init(true),
|
2020-04-09 07:06:25 +08: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 06:54:26 +08: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 10:32:40 +08:00
|
|
|
// available in the module and parses it to get the Summary object.
|
|
|
|
void ProfileSummaryInfo::refresh() {
|
|
|
|
if (hasProfileSummary())
|
|
|
|
return;
|
2019-03-01 03:55:07 +08:00
|
|
|
// First try to get context sensitive ProfileSummary.
|
|
|
|
auto *SummaryMD = M.getProfileSummary(/* IsCS */ true);
|
2020-05-14 10:32:40 +08:00
|
|
|
if (SummaryMD)
|
2019-03-01 03:55:07 +08:00
|
|
|
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
|
2020-05-14 10:32:40 +08:00
|
|
|
|
|
|
|
if (!hasProfileSummary()) {
|
|
|
|
// This will actually return PSK_Instr or PSK_Sample summary.
|
|
|
|
SummaryMD = M.getProfileSummary(/* IsCS */ false);
|
|
|
|
if (SummaryMD)
|
|
|
|
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
|
2019-03-01 03:55:07 +08:00
|
|
|
}
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!hasProfileSummary())
|
|
|
|
return;
|
|
|
|
computeThresholds();
|
2016-06-04 06:54:26 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
Optional<uint64_t> ProfileSummaryInfo::getProfileCount(
|
|
|
|
const CallBase &Call, BlockFrequencyInfo *BFI, bool AllowSynthetic) const {
|
2020-04-28 00:07:18 +08:00
|
|
|
assert((isa<CallInst>(Call) || isa<InvokeInst>(Call)) &&
|
2017-03-11 03:45:16 +08:00
|
|
|
"We can only get profile count for call/invoke instruction.");
|
2017-05-17 04:14:39 +08:00
|
|
|
if (hasSampleProfile()) {
|
2017-05-12 07:18:05 +08: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-04 01:11:41 +08:00
|
|
|
// since the sampled entry count may not be accurate. If there is no
|
|
|
|
// annotated on the instruction, return None.
|
2017-05-12 07:18:05 +08:00
|
|
|
uint64_t TotalCount;
|
2020-04-28 00:07:18 +08:00
|
|
|
if (Call.extractProfTotalWeight(TotalCount))
|
2017-05-12 07:18:05 +08:00
|
|
|
return TotalCount;
|
2017-08-04 01:11:41 +08:00
|
|
|
return None;
|
2017-05-12 07:18:05 +08:00
|
|
|
}
|
2017-03-11 03:45:16 +08:00
|
|
|
if (BFI)
|
2020-04-28 00:07:18 +08:00
|
|
|
return BFI->getBlockProfileCount(Call.getParent(), AllowSynthetic);
|
2017-03-11 03:45:16 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-10-11 05:47:28 +08: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-10-01 05:05:49 +08:00
|
|
|
/// example, no profile data is available).
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) const {
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!F || !hasProfileSummary())
|
2016-06-04 06:54:26 +08: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-18 06:24:23 +08:00
|
|
|
return FunctionCount && isHotCount(FunctionCount.getCount());
|
2016-06-04 06:54:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 01:53:10 +08: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 07:14:11 +08:00
|
|
|
/// If it returns false, it either means it is not hot or it is unknown
|
2017-12-21 01:53:10 +08:00
|
|
|
/// (for example, no profile data is available).
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isFunctionHotInCallGraph(
|
|
|
|
const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!F || !hasProfileSummary())
|
2017-03-24 07:14:11 +08:00
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
2018-01-18 06:24:23 +08:00
|
|
|
if (isHotCount(FunctionCount.getCount()))
|
2017-03-24 07:14:11 +08:00
|
|
|
return true;
|
|
|
|
|
2017-12-21 01:53:10 +08: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-28 00:07:18 +08:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2017-12-21 01:53:10 +08:00
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
if (isHotCount(TotalCallCount))
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-24 07:14:11 +08:00
|
|
|
for (const auto &BB : *F)
|
2018-11-19 13:23:16 +08:00
|
|
|
if (isHotBlock(&BB, &BFI))
|
2017-12-21 01:53:10 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
2017-03-24 07:14:11 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 01:53:10 +08: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 07:14:11 +08:00
|
|
|
/// If it returns false, it either means it is not cold or it is unknown
|
2017-12-21 01:53:10 +08:00
|
|
|
/// (for example, no profile data is available).
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isFunctionColdInCallGraph(
|
|
|
|
const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!F || !hasProfileSummary())
|
2017-03-24 07:14:11 +08:00
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
2018-01-18 06:24:23 +08:00
|
|
|
if (!isColdCount(FunctionCount.getCount()))
|
2017-03-24 07:14:11 +08:00
|
|
|
return false;
|
2017-12-21 01:53:10 +08: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-28 00:07:18 +08:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2017-12-21 01:53:10 +08:00
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
if (!isColdCount(TotalCallCount))
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-24 07:14:11 +08:00
|
|
|
for (const auto &BB : *F)
|
2018-11-19 13:23:16 +08:00
|
|
|
if (!isColdBlock(&BB, &BFI))
|
2017-12-21 01:53:10 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2017-03-24 07:14:11 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08: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 08:17:34 +08:00
|
|
|
assert(hasPartialSampleProfile() && "Expect partial sample profile");
|
|
|
|
return !F.getEntryCount().hasValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
template <bool isHot>
|
2020-02-28 02:49:04 +08:00
|
|
|
bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile(
|
2020-05-28 08:04:28 +08:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!F || !hasProfileSummary())
|
2019-09-25 06:17:51 +08:00
|
|
|
return false;
|
2020-02-28 02:49:04 +08:00
|
|
|
if (auto FunctionCount = F->getEntryCount()) {
|
|
|
|
if (isHot &&
|
|
|
|
isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
|
2019-09-25 06:17:51 +08:00
|
|
|
return true;
|
2020-02-28 02:49:04 +08:00
|
|
|
if (!isHot &&
|
|
|
|
!isColdCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-25 06:17:51 +08: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-28 00:07:18 +08:00
|
|
|
if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
|
2019-09-25 06:17:51 +08:00
|
|
|
TotalCallCount += CallCount.getValue();
|
2020-02-28 02:49:04 +08:00
|
|
|
if (isHot && isHotCountNthPercentile(PercentileCutoff, TotalCallCount))
|
2019-09-25 06:17:51 +08:00
|
|
|
return true;
|
2020-02-28 02:49:04 +08:00
|
|
|
if (!isHot && !isColdCountNthPercentile(PercentileCutoff, TotalCallCount))
|
|
|
|
return false;
|
2019-09-25 06:17:51 +08:00
|
|
|
}
|
2020-02-28 02:49:04 +08:00
|
|
|
for (const auto &BB : *F) {
|
|
|
|
if (isHot && isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI))
|
2019-09-25 06:17:51 +08:00
|
|
|
return true;
|
2020-02-28 02:49:04 +08:00
|
|
|
if (!isHot && !isColdBlockNthPercentile(PercentileCutoff, &BB, &BFI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !isHot;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like isFunctionHotInCallGraph but for a given cutoff.
|
|
|
|
bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile(
|
2020-05-28 08:04:28 +08:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isFunctionHotOrColdInCallGraphNthPercentile<true>(
|
|
|
|
PercentileCutoff, F, BFI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile(
|
2020-05-28 08:04:28 +08:00
|
|
|
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isFunctionHotOrColdInCallGraphNthPercentile<false>(
|
|
|
|
PercentileCutoff, F, BFI);
|
2019-09-25 06:17:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 05:47:28 +08: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-10-01 05:05:49 +08:00
|
|
|
/// example, no profile data is available).
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) const {
|
2016-06-10 09:42:05 +08:00
|
|
|
if (!F)
|
|
|
|
return false;
|
2017-03-11 04:50:51 +08:00
|
|
|
if (F->hasFnAttribute(Attribute::Cold))
|
2016-06-04 06:54:26 +08:00
|
|
|
return true;
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!hasProfileSummary())
|
2016-06-10 09:42:05 +08:00
|
|
|
return false;
|
2016-06-04 06:54:26 +08: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-18 06:24:23 +08:00
|
|
|
return FunctionCount && isColdCount(FunctionCount.getCount());
|
2016-06-04 06:54:26 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 05:05:49 +08:00
|
|
|
/// Compute the hot and cold thresholds.
|
2016-06-04 06:54:26 +08:00
|
|
|
void ProfileSummaryInfo::computeThresholds() {
|
|
|
|
auto &DetailedSummary = Summary->getDetailedSummary();
|
2020-07-09 02:19:59 +08:00
|
|
|
auto &HotEntry = ProfileSummaryBuilder::getEntryForPercentile(
|
|
|
|
DetailedSummary, ProfileSummaryCutoffHot);
|
2017-08-04 07:42:58 +08:00
|
|
|
HotCountThreshold = HotEntry.MinCount;
|
2018-11-03 01:39:31 +08:00
|
|
|
if (ProfileSummaryHotCount.getNumOccurrences() > 0)
|
|
|
|
HotCountThreshold = ProfileSummaryHotCount;
|
2020-07-09 02:19:59 +08:00
|
|
|
auto &ColdEntry = ProfileSummaryBuilder::getEntryForPercentile(
|
|
|
|
DetailedSummary, ProfileSummaryCutoffCold);
|
2017-08-04 07:42:58 +08:00
|
|
|
ColdCountThreshold = ColdEntry.MinCount;
|
2018-11-03 01:39:31 +08:00
|
|
|
if (ProfileSummaryColdCount.getNumOccurrences() > 0)
|
|
|
|
ColdCountThreshold = ProfileSummaryColdCount;
|
|
|
|
assert(ColdCountThreshold <= HotCountThreshold &&
|
|
|
|
"Cold count threshold cannot exceed hot count threshold!");
|
2020-04-09 07:06:25 +08: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 06:17:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
Optional<uint64_t>
|
|
|
|
ProfileSummaryInfo::computeThreshold(int PercentileCutoff) const {
|
2020-05-14 10:32:40 +08:00
|
|
|
if (!hasProfileSummary())
|
2019-09-25 06:17:51 +08:00
|
|
|
return None;
|
|
|
|
auto iter = ThresholdCache.find(PercentileCutoff);
|
|
|
|
if (iter != ThresholdCache.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
auto &DetailedSummary = Summary->getDetailedSummary();
|
2020-07-09 02:19:59 +08:00
|
|
|
auto &Entry = ProfileSummaryBuilder::getEntryForPercentile(DetailedSummary,
|
|
|
|
PercentileCutoff);
|
2019-09-25 06:17:51 +08:00
|
|
|
uint64_t CountThreshold = Entry.MinCount;
|
|
|
|
ThresholdCache[PercentileCutoff] = CountThreshold;
|
|
|
|
return CountThreshold;
|
2017-08-04 07:42:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::hasHugeWorkingSetSize() const {
|
2017-08-04 07:42:58 +08:00
|
|
|
return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
|
2016-06-04 06:54:26 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::hasLargeWorkingSetSize() const {
|
2019-09-25 06:17:51 +08:00
|
|
|
return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isHotCount(uint64_t C) const {
|
2016-06-04 06:54:26 +08:00
|
|
|
return HotCountThreshold && C >= HotCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isColdCount(uint64_t C) const {
|
2016-06-04 06:54:26 +08:00
|
|
|
return ColdCountThreshold && C <= ColdCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
template <bool isHot>
|
2020-02-28 02:49:04 +08:00
|
|
|
bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff,
|
2020-05-28 08:04:28 +08:00
|
|
|
uint64_t C) const {
|
2019-09-25 06:17:51 +08:00
|
|
|
auto CountThreshold = computeThreshold(PercentileCutoff);
|
2020-02-28 02:49:04 +08:00
|
|
|
if (isHot)
|
|
|
|
return CountThreshold && C >= CountThreshold.getValue();
|
|
|
|
else
|
|
|
|
return CountThreshold && C <= CountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff,
|
|
|
|
uint64_t C) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isHotOrColdCountNthPercentile<true>(PercentileCutoff, C);
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff,
|
|
|
|
uint64_t C) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isHotOrColdCountNthPercentile<false>(PercentileCutoff, C);
|
2019-09-25 06:17:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() const {
|
2018-08-08 02:13:10 +08:00
|
|
|
return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
|
2018-05-11 07:02:27 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() const {
|
2018-08-08 02:13:10 +08:00
|
|
|
return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
|
2018-05-11 07:02:27 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB,
|
|
|
|
BlockFrequencyInfo *BFI) const {
|
2018-11-19 13:23:16 +08:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
2017-03-10 09:44:37 +08:00
|
|
|
return Count && isHotCount(*Count);
|
2017-01-13 09:34:00 +08:00
|
|
|
}
|
|
|
|
|
2018-11-19 13:23:16 +08:00
|
|
|
bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
|
2020-05-28 08:04:28 +08:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2018-11-19 13:23:16 +08:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
2018-12-14 05:51:42 +08:00
|
|
|
return Count && isColdCount(*Count);
|
2017-01-13 09:34:00 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
template <bool isHot>
|
|
|
|
bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
auto Count = BFI->getBlockProfileCount(BB);
|
|
|
|
if (isHot)
|
|
|
|
return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
else
|
|
|
|
return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isHotBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isHotOrColdBlockNthPercentile<true>(PercentileCutoff, BB, BFI);
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08:00
|
|
|
bool ProfileSummaryInfo::isColdBlockNthPercentile(
|
|
|
|
int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
|
2020-02-28 02:49:04 +08:00
|
|
|
return isHotOrColdBlockNthPercentile<false>(PercentileCutoff, BB, BFI);
|
2019-09-25 06:17:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 10:35:05 +08:00
|
|
|
bool ProfileSummaryInfo::isHotCallSite(const CallBase &CB,
|
2020-05-28 08:04:28 +08:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2020-04-28 00:07:18 +08:00
|
|
|
auto C = getProfileCount(CB, BFI);
|
2017-03-11 03:45:16 +08:00
|
|
|
return C && isHotCount(*C);
|
2017-01-13 09:34:00 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 10:35:05 +08:00
|
|
|
bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB,
|
2020-05-28 08:04:28 +08:00
|
|
|
BlockFrequencyInfo *BFI) const {
|
2020-04-28 00:07:18 +08:00
|
|
|
auto C = getProfileCount(CB, BFI);
|
2017-08-04 01:11:41 +08:00
|
|
|
if (C)
|
|
|
|
return isColdCount(*C);
|
|
|
|
|
|
|
|
// In SamplePGO, if the caller has been sampled, and there is no profile
|
2018-04-13 02:36:01 +08:00
|
|
|
// annotated on the callsite, we consider the callsite as cold.
|
2020-04-18 10:35:05 +08:00
|
|
|
return hasSampleProfile() && CB.getCaller()->hasProfileData();
|
2016-11-10 07:36:02 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:04:28 +08: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 08:17:34 +08:00
|
|
|
return hasProfileSummary() &&
|
|
|
|
Summary->getKind() == ProfileSummary::PSK_Sample &&
|
|
|
|
(PartialProfile || Summary->isPartialProfile());
|
|
|
|
}
|
|
|
|
|
2016-06-04 06:54:26 +08:00
|
|
|
INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
|
|
|
|
"Profile summary info", false, true)
|
|
|
|
|
|
|
|
ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
|
|
|
|
: ImmutablePass(ID) {
|
|
|
|
initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:00:58 +08:00
|
|
|
bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
|
|
|
|
PSI.reset(new ProfileSummaryInfo(M));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
|
|
|
|
PSI.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:53:26 +08:00
|
|
|
AnalysisKey ProfileSummaryAnalysis::Key;
|
2016-06-17 08:11:01 +08:00
|
|
|
ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
|
|
|
|
ModuleAnalysisManager &) {
|
2016-09-29 05:00:58 +08:00
|
|
|
return ProfileSummaryInfo(M);
|
2016-06-04 06:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
|
2016-08-09 08:28:38 +08:00
|
|
|
ModuleAnalysisManager &AM) {
|
2016-06-04 06:54:26 +08: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-11 05:47:28 +08:00
|
|
|
if (PSI.isFunctionEntryHot(&F))
|
|
|
|
OS << " :hot entry ";
|
|
|
|
else if (PSI.isFunctionEntryCold(&F))
|
|
|
|
OS << " :cold entry ";
|
2016-06-04 06:54:26 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char ProfileSummaryInfoWrapperPass::ID = 0;
|