diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/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(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. diff --git a/bolt/test/max-funcs.test b/bolt/test/max-funcs.test new file mode 100644 --- /dev/null +++ b/bolt/test/max-funcs.test @@ -0,0 +1,13 @@ +## Check that --max-funcs= 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: <{{.*}}>: