[PATCH] D152751: [BOLT] Fix --max-funcs=<N> option

Maksim Panchenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 12 16:54:43 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGc4e60a7f6045: [BOLT] Fix --max-funcs=<N> option (authored by maksfb).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152751/new/

https://reviews.llvm.org/D152751

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/max-funcs.test


Index: bolt/test/max-funcs.test
===================================================================
--- /dev/null
+++ bolt/test/max-funcs.test
@@ -0,0 +1,13 @@
+## Check that --max-funcs=<N> option works properly in llvm-bolt,
+## resulting in processing of no more than N functions in the binary.
+
+REQUIRES: system-linux
+
+RUN: %clangxx %p/Inputs/bolt_icf.cpp -g -Wl,-q -o %t.exe
+RUN: llvm-bolt %t.exe --relocs -o %t --max-funcs=2
+RUN: llvm-objdump -d -j .text %t | FileCheck %s
+
+## Check that there are only two functions in the dump of .text
+CHECK: <{{.*}}>:
+CHECK: <{{.*}}>:
+CHECK-NOT: <{{.*}}>:
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -2917,7 +2917,8 @@
 
   uint64_t NumFunctionsToProcess = 0;
   auto mustSkip = [&](const BinaryFunction &Function) {
-    if (opts::MaxFunctions && NumFunctionsToProcess > opts::MaxFunctions)
+    if (opts::MaxFunctions.getNumOccurrences() &&
+        NumFunctionsToProcess >= opts::MaxFunctions)
       return true;
     for (std::string &Name : opts::SkipFunctionNames)
       if (Function.hasNameRegex(Name))
@@ -2992,7 +2993,8 @@
       Function.setIgnored();
     } else {
       ++NumFunctionsToProcess;
-      if (opts::MaxFunctions && NumFunctionsToProcess == opts::MaxFunctions)
+      if (opts::MaxFunctions.getNumOccurrences() &&
+          NumFunctionsToProcess == opts::MaxFunctions)
         outs() << "BOLT-INFO: processing ending on " << Function << '\n';
     }
   }
@@ -5787,6 +5789,8 @@
     }
 
     OverwrittenScore += Function->getFunctionScore();
+    ++CountOverwrittenFunctions;
+
     // Overwrite function in the output file.
     if (opts::Verbosity >= 2)
       outs() << "BOLT: rewriting function \"" << *Function << "\"\n";
@@ -5804,32 +5808,20 @@
       OS.seek(Pos);
     }
 
-    if (!Function->isSplit()) {
-      ++CountOverwrittenFunctions;
-      if (opts::MaxFunctions &&
-          CountOverwrittenFunctions == opts::MaxFunctions) {
-        outs() << "BOLT: maximum number of functions reached\n";
-        break;
-      }
+    if (!Function->isSplit())
       continue;
-    }
 
     // Write cold part
-    if (opts::Verbosity >= 2)
+    if (opts::Verbosity >= 2) {
       outs() << formatv("BOLT: rewriting function \"{0}\" (split parts)\n",
                         *Function);
+    }
 
     for (const FunctionFragment &FF :
          Function->getLayout().getSplitFragments()) {
       OS.pwrite(reinterpret_cast<char *>(FF.getImageAddress()),
                 FF.getImageSize(), FF.getFileOffset());
     }
-
-    ++CountOverwrittenFunctions;
-    if (opts::MaxFunctions && CountOverwrittenFunctions == opts::MaxFunctions) {
-      outs() << "BOLT: maximum number of functions reached\n";
-      break;
-    }
   }
 
   // Print function statistics for non-relocation mode.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152751.530716.patch
Type: text/x-patch
Size: 2929 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230612/bb14703a/attachment.bin>


More information about the llvm-commits mailing list