aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ProfileData/SampleProfReader.cpp
diff options
context:
space:
mode:
authorWei Mi <wmi@google.com>2020-03-03 13:19:32 -0800
committerWei Mi <wmi@google.com>2020-03-30 22:07:08 -0700
commitebad678857a94c32ce7b6931e9c642b32d278b67 (patch)
treef275e60b1d1eafc3b4f463eaec8cf0cb31cda986 /llvm/lib/ProfileData/SampleProfReader.cpp
parent1e8900cc828bd62fdcc314283c991080bae4520c (diff)
downloadllvm-ebad678857a94c32ce7b6931e9c642b32d278b67.zip
llvm-ebad678857a94c32ce7b6931e9c642b32d278b67.tar.gz
llvm-ebad678857a94c32ce7b6931e9c642b32d278b67.tar.bz2
[SampleFDO] Port MD5 name table support to extbinary format.
Compbinary format uses MD5 to represent strings in name table. That gives smaller profile without the need of compression/decompression when writing/reading the profile. The patch adds the support in extbinary format. It is off by default but user can choose to enable it. Note the feature of using MD5 in name table can bring very small chance of name conflict leading to profile mismatch. Besides, profile using the feature won't have the profile remapping support. Differential Revision: https://reviews.llvm.org/D76255
Diffstat (limited to 'llvm/lib/ProfileData/SampleProfReader.cpp')
-rw-r--r--llvm/lib/ProfileData/SampleProfReader.cpp74
1 files changed, 56 insertions, 18 deletions
diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp
index 6f74cec..33efb51 100644
--- a/llvm/lib/ProfileData/SampleProfReader.cpp
+++ b/llvm/lib/ProfileData/SampleProfReader.cpp
@@ -470,18 +470,18 @@ std::error_code SampleProfileReaderBinary::readImpl() {
return sampleprof_error::success;
}
-std::error_code
-SampleProfileReaderExtBinary::readOneSection(const uint8_t *Start,
- uint64_t Size, SecType Type) {
+std::error_code SampleProfileReaderExtBinary::readOneSection(
+ const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
Data = Start;
End = Start + Size;
- switch (Type) {
+ switch (Entry.Type) {
case SecProfSummary:
if (std::error_code EC = readSummary())
return EC;
break;
case SecNameTable:
- if (std::error_code EC = readNameTable())
+ if (std::error_code EC = readNameTableSec(
+ hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name)))
return EC;
break;
case SecLBRProfile:
@@ -546,15 +546,28 @@ std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
}
}
- for (auto NameOffset : FuncOffsetTable) {
- auto FuncName = NameOffset.first;
- if (!FuncsToUse.count(FuncName) &&
- (!Remapper || !Remapper->exist(FuncName)))
- continue;
- const uint8_t *FuncProfileAddr = Start + NameOffset.second;
- assert(FuncProfileAddr < End && "out of LBRProfile section");
- if (std::error_code EC = readFuncProfile(FuncProfileAddr))
- return EC;
+ if (useMD5()) {
+ for (auto Name : FuncsToUse) {
+ auto GUID = std::to_string(MD5Hash(Name));
+ auto iter = FuncOffsetTable.find(StringRef(GUID));
+ if (iter == FuncOffsetTable.end())
+ continue;
+ const uint8_t *FuncProfileAddr = Start + iter->second;
+ assert(FuncProfileAddr < End && "out of LBRProfile section");
+ if (std::error_code EC = readFuncProfile(FuncProfileAddr))
+ return EC;
+ }
+ } else {
+ for (auto NameOffset : FuncOffsetTable) {
+ auto FuncName = NameOffset.first;
+ if (!FuncsToUse.count(FuncName) &&
+ (!Remapper || !Remapper->exist(FuncName)))
+ continue;
+ const uint8_t *FuncProfileAddr = Start + NameOffset.second;
+ assert(FuncProfileAddr < End && "out of LBRProfile section");
+ if (std::error_code EC = readFuncProfile(FuncProfileAddr))
+ return EC;
+ }
}
Data = End;
@@ -617,7 +630,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
// DecompressBuf before reading the actual data. The pointee of
// 'Data' will be changed to buffer hold by DecompressBuf
// temporarily when reading the actual data.
- bool isCompressed = hasSecFlag(Entry, SecFlagCompress);
+ bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
if (isCompressed) {
const uint8_t *DecompressBuf;
uint64_t DecompressBufSize;
@@ -628,7 +641,7 @@ std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
SecSize = DecompressBufSize;
}
- if (std::error_code EC = readOneSection(SecStart, SecSize, Entry.Type))
+ if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
return EC;
if (Data != SecStart + SecSize)
return sampleprof_error::malformed;
@@ -705,6 +718,31 @@ std::error_code SampleProfileReaderBinary::readNameTable() {
return sampleprof_error::success;
}
+std::error_code SampleProfileReaderExtBinary::readMD5NameTable() {
+ auto Size = readNumber<uint64_t>();
+ if (std::error_code EC = Size.getError())
+ return EC;
+ NameTable.reserve(*Size);
+ MD5StringBuf = std::make_unique<std::vector<std::string>>();
+ MD5StringBuf->reserve(*Size);
+ for (uint32_t I = 0; I < *Size; ++I) {
+ auto FID = readNumber<uint64_t>();
+ if (std::error_code EC = FID.getError())
+ return EC;
+ MD5StringBuf->push_back(std::to_string(*FID));
+ // NameTable is a vector of StringRef. Here it is pushing back a
+ // StringRef initialized with the last string in MD5stringBuf.
+ NameTable.push_back(MD5StringBuf->back());
+ }
+ return sampleprof_error::success;
+}
+
+std::error_code SampleProfileReaderExtBinary::readNameTableSec(bool IsMD5) {
+ if (IsMD5)
+ return readMD5NameTable();
+ return SampleProfileReaderBinary::readNameTable();
+}
+
std::error_code SampleProfileReaderCompactBinary::readNameTable() {
auto Size = readNumber<uint64_t>();
if (std::error_code EC = Size.getError())
@@ -1210,9 +1248,9 @@ bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
}
void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
- // If the reader is in compact format, we can't remap it because
+ // If the reader uses MD5 to represent string, we can't remap it because
// we don't know what the original function names were.
- if (Reader.getFormat() == SPF_Compact_Binary) {
+ if (Reader.useMD5()) {
Ctx.diagnose(DiagnosticInfoSampleProfile(
Reader.getBuffer()->getBufferIdentifier(),
"Profile data remapping cannot be applied to profile data "