LLVM 22.0.0git
StaticDataProfileInfo.cpp
Go to the documentation of this file.
3#include "llvm/IR/Constant.h"
4#include "llvm/IR/Constants.h"
6#include "llvm/IR/Module.h"
9
10#define DEBUG_TYPE "static-data-profile-info"
11
12using namespace llvm;
13
14namespace llvm {
15namespace memprof {
16// Returns true iff the global variable has custom section either by
17// __attribute__((section("name")))
18// (https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate)
19// or #pragma clang section directives
20// (https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section).
21static bool hasExplicitSectionName(const GlobalVariable &GVar) {
22 if (GVar.hasSection())
23 return true;
24
25 auto Attrs = GVar.getAttributes();
26 if (Attrs.hasAttribute("bss-section") || Attrs.hasAttribute("data-section") ||
27 Attrs.hasAttribute("relro-section") ||
28 Attrs.hasAttribute("rodata-section"))
29 return true;
30 return false;
31}
32
36 // Skip 'llvm.'-prefixed global variables conservatively because they are
37 // often handled specially,
38 StringRef Name = GV.getName();
39 if (Name.starts_with("llvm."))
41 // Respect user-specified custom data sections.
45}
46
50} // namespace memprof
51} // namespace llvm
52
54 const Constant *C, std::optional<uint64_t> Count) {
55 if (!Count) {
57 return;
58 }
59 uint64_t &OriginalCount = ConstantProfileCounts[C];
60 OriginalCount = llvm::SaturatingAdd(*Count, OriginalCount);
61 // Clamp the count to getInstrMaxCountValue. InstrFDO reserves a few
62 // large values for special use.
63 if (OriginalCount > getInstrMaxCountValue())
64 OriginalCount = getInstrMaxCountValue();
65}
66
69 const Constant *C, const ProfileSummaryInfo *PSI, uint64_t Count) const {
70 // The accummulated counter shows the constant is hot. Return enum 'hot'
71 // whether this variable is seen by unprofiled functions or not.
72 if (PSI->isHotCount(Count))
74 // The constant is not hot, and seen by unprofiled functions. We don't want to
75 // assign it to unlikely sections, even if the counter says 'cold'. So return
76 // enum 'LukewarmOrUnknown'.
77 if (ConstantWithoutCounts.count(C))
79 // The accummulated counter shows the constant is cold so return enum 'cold'.
80 if (PSI->isColdCount(Count))
82
84}
85
88 std::optional<StringRef> MaybeSectionPrefix) const {
89 if (!MaybeSectionPrefix)
91 StringRef Prefix = *MaybeSectionPrefix;
92 assert((Prefix == "hot" || Prefix == "unlikely") &&
93 "Expect section_prefix to be one of hot or unlikely");
94 return Prefix == "hot" ? StaticDataHotness::Hot : StaticDataHotness::Cold;
95}
96
98 switch (Hotness) {
100 return "unlikely";
102 return "hot";
103 default:
104 return "";
105 }
106}
107
108std::optional<uint64_t>
110 auto I = ConstantProfileCounts.find(C);
111 if (I == ConstantProfileCounts.end())
112 return std::nullopt;
113 return I->second;
114}
115
117 const Constant *C, const ProfileSummaryInfo *PSI) const {
118 std::optional<uint64_t> Count = getConstantProfileCount(C);
119
120#ifndef NDEBUG
121 auto DbgPrintPrefix = [](StringRef Prefix) {
122 return Prefix.empty() ? "<empty>" : Prefix;
123 };
124#endif
125
127 // Module flag `HasDataAccessProf` is 1 -> empty section prefix means
128 // unknown hotness except for string literals.
131 !GV->getName().starts_with(".str")) {
132 auto HotnessFromDataAccessProf =
133 getSectionHotnessUsingDataAccessProfile(GV->getSectionPrefix());
134
135 if (!Count) {
136 StringRef Prefix = hotnessToStr(HotnessFromDataAccessProf);
137 LLVM_DEBUG(dbgs() << GV->getName() << " has section prefix "
138 << DbgPrintPrefix(Prefix)
139 << ", solely from data access profiles\n");
140 return Prefix;
141 }
142
143 // Both data access profiles and PGO counters are available. Use the
144 // hotter one.
145 auto HotnessFromPGO = getConstantHotnessUsingProfileCount(C, PSI, *Count);
147 if (HotnessFromDataAccessProf == StaticDataHotness::Hot ||
148 HotnessFromPGO == StaticDataHotness::Hot) {
149 GlobalVarHotness = StaticDataHotness::Hot;
150 } else if (HotnessFromDataAccessProf ==
152 HotnessFromPGO == StaticDataHotness::LukewarmOrUnknown) {
153 GlobalVarHotness = StaticDataHotness::LukewarmOrUnknown;
154 } else {
155 GlobalVarHotness = StaticDataHotness::Cold;
156 }
157 StringRef Prefix = hotnessToStr(GlobalVarHotness);
159 dbgs() << GV->getName() << " has section prefix "
160 << DbgPrintPrefix(Prefix)
161 << ", the max from data access profiles as "
162 << DbgPrintPrefix(hotnessToStr(HotnessFromDataAccessProf))
163 << " and PGO counters as "
164 << DbgPrintPrefix(hotnessToStr(HotnessFromPGO)) << "\n");
165 return Prefix;
166 }
167 }
168 if (!Count)
169 return "";
171}
172
174 bool EnableDataAccessProf = false;
176 M.getModuleFlag("EnableDataAccessProf")))
177 EnableDataAccessProf = MD->getZExtValue();
178 Info.reset(new StaticDataProfileInfo(EnableDataAccessProf));
179 return false;
180}
181
183 Info.reset();
184 return false;
185}
186
188 "Static Data Profile Info", false, true)
189
191 : ImmutablePass(ID) {}
192
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:114
This is an important base class in LLVM.
Definition Constant.h:43
bool hasSection() const
Check if this global has a custom object file section.
bool isDeclarationForLinker() const
AttributeSet getAttributes() const
Return the attribute set for this global.
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:285
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Analysis providing profile information.
LLVM_ABI bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
LLVM_ABI bool isHotCount(uint64_t C) const
Returns true if count C is considered hot.
This wraps the StaticDataProfileInfo object as an immutable pass, for a backend pass to operate on.
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
A class that holds the constants that represent static data and their profile information and provide...
LLVM_ABI std::optional< uint64_t > getConstantProfileCount(const Constant *C) const
If C has a count, return it. Otherwise, return std::nullopt.
LLVM_ABI StaticDataHotness getConstantHotnessUsingProfileCount(const Constant *C, const ProfileSummaryInfo *PSI, uint64_t Count) const
Return the hotness of the constant C based on its profile count Count.
LLVM_ABI StringRef hotnessToStr(StaticDataHotness Hotness) const
Return the string representation of the hotness enum Hotness.
StaticDataHotness
Use signed enums for enum value comparison, and make 'LukewarmOrUnknown' as 0 so any accidentally uni...
LLVM_ABI void addConstantProfileCount(const Constant *C, std::optional< uint64_t > Count)
If Count is not nullopt, add it to the profile count of the constant C in a saturating way,...
LLVM_ABI StringRef getConstantSectionPrefix(const Constant *C, const ProfileSummaryInfo *PSI) const
Given a constant C, returns a section prefix.
LLVM_ABI StaticDataHotness getSectionHotnessUsingDataAccessProfile(std::optional< StringRef > SectionPrefix) const
Return the hotness based on section prefix SectionPrefix.
DenseMap< const Constant *, uint64_t > ConstantProfileCounts
A constant is tracked only if the following conditions are met.
DenseSet< const Constant * > ConstantWithoutCounts
Keeps track of the constants that are seen at least once without profile counts.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:682
AnnotationKind getAnnotationKind(const GlobalVariable &GV)
Returns the annotation kind of the global variable GV.
bool IsAnnotationOK(const GlobalVariable &GV)
Returns true if the annotation kind of the global variable GV is AnnotationOK.
static bool hasExplicitSectionName(const GlobalVariable &GVar)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
uint64_t getInstrMaxCountValue()
Return the max count value. We reserver a few large values for special use.
Definition InstrProf.h:98
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition MathExtras.h:611