Skip to content

Commit bda0209

Browse files
[ProfileData] Add InstrProfWriter::writeBinaryIds (NFC) (llvm#118754)
The patch makes InstrProfWriter::writeImpl less monolithic by adding InstrProfWriter::writeBinaryIds to serialize binary IDs. This way, InstrProfWriter::writeImpl can simply call the new function instead of handling all the details within writeImpl.
1 parent fdb90ce commit bda0209

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

llvm/include/llvm/ProfileData/InstrProfWriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class InstrProfWriter {
237237
uint64_t writeHeader(const IndexedInstrProf::Header &header,
238238
const bool WritePrevVersion, ProfOStream &OS);
239239

240+
// Writes binary IDs.
241+
Error writeBinaryIds(ProfOStream &OS);
242+
240243
// Writes compressed vtable names to profiles.
241244
Error writeVTableNames(ProfOStream &OS);
242245
};

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,45 @@ uint64_t InstrProfWriter::writeHeader(const IndexedInstrProf::Header &Header,
810810
return BackPatchStartOffset;
811811
}
812812

813+
Error InstrProfWriter::writeBinaryIds(ProfOStream &OS) {
814+
// BinaryIdSection has two parts:
815+
// 1. uint64_t BinaryIdsSectionSize
816+
// 2. list of binary ids that consist of:
817+
// a. uint64_t BinaryIdLength
818+
// b. uint8_t BinaryIdData
819+
// c. uint8_t Padding (if necessary)
820+
// Calculate size of binary section.
821+
uint64_t BinaryIdsSectionSize = 0;
822+
823+
// Remove duplicate binary ids.
824+
llvm::sort(BinaryIds);
825+
BinaryIds.erase(llvm::unique(BinaryIds), BinaryIds.end());
826+
827+
for (const auto &BI : BinaryIds) {
828+
// Increment by binary id length data type size.
829+
BinaryIdsSectionSize += sizeof(uint64_t);
830+
// Increment by binary id data length, aligned to 8 bytes.
831+
BinaryIdsSectionSize += alignToPowerOf2(BI.size(), sizeof(uint64_t));
832+
}
833+
// Write binary ids section size.
834+
OS.write(BinaryIdsSectionSize);
835+
836+
for (const auto &BI : BinaryIds) {
837+
uint64_t BILen = BI.size();
838+
// Write binary id length.
839+
OS.write(BILen);
840+
// Write binary id data.
841+
for (unsigned K = 0; K < BILen; K++)
842+
OS.writeByte(BI[K]);
843+
// Write padding if necessary.
844+
uint64_t PaddingSize = alignToPowerOf2(BILen, sizeof(uint64_t)) - BILen;
845+
for (unsigned K = 0; K < PaddingSize; K++)
846+
OS.writeByte(0);
847+
}
848+
849+
return Error::success();
850+
}
851+
813852
Error InstrProfWriter::writeVTableNames(ProfOStream &OS) {
814853
std::vector<std::string> VTableNameStrs;
815854
for (StringRef VTableName : VTableNames.keys())
@@ -920,41 +959,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
920959
return E;
921960
}
922961

923-
// BinaryIdSection has two parts:
924-
// 1. uint64_t BinaryIdsSectionSize
925-
// 2. list of binary ids that consist of:
926-
// a. uint64_t BinaryIdLength
927-
// b. uint8_t BinaryIdData
928-
// c. uint8_t Padding (if necessary)
929962
uint64_t BinaryIdSectionStart = OS.tell();
930-
// Calculate size of binary section.
931-
uint64_t BinaryIdsSectionSize = 0;
932-
933-
// Remove duplicate binary ids.
934-
llvm::sort(BinaryIds);
935-
BinaryIds.erase(llvm::unique(BinaryIds), BinaryIds.end());
936-
937-
for (const auto &BI : BinaryIds) {
938-
// Increment by binary id length data type size.
939-
BinaryIdsSectionSize += sizeof(uint64_t);
940-
// Increment by binary id data length, aligned to 8 bytes.
941-
BinaryIdsSectionSize += alignToPowerOf2(BI.size(), sizeof(uint64_t));
942-
}
943-
// Write binary ids section size.
944-
OS.write(BinaryIdsSectionSize);
945-
946-
for (const auto &BI : BinaryIds) {
947-
uint64_t BILen = BI.size();
948-
// Write binary id length.
949-
OS.write(BILen);
950-
// Write binary id data.
951-
for (unsigned K = 0; K < BILen; K++)
952-
OS.writeByte(BI[K]);
953-
// Write padding if necessary.
954-
uint64_t PaddingSize = alignToPowerOf2(BILen, sizeof(uint64_t)) - BILen;
955-
for (unsigned K = 0; K < PaddingSize; K++)
956-
OS.writeByte(0);
957-
}
963+
if (auto E = writeBinaryIds(OS))
964+
return E;
958965

959966
uint64_t VTableNamesSectionStart = OS.tell();
960967

0 commit comments

Comments
 (0)