Skip to content

Commit 3d70a00

Browse files
squash commits
1 parent e313bc8 commit 3d70a00

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

llvm/include/llvm/Analysis/StaticDataProfileInfo.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ bool IsAnnotationOK(const GlobalVariable &GV);
3232
/// profile information and provides methods to operate on them.
3333
class StaticDataProfileInfo {
3434
public:
35-
/// Accummulate the profile count of a constant that will be lowered to static
36-
/// data sections.
35+
/// A constant is tracked only if the following conditions are met.
36+
/// 1) It has local (i.e., private or internal) linkage.
37+
// 2) Its data kind is one of {.rodata, .data, .bss, .data.rel.ro}.
38+
// 3) It's eligible for section prefix annotation. See `AnnotationKind`
39+
// above for ineligible reasons.
3740
DenseMap<const Constant *, uint64_t> ConstantProfileCounts;
3841

3942
/// Keeps track of the constants that are seen at least once without profile
@@ -44,6 +47,20 @@ class StaticDataProfileInfo {
4447
LLVM_ABI std::optional<uint64_t>
4548
getConstantProfileCount(const Constant *C) const;
4649

50+
enum class StaticDataHotness : uint8_t {
51+
Cold = 0,
52+
LukewarmOrUnknown = 1,
53+
Hot = 2,
54+
};
55+
56+
/// Return the hotness of the constant \p C based on its profile count \p
57+
/// Count.
58+
LLVM_ABI StaticDataHotness getSectionHotnessUsingProfileCount(
59+
const Constant *C, const ProfileSummaryInfo *PSI, uint64_t Count) const;
60+
61+
/// Return the string representation of the hotness enum \p Hotness.
62+
LLVM_ABI StringRef hotnessToStr(StaticDataHotness Hotness) const;
63+
4764
public:
4865
StaticDataProfileInfo() = default;
4966

llvm/lib/Analysis/StaticDataProfileInfo.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ void StaticDataProfileInfo::addConstantProfileCount(
6060
OriginalCount = getInstrMaxCountValue();
6161
}
6262

63+
StaticDataProfileInfo::StaticDataHotness
64+
StaticDataProfileInfo::getSectionHotnessUsingProfileCount(
65+
const Constant *C, const ProfileSummaryInfo *PSI, uint64_t Count) const {
66+
// The accummulated counter shows the constant is hot. Return 'hot' whether
67+
// this variable is seen by unprofiled functions or not.
68+
if (PSI->isHotCount(Count))
69+
return StaticDataHotness::Hot;
70+
// The constant is not hot, and seen by unprofiled functions. We don't want to
71+
// assign it to unlikely sections, even if the counter says 'cold'. So return
72+
// an empty prefix before checking whether the counter is cold.
73+
if (ConstantWithoutCounts.count(C))
74+
return StaticDataHotness::LukewarmOrUnknown;
75+
// The accummulated counter shows the constant is cold. Return 'unlikely'.
76+
if (PSI->isColdCount(Count))
77+
return StaticDataHotness::Cold;
78+
79+
return StaticDataHotness::LukewarmOrUnknown;
80+
}
81+
82+
StringRef StaticDataProfileInfo::hotnessToStr(
83+
StaticDataProfileInfo::StaticDataHotness Hotness) const {
84+
switch (Hotness) {
85+
case StaticDataProfileInfo::StaticDataHotness::Cold:
86+
return "unlikely";
87+
case StaticDataProfileInfo::StaticDataHotness::Hot:
88+
return "hot";
89+
default:
90+
return "";
91+
}
92+
}
93+
6394
std::optional<uint64_t>
6495
StaticDataProfileInfo::getConstantProfileCount(const Constant *C) const {
6596
auto I = ConstantProfileCounts.find(C);
@@ -70,23 +101,10 @@ StaticDataProfileInfo::getConstantProfileCount(const Constant *C) const {
70101

71102
StringRef StaticDataProfileInfo::getConstantSectionPrefix(
72103
const Constant *C, const ProfileSummaryInfo *PSI) const {
73-
auto Count = getConstantProfileCount(C);
104+
std::optional<uint64_t> Count = getConstantProfileCount(C);
74105
if (!Count)
75106
return "";
76-
// The accummulated counter shows the constant is hot. Return 'hot' whether
77-
// this variable is seen by unprofiled functions or not.
78-
if (PSI->isHotCount(*Count))
79-
return "hot";
80-
// The constant is not hot, and seen by unprofiled functions. We don't want to
81-
// assign it to unlikely sections, even if the counter says 'cold'. So return
82-
// an empty prefix before checking whether the counter is cold.
83-
if (ConstantWithoutCounts.count(C))
84-
return "";
85-
// The accummulated counter shows the constant is cold. Return 'unlikely'.
86-
if (PSI->isColdCount(*Count))
87-
return "unlikely";
88-
// The counter says lukewarm. Return an empty prefix.
89-
return "";
107+
return hotnessToStr(getSectionHotnessUsingProfileCount(C, PSI, *Count));
90108
}
91109

92110
bool StaticDataProfileInfoWrapperPass::doInitialization(Module &M) {

0 commit comments

Comments
 (0)