llvm-project/llvm/lib/Analysis/StaticDataProfileInfo.cpp
Mingming Liu c8a70f4c6e
[CodeGen][StaticDataPartitioning]Place local-linkage global variables in hot or unlikely prefixed sections based on profile information (#125756)
In this PR, static-data-splitter pass finds out the local-linkage global
variables in {`.rodata`, `.data.rel.ro`, `bss`, `.data`} sections by
analyzing machine instruction operands, and aggregates their accesses
from code across functions.

A follow-up item is to analyze global variable initializers and count
for access from data.
* This limitation is demonstrated by `bss2` and `data3` in
`llvm/test/CodeGen/X86/global-variable-partition.ll`.

Some stats of static-data-splitter with this patch:

**section**|**bss**|**rodata**|**data**
:-----:|:-----:|:-----:|:-----:
hot-prefixed section coverage|99.75%|97.71%|91.30%
unlikely-prefixed section size percentage|67.94%|39.37%|63.10%

1. The coverage is defined as `#perf-sample-in-hot-prefixed <data>
section / #perf-sample in <data.*> section` for each <data> section.
* The perf command samples
`MEM_INST_RETIRED.ALL_LOADS:u:pinned:precise=2` events at a high
frequency (`perf -c 2251`) for 30 seconds. The profiled binary is built
as non-PIE so `data.rel.ro` coverage data is not available.
2. The unlikely-prefixed `<data>` section size percentage is defined as
`unlikely <data> section size / the sum size of <data>.* sections` for
each `<data>` section
2025-03-28 16:31:46 -07:00

73 lines
2.4 KiB
C++

#include "llvm/Analysis/StaticDataProfileInfo.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/InitializePasses.h"
#include "llvm/ProfileData/InstrProf.h"
#include <sys/types.h>
using namespace llvm;
void StaticDataProfileInfo::addConstantProfileCount(
const Constant *C, std::optional<uint64_t> Count) {
if (!Count) {
ConstantWithoutCounts.insert(C);
return;
}
uint64_t &OriginalCount = ConstantProfileCounts[C];
OriginalCount = llvm::SaturatingAdd(*Count, OriginalCount);
// Clamp the count to getInstrMaxCountValue. InstrFDO reserves a few
// large values for special use.
if (OriginalCount > getInstrMaxCountValue())
OriginalCount = getInstrMaxCountValue();
}
std::optional<uint64_t>
StaticDataProfileInfo::getConstantProfileCount(const Constant *C) const {
auto I = ConstantProfileCounts.find(C);
if (I == ConstantProfileCounts.end())
return std::nullopt;
return I->second;
}
StringRef StaticDataProfileInfo::getConstantSectionPrefix(
const Constant *C, const ProfileSummaryInfo *PSI) const {
auto Count = getConstantProfileCount(C);
if (!Count)
return "";
// The accummulated counter shows the constant is hot. Return 'hot' whether
// this variable is seen by unprofiled functions or not.
if (PSI->isHotCount(*Count))
return "hot";
// The constant is not hot, and seen by unprofiled functions. We don't want to
// assign it to unlikely sections, even if the counter says 'cold'. So return
// an empty prefix before checking whether the counter is cold.
if (ConstantWithoutCounts.count(C))
return "";
// The accummulated counter shows the constant is cold. Return 'unlikely'.
if (PSI->isColdCount(*Count))
return "unlikely";
// The counter says lukewarm. Return an empty prefix.
return "";
}
bool StaticDataProfileInfoWrapperPass::doInitialization(Module &M) {
Info.reset(new StaticDataProfileInfo());
return false;
}
bool StaticDataProfileInfoWrapperPass::doFinalization(Module &M) {
Info.reset();
return false;
}
INITIALIZE_PASS(StaticDataProfileInfoWrapperPass, "static-data-profile-info",
"Static Data Profile Info", false, true)
StaticDataProfileInfoWrapperPass::StaticDataProfileInfoWrapperPass()
: ImmutablePass(ID) {
initializeStaticDataProfileInfoWrapperPassPass(
*PassRegistry::getPassRegistry());
}
char StaticDataProfileInfoWrapperPass::ID = 0;