[llvm] [BOLT] Heatmap fix on large binaries and printing mappings (PR #92815)

Paschalis Mpeis via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 12:46:08 PDT 2024


https://github.com/paschalis-mpeis created https://github.com/llvm/llvm-project/pull/92815

### DRAFT PR: This is WIP.

Needs:
- Add context describing the issue
- Add testing

## In short, it improves heatmaps by:
1) Fixing a bug where the computation of a text section's size was wrong. As a result, running the heatmap tool on some bigger bolted binaries, the tool ignored most of the hits in hot/cold text segments.

2) Adding to the legend the mapping between the bucket letters to the actual text sections.

>From c74aaf40b6e838e0ea633bea63f2899039367b57 Mon Sep 17 00:00:00 2001
From: "Paschalis Mpeis (aws-mem-aarch64)" <paschalis.mpeis at arm.com>
Date: Wed, 24 Apr 2024 08:25:59 +0000
Subject: [PATCH] [BOLT] Heatmap fix on large binaries and printing mappings.

---
 bolt/lib/Profile/DataAggregator.cpp | 17 ++++++++++++++---
 bolt/lib/Profile/Heatmap.cpp        | 11 +++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index e06debcee741e..8d5b5531654cb 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -2009,8 +2009,9 @@ std::error_code DataAggregator::parseMMapEvents() {
       return MI.second.PID == FileMMapInfo.second.PID;
     });
 
-    if (PIDExists)
-      continue;
+    // let duplicates to the multimap.
+    // if (PIDExists)
+    //   continue;
 
     GlobalMMapInfo.insert(FileMMapInfo);
   }
@@ -2067,7 +2068,17 @@ std::error_code DataAggregator::parseMMapEvents() {
       }
     }
 
-    BinaryMMapInfo.insert(std::make_pair(MMapInfo.PID, MMapInfo));
+    // The mapping was already in place, but there are cases where the size
+    // is wrong. Fix it if needed.
+    if(!BinaryMMapInfo.insert(std::make_pair(MMapInfo.PID, MMapInfo)).second) {
+      auto EndAddress = MMapInfo.MMapAddress + MMapInfo.Size;
+      auto FixedSize = EndAddress - BinaryMMapInfo[MMapInfo.PID].BaseAddress;
+
+      if (FixedSize != BinaryMMapInfo[MMapInfo.PID].Size) {
+          outs() << "MMap size fixed: " << Twine::utohexstr(FixedSize) << " \n";
+         BinaryMMapInfo[MMapInfo.PID].Size = FixedSize;
+      }
+    }
   }
 
   if (BinaryMMapInfo.empty()) {
diff --git a/bolt/lib/Profile/Heatmap.cpp b/bolt/lib/Profile/Heatmap.cpp
index 210a5cc98c104..233b070bde2da 100644
--- a/bolt/lib/Profile/Heatmap.cpp
+++ b/bolt/lib/Profile/Heatmap.cpp
@@ -164,6 +164,7 @@ void Heatmap::print(raw_ostream &OS) const {
 
   // Print map legend
   OS << "Legend:\n";
+  OS << "\nRegions:\n";
   uint64_t PrevValue = 0;
   for (unsigned I = 0; I < sizeof(Range) / sizeof(Range[0]); ++I) {
     const uint64_t Value = Range[I];
@@ -173,6 +174,16 @@ void Heatmap::print(raw_ostream &OS) const {
     PrevValue = Value;
   }
 
+  {
+    OS << "\nSections:\n";
+    int Idx = 0;
+    for (auto TxtSeg : TextSections) {
+      OS << static_cast<char>('A' + ((Idx++) % 26)) << ": " << TxtSeg.Name
+         << ": 0x" <<  Twine::utohexstr(TxtSeg.BeginAddress) << "-0x"
+         << Twine::utohexstr(TxtSeg.EndAddress) << "\n";
+    }
+  }
+
   // Pos - character position from right in hex form.
   auto printHeader = [&](unsigned Pos) {
     OS << "            ";



More information about the llvm-commits mailing list