From 9513f2fdf2ad50f55726154a6b6a4aa463bc457f Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Fri, 15 Nov 2024 08:24:44 -0800 Subject: [MemProf] Print full context hash when reporting hinted bytes (#114465) Improve the information printed when -memprof-report-hinted-sizes is enabled. Now print the full context hash computed from the original profile, similar to what we do when reporting matching statistics. This will make it easier to correlate with the profile. Note that the full context hash must be computed at profile match time and saved in the metadata and summary, because we may trim the context during matching when it isn't needed for distinguishing hotness. Similarly, due to the context trimming, we may have more than one full context id and total size pair per MIB in the metadata and summary, which now get a list of these pairs. Remove the old aggregate size from the metadata and summary support. One other change from the prior support is that we no longer write the size information into the combined index for the LTO backends, which don't use this information, which reduces unnecessary bloat in distributed index files. --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 67 ++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp') diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 91b1917..9ca76b5 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -7604,6 +7604,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::vector PendingCallsites; std::vector PendingAllocs; + std::vector PendingContextIds; while (true) { Expected MaybeEntry = Stream.advanceSkippingSubblocks(); @@ -8034,6 +8035,16 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { break; } + case bitc::FS_ALLOC_CONTEXT_IDS: { + // This is an array of 32-bit fixed-width values, holding each 64-bit + // context id as a pair of adjacent (most significant first) 32-bit words. + assert(Record.size() % 2 == 0); + PendingContextIds.reserve(Record.size() / 2); + for (auto R = Record.begin(); R != Record.end(); R += 2) + PendingContextIds.push_back(*R << 32 | *(R + 1)); + break; + } + case bitc::FS_PERMODULE_ALLOC_INFO: { unsigned I = 0; std::vector MIBs; @@ -8055,18 +8066,41 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { } MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList))); } - std::vector TotalSizes; - // We either have no sizes or NumMIBs of them. - assert(I == Record.size() || Record.size() - I == NumMIBs); + // We either have nothing left or at least NumMIBs context size info + // indices left (for the total sizes included when reporting of hinted + // bytes is enabled). + assert(I == Record.size() || Record.size() - I >= NumMIBs); + std::vector> AllContextSizes; if (I < Record.size()) { + assert(!PendingContextIds.empty() && + "Missing context ids for alloc sizes"); + unsigned ContextIdIndex = 0; MIBsRead = 0; - while (MIBsRead++ < NumMIBs) - TotalSizes.push_back(Record[I++]); + // The sizes are a linearized array of sizes, where for each MIB there + // is 1 or more sizes (due to context trimming, each MIB in the metadata + // and summarized here can correspond to more than one original context + // from the profile). + while (MIBsRead++ < NumMIBs) { + // First read the number of contexts recorded for this MIB. + unsigned NumContextSizeInfoEntries = Record[I++]; + assert(Record.size() - I >= NumContextSizeInfoEntries); + std::vector ContextSizes; + ContextSizes.reserve(NumContextSizeInfoEntries); + for (unsigned J = 0; J < NumContextSizeInfoEntries; J++) { + assert(ContextIdIndex < PendingContextIds.size()); + // PendingContextIds read from the preceding FS_ALLOC_CONTEXT_IDS + // should be in the same order as the total sizes. + ContextSizes.push_back( + {PendingContextIds[ContextIdIndex++], Record[I++]}); + } + AllContextSizes.push_back(std::move(ContextSizes)); + } + PendingContextIds.clear(); } PendingAllocs.push_back(AllocInfo(std::move(MIBs))); - if (!TotalSizes.empty()) { - assert(PendingAllocs.back().MIBs.size() == TotalSizes.size()); - PendingAllocs.back().TotalSizes = std::move(TotalSizes); + if (!AllContextSizes.empty()) { + assert(PendingAllocs.back().MIBs.size() == AllContextSizes.size()); + PendingAllocs.back().ContextSizeInfos = std::move(AllContextSizes); } break; } @@ -8094,21 +8128,8 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { SmallVector Versions; for (unsigned J = 0; J < NumVersions; J++) Versions.push_back(Record[I++]); - std::vector TotalSizes; - // We either have no sizes or NumMIBs of them. - assert(I == Record.size() || Record.size() - I == NumMIBs); - if (I < Record.size()) { - MIBsRead = 0; - while (MIBsRead++ < NumMIBs) { - TotalSizes.push_back(Record[I++]); - } - } - PendingAllocs.push_back( - AllocInfo(std::move(Versions), std::move(MIBs))); - if (!TotalSizes.empty()) { - assert(PendingAllocs.back().MIBs.size() == TotalSizes.size()); - PendingAllocs.back().TotalSizes = std::move(TotalSizes); - } + assert(I == Record.size()); + PendingAllocs.push_back(AllocInfo(std::move(Versions), std::move(MIBs))); break; } } -- cgit v1.1