aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
diff options
context:
space:
mode:
authorTeresa Johnson <tejohnson@google.com>2025-06-03 14:20:38 -0700
committerGitHub <noreply@github.com>2025-06-03 14:20:38 -0700
commitf2adae57808996313dfec41a180454857ebd60d1 (patch)
tree244ccea90a8c99dbf53ffcd13498aa8dfd418d3f /llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
parentf5a2f00da9b741f4f2fe925a434f608aa217cee2 (diff)
downloadllvm-f2adae57808996313dfec41a180454857ebd60d1.zip
llvm-f2adae57808996313dfec41a180454857ebd60d1.tar.gz
llvm-f2adae57808996313dfec41a180454857ebd60d1.tar.bz2
[MemProf] Optionally save context size info on largest cold allocations (#142507)
In order to allow selective reporting of context hinting during the LTO link, and in the future to allow selective more aggressive cloning, add an option to specify a minimum percent of the max cold size in the profile summary. Contexts that meet that threshold will get context size info metadata (and ThinLTO summary information) on the associated allocations. Specifying -memprof-report-hinted-sizes during the pre-LTO compile step will continue to cause all contexts to receive this metadata. But specifying -memprof-report-hinted-sizes only during the LTO link will cause only those that meet the new threshold and have the metadata to get reported. To support this, because the alloc info summary and associated bitcode requires the context size information to be in the same order as the other context information, 0s are inserted for contexts without this metadata. The bitcode writer uses a more compact format for the context ids to allow better compression of the 0s. As part of this change several helper methods are added to query whether metadata contains context size info on any or all contexts.
Diffstat (limited to 'llvm/lib/Analysis/ModuleSummaryAnalysis.cpp')
-rw-r--r--llvm/lib/Analysis/ModuleSummaryAnalysis.cpp25
1 files changed, 22 insertions, 3 deletions
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index 59fa1a4..a317ac4 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -525,6 +525,7 @@ static void computeFunctionSummary(
if (MemProfMD) {
std::vector<MIBInfo> MIBs;
std::vector<std::vector<ContextTotalSize>> ContextSizeInfos;
+ bool HasNonZeroContextSizeInfos = false;
for (auto &MDOp : MemProfMD->operands()) {
auto *MIBMD = cast<const MDNode>(MDOp);
MDNode *StackNode = getMIBStackNode(MIBMD);
@@ -544,7 +545,8 @@ static void computeFunctionSummary(
}
// If we have context size information, collect it for inclusion in
// the summary.
- assert(MIBMD->getNumOperands() > 2 || !MemProfReportHintedSizes);
+ assert(MIBMD->getNumOperands() > 2 ||
+ !metadataIncludesAllContextSizeInfo());
if (MIBMD->getNumOperands() > 2) {
std::vector<ContextTotalSize> ContextSizes;
for (unsigned I = 2; I < MIBMD->getNumOperands(); I++) {
@@ -558,14 +560,31 @@ static void computeFunctionSummary(
->getZExtValue();
ContextSizes.push_back({FullStackId, TS});
}
+ // Flag that we need to keep the ContextSizeInfos array for this
+ // alloc as it now contains non-zero context info sizes.
+ HasNonZeroContextSizeInfos = true;
ContextSizeInfos.push_back(std::move(ContextSizes));
+ } else {
+ // The ContextSizeInfos must be in the same relative position as the
+ // associated MIB. In some cases we only include a ContextSizeInfo
+ // for a subset of MIBs in an allocation. To handle that, eagerly
+ // fill any MIB entries that don't have context size info metadata
+ // with a pair of 0s. Later on we will only use this array if it
+ // ends up containing any non-zero entries (see where we set
+ // HasNonZeroContextSizeInfos above).
+ ContextSizeInfos.push_back({{0, 0}});
}
MIBs.push_back(
MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));
}
Allocs.push_back(AllocInfo(std::move(MIBs)));
- assert(!ContextSizeInfos.empty() || !MemProfReportHintedSizes);
- if (!ContextSizeInfos.empty()) {
+ assert(HasNonZeroContextSizeInfos ||
+ !metadataIncludesAllContextSizeInfo());
+ // We eagerly build the ContextSizeInfos array, but it will be filled
+ // with sub arrays of pairs of 0s if no MIBs on this alloc actually
+ // contained context size info metadata. Only save it if any MIBs had
+ // any such metadata.
+ if (HasNonZeroContextSizeInfos) {
assert(Allocs.back().MIBs.size() == ContextSizeInfos.size());
Allocs.back().ContextSizeInfos = std::move(ContextSizeInfos);
}