[llvm] [memprof] Add MemProf version (PR #86414)

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 14:59:34 PDT 2024


================
@@ -1230,10 +1231,38 @@ Error IndexedInstrProfReader::readHeader() {
             Header->MemProfOffset);
 
     const unsigned char *Ptr = Start + MemProfOffset;
-    // The value returned from RecordTableGenerator.Emit.
-    const uint64_t RecordTableOffset =
+
+    // Read the first 64-bit word, which may be RecordTableOffset in
+    // memprof::MemProfVersion0 or the MemProf version number in
+    // memprof::MemProfVersion1.
+    const uint64_t FirstWord =
         support::endian::readNext<uint64_t, llvm::endianness::little,
                                   unaligned>(Ptr);
+
+    memprof::MemProfVersion Version = memprof::MemProfVersion0;
+    if (static_cast<memprof::MemProfVersion>(FirstWord) ==
+        memprof::MemProfVersion1) {
+      // Everything is good.  We can proceed to deserialize the rest.
+      Version = memprof::MemProfVersion1;
+    } else if (FirstWord >= 24) {
+      // This is a heuristic/hack to detect memprof::MemProfVersion0,
+      // which does not have a version field in the header.
+      // In memprof::MemProfVersion0, FirstWord should be RecordTableOffset,
+      // which should be at least 24 because of the MemProf header size.
+      Version = memprof::MemProfVersion0;
+    } else {
+      return make_error<InstrProfError>(
+          instrprof_error::unsupported_version,
+          formatv("MemProf version {} not supported; version 0 or 1 required",
----------------
snehasish wrote:

Can we frame this error in a way that we don't need to change the string everytime we update the version?

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


More information about the llvm-commits mailing list