[compiler-rt] [llvm] [InstrFDO]Record the on-disk header size in indexed profile header (PR #88212)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 9 18:19:51 PDT 2024


================
@@ -1598,48 +1598,62 @@ uint64_t Header::formatVersion() const {
 Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
   using namespace support;
   static_assert(std::is_standard_layout_v<Header>,
-                "The header should be standard layout type since we use offset "
-                "of fields to read.");
-  Header H;
+                "Keep the header a standard-layout class for simplicity");
 
-  H.Magic = read(Buffer, offsetOf(&Header::Magic));
+  Header H;
+  H.Magic = read(Buffer, 0);
   // Check the magic number.
   uint64_t Magic =
       endian::byte_swap<uint64_t, llvm::endianness::little>(H.Magic);
   if (Magic != IndexedInstrProf::Magic)
     return make_error<InstrProfError>(instrprof_error::bad_magic);
 
   // Read the version.
-  H.Version = read(Buffer, offsetOf(&Header::Version));
-  if (GET_VERSION(H.formatVersion()) >
-      IndexedInstrProf::ProfVersion::CurrentVersion)
+  H.Version = read(Buffer, sizeof(uint64_t));
+  const uint64_t ProfileVersion = GET_VERSION(H.formatVersion());
+  if (ProfileVersion > IndexedInstrProf::ProfVersion::CurrentVersion)
     return make_error<InstrProfError>(instrprof_error::unsupported_version);
 
-  switch (GET_VERSION(H.formatVersion())) {
+  constexpr size_t kOnDiskSizeOffset = 9 * sizeof(uint64_t);
+  if (ProfileVersion >= ProfVersion::Version13)
+    H.Size = read(Buffer, kOnDiskSizeOffset);
+
+  size_t FieldByteOffset = H.size();
+
+  switch (ProfileVersion) {
     // When a new field is added in the header add a case statement here to
     // populate it.
     static_assert(
-        IndexedInstrProf::ProfVersion::CurrentVersion == Version12,
+        IndexedInstrProf::ProfVersion::CurrentVersion == Version13,
         "Please update the reading code below if a new field has been added, "
         "if not add a case statement to fall through to the latest version.");
+  case 13ull:
+    // Size field is already read.
+    FieldByteOffset -= sizeof(Header::Size);
+    [[fallthrough]];
----------------
minglotus-6 wrote:

Use 'sizeof(Header::<Field>` here and below for better readability.

Yet still, this implicitly depends on the fact header fields are written out as type `uint64_t` (both in-memory and on-disk) as [`OS::write`](https://github.com/llvm/llvm-project/blob/36e25772ddd049c8c742e55fbd2b3c9aaceb7060/llvm/lib/ProfileData/InstrProfWriter.cpp#L609-L637) used by indexed profile writer only writes `uint64_t` for integers.

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


More information about the llvm-commits mailing list