From 0852462b3aa0cfb06d4a3c2fa8e1f4369f3dfc11 Mon Sep 17 00:00:00 2001 From: Jan Voung Date: Thu, 13 Jun 2024 18:06:15 +0000 Subject: [PATCH 1/3] 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 Record, bool IsOldProfileFormat, bool HasProfile, bool HasRelBF) { std::vector 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 Date: Fri, 14 Jun 2024 01:34:35 +0000 Subject: [PATCH 2/3] 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 Record, bool IsOldProfileFormat, bool HasProfile, bool HasRelBF) { std::vector 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 Record, Ret.push_back(FunctionSummary::EdgeTy{ Callee, CalleeInfo(Hotness, HasTailCall, RelBF)}); } + + Ret.shrink_to_fit(); return Ret; } From 255bcdb02f808663e324073fe7f3b3d89a0be8b6 Mon Sep 17 00:00:00 2001 From: Jan Voung Date: Thu, 20 Jun 2024 13:29:26 +0000 Subject: [PATCH 3/3] Simplify, removing shrink_to_fit for old formats We aren't interested in optimizing for the old formats. --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 7bda8f856855e..55e681909c0f1 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -7333,12 +7333,10 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef Record, bool HasProfile, bool HasRelBF) { std::vector Ret; // 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)) { + // Edge. Otherwise, conservatively reserve up to Record.size. + if (!IsOldProfileFormat && (HasProfile || HasRelBF)) Ret.reserve(Record.size() / 2); - } else + else Ret.reserve(Record.size()); for (unsigned I = 0, E = Record.size(); I != E; ++I) { @@ -7358,8 +7356,6 @@ ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef Record, Ret.push_back(FunctionSummary::EdgeTy{ Callee, CalleeInfo(Hotness, HasTailCall, RelBF)}); } - - Ret.shrink_to_fit(); return Ret; }