Skip to content

Commit 7f1da8e

Browse files
Refactor getConstantSectionPrefix to prepare for the next change
1 parent c56b792 commit 7f1da8e

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
@@ -57,6 +57,37 @@ void StaticDataProfileInfo::addConstantProfileCount(
5757
OriginalCount = getInstrMaxCountValue();
5858
}
5959

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

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

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

0 commit comments

Comments
 (0)