diff options
author | Teresa Johnson <tejohnson@google.com> | 2025-06-03 14:20:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-03 14:20:38 -0700 |
commit | f2adae57808996313dfec41a180454857ebd60d1 (patch) | |
tree | 244ccea90a8c99dbf53ffcd13498aa8dfd418d3f /llvm/lib/Bitcode/Writer | |
parent | f5a2f00da9b741f4f2fe925a434f608aa217cee2 (diff) | |
download | llvm-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/Bitcode/Writer')
-rw-r--r-- | llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 8789b31..fad8ebf 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Analysis/MemoryProfileInfo.h" #include "llvm/BinaryFormat/Dwarf.h" #include "llvm/Bitcode/BitcodeCommon.h" #include "llvm/Bitcode/BitcodeReader.h" @@ -4585,14 +4586,23 @@ void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId); } - // n x context id - auto ContextIdAbbv = std::make_shared<BitCodeAbbrev>(); - ContextIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_ALLOC_CONTEXT_IDS)); - ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); - // The context ids are hashes that are close to 64 bits in size, so emitting - // as a pair of 32-bit fixed-width values is more efficient than a VBR. - ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); - unsigned ContextIdAbbvId = Stream.EmitAbbrev(std::move(ContextIdAbbv)); + unsigned ContextIdAbbvId = 0; + if (metadataMayIncludeContextSizeInfo()) { + // n x context id + auto ContextIdAbbv = std::make_shared<BitCodeAbbrev>(); + ContextIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_ALLOC_CONTEXT_IDS)); + ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + // The context ids are hashes that are close to 64 bits in size, so emitting + // as a pair of 32-bit fixed-width values is more efficient than a VBR if we + // are emitting them for all MIBs. Otherwise we use VBR to better compress 0 + // values that are expected to more frequently occur in an alloc's memprof + // summary. + if (metadataIncludesAllContextSizeInfo()) + ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); + else + ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); + ContextIdAbbvId = Stream.EmitAbbrev(std::move(ContextIdAbbv)); + } // Abbrev for FS_PERMODULE_PROFILE. Abbv = std::make_shared<BitCodeAbbrev>(); |