Skip to content

[NFC][MemProf] Move IndexedMemProfData to its own header. #140503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

snehasish
Copy link
Contributor

@snehasish snehasish commented May 19, 2025

Part of a larger refactoring with the following goals

  1. Reduce the size of MemProf.h
  2. Avoid including ModuleSummaryIndex just for a couple of types

@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-transforms

Author: Snehasish Kumar (snehasish)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/140503.diff

11 Files Affected:

  • (modified) llvm/include/llvm/ProfileData/IndexedMemProfData.h (+68-2)
  • (modified) llvm/include/llvm/ProfileData/InstrProfWriter.h (+1-1)
  • (modified) llvm/include/llvm/ProfileData/MemProf.h (-51)
  • (modified) llvm/include/llvm/ProfileData/MemProfRadixTree.h (+1)
  • (modified) llvm/include/llvm/ProfileData/MemProfReader.h (+1)
  • (modified) llvm/lib/Bitcode/Writer/BitcodeWriter.cpp (+1)
  • (modified) llvm/lib/ProfileData/InstrProfWriter.cpp (-1)
  • (modified) llvm/lib/ProfileData/MemProf.cpp (-13)
  • (modified) llvm/unittests/ProfileData/InstrProfTest.cpp (+1)
  • (modified) llvm/unittests/ProfileData/MemProfTest.cpp (+1)
  • (modified) llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp (+1)
diff --git a/llvm/include/llvm/ProfileData/IndexedMemProfData.h b/llvm/include/llvm/ProfileData/IndexedMemProfData.h
index 3c6c329d1c49d..94a16227477cb 100644
--- a/llvm/include/llvm/ProfileData/IndexedMemProfData.h
+++ b/llvm/include/llvm/ProfileData/IndexedMemProfData.h
@@ -6,18 +6,84 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// MemProf data is serialized in writeMemProf provided in this header file.
+// This file implements IndexedMemProfData, a data structure to hold MemProf
+// in a space optimized format. It also provides utility methods for writing
+// MemProf data.
 //
 //===----------------------------------------------------------------------===//
 
+#ifndef LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
+#define LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
+
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/ProfileData/MemProf.h"
 
 namespace llvm {
+namespace memprof {
+struct IndexedMemProfData {
+  // A map to hold memprof data per function. The lower 64 bits obtained from
+  // the md5 hash of the function name is used to index into the map.
+  llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;
+
+  // A map to hold frame id to frame mappings. The mappings are used to
+  // convert IndexedMemProfRecord to MemProfRecords with frame information
+  // inline.
+  llvm::MapVector<FrameId, Frame> Frames;
+
+  // A map to hold call stack id to call stacks.
+  llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;
+
+  FrameId addFrame(const Frame &F) {
+    const FrameId Id = hashFrame(F);
+    Frames.try_emplace(Id, F);
+    return Id;
+  }
+
+  CallStackId addCallStack(ArrayRef<FrameId> CS) {
+    CallStackId CSId = hashCallStack(CS);
+    CallStacks.try_emplace(CSId, CS);
+    return CSId;
+  }
+
+  CallStackId addCallStack(SmallVector<FrameId> &&CS) {
+    CallStackId CSId = hashCallStack(CS);
+    CallStacks.try_emplace(CSId, std::move(CS));
+    return CSId;
+  }
+
+private:
+  // Return a hash value based on the contents of the frame. Here we use a
+  // cryptographic hash function to minimize the chance of hash collisions.  We
+  // do persist FrameIds as part of memprof formats up to Version 2, inclusive.
+  // However, the deserializer never calls this function; it uses FrameIds
+  // merely as keys to look up Frames proper.
+  FrameId hashFrame(const Frame &F) const {
+    llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
+        HashBuilder;
+    HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
+    llvm::BLAKE3Result<8> Hash = HashBuilder.final();
+    FrameId Id;
+    std::memcpy(&Id, Hash.data(), sizeof(Hash));
+    return Id;
+  }
+
+  // Compute a CallStackId for a given call stack.
+  CallStackId hashCallStack(ArrayRef<FrameId> CS) const {
+  llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
+      HashBuilder;
+  for (FrameId F : CS)
+    HashBuilder.add(F);
+  llvm::BLAKE3Result<8> Hash = HashBuilder.final();
+  CallStackId CSId;
+  std::memcpy(&CSId, Hash.data(), sizeof(Hash));
+  return CSId;
+}
+};
+} // namespace memprof
 
 // Write the MemProf data to OS.
 Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
                    memprof::IndexedVersion MemProfVersionRequested,
                    bool MemProfFullSchema);
-
 } // namespace llvm
+#endif
diff --git a/llvm/include/llvm/ProfileData/InstrProfWriter.h b/llvm/include/llvm/ProfileData/InstrProfWriter.h
index 67d85daa81623..16d2ef3fab3e3 100644
--- a/llvm/include/llvm/ProfileData/InstrProfWriter.h
+++ b/llvm/include/llvm/ProfileData/InstrProfWriter.h
@@ -20,7 +20,7 @@
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/Object/BuildID.h"
 #include "llvm/ProfileData/InstrProf.h"
-#include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/Support/Error.h"
 #include <cstdint>
 #include <memory>
diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h
index 215102c131fff..ce5cd5ee4856b 100644
--- a/llvm/include/llvm/ProfileData/MemProf.h
+++ b/llvm/include/llvm/ProfileData/MemProf.h
@@ -842,57 +842,6 @@ struct LineLocation {
 
 // A pair of a call site location and its corresponding callee GUID.
 using CallEdgeTy = std::pair<LineLocation, uint64_t>;
-
-struct IndexedMemProfData {
-  // A map to hold memprof data per function. The lower 64 bits obtained from
-  // the md5 hash of the function name is used to index into the map.
-  llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;
-
-  // A map to hold frame id to frame mappings. The mappings are used to
-  // convert IndexedMemProfRecord to MemProfRecords with frame information
-  // inline.
-  llvm::MapVector<FrameId, Frame> Frames;
-
-  // A map to hold call stack id to call stacks.
-  llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;
-
-  FrameId addFrame(const Frame &F) {
-    const FrameId Id = hashFrame(F);
-    Frames.try_emplace(Id, F);
-    return Id;
-  }
-
-  CallStackId addCallStack(ArrayRef<FrameId> CS) {
-    CallStackId CSId = hashCallStack(CS);
-    CallStacks.try_emplace(CSId, CS);
-    return CSId;
-  }
-
-  CallStackId addCallStack(SmallVector<FrameId> &&CS) {
-    CallStackId CSId = hashCallStack(CS);
-    CallStacks.try_emplace(CSId, std::move(CS));
-    return CSId;
-  }
-
-private:
-  // Return a hash value based on the contents of the frame. Here we use a
-  // cryptographic hash function to minimize the chance of hash collisions.  We
-  // do persist FrameIds as part of memprof formats up to Version 2, inclusive.
-  // However, the deserializer never calls this function; it uses FrameIds
-  // merely as keys to look up Frames proper.
-  FrameId hashFrame(const Frame &F) const {
-    llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
-        HashBuilder;
-    HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
-    llvm::BLAKE3Result<8> Hash = HashBuilder.final();
-    FrameId Id;
-    std::memcpy(&Id, Hash.data(), sizeof(Hash));
-    return Id;
-  }
-
-  // Compute a CallStackId for a given call stack.
-  CallStackId hashCallStack(ArrayRef<FrameId> CS) const;
-};
 } // namespace memprof
 } // namespace llvm
 
diff --git a/llvm/include/llvm/ProfileData/MemProfRadixTree.h b/llvm/include/llvm/ProfileData/MemProfRadixTree.h
index 9abf9f7a8774b..39f150c903d15 100644
--- a/llvm/include/llvm/ProfileData/MemProfRadixTree.h
+++ b/llvm/include/llvm/ProfileData/MemProfRadixTree.h
@@ -14,6 +14,7 @@
 #define LLVM_PROFILEDATA_MEMPROFRADIXTREE_H
 
 #include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 
 namespace llvm {
 namespace memprof {
diff --git a/llvm/include/llvm/ProfileData/MemProfReader.h b/llvm/include/llvm/ProfileData/MemProfReader.h
index 9aa55554fdf72..130493ec77c08 100644
--- a/llvm/include/llvm/ProfileData/MemProfReader.h
+++ b/llvm/include/llvm/ProfileData/MemProfReader.h
@@ -21,6 +21,7 @@
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/Object/Binary.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/ProfileData/InstrProfReader.h"
 #include "llvm/ProfileData/MemProfData.inc"
 #include "llvm/ProfileData/MemProfRadixTree.h"
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index f8748babb1625..427a161bcef08 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -62,6 +62,7 @@
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Object/IRSymtab.h"
 #include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/ProfileData/MemProfRadixTree.h"
 #include "llvm/Support/AtomicOrdering.h"
 #include "llvm/Support/Casting.h"
diff --git a/llvm/lib/ProfileData/InstrProfWriter.cpp b/llvm/lib/ProfileData/InstrProfWriter.cpp
index 9dc1a0d0b4678..b63a8d8c072f7 100644
--- a/llvm/lib/ProfileData/InstrProfWriter.cpp
+++ b/llvm/lib/ProfileData/InstrProfWriter.cpp
@@ -18,7 +18,6 @@
 #include "llvm/IR/ProfileSummary.h"
 #include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/ProfileData/InstrProf.h"
-#include "llvm/ProfileData/MemProf.h"
 #include "llvm/ProfileData/ProfileCommon.h"
 #include "llvm/Support/Compression.h"
 #include "llvm/Support/Endian.h"
diff --git a/llvm/lib/ProfileData/MemProf.cpp b/llvm/lib/ProfileData/MemProf.cpp
index 795e97bee38f5..9955e6a6b2641 100644
--- a/llvm/lib/ProfileData/MemProf.cpp
+++ b/llvm/lib/ProfileData/MemProf.cpp
@@ -3,10 +3,8 @@
 #include "llvm/IR/Function.h"
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/ProfileData/SampleProf.h"
-#include "llvm/Support/BLAKE3.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/EndianStream.h"
-#include "llvm/Support/HashBuilder.h"
 
 namespace llvm {
 namespace memprof {
@@ -384,16 +382,5 @@ Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
   Buffer = Ptr;
   return Result;
 }
-
-CallStackId IndexedMemProfData::hashCallStack(ArrayRef<FrameId> CS) const {
-  llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
-      HashBuilder;
-  for (FrameId F : CS)
-    HashBuilder.add(F);
-  llvm::BLAKE3Result<8> Hash = HashBuilder.final();
-  CallStackId CSId;
-  std::memcpy(&CSId, Hash.data(), sizeof(Hash));
-  return CSId;
-}
 } // namespace memprof
 } // namespace llvm
diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp
index 9f1caae296cca..6fc9b9074908b 100644
--- a/llvm/unittests/ProfileData/InstrProfTest.cpp
+++ b/llvm/unittests/ProfileData/InstrProfTest.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ProfileData/InstrProfReader.h"
 #include "llvm/ProfileData/InstrProfWriter.h"
 #include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/ProfileData/MemProfRadixTree.h"
 #include "llvm/ProfileData/MemProfData.inc"
 #include "llvm/Support/Compression.h"
diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp
index 2ae9cd96f0197..147f8813a0288 100644
--- a/llvm/unittests/ProfileData/MemProfTest.cpp
+++ b/llvm/unittests/ProfileData/MemProfTest.cpp
@@ -14,6 +14,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/ProfileData/MemProfData.inc"
 #include "llvm/ProfileData/MemProfReader.h"
 #include "llvm/ProfileData/MemProfRadixTree.h"
diff --git a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
index 95828356b490b..86b551b93d097 100644
--- a/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
+++ b/llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp
@@ -14,6 +14,7 @@
 #include "llvm/ProfileData/InstrProfReader.h"
 #include "llvm/ProfileData/InstrProfWriter.h"
 #include "llvm/ProfileData/MemProf.h"
+#include "llvm/ProfileData/IndexedMemProfData.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Testing/Support/Error.h"
 #include "llvm/Transforms/Instrumentation/MemProfiler.h"

Copy link

github-actions bot commented May 19, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@snehasish snehasish force-pushed the users/snehasish/05-16-_nfc_memprof_move_indexedmemprofdata_to_its_own_header branch from 61b636b to 02c867e Compare May 19, 2025 22:15
@snehasish snehasish force-pushed the users/snehasish/05-16-_nfc_memprof_move_getguid_out_of_indexedmemprofrecord branch from 83ff2ba to 6a83dcd Compare May 19, 2025 22:15
Copy link
Contributor Author

snehasish commented May 19, 2025

Merge activity

  • May 19, 7:09 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • May 19, 7:19 PM EDT: Graphite rebased this pull request as part of a merge.
  • May 19, 7:21 PM EDT: @snehasish merged this pull request with Graphite.

@snehasish snehasish force-pushed the users/snehasish/05-16-_nfc_memprof_move_getguid_out_of_indexedmemprofrecord branch from 6a83dcd to 6ec85a1 Compare May 19, 2025 23:16
Base automatically changed from users/snehasish/05-16-_nfc_memprof_move_getguid_out_of_indexedmemprofrecord to main May 19, 2025 23:18
@snehasish snehasish force-pushed the users/snehasish/05-16-_nfc_memprof_move_indexedmemprofdata_to_its_own_header branch from 02c867e to 171c89a Compare May 19, 2025 23:19
@snehasish snehasish merged commit 0528848 into main May 19, 2025
6 of 9 checks passed
@snehasish snehasish deleted the users/snehasish/05-16-_nfc_memprof_move_indexedmemprofdata_to_its_own_header branch May 19, 2025 23:21
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Part of a larger refactoring with the following goals
1. Reduce the size of MemProf.h 
2. Avoid including ModuleSummaryIndex just for a couple of types
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
Part of a larger refactoring with the following goals
1. Reduce the size of MemProf.h 
2. Avoid including ModuleSummaryIndex just for a couple of types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants