[llvm] Update ModuleSummaryIndexBitcodeReader::makeCallList reserve amount (PR #95461)

Jan Voung via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 13 18:37:48 PDT 2024


https://github.com/jvoung updated https://github.com/llvm/llvm-project/pull/95461

>From 0852462b3aa0cfb06d4a3c2fa8e1f4369f3dfc11 Mon Sep 17 00:00:00 2001
From: Jan Voung <jvoung at gmail.com>
Date: Thu, 13 Jun 2024 18:06:15 +0000
Subject: [PATCH 1/2] Update ModuleSummaryIndexBitcodeReader::makeCallList
 reserve amount

Tighten the reserve() to `Record.size() / 2` instead of `Record.size()`
in the HasProfile/HasRelBF cases (or even / 3 for IsOldProfileFormat).
This reduces peak memory during ThinLTO indexing by ~3% in one example.

Alternatively, we could reserve and then shrink_to_fit(), but seemed
like we should just reserve the right amount (though that is a little
more complex code).
---
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 4ad3a2eaceea9..2309bdba684cd 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -7332,7 +7332,16 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
                                               bool IsOldProfileFormat,
                                               bool HasProfile, bool HasRelBF) {
   std::vector<FunctionSummary::EdgeTy> Ret;
-  Ret.reserve(Record.size());
+  if (IsOldProfileFormat) {
+    if (HasProfile)
+      Ret.reserve(Record.size() / 3);
+    else
+      Ret.reserve(Record.size() / 2);
+  } else if (HasProfile || HasRelBF) {
+    Ret.reserve(Record.size() / 2);
+  } else
+    Ret.reserve(Record.size());
+
   for (unsigned I = 0, E = Record.size(); I != E; ++I) {
     CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
     bool HasTailCall = false;

>From cf6ec19753bff3a9fff7193def0e338730543af2 Mon Sep 17 00:00:00 2001
From: Jan Voung <jvoung at gmail.com>
Date: Fri, 14 Jun 2024 01:34:35 +0000
Subject: [PATCH 2/2] Simplify the cases focusing on not IsOldProfileFormat

and add shrink_to_fit in case of drift in the future
(should be a no-op in the exact/common case).
---
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 2309bdba684cd..7bda8f856855e 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -7332,12 +7332,11 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
                                               bool IsOldProfileFormat,
                                               bool HasProfile, bool HasRelBF) {
   std::vector<FunctionSummary::EdgeTy> Ret;
-  if (IsOldProfileFormat) {
-    if (HasProfile)
-      Ret.reserve(Record.size() / 3);
-    else
-      Ret.reserve(Record.size() / 2);
-  } else if (HasProfile || HasRelBF) {
+  // In the case of new profile formats, there are two Record entries per
+  // Edge. Otherwise, conservatively reserve up to Record.size and later
+  // shrink_to_fit when we are done (and shrink_to_fit for the exact
+  // case should be a no-op).
+  if (!IsOldProfileFormat && (HasProfile || HasRelBF)) {
     Ret.reserve(Record.size() / 2);
   } else
     Ret.reserve(Record.size());
@@ -7359,6 +7358,8 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
     Ret.push_back(FunctionSummary::EdgeTy{
         Callee, CalleeInfo(Hotness, HasTailCall, RelBF)});
   }
+
+  Ret.shrink_to_fit();
   return Ret;
 }
 



More information about the llvm-commits mailing list