[llvm] [BOLT][merge-fdata] Fix basic sample profile aggregation without LBR info (PR #118481)

Tibor Dusnoki via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 00:50:37 PST 2024


https://github.com/tdusnoki updated https://github.com/llvm/llvm-project/pull/118481

>From 1ab6c85dcb0b6e211f11ff8034d9ea535cab7813 Mon Sep 17 00:00:00 2001
From: Tibor <tdusnoki at inf.u-szeged.hu>
Date: Thu, 28 Nov 2024 16:20:03 +0100
Subject: [PATCH] [BOLT][merge-fdata] Fix basic sample profile aggregation
 without LBR info

When a basic sample profile is gathered without LBR info, the generated profile contains a "no-lbr" tag in the first line of the fdata file. This PR fixes merge-fdata to recognize and save this tag to the output file.
---
 bolt/test/Inputs/lbr_profile.fdata          |  2 ++
 bolt/test/Inputs/no-lbr_profile_1.fdata     |  3 +++
 bolt/test/Inputs/no-lbr_profile_2.fdata     |  3 +++
 bolt/test/merge-fdata-bat-no-lbr.test       | 20 ++++++++++++++++++++
 bolt/test/merge-fdata-lbr-mode.test         | 15 +++++++++++++++
 bolt/test/merge-fdata-mixed-bat-no-lbr.test | 16 ++++++++++++++++
 bolt/test/merge-fdata-mixed-mode.test       | 15 +++++++++++++++
 bolt/test/merge-fdata-no-lbr-mode.test      | 18 ++++++++++++++++++
 bolt/tools/merge-fdata/merge-fdata.cpp      | 20 +++++++++++++++++++-
 9 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 bolt/test/Inputs/lbr_profile.fdata
 create mode 100644 bolt/test/Inputs/no-lbr_profile_1.fdata
 create mode 100644 bolt/test/Inputs/no-lbr_profile_2.fdata
 create mode 100644 bolt/test/merge-fdata-bat-no-lbr.test
 create mode 100644 bolt/test/merge-fdata-lbr-mode.test
 create mode 100644 bolt/test/merge-fdata-mixed-bat-no-lbr.test
 create mode 100644 bolt/test/merge-fdata-mixed-mode.test
 create mode 100644 bolt/test/merge-fdata-no-lbr-mode.test

diff --git a/bolt/test/Inputs/lbr_profile.fdata b/bolt/test/Inputs/lbr_profile.fdata
new file mode 100644
index 00000000000000..3606dd085ac341
--- /dev/null
+++ b/bolt/test/Inputs/lbr_profile.fdata
@@ -0,0 +1,2 @@
+1 foo a 1
+1 bar b 2
diff --git a/bolt/test/Inputs/no-lbr_profile_1.fdata b/bolt/test/Inputs/no-lbr_profile_1.fdata
new file mode 100644
index 00000000000000..b1aa77a528a0d3
--- /dev/null
+++ b/bolt/test/Inputs/no-lbr_profile_1.fdata
@@ -0,0 +1,3 @@
+no_lbr cycles:HG:
+1 foo c 2
+1 bar abc 20
diff --git a/bolt/test/Inputs/no-lbr_profile_2.fdata b/bolt/test/Inputs/no-lbr_profile_2.fdata
new file mode 100644
index 00000000000000..388b8543cda4f6
--- /dev/null
+++ b/bolt/test/Inputs/no-lbr_profile_2.fdata
@@ -0,0 +1,3 @@
+no_lbr cycles:HG:
+1 bar abc 10
+1 foobar def 5
diff --git a/bolt/test/merge-fdata-bat-no-lbr.test b/bolt/test/merge-fdata-bat-no-lbr.test
new file mode 100644
index 00000000000000..fd5cd162633563
--- /dev/null
+++ b/bolt/test/merge-fdata-bat-no-lbr.test
@@ -0,0 +1,20 @@
+## Check that merge-fdata correctly handles merging two fdata files with both boltedcollection and no_lbr tags.
+
+# 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: boltedcollection
+# CHECK: no_lbr
+# CHECK: main 2
+
+#--- a.fdata
+boltedcollection
+no_lbr
+main 1
+#--- b.fdata
+boltedcollection
+no_lbr
+main 1
diff --git a/bolt/test/merge-fdata-lbr-mode.test b/bolt/test/merge-fdata-lbr-mode.test
new file mode 100644
index 00000000000000..268502a1dd4402
--- /dev/null
+++ b/bolt/test/merge-fdata-lbr-mode.test
@@ -0,0 +1,15 @@
+## Check that merge-fdata tool doesn't falsely print no_lbr when not in no-lbr mode
+
+# 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-NOT: no_lbr
+# CHECK: main 2
+
+#--- a.fdata
+main 1
+#--- b.fdata
+main 1
diff --git a/bolt/test/merge-fdata-mixed-bat-no-lbr.test b/bolt/test/merge-fdata-mixed-bat-no-lbr.test
new file mode 100644
index 00000000000000..b59773d2d5054f
--- /dev/null
+++ b/bolt/test/merge-fdata-mixed-bat-no-lbr.test
@@ -0,0 +1,16 @@
+## Check that merge-fdata doesn't incorrectly merge two fdata files with boltedcollection and no_lbr tags.
+
+# REQUIRES: system-linux
+
+# RUN: split-file %s %t
+# RUN: not merge-fdata %t/a.fdata %t/b.fdata 2>&1 | FileCheck %s
+
+# CHECK: cannot mix profile collected in BOLT and non-BOLT deployments
+
+#--- a.fdata
+boltedcollection
+no_lbr
+main 1
+#--- b.fdata
+no_lbr
+main 1
diff --git a/bolt/test/merge-fdata-mixed-mode.test b/bolt/test/merge-fdata-mixed-mode.test
new file mode 100644
index 00000000000000..3e1a3bfcb94542
--- /dev/null
+++ b/bolt/test/merge-fdata-mixed-mode.test
@@ -0,0 +1,15 @@
+## Check that merge-fdata tool correctly reports error message
+## when trying to merge 'no-lbr' and 'lbr' profiles
+
+# REQUIRES: system-linux
+
+# RUN: split-file %s %t
+# RUN: not merge-fdata %t/a.fdata %t/b.fdata 2>&1 | FileCheck %s
+
+# CHECK: cannot mix 'no_lbr' and 'lbr' profiles.
+
+#--- a.fdata
+no_lbr
+main 1
+#--- b.fdata
+main 1
diff --git a/bolt/test/merge-fdata-no-lbr-mode.test b/bolt/test/merge-fdata-no-lbr-mode.test
new file mode 100644
index 00000000000000..9dfad99f79994d
--- /dev/null
+++ b/bolt/test/merge-fdata-no-lbr-mode.test
@@ -0,0 +1,18 @@
+## Check that merge-fdata tool correctly processes fdata files with header
+## string produced by no-lbr mode (no_lbr)
+
+# 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: no_lbr
+# CHECK: main 2
+
+#--- a.fdata
+no_lbr
+main 1
+#--- b.fdata
+no_lbr
+main 1
diff --git a/bolt/tools/merge-fdata/merge-fdata.cpp b/bolt/tools/merge-fdata/merge-fdata.cpp
index 89ca46c1c0a8fa..39f58a7e8800e0 100644
--- a/bolt/tools/merge-fdata/merge-fdata.cpp
+++ b/bolt/tools/merge-fdata/merge-fdata.cpp
@@ -265,6 +265,7 @@ bool isYAML(const StringRef Filename) {
 void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
   errs() << "Using legacy profile format.\n";
   std::optional<bool> BoltedCollection;
+  std::optional<bool> NoLBRCollection;
   std::mutex BoltedCollectionMutex;
   typedef StringMap<uint64_t> ProfileTy;
 
@@ -297,7 +298,22 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
               "cannot mix profile collected in BOLT and non-BOLT deployments");
         BoltedCollection = false;
       }
-
+      // Check if the string "no_lbr" is in the first line
+      // (or second line if BoltedCollection is true)
+      size_t CheckNoLBRPos = Buf.find('\n');
+      if (CheckNoLBRPos != StringRef::npos) {
+        StringRef FirstLine = Buf.substr(0, CheckNoLBRPos);
+        if (FirstLine.contains("no_lbr")) {
+          if (!NoLBRCollection.value_or(true))
+            report_error(Filename, "cannot mix 'no_lbr' and 'lbr' profiles");
+          NoLBRCollection = true;
+          Buf = Buf.drop_front(CheckNoLBRPos + 1);
+        } else {
+          if (NoLBRCollection.value_or(false))
+            report_error(Filename, "cannot mix 'no_lbr' and 'lbr' profiles");
+          NoLBRCollection = false;
+        }
+      }
       Profile = &Profiles[tid];
     }
 
@@ -336,6 +352,8 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
 
   if (BoltedCollection.value_or(false))
     output() << "boltedcollection\n";
+  if (NoLBRCollection.value_or(false))
+    output() << "no_lbr\n";
   for (const auto &[Key, Value] : MergedProfile)
     output() << Key << " " << Value << "\n";
 



More information about the llvm-commits mailing list