[llvm] [memprof] Add a version field to MemProf (PR #85344)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 18:01:58 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/85344

This patch adds a version field to the MemProf section of the indexed
profile format.  It adds no backward compatibility for now because
MemProf and PGHO are under active development.


>From 58ecdd982531235ed03d8a23eba32640a9292df4 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 14 Mar 2024 12:59:49 -0700
Subject: [PATCH] [memprof] Add a version field to MemProf

This patch adds a version field to the MemProf section of the indexed
profile format.  It adds no backward compatibility for now because
MemProf and PGHO are under active development.
---
 llvm/include/llvm/ProfileData/InstrProf.h | 3 +++
 llvm/lib/ProfileData/InstrProfReader.cpp  | 9 +++++++++
 llvm/lib/ProfileData/InstrProfWriter.cpp  | 9 ++++++---
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index 25ec06a7392027..f1515aab04d4cd 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -1165,6 +1165,9 @@ inline std::unique_ptr<Summary> allocSummary(uint32_t TotalSize) {
                                       Summary(TotalSize));
 }
 
+// Version 1: First version
+constexpr uint64_t MemProfVersion = 1;
+
 } // end namespace IndexedInstrProf
 
 namespace RawInstrProf {
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index 31b742bca14d6f..66a971c1cd21a9 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -1230,6 +1230,15 @@ Error IndexedInstrProfReader::readHeader() {
             Header->MemProfOffset);
 
     const unsigned char *Ptr = Start + MemProfOffset;
+
+    const uint64_t MemProfVersion =
+        support::endian::readNext<uint64_t, llvm::endianness::little,
+                                  unaligned>(Ptr);
+    if (MemProfVersion != IndexedInstrProf::MemProfVersion) {
+      return make_error<InstrProfError>(instrprof_error::unsupported_version,
+                                        "unsupported MemProf version");
+    }
+
     // The value returned from RecordTableGenerator.Emit.
     const uint64_t RecordTableOffset =
         support::endian::readNext<uint64_t, llvm::endianness::little,
diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index d9fe88a00bdfc4..61c14231f395c0 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -529,6 +529,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
   uint64_t MemProfSectionStart = 0;
   if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf)) {
     MemProfSectionStart = OS.tell();
+
+    OS.write(IndexedInstrProf::MemProfVersion);
+
     OS.write(0ULL); // Reserve space for the memprof record table offset.
     OS.write(0ULL); // Reserve space for the memprof frame payload offset.
     OS.write(0ULL); // Reserve space for the memprof frame table offset.
@@ -571,9 +574,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
     uint64_t FrameTableOffset = FrameTableGenerator.Emit(OS.OS, *FrameWriter);
 
     PatchItem PatchItems[] = {
-        {MemProfSectionStart, &RecordTableOffset, 1},
-        {MemProfSectionStart + sizeof(uint64_t), &FramePayloadOffset, 1},
-        {MemProfSectionStart + 2 * sizeof(uint64_t), &FrameTableOffset, 1},
+        {MemProfSectionStart + sizeof(uint64_t), &RecordTableOffset, 1},
+        {MemProfSectionStart + 2 * sizeof(uint64_t), &FramePayloadOffset, 1},
+        {MemProfSectionStart + 3 * sizeof(uint64_t), &FrameTableOffset, 1},
     };
     OS.patch(PatchItems);
   }



More information about the llvm-commits mailing list