Skip to content

Commit 7b977e0

Browse files
[nfc][InstrFDO]Encapsulate header writes in a class member function (#90142)
The smaller class member are more focused and easier to maintain. This also paves the way for partial header forward compatibility in #88212 --------- Co-authored-by: Kazu Hirata <[email protected]>
1 parent d34be64 commit 7b977e0

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

llvm/include/llvm/ProfileData/InstrProfWriter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ class InstrProfWriter {
212212
void addTemporalProfileTrace(TemporalProfTraceTy Trace);
213213

214214
Error writeImpl(ProfOStream &OS);
215+
216+
// Writes known header fields and reserves space for fields whose value are
217+
// known only after payloads are written. Returns the start byte offset for
218+
// back patching.
219+
uint64_t writeHeader(const IndexedInstrProf::Header &header,
220+
const bool WritePrevVersion, ProfOStream &OS);
215221
};
216222

217223
} // end namespace llvm

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,27 @@ static Error writeMemProf(ProfOStream &OS,
639639
memprof::MaximumSupportedVersion));
640640
}
641641

642+
uint64_t InstrProfWriter::writeHeader(const IndexedInstrProf::Header &Header,
643+
const bool WritePrevVersion,
644+
ProfOStream &OS) {
645+
// Only write out the first four fields.
646+
for (int I = 0; I < 4; I++)
647+
OS.write(reinterpret_cast<const uint64_t *>(&Header)[I]);
648+
649+
// Remember the offset of the remaining fields to allow back patching later.
650+
auto BackPatchStartOffset = OS.tell();
651+
652+
// Reserve the space for back patching later.
653+
OS.write(0); // HashOffset
654+
OS.write(0); // MemProfOffset
655+
OS.write(0); // BinaryIdOffset
656+
OS.write(0); // TemporalProfTracesOffset
657+
if (!WritePrevVersion)
658+
OS.write(0); // VTableNamesOffset
659+
660+
return BackPatchStartOffset;
661+
}
662+
642663
Error InstrProfWriter::writeImpl(ProfOStream &OS) {
643664
using namespace IndexedInstrProf;
644665
using namespace support;
@@ -651,7 +672,7 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
651672
InfoObj->CSSummaryBuilder = &CSISB;
652673

653674
// Populate the hash table generator.
654-
SmallVector<std::pair<StringRef, const ProfilingData *>, 0> OrderedData;
675+
SmallVector<std::pair<StringRef, const ProfilingData *>> OrderedData;
655676
for (const auto &I : FunctionData)
656677
if (shouldEncodeData(I.getValue()))
657678
OrderedData.emplace_back((I.getKey()), &I.getValue());
@@ -693,35 +714,8 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
693714
Header.TemporalProfTracesOffset = 0;
694715
Header.VTableNamesOffset = 0;
695716

696-
// Only write out the first four fields. We need to remember the offset of the
697-
// remaining fields to allow back patching later.
698-
for (int I = 0; I < 4; I++)
699-
OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
700-
701-
// Save the location of Header.HashOffset field in \c OS.
702-
uint64_t HashTableStartFieldOffset = OS.tell();
703-
// Reserve the space for HashOffset field.
704-
OS.write(0);
705-
706-
// Save the location of MemProf profile data. This is stored in two parts as
707-
// the schema and as a separate on-disk chained hashtable.
708-
uint64_t MemProfSectionOffset = OS.tell();
709-
// Reserve space for the MemProf table field to be patched later if this
710-
// profile contains memory profile information.
711-
OS.write(0);
712-
713-
// Save the location of binary ids section.
714-
uint64_t BinaryIdSectionOffset = OS.tell();
715-
// Reserve space for the BinaryIdOffset field to be patched later if this
716-
// profile contains binary ids.
717-
OS.write(0);
718-
719-
uint64_t TemporalProfTracesOffset = OS.tell();
720-
OS.write(0);
721-
722-
uint64_t VTableNamesOffset = OS.tell();
723-
if (!WritePrevVersion)
724-
OS.write(0);
717+
const uint64_t BackPatchStartOffset =
718+
writeHeader(Header, WritePrevVersion, OS);
725719

726720
// Reserve space to write profile summary data.
727721
uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
@@ -850,16 +844,20 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
850844
}
851845
InfoObj->CSSummaryBuilder = nullptr;
852846

847+
const size_t MemProfOffset = BackPatchStartOffset + sizeof(uint64_t);
848+
const size_t BinaryIdOffset = MemProfOffset + sizeof(uint64_t);
849+
const size_t TemporalProfTracesOffset = BinaryIdOffset + sizeof(uint64_t);
850+
const size_t VTableNamesOffset = TemporalProfTracesOffset + sizeof(uint64_t);
853851
if (!WritePrevVersion) {
854852
// Now do the final patch:
855853
PatchItem PatchItems[] = {
856854
// Patch the Header.HashOffset field.
857-
{HashTableStartFieldOffset, &HashTableStart, 1},
855+
{BackPatchStartOffset, &HashTableStart, 1},
858856
// Patch the Header.MemProfOffset (=0 for profiles without MemProf
859857
// data).
860-
{MemProfSectionOffset, &MemProfSectionStart, 1},
858+
{MemProfOffset, &MemProfSectionStart, 1},
861859
// Patch the Header.BinaryIdSectionOffset.
862-
{BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
860+
{BinaryIdOffset, &BinaryIdSectionStart, 1},
863861
// Patch the Header.TemporalProfTracesOffset (=0 for profiles without
864862
// traces).
865863
{TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},
@@ -875,12 +873,12 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
875873
// Now do the final patch:
876874
PatchItem PatchItems[] = {
877875
// Patch the Header.HashOffset field.
878-
{HashTableStartFieldOffset, &HashTableStart, 1},
876+
{BackPatchStartOffset, &HashTableStart, 1},
879877
// Patch the Header.MemProfOffset (=0 for profiles without MemProf
880878
// data).
881-
{MemProfSectionOffset, &MemProfSectionStart, 1},
879+
{MemProfOffset, &MemProfSectionStart, 1},
882880
// Patch the Header.BinaryIdSectionOffset.
883-
{BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
881+
{BinaryIdOffset, &BinaryIdSectionStart, 1},
884882
// Patch the Header.TemporalProfTracesOffset (=0 for profiles without
885883
// traces).
886884
{TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},

0 commit comments

Comments
 (0)