[llvm] [StaticDataLayout][llvm-profdata] Print data access profile summary in text profiles and sort records before printing (PR #172592)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 16 19:57:09 PST 2025


https://github.com/mingmingl-llvm created https://github.com/llvm/llvm-project/pull/172592

This change proposes to print data access profile summary in its text format and sort records before printing. This gives some aggregated information about the data access profiles.

>From c300435b6702d8289b28b5a2f63260b3f70e047f Mon Sep 17 00:00:00 2001
From: mingmingl <mingmingl at google.com>
Date: Tue, 16 Dec 2025 19:53:12 -0800
Subject: [PATCH] Print data access profile summary in text profiles

---
 llvm/lib/ProfileData/InstrProfReader.cpp      | 19 +++++++++++
 llvm/lib/ProfileData/MemProfSummary.cpp       |  2 +-
 llvm/test/Transforms/PGOProfile/memprof.ll    |  2 +-
 .../memprof_max_cold_threshold.test           |  2 +-
 .../tools/llvm-profdata/memprof-yaml.test     | 32 ++++++++++++-------
 llvm/tools/llvm-profdata/llvm-profdata.cpp    | 10 ++++++
 6 files changed, 53 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index d2ae4b5226ff6..8147ee8d0e816 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -1554,6 +1554,12 @@ memprof::AllMemProfData IndexedMemProfReader::getAllMemProfData() const {
   }
   // Populate the data access profiles for yaml output.
   if (DataAccessProfileData != nullptr) {
+    AllMemProfData.YamlifiedDataAccessProfiles.Records.reserve(
+        DataAccessProfileData->getRecords().size());
+    AllMemProfData.YamlifiedDataAccessProfiles.KnownColdSymbols.reserve(
+        DataAccessProfileData->getKnownColdSymbols().size());
+    AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes.reserve(
+        DataAccessProfileData->getKnownColdHashes().size());
     for (const auto &[SymHandleRef, RecordRef] :
          DataAccessProfileData->getRecords())
       AllMemProfData.YamlifiedDataAccessProfiles.Records.push_back(
@@ -1565,6 +1571,19 @@ memprof::AllMemProfData IndexedMemProfReader::getAllMemProfData() const {
     for (uint64_t Hash : DataAccessProfileData->getKnownColdHashes())
       AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes.push_back(
           Hash);
+    llvm::stable_sort(AllMemProfData.YamlifiedDataAccessProfiles.Records,
+                      [](const llvm::memprof::DataAccessProfRecord &lhs,
+                         const llvm::memprof::DataAccessProfRecord &rhs) {
+                        return lhs.AccessCount > rhs.AccessCount;
+                      });
+    llvm::stable_sort(
+        AllMemProfData.YamlifiedDataAccessProfiles.KnownColdSymbols,
+        [](const std::string &lhs, const std::string &rhs) {
+          return lhs < rhs;
+        });
+    llvm::stable_sort(
+        AllMemProfData.YamlifiedDataAccessProfiles.KnownColdStrHashes,
+        [](const uint64_t &lhs, const uint64_t &rhs) { return lhs < rhs; });
   }
   return AllMemProfData;
 }
diff --git a/llvm/lib/ProfileData/MemProfSummary.cpp b/llvm/lib/ProfileData/MemProfSummary.cpp
index f620b7a74b244..c72d0a9e5353a 100644
--- a/llvm/lib/ProfileData/MemProfSummary.cpp
+++ b/llvm/lib/ProfileData/MemProfSummary.cpp
@@ -18,7 +18,7 @@ using namespace llvm::memprof;
 void MemProfSummary::printSummaryYaml(raw_ostream &OS) const {
   // For now emit as YAML comments, since they aren't read on input.
   OS << "---\n";
-  OS << "# MemProfSummary:\n";
+  OS << "# MemProfHeapSummary:\n";
   OS << "#   Total contexts: " << NumContexts << "\n";
   OS << "#   Total cold contexts: " << NumColdContexts << "\n";
   OS << "#   Total hot contexts: " << NumHotContexts << "\n";
diff --git a/llvm/test/Transforms/PGOProfile/memprof.ll b/llvm/test/Transforms/PGOProfile/memprof.ll
index f6a89a8ceb86a..00175b2f3984c 100644
--- a/llvm/test/Transforms/PGOProfile/memprof.ll
+++ b/llvm/test/Transforms/PGOProfile/memprof.ll
@@ -23,7 +23,7 @@
 ;; Check that the summary can be shown (and is identical) for both the raw and indexed profiles.
 ; RUN: llvm-profdata show --memory %S/Inputs/memprof.memprofraw --profiled-binary %S/Inputs/memprof.exe | FileCheck %s --check-prefixes=SUMMARY
 ; RUN: llvm-profdata show --memory %t.memprofdata | FileCheck %s --check-prefixes=SUMMARY
-; SUMMARY: # MemProfSummary:
+; SUMMARY: # MemProfHeapSummary:
 ; SUMMARY: #   Total contexts: 8
 ; SUMMARY: #   Total cold contexts: 5
 ; SUMMARY: #   Total hot contexts: 0
diff --git a/llvm/test/Transforms/PGOProfile/memprof_max_cold_threshold.test b/llvm/test/Transforms/PGOProfile/memprof_max_cold_threshold.test
index 8f1cabf9a4e87..d1dcc86eac8e8 100644
--- a/llvm/test/Transforms/PGOProfile/memprof_max_cold_threshold.test
+++ b/llvm/test/Transforms/PGOProfile/memprof_max_cold_threshold.test
@@ -9,7 +9,7 @@
 ; RUN: llvm-profdata merge --memprof-version=4 %t/memprof_max_cold_threshold.yaml -o %t/memprof_max_cold_threshold.memprofdata
 
 ; RUN: llvm-profdata show --memory %t/memprof_max_cold_threshold.memprofdata | FileCheck %s --check-prefixes=SUMMARY
-; SUMMARY: # MemProfSummary:
+; SUMMARY: # MemProfHeapSummary:
 ; SUMMARY: #   Total contexts: 5
 ; SUMMARY: #   Total cold contexts: 4
 ; SUMMARY: #   Total hot contexts: 0
diff --git a/llvm/test/tools/llvm-profdata/memprof-yaml.test b/llvm/test/tools/llvm-profdata/memprof-yaml.test
index 6fbfbdb507f27..f6201816927f6 100644
--- a/llvm/test/tools/llvm-profdata/memprof-yaml.test
+++ b/llvm/test/tools/llvm-profdata/memprof-yaml.test
@@ -37,13 +37,18 @@
 
 ;--- memprof-in.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 2
 #   Total cold contexts: 0
 #   Total hot contexts: 0
 #   Maximum cold context total size: 0
 #   Maximum warm context total size: 666
 #   Maximum hot context total size: 0
+#
+# DataAccessProfileSummary:
+#   Num hot symbols and string literals: 2
+#   Num known cold symbols: 2
+#   Num known cold string literals: 2
 ---
 HeapProfileRecords:
   - GUID:            0xdeadbeef12345678
@@ -76,25 +81,25 @@ HeapProfileRecords:
 DataAccessProfiles:
   SampledRecords:
     - Symbol:          abcde
-      AccessCount:     100
+      AccessCount:     202
       Locations:
       - FileName:      file2.h
         Line:          123
       - FileName:      file3.cpp
-        Line:          456
+        Line:          456 
     - Hash:            101010
       AccessCount:     200
       Locations:
         - FileName:        file.cpp
           Line:            233
   KnownColdSymbols:
-    - foo
     - bar
+    - foo
   KnownColdStrHashes: [ 999, 1001 ]
 ...
 ;--- memprof-in-v3.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 2
 #   Total cold contexts: 0
 #   Total hot contexts: 0
@@ -131,7 +136,7 @@ HeapProfileRecords:
 ...
 ;--- memprof-in-alloc-sites-only.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 2
 #   Total cold contexts: 0
 #   Total hot contexts: 0
@@ -161,7 +166,7 @@ HeapProfileRecords:
 ...
 ;--- memprof-in-call-sites-only.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 0
 #   Total cold contexts: 0
 #   Total hot contexts: 0
@@ -183,7 +188,7 @@ HeapProfileRecords:
 ...
 ;--- memprof-in-no-dap.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 2
 #   Total cold contexts: 0
 #   Total hot contexts: 0
@@ -222,18 +227,23 @@ HeapProfileRecords:
 ...
 ;--- memprof-in-no-heap.yaml
 ---
-# MemProfSummary:
+# MemProfHeapSummary:
 #   Total contexts: 0
 #   Total cold contexts: 0
 #   Total hot contexts: 0
 #   Maximum cold context total size: 0
 #   Maximum warm context total size: 0
 #   Maximum hot context total size: 0
+#
+# DataAccessProfileSummary:
+#   Num hot symbols and string literals: 2
+#   Num known cold symbols: 2
+#   Num known cold string literals: 2
 ---
 DataAccessProfiles:
   SampledRecords:
     - Symbol:          abcde
-      AccessCount:     100
+      AccessCount:     202
       Locations:
       - FileName:      file2.h
         Line:          123
@@ -245,7 +255,7 @@ DataAccessProfiles:
         - FileName:        file.cpp
           Line:            233
   KnownColdSymbols:
-    - foo
     - bar
+    - foo
   KnownColdStrHashes: [ 999, 1001 ]
 ...
diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp
index e186c5a198027..a8cc69f83038c 100644
--- a/llvm/tools/llvm-profdata/llvm-profdata.cpp
+++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp
@@ -3330,6 +3330,16 @@ static int showMemProfProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
       MemProfSumBuilder.addRecord(Pair.Record);
     MemProfSumBuilder.getSummary()->printSummaryYaml(OS);
   }
+  if (!Data.YamlifiedDataAccessProfiles.isEmpty()) {
+    OS << "#\n";
+    OS << "# DataAccessProfileSummary: \n";
+    OS << "#   Num hot symbols and string literals: "
+       << Data.YamlifiedDataAccessProfiles.Records.size() << "\n";
+    OS << "#   Num known cold symbols: "
+       << Data.YamlifiedDataAccessProfiles.KnownColdSymbols.size() << "\n";
+    OS << "#   Num known cold string literals: "
+       << Data.YamlifiedDataAccessProfiles.KnownColdStrHashes.size() << "\n";
+  }
   // Construct yaml::Output with the maximum column width of 80 so that each
   // Frame fits in one line.
   yaml::Output Yout(OS, nullptr, 80);



More information about the llvm-commits mailing list