Skip to content

Commit 3cd9f16

Browse files
authored
Don't spend time initializing LLVM when running the driver (swiftlang#14896)
Tiny start-up time optimization noticed while looking at how we do PrettyStackTraceProgram. Also add PrettyStackTraceProgram to a few more of our testing tools, via the new PROGRAM_START macro.
1 parent b65932d commit 3cd9f16

File tree

17 files changed

+48
-36
lines changed

17 files changed

+48
-36
lines changed

include/swift/Basic/LLVMInitialize.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// A file that declares a macro for initializing all parts of LLVM that various
14-
// binaries in swift use. Please call the macro in the main routine of all
15-
// binaries.
13+
// A file that declares macros for initializing all parts of LLVM that various
14+
// binaries in swift use. Please call PROGRAM_START in the main routine of all
15+
// binaries, and INITIALIZE_LLVM in anything that uses Clang or LLVM IR.
1616
//
1717
//===----------------------------------------------------------------------===//
1818

1919
#ifndef SWIFT_BASIC_LLVMINITIALIZE_H
2020
#define SWIFT_BASIC_LLVMINITIALIZE_H
2121

22-
#include "llvm/Support/FileSystem.h"
23-
#include "llvm/Support/Host.h"
2422
#include "llvm/Support/ManagedStatic.h"
25-
#include "llvm/Support/Path.h"
2623
#include "llvm/Support/PrettyStackTrace.h"
27-
#include "llvm/Support/Process.h"
2824
#include "llvm/Support/Signals.h"
2925
#include "llvm/Support/TargetSelect.h"
30-
#include "llvm/Support/raw_ostream.h"
3126

32-
#define INITIALIZE_LLVM(argc, argv) \
27+
#define PROGRAM_START(argc, argv) \
3328
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); \
3429
llvm::PrettyStackTraceProgram _INITIALIZE_LLVM_STACKTRACE(argc, argv); \
35-
llvm::llvm_shutdown_obj _INITIALIZE_LLVM_SHUTDOWN_OBJ; \
36-
llvm::InitializeAllTargets(); \
37-
llvm::InitializeAllTargetMCs(); \
38-
llvm::InitializeAllAsmPrinters(); \
39-
llvm::InitializeAllAsmParsers(); \
40-
llvm::InitializeAllDisassemblers(); \
41-
llvm::InitializeAllTargetInfos();
30+
llvm::llvm_shutdown_obj _INITIALIZE_LLVM_SHUTDOWN_OBJ
31+
32+
#define INITIALIZE_LLVM() \
33+
do { \
34+
llvm::InitializeAllTargets(); \
35+
llvm::InitializeAllTargetMCs(); \
36+
llvm::InitializeAllAsmPrinters(); \
37+
llvm::InitializeAllAsmParsers(); \
38+
llvm::InitializeAllDisassemblers(); \
39+
llvm::InitializeAllTargetInfos(); \
40+
} while (0)
4241

4342
#endif // SWIFT_BASIC_LLVMINITIALIZE_H

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/Basic/FileSystem.h"
4141
#include "swift/Basic/JSONSerialization.h"
4242
#include "swift/Basic/LLVMContext.h"
43+
#include "swift/Basic/LLVMInitialize.h"
4344
#include "swift/Basic/SourceManager.h"
4445
#include "swift/Basic/Statistic.h"
4546
#include "swift/Basic/Timer.h"
@@ -1511,10 +1512,7 @@ computeStatsReporter(const CompilerInvocation &Invocation, CompilerInstance *Ins
15111512
int swift::performFrontend(ArrayRef<const char *> Args,
15121513
const char *Argv0, void *MainAddr,
15131514
FrontendObserver *observer) {
1514-
llvm::InitializeAllTargets();
1515-
llvm::InitializeAllTargetMCs();
1516-
llvm::InitializeAllAsmPrinters();
1517-
llvm::InitializeAllAsmParsers();
1515+
INITIALIZE_LLVM();
15181516

15191517
PrintingDiagnosticConsumer PDC;
15201518

tools/driver/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static bool shouldRunAsSubcommand(StringRef ExecName,
110110
extern int apinotes_main(ArrayRef<const char *> Args);
111111

112112
int main(int argc_, const char **argv_) {
113-
INITIALIZE_LLVM(argc_, argv_);
113+
PROGRAM_START(argc_, argv_);
114114

115115
SmallVector<const char *, 256> argv;
116116
llvm::SpecificBumpPtrAllocator<char> ArgAllocator;

tools/driver/modulewrap_main.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//===----------------------------------------------------------------------===//
1919

2020
#include "swift/AST/DiagnosticsFrontend.h"
21+
#include "swift/Basic/LLVMInitialize.h"
2122
#include "swift/Frontend/Frontend.h"
2223
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2324
#include "swift/Option/Options.h"
@@ -113,10 +114,7 @@ class ModuleWrapInvocation {
113114

114115
int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
115116
void *MainAddr) {
116-
llvm::InitializeAllTargets();
117-
llvm::InitializeAllTargetMCs();
118-
llvm::InitializeAllAsmPrinters();
119-
llvm::InitializeAllAsmParsers();
117+
INITIALIZE_LLVM();
120118

121119
CompilerInstance Instance;
122120
PrintingDiagnosticConsumer PDC;

tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static void printValidationInfo(llvm::StringRef data) {
6262
}
6363

6464
int main(int argc, char **argv) {
65-
INITIALIZE_LLVM(argc, argv);
65+
PROGRAM_START(argc, argv);
66+
INITIALIZE_LLVM();
6667

6768
// Command line handling.
6869
llvm::cl::list<std::string> InputNames(

tools/sil-func-extractor/SILFunctionExtractor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ void removeUnwantedFunctions(SILModule *M, ArrayRef<std::string> MangledNames,
225225
}
226226

227227
int main(int argc, char **argv) {
228-
INITIALIZE_LLVM(argc, argv);
228+
PROGRAM_START(argc, argv);
229+
INITIALIZE_LLVM();
229230

230231
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift SIL Extractor\n");
231232

tools/sil-llvm-gen/SILLLVMGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ static llvm::cl::opt<IRGenOutputKind>
113113
void anchorForGetMainExecutable() {}
114114

115115
int main(int argc, char **argv) {
116-
INITIALIZE_LLVM(argc, argv);
116+
PROGRAM_START(argc, argv);
117+
INITIALIZE_LLVM();
117118

118119
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift LLVM IR Generator\n");
119120

tools/sil-nm/SILNM.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ static void nmModule(SILModule *M) {
139139
}
140140

141141
int main(int argc, char **argv) {
142-
INITIALIZE_LLVM(argc, argv);
142+
PROGRAM_START(argc, argv);
143+
INITIALIZE_LLVM();
143144

144145
llvm::cl::ParseCommandLineOptions(argc, argv, "SIL NM\n");
145146

tools/sil-opt/SILOpt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ static void runCommandLineSelectedPasses(SILModule *Module,
264264
void anchorForGetMainExecutable() {}
265265

266266
int main(int argc, char **argv) {
267-
INITIALIZE_LLVM(argc, argv);
267+
PROGRAM_START(argc, argv);
268+
INITIALIZE_LLVM();
268269

269270
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift SIL optimizer\n");
270271

tools/sil-passpipeline-dumper/SILPassPipelineDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, PassPipelineKind Kind) {
4444
} // namespace llvm
4545

4646
int main(int argc, char **argv) {
47-
INITIALIZE_LLVM(argc, argv);
47+
PROGRAM_START(argc, argv);
48+
INITIALIZE_LLVM();
4849

4950
llvm::cl::ParseCommandLineOptions(argc, argv,
5051
"Swift SIL Pass Pipeline Dumper\n");

0 commit comments

Comments
 (0)