Skip to content

Commit 776476c

Browse files
Reapply "[MemProf] Use radix tree for alloc contexts in bitcode summaries" (#117395) (#117404)
This reverts commit fdb050a, and restores ccb4702, with a fix for build bot failures. Specifically, add ProfileData to the dependences of the BitWriter library, which was causing shared library builds of LLVM to fail. Reproduced the failure with a shared library build and confirmed this change fixes that build failure.
1 parent aa5dc53 commit 776476c

File tree

8 files changed

+244
-30
lines changed

8 files changed

+244
-30
lines changed

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,12 @@ enum GlobalValueSummarySymtabCodes {
307307
// [valueid, n x stackidindex]
308308
FS_PERMODULE_CALLSITE_INFO = 26,
309309
// Summary of per-module allocation memprof metadata.
310-
// [nummib, nummib x (alloc type, numstackids, numstackids x stackidindex),
310+
// [nummib, nummib x (alloc type, context radix tree index),
311311
// [nummib x (numcontext x total size)]?]
312312
FS_PERMODULE_ALLOC_INFO = 27,
313313
// Summary of combined index memprof callsite metadata.
314-
// [valueid, numstackindices, numver,
315-
// numstackindices x stackidindex, numver x version]
314+
// [valueid, context radix tree index, numver,
315+
// numver x version]
316316
FS_COMBINED_CALLSITE_INFO = 28,
317317
// Summary of combined index allocation memprof metadata.
318318
// [nummib, numver,
@@ -331,6 +331,10 @@ enum GlobalValueSummarySymtabCodes {
331331
// the entries must be in the exact same order as the corresponding sizes.
332332
// [nummib x (numcontext x full stack id)]
333333
FS_ALLOC_CONTEXT_IDS = 31,
334+
// Linearized radix tree of allocation contexts. See the description above the
335+
// CallStackRadixTreeBuilder class in ProfileData/MemProf.h for format.
336+
// [n x entry]
337+
FS_CONTEXT_RADIX_TREE_ARRAY = 32,
334338
};
335339

336340
enum MetadataCodes {

llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ GetCodeName(unsigned CodeID, unsigned BlockID,
329329
STRINGIFY_CODE(FS, COMBINED_ALLOC_INFO)
330330
STRINGIFY_CODE(FS, STACK_IDS)
331331
STRINGIFY_CODE(FS, ALLOC_CONTEXT_IDS)
332+
STRINGIFY_CODE(FS, CONTEXT_RADIX_TREE_ARRAY)
332333
}
333334
case bitc::METADATA_ATTACHMENT_ID:
334335
switch (CodeID) {

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,10 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
987987
/// ids from the lists in the callsite and alloc entries to the index.
988988
std::vector<uint64_t> StackIds;
989989

990+
/// Linearized radix tree of allocation contexts. See the description above
991+
/// the CallStackRadixTreeBuilder class in ProfileData/MemProf.h for format.
992+
std::vector<uint64_t> RadixArray;
993+
990994
public:
991995
ModuleSummaryIndexBitcodeReader(
992996
BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex,
@@ -1013,6 +1017,8 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
10131017
TypeIdCompatibleVtableInfo &TypeId);
10141018
std::vector<FunctionSummary::ParamAccess>
10151019
parseParamAccesses(ArrayRef<uint64_t> Record);
1020+
SmallVector<unsigned> parseAllocInfoContext(ArrayRef<uint64_t> Record,
1021+
unsigned &I);
10161022

10171023
template <bool AllowNullValueInfo = false>
10181024
std::pair<ValueInfo, GlobalValue::GUID>
@@ -7544,6 +7550,48 @@ void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord(
75447550
parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId);
75457551
}
75467552

7553+
SmallVector<unsigned> ModuleSummaryIndexBitcodeReader::parseAllocInfoContext(
7554+
ArrayRef<uint64_t> Record, unsigned &I) {
7555+
SmallVector<unsigned> StackIdList;
7556+
// For backwards compatibility with old format before radix tree was
7557+
// used, simply see if we found a radix tree array record (and thus if
7558+
// the RadixArray is non-empty).
7559+
if (RadixArray.empty()) {
7560+
unsigned NumStackEntries = Record[I++];
7561+
assert(Record.size() - I >= NumStackEntries);
7562+
StackIdList.reserve(NumStackEntries);
7563+
for (unsigned J = 0; J < NumStackEntries; J++) {
7564+
assert(Record[I] < StackIds.size());
7565+
StackIdList.push_back(
7566+
TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]]));
7567+
}
7568+
} else {
7569+
unsigned RadixIndex = Record[I++];
7570+
// See the comments above CallStackRadixTreeBuilder in ProfileData/MemProf.h
7571+
// for a detailed description of the radix tree array format. Briefly, the
7572+
// first entry will be the number of frames, any negative values are the
7573+
// negative of the offset of the next frame, and otherwise the frames are in
7574+
// increasing linear order.
7575+
assert(RadixIndex < RadixArray.size());
7576+
unsigned NumStackIds = RadixArray[RadixIndex++];
7577+
StackIdList.reserve(NumStackIds);
7578+
while (NumStackIds--) {
7579+
assert(RadixIndex < RadixArray.size());
7580+
unsigned Elem = RadixArray[RadixIndex];
7581+
if (static_cast<std::make_signed_t<unsigned>>(Elem) < 0) {
7582+
RadixIndex = RadixIndex - Elem;
7583+
assert(RadixIndex < RadixArray.size());
7584+
Elem = RadixArray[RadixIndex];
7585+
// We shouldn't encounter a second offset in a row.
7586+
assert(static_cast<std::make_signed_t<unsigned>>(Elem) >= 0);
7587+
}
7588+
RadixIndex++;
7589+
StackIdList.push_back(TheIndex.addOrGetStackIdIndex(StackIds[Elem]));
7590+
}
7591+
}
7592+
return StackIdList;
7593+
}
7594+
75477595
static void setSpecialRefs(SmallVectorImpl<ValueInfo> &Refs, unsigned ROCnt,
75487596
unsigned WOCnt) {
75497597
// Readonly and writeonly refs are in the end of the refs list.
@@ -8010,6 +8058,11 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
80108058
break;
80118059
}
80128060

8061+
case bitc::FS_CONTEXT_RADIX_TREE_ARRAY: { // [n x entry]
8062+
RadixArray = ArrayRef<uint64_t>(Record);
8063+
break;
8064+
}
8065+
80138066
case bitc::FS_PERMODULE_CALLSITE_INFO: {
80148067
unsigned ValueID = Record[0];
80158068
SmallVector<unsigned> StackIdList;
@@ -8065,14 +8118,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
80658118
(Version < 10 && I < Record.size())) {
80668119
assert(Record.size() - I >= 2);
80678120
AllocationType AllocType = (AllocationType)Record[I++];
8068-
unsigned NumStackEntries = Record[I++];
8069-
assert(Record.size() - I >= NumStackEntries);
8070-
SmallVector<unsigned> StackIdList;
8071-
for (unsigned J = 0; J < NumStackEntries; J++) {
8072-
assert(Record[I] < StackIds.size());
8073-
StackIdList.push_back(
8074-
TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]]));
8075-
}
8121+
auto StackIdList = parseAllocInfoContext(Record, I);
80768122
MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList)));
80778123
}
80788124
// We either have nothing left or at least NumMIBs context size info
@@ -8123,14 +8169,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
81238169
while (MIBsRead++ < NumMIBs) {
81248170
assert(Record.size() - I >= 2);
81258171
AllocationType AllocType = (AllocationType)Record[I++];
8126-
unsigned NumStackEntries = Record[I++];
8127-
assert(Record.size() - I >= NumStackEntries);
8128-
SmallVector<unsigned> StackIdList;
8129-
for (unsigned J = 0; J < NumStackEntries; J++) {
8130-
assert(Record[I] < StackIds.size());
8131-
StackIdList.push_back(
8132-
TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]]));
8133-
}
8172+
auto StackIdList = parseAllocInfoContext(Record, I);
81348173
MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList)));
81358174
}
81368175
assert(Record.size() - I >= NumVersions);

0 commit comments

Comments
 (0)