Skip to content

Commit 97103c6

Browse files
resolve comments
1 parent 9302b2b commit 97103c6

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

llvm/include/llvm/Analysis/StaticDataProfileInfo.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "llvm/ADT/DenseMap.h"
55
#include "llvm/ADT/DenseSet.h"
6+
#include "llvm/Analysis/ProfileSummaryInfo.h"
67
#include "llvm/IR/Constant.h"
78
#include "llvm/Pass.h"
89

@@ -20,6 +21,9 @@ class StaticDataProfileInfo {
2021
/// counts.
2122
DenseSet<const Constant *> ConstantWithoutCounts;
2223

24+
/// If \p C has a count, return it. Otherwise, return std::nullopt.
25+
std::optional<uint64_t> getConstantProfileCount(const Constant *C) const;
26+
2327
public:
2428
StaticDataProfileInfo() = default;
2529

@@ -30,13 +34,16 @@ class StaticDataProfileInfo {
3034
void addConstantProfileCount(const Constant *C,
3135
std::optional<uint64_t> Count);
3236

33-
/// If \p C has a count, return it. Otherwise, return std::nullopt.
34-
std::optional<uint64_t> getConstantProfileCount(const Constant *C) const;
35-
36-
/// Return true if the constant \p C is seen at least once without profiles.
37-
bool hasUnknownCount(const Constant *C) const {
38-
return ConstantWithoutCounts.count(C);
39-
}
37+
/// Return a section prefix for the constant \p C based on its profile count.
38+
/// - If a constant doesn't have a counter, return an empty string.
39+
/// - Otherwise,
40+
/// - If it has a hot count, return "hot".
41+
/// - If it is seen by unprofiled function, return an empty string.
42+
/// - If it has a cold count, return "unlikely".
43+
/// - Otherwise (e.g. it's used by lukewarm functions), return an empty
44+
/// string.
45+
StringRef getConstantSectionPrefix(const Constant *C,
46+
const ProfileSummaryInfo *PSI) const;
4047
};
4148

4249
/// This wraps the StaticDataProfileInfo object as an immutable pass, for a

llvm/lib/Analysis/StaticDataProfileInfo.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "llvm/Analysis/StaticDataProfileInfo.h"
2+
#include "llvm/Analysis/ProfileSummaryInfo.h"
23
#include "llvm/IR/Constant.h"
34
#include "llvm/IR/GlobalVariable.h"
45
#include "llvm/InitializePasses.h"
@@ -13,7 +14,7 @@ void StaticDataProfileInfo::addConstantProfileCount(
1314
return;
1415
}
1516
uint64_t &OriginalCount = ConstantProfileCounts[C];
16-
OriginalCount += llvm::SaturatingAdd(*Count, OriginalCount);
17+
OriginalCount = llvm::SaturatingAdd(*Count, OriginalCount);
1718
// Clamp the count to getInstrMaxCountValue. InstrFDO reserves a few
1819
// large values for special use.
1920
if (OriginalCount > getInstrMaxCountValue())
@@ -28,6 +29,20 @@ StaticDataProfileInfo::getConstantProfileCount(const Constant *C) const {
2829
return I->second;
2930
}
3031

32+
StringRef StaticDataProfileInfo::getConstantSectionPrefix(
33+
const Constant *C, const ProfileSummaryInfo *PSI) const {
34+
auto Count = getConstantProfileCount(C);
35+
if (!Count)
36+
return "";
37+
if (PSI->isHotCount(*Count))
38+
return "hot";
39+
if (ConstantWithoutCounts.count(C))
40+
return "";
41+
if (PSI->isColdCount(*Count))
42+
return "unlikely";
43+
return "";
44+
}
45+
3146
bool StaticDataProfileInfoWrapperPass::doInitialization(Module &M) {
3247
Info.reset(new StaticDataProfileInfo());
3348
return false;

llvm/lib/CodeGen/StaticDataAnnotator.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,12 @@ bool StaticDataAnnotator::runOnModule(Module &M) {
8282
if (GV.isDeclarationForLinker())
8383
continue;
8484

85-
// Skip global variables without profile counts. The module may not be
86-
// profiled or instrumented.
87-
auto Count = SDPI->getConstantProfileCount(&GV);
88-
if (!Count)
85+
StringRef SectionPrefix = SDPI->getConstantSectionPrefix(&GV, PSI);
86+
if (SectionPrefix.empty() || alreadyHasSectionPrefix(GV, SectionPrefix))
8987
continue;
9088

91-
if (PSI->isHotCount(*Count) && !alreadyHasSectionPrefix(GV, "hot")) {
92-
// The variable counter is hot, set 'hot' section prefix if the section
93-
// prefix isn't hot already.
94-
GV.setSectionPrefix("hot");
95-
Changed = true;
96-
} else if (PSI->isColdCount(*Count) && !SDPI->hasUnknownCount(&GV) &&
97-
!alreadyHasSectionPrefix(GV, "unlikely")) {
98-
// The variable counter is cold, set 'unlikely' section prefix when
99-
// 1) the section prefix isn't unlikely already, and
100-
// 2) the variable is not seen without profile counts. The reason is that
101-
// a variable without profile counts doesn't have all its uses profiled,
102-
// for example when a function is not instrumented, or not sampled (new
103-
// code paths).
104-
GV.setSectionPrefix("unlikely");
105-
Changed = true;
106-
}
89+
GV.setSectionPrefix(SectionPrefix);
90+
Changed = true;
10791
}
10892

10993
return Changed;

0 commit comments

Comments
 (0)