[llvm] [nfc][InstrFDO]Encapsulate header writes in a class member function (PR #90142)
Mingming Liu via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 25 15:50:45 PDT 2024
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/90142
The smaller class member are more focused and easier to maintain. This also paves the way for partial header forward compatibility in https://github.com/llvm/llvm-project/pull/88212
>From 90109dbe22358645b9e67469f366c3462d15578d Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 25 Apr 2024 15:45:31 -0700
Subject: [PATCH 1/2] [nfc][InstrFDO]Encapsulate header writes in a class
member function
---
.../llvm/ProfileData/InstrProfWriter.h | 13 +++
llvm/lib/ProfileData/InstrProfWriter.cpp | 79 +++++++++++--------
2 files changed, 58 insertions(+), 34 deletions(-)
diff --git a/llvm/include/llvm/ProfileData/InstrProfWriter.h b/llvm/include/llvm/ProfileData/InstrProfWriter.h
index 4b42392bc1e061..adf47925798f46 100644
--- a/llvm/include/llvm/ProfileData/InstrProfWriter.h
+++ b/llvm/include/llvm/ProfileData/InstrProfWriter.h
@@ -197,6 +197,13 @@ class InstrProfWriter {
const OverlapFuncFilters &FuncFilter);
private:
+ struct HeaderFieldOffsets {
+ uint64_t HashTableStartFieldOffset;
+ uint64_t MemProfSectionOffset;
+ uint64_t BinaryIdSectionOffset;
+ uint64_t TemporalProfTracesOffset;
+ uint64_t VTableNamesOffset;
+ };
void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
uint64_t Weight, function_ref<void(Error)> Warn);
bool shouldEncodeData(const ProfilingData &PD);
@@ -204,6 +211,12 @@ class InstrProfWriter {
void addTemporalProfileTrace(TemporalProfTraceTy Trace);
Error writeImpl(ProfOStream &OS);
+
+ // Writes known header fields and preserves space for fields whose value are
+ // known only after payloads are written.
+ // Returns the number of bytes written and outputs the byte offset for back
+ // patching.
+ uint64_t writeHeader(ProfOStream &OS, HeaderFieldOffsets &Offsets);
};
} // end namespace llvm
diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index 7c56cde3e6cedd..c6971d2e152ed7 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -552,26 +552,10 @@ static Error writeMemProf(
memprof::MaximumSupportedVersion));
}
-Error InstrProfWriter::writeImpl(ProfOStream &OS) {
- using namespace IndexedInstrProf;
- using namespace support;
-
- OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
-
- InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
- InfoObj->SummaryBuilder = &ISB;
- InstrProfSummaryBuilder CSISB(ProfileSummaryBuilder::DefaultCutoffs);
- InfoObj->CSSummaryBuilder = &CSISB;
-
- // Populate the hash table generator.
- SmallVector<std::pair<StringRef, const ProfilingData *>, 0> OrderedData;
- for (const auto &I : FunctionData)
- if (shouldEncodeData(I.getValue()))
- OrderedData.emplace_back((I.getKey()), &I.getValue());
- llvm::sort(OrderedData, less_first());
- for (const auto &I : OrderedData)
- Generator.insert(I.first, I.second);
-
+uint64_t InstrProfWriter::writeHeader(ProfOStream &OS,
+ HeaderFieldOffsets &Offsets) {
+ // Records the offset before writing any fields.
+ const uint64_t StartOffset = OS.tell();
// Write the header.
IndexedInstrProf::Header Header;
Header.Magic = IndexedInstrProf::Magic;
@@ -612,30 +596,57 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
// Save the location of Header.HashOffset field in \c OS.
- uint64_t HashTableStartFieldOffset = OS.tell();
+ Offsets.HashTableStartFieldOffset = OS.tell();
// Reserve the space for HashOffset field.
OS.write(0);
// Save the location of MemProf profile data. This is stored in two parts as
// the schema and as a separate on-disk chained hashtable.
- uint64_t MemProfSectionOffset = OS.tell();
+ Offsets.MemProfSectionOffset = OS.tell();
// Reserve space for the MemProf table field to be patched later if this
// profile contains memory profile information.
OS.write(0);
// Save the location of binary ids section.
- uint64_t BinaryIdSectionOffset = OS.tell();
+ Offsets.BinaryIdSectionOffset = OS.tell();
// Reserve space for the BinaryIdOffset field to be patched later if this
// profile contains binary ids.
OS.write(0);
- uint64_t TemporalProfTracesOffset = OS.tell();
+ Offsets.TemporalProfTracesOffset = OS.tell();
OS.write(0);
- uint64_t VTableNamesOffset = OS.tell();
+ Offsets.VTableNamesOffset = OS.tell();
if (!WritePrevVersion)
OS.write(0);
+ return OS.tell() - StartOffset;
+}
+
+Error InstrProfWriter::writeImpl(ProfOStream &OS) {
+ using namespace IndexedInstrProf;
+ using namespace support;
+
+ OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
+
+ InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
+ InfoObj->SummaryBuilder = &ISB;
+ InstrProfSummaryBuilder CSISB(ProfileSummaryBuilder::DefaultCutoffs);
+ InfoObj->CSSummaryBuilder = &CSISB;
+
+ // Populate the hash table generator.
+ SmallVector<std::pair<StringRef, const ProfilingData *>, 0> OrderedData;
+ for (const auto &I : FunctionData)
+ if (shouldEncodeData(I.getValue()))
+ OrderedData.emplace_back((I.getKey()), &I.getValue());
+ llvm::sort(OrderedData, less_first());
+ for (const auto &I : OrderedData)
+ Generator.insert(I.first, I.second);
+
+ HeaderFieldOffsets Offsets;
+
+ this->writeHeader(OS, Offsets);
+
// Reserve space to write profile summary data.
uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
@@ -767,16 +778,16 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
// Now do the final patch:
PatchItem PatchItems[] = {
// Patch the Header.HashOffset field.
- {HashTableStartFieldOffset, &HashTableStart, 1},
+ {Offsets.HashTableStartFieldOffset, &HashTableStart, 1},
// Patch the Header.MemProfOffset (=0 for profiles without MemProf
// data).
- {MemProfSectionOffset, &MemProfSectionStart, 1},
+ {Offsets.MemProfSectionOffset, &MemProfSectionStart, 1},
// Patch the Header.BinaryIdSectionOffset.
- {BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
+ {Offsets.BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
// Patch the Header.TemporalProfTracesOffset (=0 for profiles without
// traces).
- {TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},
- {VTableNamesOffset, &VTableNamesSectionStart, 1},
+ {Offsets.TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},
+ {Offsets.VTableNamesOffset, &VTableNamesSectionStart, 1},
// Patch the summary data.
{SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
(int)(SummarySize / sizeof(uint64_t))},
@@ -788,15 +799,15 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
// Now do the final patch:
PatchItem PatchItems[] = {
// Patch the Header.HashOffset field.
- {HashTableStartFieldOffset, &HashTableStart, 1},
+ {Offsets.HashTableStartFieldOffset, &HashTableStart, 1},
// Patch the Header.MemProfOffset (=0 for profiles without MemProf
// data).
- {MemProfSectionOffset, &MemProfSectionStart, 1},
+ {Offsets.MemProfSectionOffset, &MemProfSectionStart, 1},
// Patch the Header.BinaryIdSectionOffset.
- {BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
+ {Offsets.BinaryIdSectionOffset, &BinaryIdSectionStart, 1},
// Patch the Header.TemporalProfTracesOffset (=0 for profiles without
// traces).
- {TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},
+ {Offsets.TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1},
// Patch the summary data.
{SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
(int)(SummarySize / sizeof(uint64_t))},
>From 3c108ade5bbb7dfa236e2c63989992fcf40c654b Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Thu, 25 Apr 2024 15:47:32 -0700
Subject: [PATCH 2/2] 'void' unused return value. The value will be used in a
follow-up patch
---
llvm/lib/ProfileData/InstrProfWriter.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index c6971d2e152ed7..c531d37aaa8379 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -645,7 +645,7 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
HeaderFieldOffsets Offsets;
- this->writeHeader(OS, Offsets);
+ (void)this->writeHeader(OS, Offsets);
// Reserve space to write profile summary data.
uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
More information about the llvm-commits
mailing list