Skip to content

Conversation

@yozhu
Copy link
Contributor

@yozhu yozhu commented Nov 14, 2025

With this option we can pass to BOLT names of functions to be printed through a file instead of specifying those functions all on command line.

Summary:
With this option we can pass to BOLT names of function to be printed
through a file instead of having them specified on command line.
@llvmbot llvmbot added the BOLT label Nov 14, 2025
@yozhu yozhu changed the title [BOLT][print] Add option '--print-only-file' [BOLT][print] Add option '--print-only-file' (NFC) Nov 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2025

@llvm/pr-subscribers-bolt

Author: YongKang Zhu (yozhu)

Changes

With this option we can pass to BOLT names of functions to be printed through a file instead of specifying those functions all on command line.


Full diff: https://github.com/llvm/llvm-project/pull/168023.diff

5 Files Affected:

  • (modified) bolt/include/bolt/Rewrite/RewriteInstance.h (+3)
  • (modified) bolt/lib/Core/BinaryFunction.cpp (+2-8)
  • (modified) bolt/lib/Rewrite/RewriteInstance.cpp (+18-9)
  • (modified) bolt/lib/Utils/CommandLineOpts.cpp (+10)
  • (added) bolt/test/print-only.test (+25)
diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h
index 0fe2e32b61933..35abf6b4d4ddd 100644
--- a/bolt/include/bolt/Rewrite/RewriteInstance.h
+++ b/bolt/include/bolt/Rewrite/RewriteInstance.h
@@ -139,6 +139,9 @@ class RewriteInstance {
   void handleRelocation(const object::SectionRef &RelocatedSection,
                         const RelocationRef &Rel);
 
+  /// Collect functions that are specified to be bumped.
+  void selectFunctionsToPrint();
+
   /// Mark functions that are not meant for processing as ignored.
   void selectFunctionsToProcess();
 
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index a0d8385aa3824..a5fdf79a737f5 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -61,6 +61,8 @@ extern cl::OptionCategory BoltOptCategory;
 
 extern cl::opt<bool> EnableBAT;
 extern cl::opt<bool> Instrument;
+extern cl::list<std::string> PrintOnly;
+extern cl::opt<std::string> PrintOnlyFile;
 extern cl::opt<bool> StrictMode;
 extern cl::opt<bool> UpdateDebugSections;
 extern cl::opt<unsigned> Verbosity;
@@ -133,14 +135,6 @@ PrintDynoStatsOnly("print-dyno-stats-only",
   cl::Hidden,
   cl::cat(BoltCategory));
 
-static cl::list<std::string>
-PrintOnly("print-only",
-  cl::CommaSeparated,
-  cl::desc("list of functions to print"),
-  cl::value_desc("func1,func2,func3,..."),
-  cl::Hidden,
-  cl::cat(BoltCategory));
-
 cl::opt<bool>
     TimeBuild("time-build",
               cl::desc("print time spent constructing binary functions"),
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 8d6731e7540a8..0e100bec01ca6 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -82,6 +82,8 @@ extern cl::opt<bool> Hugify;
 extern cl::opt<bool> Instrument;
 extern cl::opt<bool> KeepNops;
 extern cl::opt<bool> Lite;
+extern cl::list<std::string> PrintOnly;
+extern cl::opt<std::string> PrintOnlyFile;
 extern cl::list<std::string> ReorderData;
 extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
 extern cl::opt<bool> TerminalHLT;
@@ -730,6 +732,8 @@ Error RewriteInstance::run() {
              << "\n";
   BC->outs() << "BOLT-INFO: BOLT version: " << BoltRevision << "\n";
 
+  selectFunctionsToPrint();
+
   if (Error E = discoverStorage())
     return E;
   if (Error E = readSpecialSections())
@@ -3100,17 +3104,22 @@ static BinaryFunction *getInitFunctionIfStaticBinary(BinaryContext &BC) {
   return BC.getBinaryFunctionAtAddress(BD->getAddress());
 }
 
+static void populateFunctionNames(cl::opt<std::string> &FunctionNamesFile,
+                                  cl::list<std::string> &FunctionNames) {
+  if (FunctionNamesFile.empty())
+    return;
+  std::ifstream FuncsFile(FunctionNamesFile, std::ios::in);
+  std::string FuncName;
+  while (std::getline(FuncsFile, FuncName))
+    FunctionNames.push_back(FuncName);
+}
+
+void RewriteInstance::selectFunctionsToPrint() {
+  populateFunctionNames(opts::PrintOnlyFile, opts::PrintOnly);
+}
+
 void RewriteInstance::selectFunctionsToProcess() {
   // Extend the list of functions to process or skip from a file.
-  auto populateFunctionNames = [](cl::opt<std::string> &FunctionNamesFile,
-                                  cl::list<std::string> &FunctionNames) {
-    if (FunctionNamesFile.empty())
-      return;
-    std::ifstream FuncsFile(FunctionNamesFile, std::ios::in);
-    std::string FuncName;
-    while (std::getline(FuncsFile, FuncName))
-      FunctionNames.push_back(FuncName);
-  };
   populateFunctionNames(opts::FunctionNamesFile, opts::ForceFunctionNames);
   populateFunctionNames(opts::SkipFunctionNamesFile, opts::SkipFunctionNames);
   populateFunctionNames(opts::FunctionNamesFileNR, opts::ForceFunctionNamesNR);
diff --git a/bolt/lib/Utils/CommandLineOpts.cpp b/bolt/lib/Utils/CommandLineOpts.cpp
index 5be04d2ceea94..b7eb209af8aca 100644
--- a/bolt/lib/Utils/CommandLineOpts.cpp
+++ b/bolt/lib/Utils/CommandLineOpts.cpp
@@ -245,6 +245,16 @@ cl::opt<bool> PrintCacheMetrics(
     cl::desc("calculate and print various metrics for instruction cache"),
     cl::cat(BoltOptCategory));
 
+cl::list<std::string> PrintOnly("print-only", cl::CommaSeparated,
+                                cl::desc("list of functions to print"),
+                                cl::value_desc("func1,func2,func3,..."),
+                                cl::Hidden, cl::cat(BoltCategory));
+
+cl::opt<std::string>
+    PrintOnlyFile("print-only-file",
+                  cl::desc("file with list of functions to print"), cl::Hidden,
+                  cl::cat(BoltCategory));
+
 cl::opt<bool> PrintSections("print-sections",
                             cl::desc("print all registered sections"),
                             cl::Hidden, cl::cat(BoltCategory));
diff --git a/bolt/test/print-only.test b/bolt/test/print-only.test
new file mode 100644
index 0000000000000..1c2ce49de3669
--- /dev/null
+++ b/bolt/test/print-only.test
@@ -0,0 +1,25 @@
+# Verify if `--print-only` and `--print-only-files` work fine.
+
+# REQURIES: system-linux
+
+# RUN: %clang %cflags -x c %p/Inputs/bolt_icf.cpp -o %t -Wl,-q
+# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
+# RUN:     --print-only=foo.*,bar.*,main.* 2>&1 | FileCheck %s
+
+# RUN: echo "bar.*" > %t.pof
+# RUN: echo "main.*" >> %t.pof
+# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
+# RUN:     --print-only=foo.* --print-only-file=%t.pof \
+# RUN:     2>&1 | FileCheck %s
+
+# RUN: echo "foo.*" >> %t.pof
+# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
+# RUN:     --print-only-file=%t.pof 2>&1 | FileCheck %s
+
+# CHECK-NOT: Binary Function "fiz" after building cfg
+# CHECK-NOT: Binary Function "faz" after building cfg
+# CHECK-NOT: Binary Function "zip" after building cfg
+# CHECK-NOT: Binary Function "zap" after building cfg
+# CHECK: Binary Function "foo" after building cfg
+# CHECK: Binary Function "bar" after building cfg
+# CHECK: Binary Function "main" after building cfg

Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks YongKang, looks good!

@@ -0,0 +1,25 @@
# Verify if `--print-only` and `--print-only-files` work fine.

# REQURIES: system-linux
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:

Suggested change
# REQURIES: system-linux
# REQUIRES: system-linux

@yozhu
Copy link
Contributor Author

yozhu commented Nov 14, 2025

Thanks for the review, Paschalis!

@yozhu yozhu merged commit ac6daa8 into llvm:main Nov 14, 2025
10 checks passed
@yozhu yozhu deleted the printonlyfile branch November 14, 2025 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants