[llvm] 6be2e89 - [MemProf] Emit richer optimization remarks for single-type allocations (#181089)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 12 12:55:59 PST 2026
Author: Teresa Johnson
Date: 2026-02-12T12:55:54-08:00
New Revision: 6be2e8902951dabfc15a0c6f0bb872742a959aa3
URL: https://github.com/llvm/llvm-project/commit/6be2e8902951dabfc15a0c6f0bb872742a959aa3
DIFF: https://github.com/llvm/llvm-project/commit/6be2e8902951dabfc15a0c6f0bb872742a959aa3.diff
LOG: [MemProf] Emit richer optimization remarks for single-type allocations (#181089)
Emit per-context optimization remarks, when we apply a hint
unambiguously to an allocation, including the full allocation context
hash and total profiled size. When no context size information is
available, we fall back to a single remark per allocation. While this is
somewhat redundant with the message emitted for
-memprof-report-hinted-sizes, the latter enables emitting additional
messages, and so it is left in for now.
To support this consistently, added a new internal option
-memprof-keep-context-size-info that ensures context size information
is tracked and available for these remarks even when hinted size
reporting isn't fully enabled. This also will be useful for providing
this additional information to the LTO link for remarks there (in a
subsequent PR).
Updated memprof.ll to verify both the detailed per-context remarks
(when size info is kept) and the original per-allocation remarks (when
it is not).
Added:
Modified:
llvm/lib/Analysis/MemoryProfileInfo.cpp
llvm/test/Transforms/PGOProfile/memprof.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/MemoryProfileInfo.cpp b/llvm/lib/Analysis/MemoryProfileInfo.cpp
index fb22a098c60fb..27d741f310914 100644
--- a/llvm/lib/Analysis/MemoryProfileInfo.cpp
+++ b/llvm/lib/Analysis/MemoryProfileInfo.cpp
@@ -54,6 +54,12 @@ cl::opt<unsigned> MinPercentMaxColdSize(
"memprof-min-percent-max-cold-size", cl::init(100), cl::Hidden,
cl::desc("Min percent of max cold bytes for critical cold context"));
+// Use this to keep the context size information in the memprof metadata for use
+// in remarks.
+cl::opt<bool> MemProfKeepContextSizeInfo(
+ "memprof-keep-context-size-info", cl::init(false), cl::Hidden,
+ cl::desc("Keep context size information in memprof metadata"));
+
LLVM_ABI cl::opt<bool> MemProfUseAmbiguousAttributes(
"memprof-ambiguous-attributes", cl::init(true), cl::Hidden,
cl::desc("Apply ambiguous memprof attribute to ambiguous allocations"));
@@ -61,7 +67,8 @@ LLVM_ABI cl::opt<bool> MemProfUseAmbiguousAttributes(
} // end namespace llvm
bool llvm::memprof::metadataIncludesAllContextSizeInfo() {
- return MemProfReportHintedSizes || MinClonedColdBytePercent < 100;
+ return MemProfReportHintedSizes || MemProfKeepContextSizeInfo ||
+ MinClonedColdBytePercent < 100;
}
bool llvm::memprof::metadataMayIncludeContextSizeInfo() {
@@ -518,28 +525,47 @@ void CallStackTrie::addSingleAllocTypeAttribute(CallBase *CI, AllocationType AT,
// to an unambiguous one.
removeAnyExistingAmbiguousAttribute(CI);
CI->addFnAttr(A);
- if (MemProfReportHintedSizes) {
- std::vector<ContextSizeTypePair> ContextInfo;
- collectContextInfo(Alloc, ContextInfo);
- for (const auto &[CSI, OrigAT] : ContextInfo) {
- const auto &[FullStackId, TotalSize] = CSI;
- // If the original alloc type is not the one being applied as the hint,
- // report that we ignored this context.
- if (AT != OrigAT) {
+
+ std::vector<ContextSizeTypePair> ContextInfo;
+ collectContextInfo(Alloc, ContextInfo);
+
+ // If we don't have context size info, just emit a single remark for this
+ // allocation.
+ if (ContextInfo.empty()) {
+ if (ORE)
+ ORE->emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CI)
+ << ore::NV("AllocationCall", CI) << " in function "
+ << ore::NV("Caller", CI->getFunction())
+ << " marked with memprof allocation attribute "
+ << ore::NV("Attribute", AllocTypeString));
+ return;
+ }
+
+ // Emit remarks or stderr reporting if requested.
+ for (const auto &[CSI, OrigAT] : ContextInfo) {
+ const auto &[FullStackId, TotalSize] = CSI;
+ // If the original alloc type is not the one being applied as the hint,
+ // then don't report that it was hinted. Optionally report that we ignored
+ // this context.
+ if (AT != OrigAT) {
+ if (MemProfReportHintedSizes)
emitIgnoredNonColdContextMessage("ignored", FullStackId, "", TotalSize);
- continue;
- }
+ continue;
+ }
+ if (MemProfReportHintedSizes)
errs() << "MemProf hinting: Total size for full allocation context hash "
<< FullStackId << " and " << Descriptor << " alloc type "
<< getAllocTypeAttributeString(AT) << ": " << TotalSize << "\n";
- }
+ if (ORE)
+ ORE->emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CI)
+ << ore::NV("AllocationCall", CI) << " in function "
+ << ore::NV("Caller", CI->getFunction())
+ << " marked with memprof allocation attribute "
+ << ore::NV("Attribute", AllocTypeString)
+ << " for full allocation context hash "
+ << ore::NV("FullStackId", FullStackId) << " with total size "
+ << ore::NV("TotalSize", TotalSize));
}
- if (ORE)
- ORE->emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CI)
- << ore::NV("AllocationCall", CI) << " in function "
- << ore::NV("Caller", CI->getFunction())
- << " marked with memprof allocation attribute "
- << ore::NV("Attribute", AllocTypeString));
}
// Build and attach the minimal necessary MIB metadata. If the alloc has a
diff --git a/llvm/test/Transforms/PGOProfile/memprof.ll b/llvm/test/Transforms/PGOProfile/memprof.ll
index a1f0f1d403c8f..037ed3b256fb7 100644
--- a/llvm/test/Transforms/PGOProfile/memprof.ll
+++ b/llvm/test/Transforms/PGOProfile/memprof.ll
@@ -77,14 +77,24 @@
;; Check that the total sizes are reported if requested. A message should be
;; emitted for the pruned context. Also check that remarks are emitted for the
;; allocations hinted without context sensitivity.
-; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-report-hinted-sizes -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=TOTALSIZESSINGLE,TOTALSIZES,TOTALSIZENOKEEPALL,REMARKSINGLE
+;; Per-context remarks with size info should be emitted when -memprof-report-hinted-sizes is enabled.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-report-hinted-sizes -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=TOTALSIZESSINGLE,TOTALSIZES,TOTALSIZENOKEEPALL,REMARKSINGLESIZE
+;; Per-context remarks with size info should be emitted when -memprof-keep-context-size-info is enabled.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-keep-context-size-info -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=REMARKSINGLESIZE
+;; Only per-allocation remarks should be emitted by default.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=REMARKSINGLEBASIC
;; Check that the total sizes are reported if requested, and prevent pruning
;; via -memprof-keep-all-not-cold-contexts.
; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-report-hinted-sizes -memprof-keep-all-not-cold-contexts 2>&1 | FileCheck %s --check-prefixes=TOTALSIZESSINGLE,TOTALSIZES,TOTALSIZESKEEPALL
-;; Check that we hint additional allocations with a threshold < 100%
-; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-report-hinted-sizes -memprof-matching-cold-threshold=60 2>&1 | FileCheck %s --check-prefixes=TOTALSIZESSINGLE,TOTALSIZESTHRESH60
+;; Check that we hint additional allocations with a threshold < 100%.
+;; Per-context remarks with size info should be emitted when -memprof-report-hinted-sizes is enabled.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-report-hinted-sizes -memprof-matching-cold-threshold=60 -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=TOTALSIZESSINGLE,TOTALSIZESTHRESH60,REMARKSINGLESIZE,REMARKDOMSIZE
+;; Per-context remarks with size info should be emitted when -memprof-keep-context-size-info is enabled.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-matching-cold-threshold=60 -memprof-keep-context-size-info -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=REMARKSINGLESIZE,REMARKDOMSIZE
+;; Only per-allocation remarks should be emitted by default.
+; RUN: opt < %s -passes='memprof-use<profile-filename=%t.memprofdata>' -pgo-warn-missing-function -S -memprof-matching-cold-threshold=60 -pass-remarks=memory-profile-info 2>&1 | FileCheck %s --check-prefixes=REMARKSINGLEBASIC,REMARKDOMBASIC
;; Make sure that the -memprof-cloning-cold-threshold flag is enough to cause
;; the size metadata to be generated for the LTO link.
@@ -399,15 +409,32 @@ for.end: ; preds = %for.cond
;; For non-context sensitive allocations that get attributes we emit a message
;; with the full allocation context hash, type, and size in bytes.
; TOTALSIZESTHRESH60: Total size for full allocation context hash 8525406123785421946 and dominant alloc type cold: 10
+;; Per-context remark with size info.
+; REMARKDOMSIZE: remark: memprof.cc:5:10: call in function _Z3foov marked with memprof allocation attribute cold for full allocation context hash 8525406123785421946 with total size 10
+;; Per-allocation remark without size info.
+; REMARKDOMBASIC: remark: memprof.cc:5:10: call in function _Z3foov marked with memprof allocation attribute cold
; TOTALSIZESTHRESH60: Total size for full allocation context hash 11714230664165068698 and dominant alloc type cold: 10
+;; Per-context remark with size info.
+; REMARKDOMSIZE: remark: memprof.cc:5:10: call in function _Z3foov marked with memprof allocation attribute cold for full allocation context hash 11714230664165068698 with total size 10
; TOTALSIZESTHRESH60: Total size for ignored non-cold full allocation context hash 5725971306423925017: 10
; TOTALSIZESTHRESH60: Total size for full allocation context hash 16342802530253093571 and dominant alloc type cold: 10
+;; Per-context remark with size info.
+; REMARKDOMSIZE: remark: memprof.cc:5:10: call in function _Z3foov marked with memprof allocation attribute cold for full allocation context hash 16342802530253093571 with total size 10
; TOTALSIZESTHRESH60: Total size for full allocation context hash 18254812774972004394 and dominant alloc type cold: 10
+;; Per-context remark with size info.
+; REMARKDOMSIZE: remark: memprof.cc:5:10: call in function _Z3foov marked with memprof allocation attribute cold for full allocation context hash 18254812774972004394 with total size 10
; TOTALSIZESTHRESH60: Total size for ignored non-cold full allocation context hash 1093248920606587996: 10
; TOTALSIZESSINGLE: Total size for full allocation context hash 6792096022461663180 and single alloc type notcold: 10
-; REMARKSINGLE: remark: memprof.cc:25:13: call in function main marked with memprof allocation attribute notcold
+;; Per-context remark with size info.
+; REMARKSINGLESIZE: remark: memprof.cc:25:13: call in function main marked with memprof allocation attribute notcold for full allocation context hash 6792096022461663180 with total size 10
+;; Per-allocation remark without size info.
+; REMARKSINGLEBASIC: remark: memprof.cc:25:13: call in function main marked with memprof allocation attribute notcold
; TOTALSIZESSINGLE: Total size for full allocation context hash 15737101490731057601 and single alloc type cold: 10
-; REMARKSINGLE: remark: memprof.cc:26:13: call in function main marked with memprof allocation attribute cold
+;; Per-context remark with size info.
+; REMARKSINGLESIZE: remark: memprof.cc:26:13: call in function main marked with memprof allocation attribute cold for full allocation context hash 15737101490731057601 with total size 10
+;; Per-allocation remark without size info.
+; REMARKSINGLEBASIC: remark: memprof.cc:26:13: call in function main marked with memprof allocation attribute cold
+
;; For context sensitive allocations the full context hash and size in bytes
;; are in separate metadata nodes included on the MIB metadata.
; TOTALSIZES: !"cold", ![[CONTEXT1:[0-9]+]]}
More information about the llvm-commits
mailing list