[llvm] [BOLT][NFCI] Centralize DataReader::setEntryCounts (PR #196411)

Amir Ayupov via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 14:04:47 PDT 2026


https://github.com/aaupov updated https://github.com/llvm/llvm-project/pull/196411

>From b3793a068c095c856236423e8dcbbc79b3febc27 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Thu, 7 May 2026 13:07:18 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 bolt/include/bolt/Profile/DataReader.h |  9 ++----
 bolt/lib/Profile/DataReader.cpp        | 41 +++++++++++++++-----------
 2 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/bolt/include/bolt/Profile/DataReader.h b/bolt/include/bolt/Profile/DataReader.h
index 31b23ff4cdd8e..44838902fd9b3 100644
--- a/bolt/include/bolt/Profile/DataReader.h
+++ b/bolt/include/bolt/Profile/DataReader.h
@@ -92,12 +92,6 @@ struct FuncBranchData {
   ContainerTy Data;
   ContainerTy EntryData;
 
-  /// Total execution count for the function.
-  int64_t ExecutionCount{0};
-
-  /// Total entry count from external code for the function.
-  uint64_t ExternEntryCount{0};
-
   /// Indicate if the data was used.
   bool Used{false};
 
@@ -115,6 +109,9 @@ struct FuncBranchData {
   /// by counting the number of executed branches for each BranchInfo
   uint64_t getNumExecutedBranches() const;
 
+  /// Set entry counts derived to \p BF.
+  void setEntryCounts(BinaryFunction &BF) const;
+
   /// Aggregation helpers
   DenseMap<uint64_t, DenseMap<uint64_t, size_t>> IntraIndex;
   DenseMap<uint64_t, DenseMap<Location, size_t>> InterIndex;
diff --git a/bolt/lib/Profile/DataReader.cpp b/bolt/lib/Profile/DataReader.cpp
index 38e32d12028d3..f02a6d720d882 100644
--- a/bolt/lib/Profile/DataReader.cpp
+++ b/bolt/lib/Profile/DataReader.cpp
@@ -84,8 +84,6 @@ void FuncBranchData::appendFrom(const FuncBranchData &FBD, uint64_t Offset) {
     }
   }
   llvm::stable_sort(Data);
-  ExecutionCount += FBD.ExecutionCount;
-  ExternEntryCount += FBD.ExternEntryCount;
   for (auto I = FBD.EntryData.begin(), E = FBD.EntryData.end(); I != E; ++I) {
     assert(I->To.Name == FBD.Name);
     auto NewElmt = EntryData.insert(EntryData.end(), *I);
@@ -104,6 +102,20 @@ uint64_t FuncBranchData::getNumExecutedBranches() const {
   return ExecutedBranches;
 }
 
+void FuncBranchData::setEntryCounts(BinaryFunction &BF) const {
+  uint64_t ExecCount = 0;
+  uint64_t ExternEntryCount = 0;
+  for (const BranchInfo &BI : EntryData) {
+    if (BI.To.Offset != 0)
+      continue;
+    ExecCount += BI.Branches;
+    if (!BI.From.IsSymbol)
+      ExternEntryCount += BI.Branches;
+  }
+  BF.setExecutionCount(ExecCount);
+  BF.setExternEntryCount(ExternEntryCount);
+}
+
 void BasicSampleInfo::mergeWith(const BasicSampleInfo &SI) { Hits += SI.Hits; }
 
 void BasicSampleInfo::print(raw_ostream &OS) const {
@@ -240,8 +252,7 @@ Error DataReader::preprocessProfile(BinaryContext &BC) {
     }
     if (FuncBranchData *FuncData = getBranchDataForNames(Function.getNames())) {
       setBranchData(Function, FuncData);
-      Function.ExecutionCount = FuncData->ExecutionCount;
-      Function.ExternEntryCount = FuncData->ExternEntryCount;
+      FuncData->setEntryCounts(Function);
       FuncData->Used = true;
     }
   }
@@ -333,6 +344,10 @@ std::error_code DataReader::parseInput() {
 }
 
 void DataReader::readProfile(BinaryFunction &BF) {
+  // Set entry counts for the common case.
+  if (FuncBranchData *FBD = getBranchData(BF))
+    FBD->setEntryCounts(BF);
+
   if (BF.empty())
     return;
 
@@ -351,6 +366,10 @@ void DataReader::readProfile(BinaryFunction &BF) {
   if (!FBD)
     return;
 
+  // Re-set entry counts in case FBD was swapped (LTO) or merged
+  // (fetchProfileForOtherEntryPoints).
+  FBD->setEntryCounts(BF);
+
   // Assign basic block counts to function entry points. These only include
   // counts for outside entries.
   //
@@ -397,8 +416,6 @@ void DataReader::matchProfileData(BinaryFunction &BF) {
     if (BF.ProfileMatchRatio == 1.0f) {
       if (fetchProfileForOtherEntryPoints(BF)) {
         BF.ProfileMatchRatio = evaluateProfileData(BF, *FBD);
-        BF.ExecutionCount = FBD->ExecutionCount;
-        BF.ExternEntryCount = FBD->ExternEntryCount;
         BF.RawSampleCount = FBD->getNumExecutedBranches();
       }
       return;
@@ -428,8 +445,6 @@ void DataReader::matchProfileData(BinaryFunction &BF) {
     // Update function profile data with the new set.
     setBranchData(BF, NewBranchData);
     NewBranchData->Used = true;
-    BF.ExecutionCount = NewBranchData->ExecutionCount;
-    BF.ExternEntryCount = NewBranchData->ExternEntryCount;
     BF.ProfileMatchRatio = 1.0f;
     break;
   }
@@ -1168,16 +1183,6 @@ std::error_code DataReader::parse() {
       I = GetOrCreateFuncEntry(BI.To.Name);
       I->second.EntryData.emplace_back(std::move(BI));
     }
-
-    // If destination is the function start - update execution count.
-    // NB: the data is skewed since we cannot tell tail recursion from
-    //     branches to the function start.
-    if (BI.To.IsSymbol && BI.To.Offset == 0) {
-      I = GetOrCreateFuncEntry(BI.To.Name);
-      I->second.ExecutionCount += BI.Branches;
-      if (!BI.From.IsSymbol)
-        I->second.ExternEntryCount += BI.Branches;
-    }
   }
 
   while (hasMemData()) {



More information about the llvm-commits mailing list