Skip to content

Commit

Permalink
[NFC][SampleFDO] Move some common stuff from SampleProfileReaderExtBi…
Browse files Browse the repository at this point in the history
…nary/WriterExtBinary

to their parent classes.

SampleProfileReaderExtBinary/SampleProfileWriterExtBinary specify the typical
section layout currently used by SampleFDO. Currently a lot of section
reader/writer stay in the two classes. However, as we expect to have more
types of SampleFDO profiles, we hope those new types of profiles can share
the common sections while configuring their own sections easily with minimal
change. That is why I move some common stuff from
SampleProfileReaderExtBinary/SampleProfileWriterExtBinary to
SampleProfileReaderExtBinaryBase/SampleProfileWriterExtBinaryBase so new
profiles class inheriting from the base class can reuse them.

Differential Revision: https://reviews.llvm.org/D89524
  • Loading branch information
wmi-11 committed Oct 22, 2020
1 parent c2730e6 commit 93953d4
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 108 deletions.
76 changes: 41 additions & 35 deletions llvm/include/llvm/ProfileData/SampleProfReader.h
Expand Up @@ -598,40 +598,23 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {

protected:
std::vector<SecHdrTableEntry> SecHdrTable;
std::unique_ptr<ProfileSymbolList> ProfSymList;
std::error_code readSecHdrTableEntry();
std::error_code readSecHdrTable();
virtual std::error_code readHeader() override;
virtual std::error_code verifySPMagic(uint64_t Magic) override = 0;
virtual std::error_code readOneSection(const uint8_t *Start, uint64_t Size,
const SecHdrTableEntry &Entry) = 0;

public:
SampleProfileReaderExtBinaryBase(std::unique_ptr<MemoryBuffer> B,
LLVMContext &C, SampleProfileFormat Format)
: SampleProfileReaderBinary(std::move(B), C, Format) {}

/// Read sample profiles in extensible format from the associated file.
std::error_code readImpl() override;

/// Get the total size of all \p Type sections.
uint64_t getSectionSize(SecType Type);
/// Get the total size of header and all sections.
uint64_t getFileSize();
virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) override;
};

class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {
private:
virtual std::error_code verifySPMagic(uint64_t Magic) override;
virtual std::error_code
readOneSection(const uint8_t *Start, uint64_t Size,
const SecHdrTableEntry &Entry) override;
std::error_code readProfileSymbolList();
std::error_code readFuncOffsetTable();
std::error_code readFuncProfiles();
std::error_code readMD5NameTable();
std::error_code readNameTableSec(bool IsMD5);
std::error_code readProfileSymbolList();

virtual std::error_code readHeader() override;
virtual std::error_code verifySPMagic(uint64_t Magic) override = 0;
virtual std::error_code readOneSection(const uint8_t *Start, uint64_t Size,
const SecHdrTableEntry &Entry);
// placeholder for subclasses to dispatch their own section readers.
virtual std::error_code readCustomSection(const SecHdrTableEntry &Entry) = 0;

std::unique_ptr<ProfileSymbolList> ProfSymList;

/// The table mapping from function name to the offset of its FunctionSample
/// towards file start.
Expand All @@ -651,16 +634,18 @@ class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {
std::unique_ptr<std::vector<std::string>> MD5StringBuf;

public:
SampleProfileReaderExtBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
SampleProfileFormat Format = SPF_Ext_Binary)
: SampleProfileReaderExtBinaryBase(std::move(B), C, Format) {}
SampleProfileReaderExtBinaryBase(std::unique_ptr<MemoryBuffer> B,
LLVMContext &C, SampleProfileFormat Format)
: SampleProfileReaderBinary(std::move(B), C, Format) {}

/// \brief Return true if \p Buffer is in the format supported by this class.
static bool hasFormat(const MemoryBuffer &Buffer);
/// Read sample profiles in extensible format from the associated file.
std::error_code readImpl() override;

virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() override {
return std::move(ProfSymList);
};
/// Get the total size of all \p Type sections.
uint64_t getSectionSize(SecType Type);
/// Get the total size of header and all sections.
uint64_t getFileSize();
virtual bool dumpSectionInfo(raw_ostream &OS = dbgs()) override;

/// Collect functions with definitions in Module \p M.
void collectFuncsFrom(const Module &M) override;
Expand All @@ -670,6 +655,27 @@ class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {
assert(!NameTable.empty() && "NameTable should have been initialized");
return MD5StringBuf && !MD5StringBuf->empty();
}

virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() override {
return std::move(ProfSymList);
};
};

class SampleProfileReaderExtBinary : public SampleProfileReaderExtBinaryBase {
private:
virtual std::error_code verifySPMagic(uint64_t Magic) override;
virtual std::error_code
readCustomSection(const SecHdrTableEntry &Entry) override {
return sampleprof_error::success;
};

public:
SampleProfileReaderExtBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
SampleProfileFormat Format = SPF_Ext_Binary)
: SampleProfileReaderExtBinaryBase(std::move(B), C, Format) {}

/// \brief Return true if \p Buffer is in the format supported by this class.
static bool hasFormat(const MemoryBuffer &Buffer);
};

class SampleProfileReaderCompactBinary : public SampleProfileReaderBinary {
Expand Down
83 changes: 51 additions & 32 deletions llvm/include/llvm/ProfileData/SampleProfWriter.h
Expand Up @@ -152,6 +152,24 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {

virtual void setToCompressAllSections() override;
void setToCompressSection(SecType Type);
virtual std::error_code writeSample(const FunctionSamples &S) override;

// Set to use MD5 to represent string in NameTable.
virtual void setUseMD5() override {
UseMD5 = true;
addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagMD5Name);
}

// Set the profile to be partial. It means the profile is for
// common/shared code. The common profile is usually merged from
// profiles collected from running other targets.
virtual void setPartialProfile() override {
addSectionFlag(SecProfSummary, SecProfSummaryFlags::SecFlagPartial);
}

virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
ProfSymList = PSL;
};

protected:
uint64_t markSectionStart(SecType Type);
Expand All @@ -164,16 +182,38 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
}
}

// placeholder for subclasses to dispatch their own section writers.
virtual std::error_code writeCustomSection(SecType Type) = 0;

virtual void initSectionHdrLayout() = 0;
// specify the order to write sections.
virtual std::error_code
writeSections(const StringMap<FunctionSamples> &ProfileMap) = 0;

// Dispatch section writer for each section.
virtual std::error_code
writeOneSection(SecType Type, const StringMap<FunctionSamples> &ProfileMap);

// Helper function to write name table.
virtual std::error_code writeNameTable() override;

// Functions to write various kinds of sections.
std::error_code
writeNameTableSection(const StringMap<FunctionSamples> &ProfileMap);
std::error_code writeFuncOffsetTable();
std::error_code writeProfileSymbolListSection();

// Specifiy the order of sections in section header table. Note
// the order of sections in the profile may be different that the
// order in SectionHdrLayout. sample Reader will follow the order
// in SectionHdrLayout to read each section.
SmallVector<SecHdrTableEntry, 8> SectionHdrLayout;

// Save the start of SecLBRProfile so we can compute the offset to the
// start of SecLBRProfile for each Function's Profile and will keep it
// in FuncOffsetTable.
uint64_t SecLBRProfileStart = 0;

private:
void allocSecHdrTable();
std::error_code writeSecHdrTable();
Expand All @@ -198,6 +238,14 @@ class SampleProfileWriterExtBinaryBase : public SampleProfileWriterBinary {
uint64_t SecHdrTableOffset;
// Initial Section Flags setting.
std::vector<SecHdrTableEntry> SecHdrTable;

// FuncOffsetTable maps function name to its profile offset in SecLBRProfile
// section. It is used to load function profile on demand.
MapVector<StringRef, uint64_t> FuncOffsetTable;
// Whether to use MD5 to represent string.
bool UseMD5 = false;

ProfileSymbolList *ProfSymList = nullptr;
};

class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
Expand All @@ -207,24 +255,6 @@ class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
initSectionHdrLayout();
}

virtual std::error_code writeSample(const FunctionSamples &S) override;
virtual void setProfileSymbolList(ProfileSymbolList *PSL) override {
ProfSymList = PSL;
};

// Set to use MD5 to represent string in NameTable.
virtual void setUseMD5() override {
UseMD5 = true;
addSectionFlag(SecNameTable, SecNameTableFlags::SecFlagMD5Name);
}

// Set the profile to be partial. It means the profile is for
// common/shared code. The common profile is usually merged from
// profiles collected from running other targets.
virtual void setPartialProfile() override {
addSectionFlag(SecProfSummary, SecProfSummaryFlags::SecFlagPartial);
}

private:
virtual void initSectionHdrLayout() override {
// Note that SecFuncOffsetTable section is written after SecLBRProfile
Expand All @@ -246,20 +276,9 @@ class SampleProfileWriterExtBinary : public SampleProfileWriterExtBinaryBase {
virtual std::error_code
writeSections(const StringMap<FunctionSamples> &ProfileMap) override;

std::error_code writeFuncOffsetTable();
virtual std::error_code writeNameTable() override;

ProfileSymbolList *ProfSymList = nullptr;

// Save the start of SecLBRProfile so we can compute the offset to the
// start of SecLBRProfile for each Function's Profile and will keep it
// in FuncOffsetTable.
uint64_t SecLBRProfileStart = 0;
// FuncOffsetTable maps function name to its profile offset in SecLBRProfile
// section. It is used to load function profile on demand.
MapVector<StringRef, uint64_t> FuncOffsetTable;
// Whether to use MD5 to represent string.
bool UseMD5 = false;
virtual std::error_code writeCustomSection(SecType Type) override {
return sampleprof_error::success;
};
};

// CompactBinary is a compact format of binary profile which both reduces
Expand Down
24 changes: 13 additions & 11 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Expand Up @@ -470,7 +470,7 @@ std::error_code SampleProfileReaderBinary::readImpl() {
return sampleprof_error::success;
}

std::error_code SampleProfileReaderExtBinary::readOneSection(
std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
Data = Start;
End = Start + Size;
Expand All @@ -490,28 +490,30 @@ std::error_code SampleProfileReaderExtBinary::readOneSection(
if (std::error_code EC = readFuncProfiles())
return EC;
break;
case SecProfileSymbolList:
if (std::error_code EC = readProfileSymbolList())
return EC;
break;
case SecFuncOffsetTable:
if (std::error_code EC = readFuncOffsetTable())
return EC;
break;
case SecProfileSymbolList:
if (std::error_code EC = readProfileSymbolList())
return EC;
break;
default:
if (std::error_code EC = readCustomSection(Entry))
return EC;
break;
}
return sampleprof_error::success;
}

void SampleProfileReaderExtBinary::collectFuncsFrom(const Module &M) {
void SampleProfileReaderExtBinaryBase::collectFuncsFrom(const Module &M) {
UseAllFuncs = false;
FuncsToUse.clear();
for (auto &F : M)
FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
}

std::error_code SampleProfileReaderExtBinary::readFuncOffsetTable() {
std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
auto Size = readNumber<uint64_t>();
if (std::error_code EC = Size.getError())
return EC;
Expand All @@ -531,7 +533,7 @@ std::error_code SampleProfileReaderExtBinary::readFuncOffsetTable() {
return sampleprof_error::success;
}

std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
const uint8_t *Start = Data;
if (UseAllFuncs) {
while (Data < End) {
Expand Down Expand Up @@ -576,7 +578,7 @@ std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
return sampleprof_error::success;
}

std::error_code SampleProfileReaderExtBinary::readProfileSymbolList() {
std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
if (!ProfSymList)
ProfSymList = std::make_unique<ProfileSymbolList>();

Expand Down Expand Up @@ -720,7 +722,7 @@ std::error_code SampleProfileReaderBinary::readNameTable() {
return sampleprof_error::success;
}

std::error_code SampleProfileReaderExtBinary::readMD5NameTable() {
std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
auto Size = readNumber<uint64_t>();
if (std::error_code EC = Size.getError())
return EC;
Expand All @@ -739,7 +741,7 @@ std::error_code SampleProfileReaderExtBinary::readMD5NameTable() {
return sampleprof_error::success;
}

std::error_code SampleProfileReaderExtBinary::readNameTableSec(bool IsMD5) {
std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
if (IsMD5)
return readMD5NameTable();
return SampleProfileReaderBinary::readNameTable();
Expand Down

0 comments on commit 93953d4

Please sign in to comment.