[llvm] [nfc][InstrFDO]Encapsulate header writes in a class member function (PR #90142)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 19:07:11 PDT 2024


================
@@ -552,6 +552,41 @@ static Error writeMemProf(
               memprof::MaximumSupportedVersion));
 }
 
+size_t InstrProfWriter::writeHeader(IndexedInstrProf::Header &Header,
+                                    const bool WritePrevVersion,
+                                    ProfOStream &OS,
+                                    size_t &BackPatchStartOffset) {
+  // Records the offset before writing any fields.
+  const uint64_t StartOffset = OS.tell();
+  // Only write out the first four fields. We need to remember the offset of the
+  // remaining fields to allow back patching later.
+  for (int I = 0; I < 4; I++)
+    OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
+
+  BackPatchStartOffset = OS.tell();
+
+  // Reserve the space for HashOffset field.
+  OS.write(0);
+
+  // Reserve space for the MemProf table field to be patched later if this
+  // profile contains memory profile information.
+  OS.write(0);
+
+  // Reserve space for the BinaryIdOffset field to be patched later if this
+  // profile contains binary ids.
+  OS.write(0);
+
+  // Reserve space for temporal profile traces for back patching later.
+  OS.write(0);
+
+  // Reserve space for vtable names for back patching later.
+  if (!WritePrevVersion)
+    OS.write(0);
+
+  // Returns the number of bytes written in the header section.
+  return OS.tell() - StartOffset;
----------------
kazutakahirata wrote:

May I suggest `return BackPatchStartOffset;` instead?

The caller can easily compute `OS.tell() - StartOffset` on its own.  Also, the expression is simple enough and self explanatory.

https://github.com/llvm/llvm-project/pull/90142


More information about the llvm-commits mailing list