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 @@ -658,6 +658,16 @@ /// Read the whole name table with ULEB128 encoded MD5 values. std::error_code readMD5NameTable(); + /// 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; @@ -679,9 +689,9 @@ /// The starting address of NameTable containing fixed length MD5. const uint8_t *MD5NameMemStart = nullptr; - /// Read a string indirectly via the name table. - ErrorOr readStringFromTable(); - 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); @@ -749,8 +759,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; @@ -765,10 +773,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 @@ -535,19 +535,12 @@ StringRef &SR = NameTable[*Idx]; if (!SR.data()) { assert(MD5NameMemStart); - auto FID = reinterpret_cast(MD5NameMemStart)[*Idx]; + auto FID = reinterpret_cast(MD5NameMemStart)[*Idx]; SR = MD5StringBuf.emplace_back(std::to_string(FID)); } return SR; } -ErrorOr SampleProfileReaderBinary::readSampleContextFromTable() { - auto FName(readStringFromTable()); - if (std::error_code EC = FName.getError()) - return EC; - return SampleContext(*FName); -} - std::error_code SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { auto NumSamples = readNumber(); @@ -666,18 +659,16 @@ return sampleprof_error::success; } -ErrorOr -SampleProfileReaderExtBinaryBase::readContextFromTable() { +ErrorOr SampleProfileReaderBinary::readContextFromTable() { auto ContextIdx = readNumber(); if (std::error_code EC = ContextIdx.getError()) return EC; - if (*ContextIdx >= CSNameTable->size()) + if (*ContextIdx >= CSNameTable.size()) return sampleprof_error::truncated_name_table; - return (*CSNameTable)[*ContextIdx]; + return CSNameTable[*ContextIdx]; } -ErrorOr -SampleProfileReaderExtBinaryBase::readSampleContextFromTable() { +ErrorOr SampleProfileReaderBinary::readSampleContextFromTable() { if (ProfileIsCS) { auto FContext(readContextFromTable()); if (std::error_code EC = FContext.getError()) @@ -1124,11 +1115,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; @@ -1147,13 +1137,11 @@ 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; }