Skip to content

Commit fa3b587

Browse files
committed
[llvm]NFC] Simplify ProfileSummaryInfo state transitions
ProfileSummaryInfo is updated seldom, as result of very specific triggers. This patch clearly demarcates state updates from read-only uses. This, arguably, improves readability and maintainability.
1 parent 48cb380 commit fa3b587

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

llvm/include/llvm/Analysis/ProfileSummaryInfo.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class ProfileSummaryInfo {
4040
private:
4141
Module &M;
4242
std::unique_ptr<ProfileSummary> Summary;
43-
bool computeSummary();
4443
void computeThresholds();
4544
// Count thresholds to answer isHotCount and isColdCount queries.
4645
Optional<uint64_t> HotCountThreshold, ColdCountThreshold;
@@ -56,15 +55,17 @@ class ProfileSummaryInfo {
5655
Optional<uint64_t> computeThreshold(int PercentileCutoff);
5756
// The map that caches the threshold values. The keys are the percentile
5857
// cutoff values and the values are the corresponding threshold values.
59-
DenseMap<int, uint64_t> ThresholdCache;
58+
mutable DenseMap<int, uint64_t> ThresholdCache;
6059

6160
public:
62-
ProfileSummaryInfo(Module &M) : M(M) {}
63-
ProfileSummaryInfo(ProfileSummaryInfo &&Arg)
64-
: M(Arg.M), Summary(std::move(Arg.Summary)) {}
61+
ProfileSummaryInfo(Module &M) : M(M) { refresh(); }
62+
ProfileSummaryInfo(ProfileSummaryInfo &&Arg) = default;
63+
64+
/// If no summary is present, attempt to refresh.
65+
void refresh();
6566

6667
/// Returns true if profile summary is available.
67-
bool hasProfileSummary() { return computeSummary(); }
68+
bool hasProfileSummary() const { return Summary != nullptr; }
6869

6970
/// Returns true if module \c M has sample profile.
7071
bool hasSampleProfile() {

llvm/lib/Analysis/ProfileSummaryInfo.cpp

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,24 @@ static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
8686
// The profile summary metadata may be attached either by the frontend or by
8787
// any backend passes (IR level instrumentation, for example). This method
8888
// checks if the Summary is null and if so checks if the summary metadata is now
89-
// available in the module and parses it to get the Summary object. Returns true
90-
// if a valid Summary is available.
91-
bool ProfileSummaryInfo::computeSummary() {
92-
if (Summary)
93-
return true;
89+
// available in the module and parses it to get the Summary object.
90+
void ProfileSummaryInfo::refresh() {
91+
if (hasProfileSummary())
92+
return;
9493
// First try to get context sensitive ProfileSummary.
9594
auto *SummaryMD = M.getProfileSummary(/* IsCS */ true);
96-
if (SummaryMD) {
95+
if (SummaryMD)
9796
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
98-
return true;
97+
98+
if (!hasProfileSummary()) {
99+
// This will actually return PSK_Instr or PSK_Sample summary.
100+
SummaryMD = M.getProfileSummary(/* IsCS */ false);
101+
if (SummaryMD)
102+
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
99103
}
100-
// This will actually return PSK_Instr or PSK_Sample summary.
101-
SummaryMD = M.getProfileSummary(/* IsCS */ false);
102-
if (!SummaryMD)
103-
return false;
104-
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
105-
return true;
104+
if (!hasProfileSummary())
105+
return;
106+
computeThresholds();
106107
}
107108

108109
Optional<uint64_t> ProfileSummaryInfo::getProfileCount(const CallBase &Call,
@@ -129,7 +130,7 @@ Optional<uint64_t> ProfileSummaryInfo::getProfileCount(const CallBase &Call,
129130
/// either means it is not hot or it is unknown whether it is hot or not (for
130131
/// example, no profile data is available).
131132
bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
132-
if (!F || !computeSummary())
133+
if (!F || !hasProfileSummary())
133134
return false;
134135
auto FunctionCount = F->getEntryCount();
135136
// FIXME: The heuristic used below for determining hotness is based on
@@ -145,7 +146,7 @@ bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
145146
/// (for example, no profile data is available).
146147
bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
147148
BlockFrequencyInfo &BFI) {
148-
if (!F || !computeSummary())
149+
if (!F || !hasProfileSummary())
149150
return false;
150151
if (auto FunctionCount = F->getEntryCount())
151152
if (isHotCount(FunctionCount.getCount()))
@@ -174,7 +175,7 @@ bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
174175
/// (for example, no profile data is available).
175176
bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
176177
BlockFrequencyInfo &BFI) {
177-
if (!F || !computeSummary())
178+
if (!F || !hasProfileSummary())
178179
return false;
179180
if (auto FunctionCount = F->getEntryCount())
180181
if (!isColdCount(FunctionCount.getCount()))
@@ -204,7 +205,7 @@ bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) {
204205
template<bool isHot>
205206
bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile(
206207
int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) {
207-
if (!F || !computeSummary())
208+
if (!F || !hasProfileSummary())
208209
return false;
209210
if (auto FunctionCount = F->getEntryCount()) {
210211
if (isHot &&
@@ -256,7 +257,7 @@ bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
256257
return false;
257258
if (F->hasFnAttribute(Attribute::Cold))
258259
return true;
259-
if (!computeSummary())
260+
if (!hasProfileSummary())
260261
return false;
261262
auto FunctionCount = F->getEntryCount();
262263
// FIXME: The heuristic used below for determining coldness is based on
@@ -267,8 +268,6 @@ bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
267268

268269
/// Compute the hot and cold thresholds.
269270
void ProfileSummaryInfo::computeThresholds() {
270-
if (!computeSummary())
271-
return;
272271
auto &DetailedSummary = Summary->getDetailedSummary();
273272
auto &HotEntry =
274273
getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
@@ -289,7 +288,7 @@ void ProfileSummaryInfo::computeThresholds() {
289288
}
290289

291290
Optional<uint64_t> ProfileSummaryInfo::computeThreshold(int PercentileCutoff) {
292-
if (!computeSummary())
291+
if (!hasProfileSummary())
293292
return None;
294293
auto iter = ThresholdCache.find(PercentileCutoff);
295294
if (iter != ThresholdCache.end()) {
@@ -304,26 +303,18 @@ Optional<uint64_t> ProfileSummaryInfo::computeThreshold(int PercentileCutoff) {
304303
}
305304

306305
bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
307-
if (!HasHugeWorkingSetSize)
308-
computeThresholds();
309306
return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
310307
}
311308

312309
bool ProfileSummaryInfo::hasLargeWorkingSetSize() {
313-
if (!HasLargeWorkingSetSize)
314-
computeThresholds();
315310
return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue();
316311
}
317312

318313
bool ProfileSummaryInfo::isHotCount(uint64_t C) {
319-
if (!HotCountThreshold)
320-
computeThresholds();
321314
return HotCountThreshold && C >= HotCountThreshold.getValue();
322315
}
323316

324317
bool ProfileSummaryInfo::isColdCount(uint64_t C) {
325-
if (!ColdCountThreshold)
326-
computeThresholds();
327318
return ColdCountThreshold && C <= ColdCountThreshold.getValue();
328319
}
329320

@@ -346,14 +337,10 @@ bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff, uint64_t
346337
}
347338

348339
uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
349-
if (!HotCountThreshold)
350-
computeThresholds();
351340
return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
352341
}
353342

354343
uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
355-
if (!ColdCountThreshold)
356-
computeThresholds();
357344
return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
358345
}
359346

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,10 +1848,11 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM,
18481848
GUIDToFuncNameMapper Mapper(M, *Reader, GUIDToFuncNameMap);
18491849

18501850
PSI = _PSI;
1851-
if (M.getProfileSummary(/* IsCS */ false) == nullptr)
1851+
if (M.getProfileSummary(/* IsCS */ false) == nullptr) {
18521852
M.setProfileSummary(Reader->getSummary().getMD(M.getContext()),
18531853
ProfileSummary::PSK_Sample);
1854-
1854+
PSI->refresh();
1855+
}
18551856
// Compute the total number of samples collected in this profile.
18561857
for (const auto &I : Reader->getProfiles())
18571858
TotalCollectedSamples += I.second.getTotalSamples();

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,7 @@ static bool annotateAllFunctions(
16131613
M.setProfileSummary(PGOReader->getSummary(IsCS).getMD(M.getContext()),
16141614
IsCS ? ProfileSummary::PSK_CSInstr
16151615
: ProfileSummary::PSK_Instr);
1616+
PSI->refresh();
16161617

16171618
std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
16181619
collectComdatMembers(M, ComdatMembers);

0 commit comments

Comments
 (0)