Skip to content

Commit a606589

Browse files
committed
[NFC][BOLT] Make file-local cl::opt global variables static
1 parent 5916903 commit a606589

16 files changed

+328
-92
lines changed

bolt/lib/Core/BinaryContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ using namespace llvm;
4646

4747
namespace opts {
4848

49-
cl::opt<bool> NoHugePages("no-huge-pages",
50-
cl::desc("use regular size pages for code alignment"),
51-
cl::Hidden, cl::cat(BoltCategory));
49+
static cl::opt<bool>
50+
NoHugePages("no-huge-pages",
51+
cl::desc("use regular size pages for code alignment"),
52+
cl::Hidden, cl::cat(BoltCategory));
5253

5354
static cl::opt<bool>
5455
PrintDebugInfo("print-debug-info",

bolt/lib/Core/BinaryData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace opts {
2424
extern cl::OptionCategory BoltCategory;
2525
extern cl::opt<unsigned> Verbosity;
2626

27-
cl::opt<bool>
27+
static cl::opt<bool>
2828
PrintSymbolAliases("print-aliases",
2929
cl::desc("print aliases when printing objects"),
3030
cl::Hidden, cl::cat(BoltCategory));

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern cl::opt<unsigned> Verbosity;
6767

6868
extern bool processAllFunctions();
6969

70-
cl::opt<bool> CheckEncoding(
70+
static cl::opt<bool> CheckEncoding(
7171
"check-encoding",
7272
cl::desc("perform verification of LLVM instruction encoding/decoding. "
7373
"Every instruction in the input is decoded and re-encoded. "
@@ -144,14 +144,11 @@ cl::opt<bool>
144144
cl::desc("print time spent constructing binary functions"),
145145
cl::Hidden, cl::cat(BoltCategory));
146146

147-
cl::opt<bool>
148-
TrapOnAVX512("trap-avx512",
149-
cl::desc("in relocation mode trap upon entry to any function that uses "
150-
"AVX-512 instructions"),
151-
cl::init(false),
152-
cl::ZeroOrMore,
153-
cl::Hidden,
154-
cl::cat(BoltCategory));
147+
static cl::opt<bool> TrapOnAVX512(
148+
"trap-avx512",
149+
cl::desc("in relocation mode trap upon entry to any function that uses "
150+
"AVX-512 instructions"),
151+
cl::init(false), cl::ZeroOrMore, cl::Hidden, cl::cat(BoltCategory));
155152

156153
bool shouldPrint(const BinaryFunction &Function) {
157154
if (Function.isIgnored())

bolt/lib/Passes/Aligner.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@ extern cl::opt<bool> AlignBlocks;
2525
extern cl::opt<bool> PreserveBlocksAlignment;
2626
extern cl::opt<unsigned> AlignFunctions;
2727

28-
cl::opt<unsigned>
29-
AlignBlocksMinSize("align-blocks-min-size",
30-
cl::desc("minimal size of the basic block that should be aligned"),
31-
cl::init(0),
32-
cl::ZeroOrMore,
33-
cl::Hidden,
34-
cl::cat(BoltOptCategory));
35-
36-
cl::opt<unsigned> AlignBlocksThreshold(
28+
static cl::opt<unsigned> AlignBlocksMinSize(
29+
"align-blocks-min-size",
30+
cl::desc("minimal size of the basic block that should be aligned"),
31+
cl::init(0), cl::ZeroOrMore, cl::Hidden, cl::cat(BoltOptCategory));
32+
33+
static cl::opt<unsigned> AlignBlocksThreshold(
3734
"align-blocks-threshold",
3835
cl::desc(
3936
"align only blocks with frequency larger than containing function "
@@ -42,19 +39,17 @@ cl::opt<unsigned> AlignBlocksThreshold(
4239
"containing function."),
4340
cl::init(800), cl::Hidden, cl::cat(BoltOptCategory));
4441

45-
cl::opt<unsigned> AlignFunctionsMaxBytes(
42+
static cl::opt<unsigned> AlignFunctionsMaxBytes(
4643
"align-functions-max-bytes",
4744
cl::desc("maximum number of bytes to use to align functions"), cl::init(32),
4845
cl::cat(BoltOptCategory));
4946

50-
cl::opt<unsigned>
51-
BlockAlignment("block-alignment",
52-
cl::desc("boundary to use for alignment of basic blocks"),
53-
cl::init(16),
54-
cl::ZeroOrMore,
55-
cl::cat(BoltOptCategory));
47+
static cl::opt<unsigned>
48+
BlockAlignment("block-alignment",
49+
cl::desc("boundary to use for alignment of basic blocks"),
50+
cl::init(16), cl::ZeroOrMore, cl::cat(BoltOptCategory));
5651

57-
cl::opt<bool>
52+
static cl::opt<bool>
5853
UseCompactAligner("use-compact-aligner",
5954
cl::desc("Use compact approach for aligning functions"),
6055
cl::init(true), cl::cat(BoltOptCategory));

bolt/lib/Passes/ContinuityStats.cpp

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
//===- bolt/Passes/ContinuityStats.cpp --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the continuity stats calculation pass.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "bolt/Passes/ContinuityStats.h"
14+
#include "bolt/Core/BinaryBasicBlock.h"
15+
#include "bolt/Core/BinaryFunction.h"
16+
#include "bolt/Utils/CommandLineOpts.h"
17+
#include "llvm/Support/CommandLine.h"
18+
#include <queue>
19+
#include <unordered_map>
20+
#include <unordered_set>
21+
22+
#define DEBUG_TYPE "bolt-opts"
23+
24+
using namespace llvm;
25+
using namespace bolt;
26+
27+
namespace opts {
28+
extern cl::opt<unsigned> Verbosity;
29+
static cl::opt<unsigned> NumFunctionsForContinuityCheck(
30+
"num-functions-for-continuity-check",
31+
cl::desc("number of hottest functions to print aggregated "
32+
"CFG discontinuity stats of."),
33+
cl::init(1000), cl::ZeroOrMore, cl::Hidden, cl::cat(BoltOptCategory));
34+
} // namespace opts
35+
36+
namespace {
37+
using FunctionListType = std::vector<const BinaryFunction *>;
38+
using function_iterator = FunctionListType::iterator;
39+
40+
template <typename T>
41+
void printDistribution(raw_ostream &OS, std::vector<T> &values,
42+
bool Fraction = false) {
43+
if (values.empty())
44+
return;
45+
// Sort values from largest to smallest and print the MAX, TOP 1%, 5%, 10%,
46+
// 20%, 50%, 80%, MIN. If Fraction is true, then values are printed as
47+
// fractions instead of integers.
48+
std::sort(values.begin(), values.end());
49+
50+
auto printLine = [&](std::string Text, double Percent) {
51+
int Rank = int(values.size() * (1.0 - Percent / 100));
52+
if (Percent == 0)
53+
Rank = values.size() - 1;
54+
if (Fraction)
55+
OS << " " << Text << std::string(9 - Text.length(), ' ') << ": "
56+
<< format("%.2lf%%", values[Rank] * 100) << "\n";
57+
else
58+
OS << " " << Text << std::string(9 - Text.length(), ' ') << ": "
59+
<< values[Rank] << "\n";
60+
};
61+
62+
printLine("MAX", 0);
63+
const int percentages[] = {1, 5, 10, 20, 50, 80};
64+
for (size_t i = 0; i < sizeof(percentages) / sizeof(percentages[0]); ++i) {
65+
printLine("TOP " + std::to_string(percentages[i]) + "%", percentages[i]);
66+
}
67+
printLine("MIN", 100);
68+
}
69+
70+
void printCFGContinuityStats(raw_ostream &OS,
71+
iterator_range<function_iterator> &Functions) {
72+
// Given a perfect profile, every positive-execution-count BB should be
73+
// connected to an entry of the function through a positive-execution-count
74+
// directed path in the control flow graph.
75+
std::vector<size_t> NumUnreachables;
76+
std::vector<size_t> SumECUnreachables;
77+
std::vector<double> FractionECUnreachables;
78+
79+
for (auto it = Functions.begin(); it != Functions.end(); ++it) {
80+
const BinaryFunction *Function = *it;
81+
if (Function->size() <= 1)
82+
continue;
83+
84+
// Compute the sum of all BB execution counts (ECs).
85+
size_t NumPosECBBs = 0;
86+
size_t SumAllBBEC = 0;
87+
for (const BinaryBasicBlock &BB : *Function) {
88+
const size_t BBEC = BB.getKnownExecutionCount();
89+
NumPosECBBs += BBEC > 0 ? 1 : 0;
90+
SumAllBBEC += BBEC;
91+
}
92+
93+
// Perform BFS on subgraph of CFG induced by positive weight edges.
94+
// Compute the number of BBs reachable from the entry(s) of the function and
95+
// the sum of their execution counts (ECs).
96+
std::unordered_map<unsigned, const BinaryBasicBlock *> IndexToBB;
97+
std::unordered_set<unsigned> Visited;
98+
std::queue<unsigned> Queue;
99+
for (const BinaryBasicBlock &BB : *Function) {
100+
// Make sure BB.getIndex() is not already in IndexToBB.
101+
assert(IndexToBB.find(BB.getIndex()) == IndexToBB.end());
102+
IndexToBB[BB.getIndex()] = &BB;
103+
if (BB.isEntryPoint() && BB.getKnownExecutionCount() > 0) {
104+
Queue.push(BB.getIndex());
105+
Visited.insert(BB.getIndex());
106+
}
107+
}
108+
while (!Queue.empty()) {
109+
const unsigned BBIndex = Queue.front();
110+
const BinaryBasicBlock *BB = IndexToBB[BBIndex];
111+
Queue.pop();
112+
auto SuccBIIter = BB->branch_info_begin();
113+
for (const BinaryBasicBlock *Succ : BB->successors()) {
114+
const uint64_t Count = SuccBIIter->Count;
115+
if (Count == BinaryBasicBlock::COUNT_NO_PROFILE || Count == 0) {
116+
++SuccBIIter;
117+
continue;
118+
}
119+
if (!Visited.insert(Succ->getIndex()).second) {
120+
++SuccBIIter;
121+
continue;
122+
}
123+
Queue.push(Succ->getIndex());
124+
++SuccBIIter;
125+
}
126+
}
127+
128+
const size_t NumReachableBBs = Visited.size();
129+
130+
// Loop through Visited, and sum the corresponding BBs' execution counts
131+
// (ECs).
132+
size_t SumReachableBBEC = 0;
133+
for (const unsigned BBIndex : Visited) {
134+
const BinaryBasicBlock *BB = IndexToBB[BBIndex];
135+
SumReachableBBEC += BB->getKnownExecutionCount();
136+
}
137+
138+
const size_t NumPosECBBsUnreachableFromEntry =
139+
NumPosECBBs - NumReachableBBs;
140+
const size_t SumUnreachableBBEC = SumAllBBEC - SumReachableBBEC;
141+
const double FractionECUnreachable =
142+
(double)SumUnreachableBBEC / SumAllBBEC;
143+
144+
if (opts::Verbosity >= 2 && FractionECUnreachable >= 0.05) {
145+
OS << "Non-trivial CFG discontinuity observed in function "
146+
<< Function->getPrintName() << "\n";
147+
LLVM_DEBUG(Function->dump());
148+
}
149+
150+
NumUnreachables.push_back(NumPosECBBsUnreachableFromEntry);
151+
SumECUnreachables.push_back(SumUnreachableBBEC);
152+
FractionECUnreachables.push_back(FractionECUnreachable);
153+
}
154+
155+
if (FractionECUnreachables.empty())
156+
return;
157+
158+
std::sort(FractionECUnreachables.begin(), FractionECUnreachables.end());
159+
const int Rank = int(FractionECUnreachables.size() * 0.95);
160+
OS << format("top 5%% function CFG discontinuity is %.2lf%%\n",
161+
FractionECUnreachables[Rank] * 100);
162+
163+
if (opts::Verbosity >= 1) {
164+
OS << "abbreviations: EC = execution count, POS BBs = positive EC BBs\n"
165+
<< "distribution of NUM(unreachable POS BBs) among all focal "
166+
"functions\n";
167+
printDistribution(OS, NumUnreachables);
168+
169+
OS << "distribution of SUM_EC(unreachable POS BBs) among all focal "
170+
"functions\n";
171+
printDistribution(OS, SumECUnreachables);
172+
173+
OS << "distribution of [(SUM_EC(unreachable POS BBs) / SUM_EC(all "
174+
"POS BBs))] among all focal functions\n";
175+
printDistribution(OS, FractionECUnreachables, /*Fraction=*/true);
176+
}
177+
}
178+
179+
void printAll(BinaryContext &BC, FunctionListType &ValidFunctions,
180+
size_t NumTopFunctions) {
181+
// Sort the list of functions by execution counts (reverse).
182+
llvm::sort(ValidFunctions,
183+
[&](const BinaryFunction *A, const BinaryFunction *B) {
184+
return A->getKnownExecutionCount() > B->getKnownExecutionCount();
185+
});
186+
187+
const size_t RealNumTopFunctions =
188+
std::min(NumTopFunctions, ValidFunctions.size());
189+
190+
iterator_range<function_iterator> Functions(
191+
ValidFunctions.begin(), ValidFunctions.begin() + RealNumTopFunctions);
192+
193+
BC.outs() << format("BOLT-INFO: among the hottest %zu functions ",
194+
RealNumTopFunctions);
195+
printCFGContinuityStats(BC.outs(), Functions);
196+
197+
// Print more detailed bucketed stats if requested.
198+
if (opts::Verbosity >= 1 && RealNumTopFunctions >= 5) {
199+
const size_t PerBucketSize = RealNumTopFunctions / 5;
200+
BC.outs() << format(
201+
"Detailed stats for 5 buckets, each with %zu functions:\n",
202+
PerBucketSize);
203+
204+
// For each bucket, print the CFG continuity stats of the functions in the
205+
// bucket.
206+
for (size_t BucketIndex = 0; BucketIndex < 5; ++BucketIndex) {
207+
const size_t StartIndex = BucketIndex * PerBucketSize;
208+
const size_t EndIndex = StartIndex + PerBucketSize;
209+
iterator_range<function_iterator> Functions(
210+
ValidFunctions.begin() + StartIndex,
211+
ValidFunctions.begin() + EndIndex);
212+
const size_t MaxFunctionExecutionCount =
213+
ValidFunctions[StartIndex]->getKnownExecutionCount();
214+
const size_t MinFunctionExecutionCount =
215+
ValidFunctions[EndIndex - 1]->getKnownExecutionCount();
216+
BC.outs() << format("----------------\n| Bucket %zu: "
217+
"|\n----------------\n",
218+
BucketIndex + 1)
219+
<< format(
220+
"execution counts of the %zu functions in the bucket: "
221+
"%zu-%zu\n",
222+
EndIndex - StartIndex, MinFunctionExecutionCount,
223+
MaxFunctionExecutionCount);
224+
printCFGContinuityStats(BC.outs(), Functions);
225+
}
226+
}
227+
}
228+
} // namespace
229+
230+
bool PrintContinuityStats::shouldOptimize(const BinaryFunction &BF) const {
231+
if (BF.empty() || !BF.hasValidProfile())
232+
return false;
233+
234+
return BinaryFunctionPass::shouldOptimize(BF);
235+
}
236+
237+
Error PrintContinuityStats::runOnFunctions(BinaryContext &BC) {
238+
// Create a list of functions with valid profiles.
239+
FunctionListType ValidFunctions;
240+
for (const auto &BFI : BC.getBinaryFunctions()) {
241+
const BinaryFunction *Function = &BFI.second;
242+
if (PrintContinuityStats::shouldOptimize(*Function))
243+
ValidFunctions.push_back(Function);
244+
}
245+
if (ValidFunctions.empty() || opts::NumFunctionsForContinuityCheck == 0)
246+
return Error::success();
247+
248+
printAll(BC, ValidFunctions, opts::NumFunctionsForContinuityCheck);
249+
return Error::success();
250+
}

bolt/lib/Passes/FrameOptimizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ FrameOptimization("frame-opt",
4343
cl::ZeroOrMore,
4444
cl::cat(BoltOptCategory));
4545

46-
cl::opt<bool> RemoveStores(
46+
static cl::opt<bool> RemoveStores(
4747
"frame-opt-rm-stores", cl::init(FOP_NONE),
4848
cl::desc("apply additional analysis to remove stores (experimental)"),
4949
cl::cat(BoltOptCategory));

bolt/lib/Passes/PLTCall.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@ namespace opts {
2222

2323
extern cl::OptionCategory BoltOptCategory;
2424

25-
cl::opt<bolt::PLTCall::OptType>
26-
PLT("plt",
27-
cl::desc("optimize PLT calls (requires linking with -znow)"),
28-
cl::init(bolt::PLTCall::OT_NONE),
29-
cl::values(clEnumValN(bolt::PLTCall::OT_NONE,
30-
"none",
31-
"do not optimize PLT calls"),
32-
clEnumValN(bolt::PLTCall::OT_HOT,
33-
"hot",
34-
"optimize executed (hot) PLT calls"),
35-
clEnumValN(bolt::PLTCall::OT_ALL,
36-
"all",
37-
"optimize all PLT calls")),
38-
cl::ZeroOrMore,
39-
cl::cat(BoltOptCategory));
40-
25+
static cl::opt<bolt::PLTCall::OptType>
26+
PLT("plt", cl::desc("optimize PLT calls (requires linking with -znow)"),
27+
cl::init(bolt::PLTCall::OT_NONE),
28+
cl::values(clEnumValN(bolt::PLTCall::OT_NONE, "none",
29+
"do not optimize PLT calls"),
30+
clEnumValN(bolt::PLTCall::OT_HOT, "hot",
31+
"optimize executed (hot) PLT calls"),
32+
clEnumValN(bolt::PLTCall::OT_ALL, "all",
33+
"optimize all PLT calls")),
34+
cl::ZeroOrMore, cl::cat(BoltOptCategory));
4135
}
4236

4337
namespace llvm {

0 commit comments

Comments
 (0)