[llvm] [BOLT] Enable merge branch profile with memory profile (PR #174405)
Haibo Jiang via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 5 06:24:02 PST 2026
https://github.com/Jianghibo updated https://github.com/llvm/llvm-project/pull/174405
>From a43b03d10c98b131ab7f02be3dc6a907abf2f7a1 Mon Sep 17 00:00:00 2001
From: jianghaibo <jianghaibo9 at huawei.com>
Date: Sun, 4 Jan 2026 22:28:40 +0800
Subject: [PATCH] [BOLT] Enable merge branch profile with memory profile
Allows merging the memory profiles with basic sampling profiles or
LBR sampling profils.
Using std::map instead of StringMap to keep records in order: For
profiles that mix branch and memory information, since llvm-bolt
processes branch and memory information sequentially, it is
essential to ensure that the data of these two types remains
relatively independent.
---
.../test/merge-fdata-mixed-branch-memory.test | 15 ++++++++++++++
bolt/tools/merge-fdata/merge-fdata.cpp | 20 +++++++++++++++----
2 files changed, 31 insertions(+), 4 deletions(-)
create mode 100644 bolt/test/merge-fdata-mixed-branch-memory.test
diff --git a/bolt/test/merge-fdata-mixed-branch-memory.test b/bolt/test/merge-fdata-mixed-branch-memory.test
new file mode 100644
index 0000000000000..b7a162b2db76d
--- /dev/null
+++ b/bolt/test/merge-fdata-mixed-branch-memory.test
@@ -0,0 +1,15 @@
+## Check that merge-fdata correctly handles merging two fdata files with no_lbr/lbr sampling and memory sampling.
+
+# REQUIRES: system-linux
+
+# RUN: split-file %s %t
+# RUN: merge-fdata %t/a.fdata %t/b.fdata -o %t/merged.fdata
+# RUN: FileCheck %s --input-file %t/merged.fdata
+#
+# CHECK: 1 main 0 1 main 2 0 1
+# CHECK: 4 main 10 4 otherSym 123 1
+
+#--- a.fdata
+1 main 0 1 main 2 0 1
+#--- b.fdata
+4 main 10 4 otherSym 123 1
diff --git a/bolt/tools/merge-fdata/merge-fdata.cpp b/bolt/tools/merge-fdata/merge-fdata.cpp
index f5b0251ea0331..5d3691bf57589 100644
--- a/bolt/tools/merge-fdata/merge-fdata.cpp
+++ b/bolt/tools/merge-fdata/merge-fdata.cpp
@@ -263,6 +263,10 @@ bool isYAML(const StringRef Filename) {
return false;
}
+bool isBasicOrLBRItem(APInt SymbolValue) {
+ return SymbolValue.sge(0) && SymbolValue.sle(2);
+}
+
void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
errs() << "Using legacy profile format.\n";
std::optional<bool> BoltedCollection;
@@ -319,8 +323,12 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
auto [Signature, ExecCount] = Line.rsplit(' ');
if (ExecCount.getAsInteger(10, Count.Exec))
report_error(Filename, "Malformed / corrupted execution count");
+
+ auto [Symbol, Record] = Signature.split(' ');
+ APInt SymbolValue;
+ Symbol.getAsInteger(10, SymbolValue);
// Only LBR profile has misprediction field
- if (!NoLBRCollection.value_or(false)) {
+ if (!NoLBRCollection.value_or(false) && isBasicOrLBRItem(SymbolValue)) {
auto [SignatureLBR, MispredCount] = Signature.rsplit(' ');
Signature = SignatureLBR;
if (MispredCount.getAsInteger(10, Count.Mispred))
@@ -343,10 +351,11 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
Pool.async(ParseProfile, std::cref(Filename), std::ref(ParsedProfiles));
Pool.wait();
- ProfileTy MergedProfile;
+ std::map<StringRef, CounterTy> MergedProfile;
for (const auto &[Thread, Profile] : ParsedProfiles)
for (const auto &[Key, Value] : Profile) {
- CounterTy Count = MergedProfile.lookup(Key) + Value;
+ auto It = MergedProfile.find(Key);
+ CounterTy Count = It != MergedProfile.end() ? It->second + Value : Value;
MergedProfile.insert_or_assign(Key, Count);
}
@@ -356,7 +365,10 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
output() << "no_lbr\n";
for (const auto &[Key, Value] : MergedProfile) {
output() << Key << " ";
- if (!NoLBRCollection.value_or(false))
+ auto [Symbol, Record] = Key.split(' ');
+ APInt SymbolValue;
+ Symbol.getAsInteger(10, SymbolValue);
+ if (!NoLBRCollection.value_or(false) && isBasicOrLBRItem(SymbolValue))
output() << Value.Mispred << " ";
output() << Value.Exec << "\n";
}
More information about the llvm-commits
mailing list