Skip to content

Conversation

@mingmingl-llvm
Copy link
Contributor

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.

@mingmingl-llvm mingmingl-llvm requested review from kazutakahirata, snehasish and teresajohnson and removed request for kazutakahirata December 17, 2025 03:57
@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels Dec 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-transforms

Author: Mingming Liu (mingmingl-llvm)

Changes

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.


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

6 Files Affected:

  • (modified) llvm/lib/ProfileData/InstrProfReader.cpp (+19)
  • (modified) llvm/lib/ProfileData/MemProfSummary.cpp (+1-1)
  • (modified) llvm/test/Transforms/PGOProfile/memprof.ll (+1-1)
  • (modified) llvm/test/Transforms/PGOProfile/memprof_max_cold_threshold.test (+1-1)
  • (modified) llvm/test/tools/llvm-profdata/memprof-yaml.test (+21-11)
  • (modified) llvm/tools/llvm-profdata/llvm-profdata.cpp (+10)
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);

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.

3 participants