diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h --- a/llvm/include/llvm/ProfileData/SampleProfReader.h +++ b/llvm/include/llvm/ProfileData/SampleProfReader.h @@ -657,6 +657,13 @@ /// Read a string indirectly via the name table. ErrorOr readStringFromTable(); + /// Read a context indirectly via the CSNameTable. + ErrorOr readContextFromTable(); + + /// Read a context indirectly via the CSNameTable if the profile has context, + /// otherwise same as readStringFromTable. + ErrorOr readSampleContextFromTable(); + /// Points to the current location in the buffer. const uint8_t *Data = nullptr; @@ -678,7 +685,9 @@ /// The starting address of NameTable containing fixed length MD5. const uint8_t *MD5NameMemStart = nullptr; - virtual ErrorOr readSampleContextFromTable(); + /// CSNameTable is used to save full context vectors. It is the backing buffer + /// for SampleContextFrames. + std::vector CSNameTable; private: std::error_code readSummaryEntry(std::vector &Entries); @@ -746,8 +755,6 @@ const SecHdrTableEntry &Entry); // placeholder for subclasses to dispatch their own section readers. virtual std::error_code readCustomSection(const SecHdrTableEntry &Entry) = 0; - ErrorOr readSampleContextFromTable() override; - ErrorOr readContextFromTable(); std::unique_ptr ProfSymList; @@ -762,10 +769,6 @@ /// The set containing the functions to use when compiling a module. DenseSet FuncsToUse; - /// CSNameTable is used to save full context vectors. This serves as an - /// underlying immutable buffer for all clients. - std::unique_ptr> CSNameTable; - /// If SkipFlatProf is true, skip the sections with /// SecFlagFlat flag. bool SkipFlatProf = false; diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/llvm/lib/ProfileData/SampleProfReader.cpp @@ -546,11 +546,27 @@ return SR; } -ErrorOr SampleProfileReaderBinary::readSampleContextFromTable() { - auto FName(readStringFromTable()); - if (std::error_code EC = FName.getError()) +ErrorOr SampleProfileReaderBinary::readContextFromTable() { + auto ContextIdx = readNumber(); + if (std::error_code EC = ContextIdx.getError()) return EC; - return SampleContext(*FName); + if (*ContextIdx >= CSNameTable.size()) + return sampleprof_error::truncated_name_table; + return CSNameTable[*ContextIdx]; +} + +ErrorOr SampleProfileReaderBinary::readSampleContextFromTable() { + if (ProfileIsCS) { + auto FContext(readContextFromTable()); + if (std::error_code EC = FContext.getError()) + return EC; + return SampleContext(*FContext); + } else { + auto FName(readStringFromTable()); + if (std::error_code EC = FName.getError()) + return EC; + return SampleContext(*FName); + } } std::error_code @@ -671,31 +687,6 @@ return sampleprof_error::success; } -ErrorOr -SampleProfileReaderExtBinaryBase::readContextFromTable() { - auto ContextIdx = readNumber(); - if (std::error_code EC = ContextIdx.getError()) - return EC; - if (*ContextIdx >= CSNameTable->size()) - return sampleprof_error::truncated_name_table; - return (*CSNameTable)[*ContextIdx]; -} - -ErrorOr -SampleProfileReaderExtBinaryBase::readSampleContextFromTable() { - if (ProfileIsCS) { - auto FContext(readContextFromTable()); - if (std::error_code EC = FContext.getError()) - return EC; - return SampleContext(*FContext); - } else { - auto FName(readStringFromTable()); - if (std::error_code EC = FName.getError()) - return EC; - return SampleContext(*FName); - } -} - std::error_code SampleProfileReaderExtBinaryBase::readOneSection( const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) { Data = Start; @@ -1035,7 +1026,7 @@ SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5, bool FixedLengthMD5) { if (FixedLengthMD5) { - if (IsMD5) + if (!IsMD5) errs() << "If FixedLengthMD5 is true, UseMD5 has to be true"; auto Size = readNumber(); if (std::error_code EC = Size.getError()) @@ -1089,11 +1080,10 @@ if (std::error_code EC = Size.getError()) return EC; - std::vector *PNameVec = - new std::vector(); - PNameVec->reserve(*Size); + CSNameTable.clear(); + CSNameTable.reserve(*Size); for (size_t I = 0; I < *Size; ++I) { - PNameVec->emplace_back(SampleContextFrameVector()); + CSNameTable.emplace_back(SampleContextFrameVector()); auto ContextSize = readNumber(); if (std::error_code EC = ContextSize.getError()) return EC; @@ -1112,18 +1102,15 @@ if (std::error_code EC = Discriminator.getError()) return EC; - PNameVec->back().emplace_back( + CSNameTable.back().emplace_back( FName.get(), LineLocation(LineOffset.get(), Discriminator.get())); } } - // From this point the underlying object of CSNameTable should be immutable. - CSNameTable.reset(PNameVec); return sampleprof_error::success; } std::error_code - SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute, FunctionSamples *FProfile) { if (Data < End) {