Skip to content

Commit fe925af

Browse files
jurahulLukacma
authored andcommitted
[NFC][LLVM] Code cleanup in opt (llvm#164077)
- Use namespace qualifiers to define variables declared in `llvm` namespace. - move file local `TimeTracerRAII` struct into anonymous namespace. - Use explicit types in a few places. - Convert the loop over `PassList` to a range for loop.
1 parent 1ec5b75 commit fe925af

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,30 @@
4545
using namespace llvm;
4646
using namespace opt_tool;
4747

48-
namespace llvm {
49-
cl::opt<bool> DebugifyEach(
48+
cl::opt<bool> llvm::DebugifyEach(
5049
"debugify-each",
5150
cl::desc("Start each pass with debugify and end it with check-debugify"));
5251

53-
cl::opt<std::string>
54-
DebugifyExport("debugify-export",
55-
cl::desc("Export per-pass debugify statistics to this file"),
56-
cl::value_desc("filename"));
52+
cl::opt<std::string> llvm::DebugifyExport(
53+
"debugify-export",
54+
cl::desc("Export per-pass debugify statistics to this file"),
55+
cl::value_desc("filename"));
5756

58-
cl::opt<bool> VerifyEachDebugInfoPreserve(
57+
cl::opt<bool> llvm::VerifyEachDebugInfoPreserve(
5958
"verify-each-debuginfo-preserve",
6059
cl::desc("Start each pass with collecting and end it with checking of "
6160
"debug info preservation."));
6261

62+
cl::opt<std::string> llvm::VerifyDIPreserveExport(
63+
"verify-di-preserve-export",
64+
cl::desc("Export debug info preservation failures into "
65+
"specified (JSON) file (should be abs path as we use"
66+
" append mode to insert new JSON objects)"),
67+
cl::value_desc("filename"), cl::init(""));
68+
6369
static cl::opt<bool> EnableLoopFusion("enable-loopfusion", cl::init(false),
6470
cl::Hidden,
6571
cl::desc("Enable the LoopFuse Pass"));
66-
cl::opt<std::string>
67-
VerifyDIPreserveExport("verify-di-preserve-export",
68-
cl::desc("Export debug info preservation failures into "
69-
"specified (JSON) file (should be abs path as we use"
70-
" append mode to insert new JSON objects)"),
71-
cl::value_desc("filename"), cl::init(""));
72-
73-
} // namespace llvm
7472

7573
enum class DebugLogging { None, Normal, Verbose, Quiet };
7674

@@ -356,7 +354,7 @@ bool llvm::runPassPipeline(
356354
ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut,
357355
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
358356
ArrayRef<PassPlugin> PassPlugins,
359-
ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks,
357+
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks,
360358
OutputKind OK, VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder,
361359
bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,
362360
bool EmitModuleHash, bool EnableDebugify, bool VerifyDIPreserve,

llvm/tools/opt/NewPMDriver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ enum PGOKind {
5252
SampleUse
5353
};
5454
enum CSPGOKind { NoCSPGO, CSInstrGen, CSInstrUse };
55-
}
55+
} // namespace opt_tool
5656

5757
void printPasses(raw_ostream &OS);
5858

@@ -70,7 +70,7 @@ bool runPassPipeline(
7070
ToolOutputFile *Out, ToolOutputFile *ThinLinkOut,
7171
ToolOutputFile *OptRemarkFile, StringRef PassPipeline,
7272
ArrayRef<PassPlugin> PassPlugins,
73-
ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks,
73+
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks,
7474
opt_tool::OutputKind OK, opt_tool::VerifierKind VK,
7575
bool ShouldPreserveAssemblyUseListOrder,
7676
bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex,

llvm/tools/opt/opt.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
#include "llvm/ADT/ArrayRef.h"
1515
#include <functional>
1616

17+
using namespace llvm;
18+
1719
namespace llvm {
1820
class PassBuilder;
1921
}
2022

21-
extern "C" int optMain(int argc, char **argv,
22-
llvm::ArrayRef<std::function<void(llvm::PassBuilder &)>>
23-
PassBuilderCallbacks);
23+
extern "C" int
24+
optMain(int argc, char **argv,
25+
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks);
2426

2527
int main(int argc, char **argv) { return optMain(argc, argv, {}); }

llvm/tools/opt/optdriver.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,25 @@ static CodeGenOptLevel GetCodeGenOptLevel() {
296296
return static_cast<CodeGenOptLevel>(unsigned(CodeGenOptLevelCL));
297297
}
298298

299+
namespace {
299300
struct TimeTracerRAII {
300301
TimeTracerRAII(StringRef ProgramName) {
301302
if (TimeTrace)
302303
timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
303304
}
304305
~TimeTracerRAII() {
305-
if (TimeTrace) {
306-
if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
307-
handleAllErrors(std::move(E), [&](const StringError &SE) {
308-
errs() << SE.getMessage() << "\n";
309-
});
310-
return;
311-
}
312-
timeTraceProfilerCleanup();
306+
if (!TimeTrace)
307+
return;
308+
if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
309+
handleAllErrors(std::move(E), [&](const StringError &SE) {
310+
errs() << SE.getMessage() << "\n";
311+
});
312+
return;
313313
}
314+
timeTraceProfilerCleanup();
314315
}
315316
};
317+
} // namespace
316318

317319
// For use in NPM transition. Currently this contains most codegen-specific
318320
// passes. Remove passes from here when porting to the NPM.
@@ -377,18 +379,18 @@ static bool shouldPinPassToLegacyPM(StringRef Pass) {
377379
"callbrprepare",
378380
"scalarizer",
379381
};
380-
for (const auto &P : PassNamePrefix)
382+
for (StringLiteral P : PassNamePrefix)
381383
if (Pass.starts_with(P))
382384
return true;
383-
for (const auto &P : PassNameContain)
385+
for (StringLiteral P : PassNameContain)
384386
if (Pass.contains(P))
385387
return true;
386388
return llvm::is_contained(PassNameExact, Pass);
387389
}
388390

389391
// For use in NPM transition.
390392
static bool shouldForceLegacyPM() {
391-
for (const auto &P : PassList) {
393+
for (const PassInfo *P : PassList) {
392394
StringRef Arg = P->getPassArgument();
393395
if (shouldPinPassToLegacyPM(Arg))
394396
return true;
@@ -399,9 +401,9 @@ static bool shouldForceLegacyPM() {
399401
//===----------------------------------------------------------------------===//
400402
// main for opt
401403
//
402-
extern "C" int optMain(
403-
int argc, char **argv,
404-
ArrayRef<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks) {
404+
extern "C" int
405+
optMain(int argc, char **argv,
406+
ArrayRef<std::function<void(PassBuilder &)>> PassBuilderCallbacks) {
405407
InitLLVM X(argc, argv);
406408

407409
// Enable debug stream buffering.
@@ -673,7 +675,7 @@ extern "C" int optMain(
673675
else {
674676
// Disable individual builtin functions in TargetLibraryInfo.
675677
LibFunc F;
676-
for (auto &FuncName : DisableBuiltins) {
678+
for (const std::string &FuncName : DisableBuiltins) {
677679
if (TLII.getLibFunc(FuncName, F))
678680
TLII.setUnavailable(F);
679681
else {
@@ -683,7 +685,7 @@ extern "C" int optMain(
683685
}
684686
}
685687

686-
for (auto &FuncName : EnableBuiltins) {
688+
for (const std::string &FuncName : EnableBuiltins) {
687689
if (TLII.getLibFunc(FuncName, F))
688690
TLII.setAvailable(F);
689691
else {
@@ -814,9 +816,8 @@ extern "C" int optMain(
814816
Passes.add(TPC);
815817
}
816818

817-
// Create a new optimization pass for each one specified on the command line
818-
for (unsigned i = 0; i < PassList.size(); ++i) {
819-
const PassInfo *PassInf = PassList[i];
819+
// Create a new optimization pass for each one specified on the command line.
820+
for (const PassInfo *PassInf : PassList) {
820821
if (PassInf->getNormalCtor()) {
821822
Pass *P = PassInf->getNormalCtor()();
822823
if (P) {
@@ -826,9 +827,10 @@ extern "C" int optMain(
826827
if (VerifyEach)
827828
Passes.add(createVerifierPass());
828829
}
829-
} else
830+
} else {
830831
errs() << argv[0] << ": cannot create pass: " << PassInf->getPassName()
831832
<< "\n";
833+
}
832834
}
833835

834836
// Check that the module is well formed on completion of optimization

0 commit comments

Comments
 (0)