[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
Tue Dec 10 04:06:07 PST 2024


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

>From 0489643846899b9673d9bebe2d8de08cfcc80724 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-lbr-mode.test     |  6 ++++++
 bolt/test/merge-fdata-mixed-mode.test   |  7 +++++++
 bolt/test/merge-fdata-no-lbr-mode.test  |  8 ++++++++
 bolt/tools/merge-fdata/merge-fdata.cpp  | 19 ++++++++++++++++++-
 7 files changed, 47 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-lbr-mode.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-lbr-mode.test b/bolt/test/merge-fdata-lbr-mode.test
new file mode 100644
index 00000000000000..967860f246ab51
--- /dev/null
+++ b/bolt/test/merge-fdata-lbr-mode.test
@@ -0,0 +1,6 @@
+## Check that merge-fdata tool doesn't falsely print no_lbr when not in no-lbr mode
+
+RUN: merge-fdata %S/Inputs/lbr_profile.fdata %S/Inputs/lbr_profile.fdata \
+RUN:                             | FileCheck %s --check-prefix=CHECK-FDATA
+
+CHECK-FDATA-NOT: no_lbr
diff --git a/bolt/test/merge-fdata-mixed-mode.test b/bolt/test/merge-fdata-mixed-mode.test
new file mode 100644
index 00000000000000..70ce22d8cd700a
--- /dev/null
+++ b/bolt/test/merge-fdata-mixed-mode.test
@@ -0,0 +1,7 @@
+## Check that merge-fdata tool correctly reports error message
+## when trying to merge 'no-lbr' and 'lbr' profiles
+# RUN: not merge-fdata %S/Inputs/no-lbr_profile_1.fdata \
+# RUN:                      %S/Inputs/lbr_profile.fdata \
+# RUN:                                2>&1 | FileCheck %s
+
+# CHECK: cannot mix 'no_lbr' and 'lbr' profiles.
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..ba70856d8f4d22
--- /dev/null
+++ b/bolt/test/merge-fdata-no-lbr-mode.test
@@ -0,0 +1,8 @@
+## Check that merge-fdata tool correctly processes fdata files with header
+## string produced by no-lbr mode (no_lbr)
+RUN: merge-fdata %S/Inputs/no-lbr_profile_1.fdata \
+RUN:             %S/Inputs/no-lbr_profile_2.fdata \
+RUN:      | FileCheck %s --check-prefix=CHECK-FDATA
+
+CHECK-FDATA: no_lbr
+CHECK-FDATA: 1 bar abc 30
diff --git a/bolt/tools/merge-fdata/merge-fdata.cpp b/bolt/tools/merge-fdata/merge-fdata.cpp
index 89ca46c1c0a8fa..6877fb17c6264e 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,21 @@ 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
+      size_t FirstNewline = Buf.find('\n');
+      if (FirstNewline != StringRef::npos) {
+        StringRef FirstLine = Buf.substr(0, FirstNewline);
+        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(FirstNewline + 1);
+        } else {
+          if (NoLBRCollection.value_or(false))
+            report_error(Filename, "cannot mix 'no_lbr' and 'lbr' profiles");
+          NoLBRCollection = false;
+        }
+      }
       Profile = &Profiles[tid];
     }
 
@@ -336,6 +351,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