Skip to content

Commit 776bb27

Browse files
committed
[llvm-profdata] ProfileReader cleanup - preparation for MD5 refactoring - 2
Cleanup profile reader classes to prepare for complex refactoring as propsed in D147740, continuing D148868 This is patch 2/n. This patch refactors CSNameTable and related things The decision to move CSNameTable up to the base class is because a planned improvement (D147740) to use MD5 to lookup Functions/Context frames. In this case we want a unified data structure between contextless function or Context frames, so that it can be mapped by MD5 value. Since Context Frames can represent contextless functions, it is being used for MD5 lookup, therefore exposing it to the base class Reviewed By: snehasish, wenlei Differential Revision: https://reviews.llvm.org/D148872
1 parent 4e93f91 commit 776bb27

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

llvm/include/llvm/ProfileData/SampleProfReader.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,13 @@ class SampleProfileReaderBinary : public SampleProfileReader {
657657
/// Read a string indirectly via the name table.
658658
ErrorOr<StringRef> readStringFromTable();
659659

660+
/// Read a context indirectly via the CSNameTable.
661+
ErrorOr<SampleContextFrames> readContextFromTable();
662+
663+
/// Read a context indirectly via the CSNameTable if the profile has context,
664+
/// otherwise same as readStringFromTable.
665+
ErrorOr<SampleContext> readSampleContextFromTable();
666+
660667
/// Points to the current location in the buffer.
661668
const uint8_t *Data = nullptr;
662669

@@ -678,7 +685,9 @@ class SampleProfileReaderBinary : public SampleProfileReader {
678685
/// The starting address of NameTable containing fixed length MD5.
679686
const uint8_t *MD5NameMemStart = nullptr;
680687

681-
virtual ErrorOr<SampleContext> readSampleContextFromTable();
688+
/// CSNameTable is used to save full context vectors. It is the backing buffer
689+
/// for SampleContextFrames.
690+
std::vector<SampleContextFrameVector> CSNameTable;
682691

683692
private:
684693
std::error_code readSummaryEntry(std::vector<ProfileSummaryEntry> &Entries);
@@ -746,8 +755,6 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
746755
const SecHdrTableEntry &Entry);
747756
// placeholder for subclasses to dispatch their own section readers.
748757
virtual std::error_code readCustomSection(const SecHdrTableEntry &Entry) = 0;
749-
ErrorOr<SampleContext> readSampleContextFromTable() override;
750-
ErrorOr<SampleContextFrames> readContextFromTable();
751758

752759
std::unique_ptr<ProfileSymbolList> ProfSymList;
753760

@@ -762,10 +769,6 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
762769
/// The set containing the functions to use when compiling a module.
763770
DenseSet<StringRef> FuncsToUse;
764771

765-
/// CSNameTable is used to save full context vectors. This serves as an
766-
/// underlying immutable buffer for all clients.
767-
std::unique_ptr<const std::vector<SampleContextFrameVector>> CSNameTable;
768-
769772
/// If SkipFlatProf is true, skip the sections with
770773
/// SecFlagFlat flag.
771774
bool SkipFlatProf = false;

llvm/lib/ProfileData/SampleProfReader.cpp

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,27 @@ ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
546546
return SR;
547547
}
548548

549-
ErrorOr<SampleContext> SampleProfileReaderBinary::readSampleContextFromTable() {
550-
auto FName(readStringFromTable());
551-
if (std::error_code EC = FName.getError())
549+
ErrorOr<SampleContextFrames> SampleProfileReaderBinary::readContextFromTable() {
550+
auto ContextIdx = readNumber<size_t>();
551+
if (std::error_code EC = ContextIdx.getError())
552552
return EC;
553-
return SampleContext(*FName);
553+
if (*ContextIdx >= CSNameTable.size())
554+
return sampleprof_error::truncated_name_table;
555+
return CSNameTable[*ContextIdx];
556+
}
557+
558+
ErrorOr<SampleContext> SampleProfileReaderBinary::readSampleContextFromTable() {
559+
if (ProfileIsCS) {
560+
auto FContext(readContextFromTable());
561+
if (std::error_code EC = FContext.getError())
562+
return EC;
563+
return SampleContext(*FContext);
564+
} else {
565+
auto FName(readStringFromTable());
566+
if (std::error_code EC = FName.getError())
567+
return EC;
568+
return SampleContext(*FName);
569+
}
554570
}
555571

556572
std::error_code
@@ -671,31 +687,6 @@ std::error_code SampleProfileReaderBinary::readImpl() {
671687
return sampleprof_error::success;
672688
}
673689

674-
ErrorOr<SampleContextFrames>
675-
SampleProfileReaderExtBinaryBase::readContextFromTable() {
676-
auto ContextIdx = readNumber<size_t>();
677-
if (std::error_code EC = ContextIdx.getError())
678-
return EC;
679-
if (*ContextIdx >= CSNameTable->size())
680-
return sampleprof_error::truncated_name_table;
681-
return (*CSNameTable)[*ContextIdx];
682-
}
683-
684-
ErrorOr<SampleContext>
685-
SampleProfileReaderExtBinaryBase::readSampleContextFromTable() {
686-
if (ProfileIsCS) {
687-
auto FContext(readContextFromTable());
688-
if (std::error_code EC = FContext.getError())
689-
return EC;
690-
return SampleContext(*FContext);
691-
} else {
692-
auto FName(readStringFromTable());
693-
if (std::error_code EC = FName.getError())
694-
return EC;
695-
return SampleContext(*FName);
696-
}
697-
}
698-
699690
std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
700691
const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
701692
Data = Start;
@@ -1035,7 +1026,7 @@ std::error_code
10351026
SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5,
10361027
bool FixedLengthMD5) {
10371028
if (FixedLengthMD5) {
1038-
if (IsMD5)
1029+
if (!IsMD5)
10391030
errs() << "If FixedLengthMD5 is true, UseMD5 has to be true";
10401031
auto Size = readNumber<size_t>();
10411032
if (std::error_code EC = Size.getError())
@@ -1089,11 +1080,10 @@ std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
10891080
if (std::error_code EC = Size.getError())
10901081
return EC;
10911082

1092-
std::vector<SampleContextFrameVector> *PNameVec =
1093-
new std::vector<SampleContextFrameVector>();
1094-
PNameVec->reserve(*Size);
1083+
CSNameTable.clear();
1084+
CSNameTable.reserve(*Size);
10951085
for (size_t I = 0; I < *Size; ++I) {
1096-
PNameVec->emplace_back(SampleContextFrameVector());
1086+
CSNameTable.emplace_back(SampleContextFrameVector());
10971087
auto ContextSize = readNumber<uint32_t>();
10981088
if (std::error_code EC = ContextSize.getError())
10991089
return EC;
@@ -1112,18 +1102,15 @@ std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
11121102
if (std::error_code EC = Discriminator.getError())
11131103
return EC;
11141104

1115-
PNameVec->back().emplace_back(
1105+
CSNameTable.back().emplace_back(
11161106
FName.get(), LineLocation(LineOffset.get(), Discriminator.get()));
11171107
}
11181108
}
11191109

1120-
// From this point the underlying object of CSNameTable should be immutable.
1121-
CSNameTable.reset(PNameVec);
11221110
return sampleprof_error::success;
11231111
}
11241112

11251113
std::error_code
1126-
11271114
SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute,
11281115
FunctionSamples *FProfile) {
11291116
if (Data < End) {

0 commit comments

Comments
 (0)