Skip to content

Commit ac6daa8

Browse files
authored
[BOLT][print] Add option '--print-only-file' (NFC) (#168023)
With this option we can pass to BOLT names of functions to be printed through a file instead of specifying them all on command line.
1 parent bbece4b commit ac6daa8

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class RewriteInstance {
139139
void handleRelocation(const object::SectionRef &RelocatedSection,
140140
const RelocationRef &Rel);
141141

142+
/// Collect functions that are specified to be bumped.
143+
void selectFunctionsToPrint();
144+
142145
/// Mark functions that are not meant for processing as ignored.
143146
void selectFunctionsToProcess();
144147

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ extern cl::OptionCategory BoltOptCategory;
6161

6262
extern cl::opt<bool> EnableBAT;
6363
extern cl::opt<bool> Instrument;
64+
extern cl::list<std::string> PrintOnly;
65+
extern cl::opt<std::string> PrintOnlyFile;
6466
extern cl::opt<bool> StrictMode;
6567
extern cl::opt<bool> UpdateDebugSections;
6668
extern cl::opt<unsigned> Verbosity;
@@ -133,14 +135,6 @@ PrintDynoStatsOnly("print-dyno-stats-only",
133135
cl::Hidden,
134136
cl::cat(BoltCategory));
135137

136-
static cl::list<std::string>
137-
PrintOnly("print-only",
138-
cl::CommaSeparated,
139-
cl::desc("list of functions to print"),
140-
cl::value_desc("func1,func2,func3,..."),
141-
cl::Hidden,
142-
cl::cat(BoltCategory));
143-
144138
cl::opt<bool>
145139
TimeBuild("time-build",
146140
cl::desc("print time spent constructing binary functions"),

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ extern cl::opt<bool> Hugify;
8282
extern cl::opt<bool> Instrument;
8383
extern cl::opt<bool> KeepNops;
8484
extern cl::opt<bool> Lite;
85+
extern cl::list<std::string> PrintOnly;
86+
extern cl::opt<std::string> PrintOnlyFile;
8587
extern cl::list<std::string> ReorderData;
8688
extern cl::opt<bolt::ReorderFunctions::ReorderType> ReorderFunctions;
8789
extern cl::opt<bool> TerminalHLT;
@@ -730,6 +732,8 @@ Error RewriteInstance::run() {
730732
<< "\n";
731733
BC->outs() << "BOLT-INFO: BOLT version: " << BoltRevision << "\n";
732734

735+
selectFunctionsToPrint();
736+
733737
if (Error E = discoverStorage())
734738
return E;
735739
if (Error E = readSpecialSections())
@@ -3100,17 +3104,22 @@ static BinaryFunction *getInitFunctionIfStaticBinary(BinaryContext &BC) {
31003104
return BC.getBinaryFunctionAtAddress(BD->getAddress());
31013105
}
31023106

3107+
static void populateFunctionNames(cl::opt<std::string> &FunctionNamesFile,
3108+
cl::list<std::string> &FunctionNames) {
3109+
if (FunctionNamesFile.empty())
3110+
return;
3111+
std::ifstream FuncsFile(FunctionNamesFile, std::ios::in);
3112+
std::string FuncName;
3113+
while (std::getline(FuncsFile, FuncName))
3114+
FunctionNames.push_back(FuncName);
3115+
}
3116+
3117+
void RewriteInstance::selectFunctionsToPrint() {
3118+
populateFunctionNames(opts::PrintOnlyFile, opts::PrintOnly);
3119+
}
3120+
31033121
void RewriteInstance::selectFunctionsToProcess() {
31043122
// Extend the list of functions to process or skip from a file.
3105-
auto populateFunctionNames = [](cl::opt<std::string> &FunctionNamesFile,
3106-
cl::list<std::string> &FunctionNames) {
3107-
if (FunctionNamesFile.empty())
3108-
return;
3109-
std::ifstream FuncsFile(FunctionNamesFile, std::ios::in);
3110-
std::string FuncName;
3111-
while (std::getline(FuncsFile, FuncName))
3112-
FunctionNames.push_back(FuncName);
3113-
};
31143123
populateFunctionNames(opts::FunctionNamesFile, opts::ForceFunctionNames);
31153124
populateFunctionNames(opts::SkipFunctionNamesFile, opts::SkipFunctionNames);
31163125
populateFunctionNames(opts::FunctionNamesFileNR, opts::ForceFunctionNamesNR);

bolt/lib/Utils/CommandLineOpts.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ cl::opt<bool> PrintCacheMetrics(
245245
cl::desc("calculate and print various metrics for instruction cache"),
246246
cl::cat(BoltOptCategory));
247247

248+
cl::list<std::string> PrintOnly("print-only", cl::CommaSeparated,
249+
cl::desc("list of functions to print"),
250+
cl::value_desc("func1,func2,func3,..."),
251+
cl::Hidden, cl::cat(BoltCategory));
252+
253+
cl::opt<std::string>
254+
PrintOnlyFile("print-only-file",
255+
cl::desc("file with list of functions to print"), cl::Hidden,
256+
cl::cat(BoltCategory));
257+
248258
cl::opt<bool> PrintSections("print-sections",
249259
cl::desc("print all registered sections"),
250260
cl::Hidden, cl::cat(BoltCategory));

bolt/test/print-only.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Verify if `--print-only` and `--print-only-files` work fine.
2+
3+
# REQUIRES: system-linux
4+
5+
# RUN: %clang %cflags -x c %p/Inputs/bolt_icf.cpp -o %t -Wl,-q
6+
# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
7+
# RUN: --print-only=foo.*,bar.*,main.* 2>&1 | FileCheck %s
8+
9+
# RUN: echo "bar.*" > %t.pof
10+
# RUN: echo "main.*" >> %t.pof
11+
# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
12+
# RUN: --print-only=foo.* --print-only-file=%t.pof \
13+
# RUN: 2>&1 | FileCheck %s
14+
15+
# RUN: echo "foo.*" >> %t.pof
16+
# RUN: llvm-bolt %t -o %t.bolt --icf=none --print-cfg \
17+
# RUN: --print-only-file=%t.pof 2>&1 | FileCheck %s
18+
19+
# CHECK-NOT: Binary Function "fiz" after building cfg
20+
# CHECK-NOT: Binary Function "faz" after building cfg
21+
# CHECK-NOT: Binary Function "zip" after building cfg
22+
# CHECK-NOT: Binary Function "zap" after building cfg
23+
# CHECK: Binary Function "foo" after building cfg
24+
# CHECK: Binary Function "bar" after building cfg
25+
# CHECK: Binary Function "main" after building cfg

0 commit comments

Comments
 (0)