Skip to content

Commit 9dcd2b5

Browse files
David UngarCodaFi
authored andcommitted
Allocate LLVMContext in IRGenModule.
1 parent 69f95a2 commit 9dcd2b5

File tree

14 files changed

+61
-64
lines changed

14 files changed

+61
-64
lines changed

include/swift/AST/IRGenRequests.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct IRGenDescriptor {
4949
StringRef ModuleName;
5050
const PrimarySpecificPaths &PSPs;
5151
StringRef PrivateDiscriminator;
52-
llvm::LLVMContext &LLVMContext;
5352
ArrayRef<std::string> parallelOutputFilenames;
5453
llvm::GlobalVariable **outModuleHash;
5554
llvm::StringSet<> *LinkerDirectives;
@@ -73,15 +72,14 @@ struct IRGenDescriptor {
7372
forFile(const IRGenOptions &Opts, SourceFile &SF,
7473
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
7574
const PrimarySpecificPaths &PSPs, StringRef PrivateDiscriminator,
76-
llvm::LLVMContext &LLVMContext, llvm::GlobalVariable **outModuleHash,
75+
llvm::GlobalVariable **outModuleHash,
7776
llvm::StringSet<> *LinkerDirectives) {
7877
return IRGenDescriptor{Opts,
7978
&SF,
8079
SILMod.release(),
8180
ModuleName,
8281
PSPs,
8382
PrivateDiscriminator,
84-
LLVMContext,
8583
{},
8684
outModuleHash,
8785
LinkerDirectives};
@@ -91,7 +89,6 @@ struct IRGenDescriptor {
9189
forWholeModule(const IRGenOptions &Opts, swift::ModuleDecl *M,
9290
std::unique_ptr<SILModule> &&SILMod, StringRef ModuleName,
9391
const PrimarySpecificPaths &PSPs,
94-
llvm::LLVMContext &LLVMContext,
9592
ArrayRef<std::string> parallelOutputFilenames,
9693
llvm::GlobalVariable **outModuleHash,
9794
llvm::StringSet<> *LinkerDirectives) {
@@ -101,7 +98,6 @@ struct IRGenDescriptor {
10198
ModuleName,
10299
PSPs,
103100
"",
104-
LLVMContext,
105101
parallelOutputFilenames,
106102
outModuleHash,
107103
LinkerDirectives};

include/swift/IRGen/IRGenPublic.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class IRGenModule;
3131
std::pair<IRGenerator *, IRGenModule *>
3232
createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
3333
StringRef MainInputFilenameForDebugInfo,
34-
StringRef PrivateDiscriminator,
35-
llvm::LLVMContext &LLVMContext);
34+
StringRef PrivateDiscriminator);
3635

3736
/// Delete the IRGenModule and IRGenerator obtained by the above call.
3837
void deleteIRGenModule(std::pair<IRGenerator *, IRGenModule *> &Module);

include/swift/Subsystems.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ namespace swift {
234234
performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
235235
std::unique_ptr<SILModule> SILMod,
236236
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
237-
llvm::LLVMContext &LLVMContext,
238237
ArrayRef<std::string> parallelOutputFilenames,
239238
llvm::GlobalVariable **outModuleHash = nullptr,
240239
llvm::StringSet<> *LinkerDirectives = nullptr);
@@ -247,7 +246,6 @@ namespace swift {
247246
std::unique_ptr<SILModule> SILMod,
248247
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
249248
StringRef PrivateDiscriminator,
250-
llvm::LLVMContext &LLVMContext,
251249
llvm::GlobalVariable **outModuleHash = nullptr,
252250
llvm::StringSet<> *LinkerDirectives = nullptr);
253251

@@ -289,9 +287,7 @@ namespace swift {
289287
UnifiedStatsReporter *Stats);
290288

291289
/// Dump YAML describing all fixed-size types imported from the given module.
292-
bool performDumpTypeInfo(const IRGenOptions &Opts,
293-
SILModule &SILMod,
294-
llvm::LLVMContext &LLVMContext);
290+
bool performDumpTypeInfo(const IRGenOptions &Opts, SILModule &SILMod);
295291

296292
/// Creates a TargetMachine from the IRGen opts and AST Context.
297293
std::unique_ptr<llvm::TargetMachine>

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,18 +1384,15 @@ static void generateIR(const IRGenOptions &IRGenOpts,
13841384
llvm::GlobalVariable *&HashGlobal,
13851385
ArrayRef<std::string> parallelOutputFilenames,
13861386
llvm::StringSet<> &LinkerDirectives) {
1387-
// FIXME: We shouldn't need to use the global context here, but
1388-
// something is persisting across calls to performIRGeneration.
1389-
auto &LLVMContext = getGlobalLLVMContext();
13901387
IRModule = MSF.is<SourceFile *>()
13911388
? performIRGeneration(IRGenOpts, *MSF.get<SourceFile *>(),
13921389
std::move(SM), OutputFilename, PSPs,
13931390
MSF.get<SourceFile *>()->getPrivateDiscriminator().str(),
1394-
LLVMContext, &HashGlobal,
1391+
&HashGlobal,
13951392
&LinkerDirectives)
13961393
: performIRGeneration(IRGenOpts, MSF.get<ModuleDecl *>(),
13971394
std::move(SM), OutputFilename, PSPs,
1398-
LLVMContext, parallelOutputFilenames,
1395+
parallelOutputFilenames,
13991396
&HashGlobal, &LinkerDirectives);
14001397
}
14011398

@@ -1663,7 +1660,7 @@ static bool performCompileStepsPostSILGen(
16631660
Stats->flushTracesAndProfiles();
16641661

16651662
if (Action == FrontendOptions::ActionType::DumpTypeInfo)
1666-
return performDumpTypeInfo(IRGenOpts, *SM, getGlobalLLVMContext());
1663+
return performDumpTypeInfo(IRGenOpts, *SM);
16671664

16681665
if (Action == FrontendOptions::ActionType::Immediate)
16691666
return processCommandLineAndRunImmediately(

lib/IRGen/IRGen.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,7 @@ static void initLLVMModule(const IRGenModule &IGM, ModuleDecl &M) {
913913
std::pair<IRGenerator *, IRGenModule *>
914914
swift::irgen::createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
915915
StringRef MainInputFilenameForDebugInfo,
916-
StringRef PrivateDiscriminator,
917-
llvm::LLVMContext &LLVMContext) {
916+
StringRef PrivateDiscriminator) {
918917

919918
IRGenOptions Opts;
920919
IRGenerator *irgen = new IRGenerator(Opts, *SILMod);
@@ -923,10 +922,9 @@ swift::irgen::createIRGenModule(SILModule *SILMod, StringRef OutputFilename,
923922
return std::make_pair(nullptr, nullptr);
924923

925924
// Create the IR emitter.
926-
IRGenModule *IGM =
927-
new IRGenModule(*irgen, std::move(targetMachine), nullptr, LLVMContext,
928-
"", OutputFilename, MainInputFilenameForDebugInfo,
929-
PrivateDiscriminator);
925+
IRGenModule *IGM = new IRGenModule(
926+
*irgen, std::move(targetMachine), nullptr, "", OutputFilename,
927+
MainInputFilenameForDebugInfo, PrivateDiscriminator);
930928

931929
initLLVMModule(*IGM, *SILMod->getSwiftModule());
932930

@@ -955,7 +953,7 @@ performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
955953
std::unique_ptr<SILModule> SILMod, StringRef ModuleName,
956954
const PrimarySpecificPaths &PSPs,
957955
StringRef PrivateDiscriminator,
958-
llvm::LLVMContext &LLVMContext, SourceFile *SF = nullptr,
956+
SourceFile *SF = nullptr,
959957
llvm::GlobalVariable **outModuleHash = nullptr,
960958
llvm::StringSet<> *linkerDirectives = nullptr) {
961959
auto &Ctx = M->getASTContext();
@@ -967,7 +965,7 @@ performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
967965
if (!targetMachine) return nullptr;
968966

969967
// Create the IR emitter.
970-
IRGenModule IGM(irgen, std::move(targetMachine), SF, LLVMContext, ModuleName,
968+
IRGenModule IGM(irgen, std::move(targetMachine), SF, ModuleName,
971969
PSPs.OutputFilename, PSPs.MainInputFilenameForDebugInfo,
972970
PrivateDiscriminator);
973971

@@ -1214,13 +1212,9 @@ static void performParallelIRGeneration(
12141212
auto targetMachine = irgen.createTargetMachine();
12151213
if (!targetMachine) continue;
12161214

1217-
// This (and the IGM itself) will get deleted by the IGMDeleter
1218-
// as long as the IGM is registered with the IRGenerator.
1219-
auto Context = new LLVMContext();
1220-
12211215
// Create the IR emitter.
12221216
IRGenModule *IGM =
1223-
new IRGenModule(irgen, std::move(targetMachine), nextSF, *Context,
1217+
new IRGenModule(irgen, std::move(targetMachine), nextSF,
12241218
ModuleName, *OutputIter++, nextSF->getFilename(),
12251219
nextSF->getPrivateDiscriminator().str());
12261220
IGMcreated = true;
@@ -1370,11 +1364,11 @@ static void performParallelIRGeneration(
13701364
std::unique_ptr<llvm::Module> swift::performIRGeneration(
13711365
const IRGenOptions &Opts, swift::ModuleDecl *M,
13721366
std::unique_ptr<SILModule> SILMod, StringRef ModuleName,
1373-
const PrimarySpecificPaths &PSPs, llvm::LLVMContext &LLVMContext,
1367+
const PrimarySpecificPaths &PSPs,
13741368
ArrayRef<std::string> parallelOutputFilenames,
13751369
llvm::GlobalVariable **outModuleHash, llvm::StringSet<> *LinkerDirectives) {
13761370
auto desc = IRGenDescriptor::forWholeModule(
1377-
Opts, M, std::move(SILMod), ModuleName, PSPs, LLVMContext,
1371+
Opts, M, std::move(SILMod), ModuleName, PSPs,
13781372
parallelOutputFilenames, outModuleHash, LinkerDirectives);
13791373
return llvm::cantFail(
13801374
M->getASTContext().evaluator(IRGenWholeModuleRequest{desc}));
@@ -1396,7 +1390,7 @@ IRGenWholeModuleRequest::evaluate(Evaluator &evaluator,
13961390
}
13971391
return ::performIRGeneration(
13981392
desc.Opts, M, std::unique_ptr<SILModule>(desc.SILMod), desc.ModuleName,
1399-
desc.PSPs, "", desc.LLVMContext, nullptr, desc.outModuleHash,
1393+
desc.PSPs, "", nullptr, desc.outModuleHash,
14001394
desc.LinkerDirectives);
14011395
}
14021396

@@ -1405,11 +1399,10 @@ performIRGeneration(const IRGenOptions &Opts, SourceFile &SF,
14051399
std::unique_ptr<SILModule> SILMod,
14061400
StringRef ModuleName, const PrimarySpecificPaths &PSPs,
14071401
StringRef PrivateDiscriminator,
1408-
llvm::LLVMContext &LLVMContext,
14091402
llvm::GlobalVariable **outModuleHash,
14101403
llvm::StringSet<> *LinkerDirectives) {
14111404
auto desc = IRGenDescriptor::forFile(Opts, SF, std::move(SILMod), ModuleName,
1412-
PSPs, PrivateDiscriminator, LLVMContext,
1405+
PSPs, PrivateDiscriminator,
14131406
outModuleHash, LinkerDirectives);
14141407
return llvm::cantFail(
14151408
SF.getASTContext().evaluator(IRGenSourceFileRequest{desc}));
@@ -1421,15 +1414,13 @@ IRGenSourceFileRequest::evaluate(Evaluator &evaluator,
14211414
auto *SF = desc.Ctx.get<SourceFile *>();
14221415
return ::performIRGeneration(
14231416
desc.Opts, SF->getParentModule(), std::unique_ptr<SILModule>(desc.SILMod),
1424-
desc.ModuleName, desc.PSPs, desc.PrivateDiscriminator, desc.LLVMContext,
1417+
desc.ModuleName, desc.PSPs, desc.PrivateDiscriminator,
14251418
SF, desc.outModuleHash, desc.LinkerDirectives);
14261419
}
14271420

14281421
void
14291422
swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
14301423
StringRef OutputPath) {
1431-
LLVMContext VMContext;
1432-
14331424
auto &Ctx = SILMod.getASTContext();
14341425
assert(!Ctx.hadError());
14351426

@@ -1440,12 +1431,13 @@ swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
14401431
auto targetMachine = irgen.createTargetMachine();
14411432
if (!targetMachine) return;
14421433

1443-
IRGenModule IGM(irgen, std::move(targetMachine), nullptr, VMContext,
1434+
IRGenModule IGM(irgen, std::move(targetMachine), nullptr,
14441435
OutputPath, OutputPath, "", "");
14451436
initLLVMModule(IGM, *SILMod.getSwiftModule());
14461437
auto *Ty = llvm::ArrayType::get(IGM.Int8Ty, Buffer.size());
14471438
auto *Data =
1448-
llvm::ConstantDataArray::getString(VMContext, Buffer, /*AddNull=*/false);
1439+
llvm::ConstantDataArray::getString(IGM.getLLVMContext(),
1440+
Buffer, /*AddNull=*/false);
14491441
auto &M = *IGM.getModule();
14501442
auto *ASTSym = new llvm::GlobalVariable(M, Ty, /*constant*/ true,
14511443
llvm::GlobalVariable::InternalLinkage,

lib/IRGen/IRGenModule.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,18 @@ static void sanityCheckStdlib(IRGenModule &IGM) {
192192

193193
IRGenModule::IRGenModule(IRGenerator &irgen,
194194
std::unique_ptr<llvm::TargetMachine> &&target,
195-
SourceFile *SF, llvm::LLVMContext &LLVMContext,
195+
SourceFile *SF,
196196
StringRef ModuleName, StringRef OutputFilename,
197197
StringRef MainInputFilenameForDebugInfo,
198198
StringRef PrivateDiscriminator)
199199
: IRGen(irgen), Context(irgen.SIL.getASTContext()),
200-
ClangCodeGen(createClangCodeGenerator(Context, LLVMContext, irgen.Opts,
200+
// The LLVMContext (and the IGM itself) will get deleted by the IGMDeleter
201+
// as long as the IGM is registered with the IRGenerator.
202+
ClangCodeGen(createClangCodeGenerator(Context, *(new llvm::LLVMContext()),
203+
irgen.Opts,
201204
ModuleName, PrivateDiscriminator)),
202-
Module(*ClangCodeGen->GetModule()), LLVMContext(Module.getContext()),
205+
Module(*ClangCodeGen->GetModule()),
206+
LLVMContext(Module.getContext()),
203207
DataLayout(irgen.getClangDataLayout()),
204208
Triple(irgen.getEffectiveClangTriple()), TargetMachine(std::move(target)),
205209
silConv(irgen.SIL), OutputFilename(OutputFilename),

lib/IRGen/IRGenModule.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,15 +1273,14 @@ private: \
12731273
/// The \p SF is the source file for which the llvm module is generated when
12741274
/// doing multi-threaded whole-module compilation. Otherwise it is null.
12751275
IRGenModule(IRGenerator &irgen, std::unique_ptr<llvm::TargetMachine> &&target,
1276-
SourceFile *SF, llvm::LLVMContext &LLVMContext,
1276+
SourceFile *SF,
12771277
StringRef ModuleName, StringRef OutputFilename,
12781278
StringRef MainInputFilenameForDebugInfo,
12791279
StringRef PrivateDiscriminator);
12801280

12811281
/// The constructor used when we just need an IRGenModule for type lowering.
1282-
IRGenModule(IRGenerator &irgen, std::unique_ptr<llvm::TargetMachine> &&target,
1283-
llvm::LLVMContext &LLVMContext)
1284-
: IRGenModule(irgen, std::move(target), /*SF=*/nullptr, LLVMContext,
1282+
IRGenModule(IRGenerator &irgen, std::unique_ptr<llvm::TargetMachine> &&target)
1283+
: IRGenModule(irgen, std::move(target), /*SF=*/nullptr,
12851284
"<fake module name>", "<fake output filename>",
12861285
"<fake main input filename>", "") {}
12871286

lib/IRGen/TypeLayoutDumper.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,13 @@ void TypeLayoutDumper::write(ArrayRef<ModuleDecl *> AllModules,
152152
}
153153
}
154154

155-
bool swift::performDumpTypeInfo(const IRGenOptions &Opts,
156-
SILModule &SILMod,
157-
llvm::LLVMContext &LLVMContext) {
155+
bool swift::performDumpTypeInfo(const IRGenOptions &Opts, SILModule &SILMod) {
158156
auto &Ctx = SILMod.getASTContext();
159157
assert(!Ctx.hadError());
160158
(void)Ctx;
161159

162160
IRGenerator IRGen(Opts, SILMod);
163-
IRGenModule IGM(IRGen, IRGen.createTargetMachine(), LLVMContext);
161+
IRGenModule IGM(IRGen, IRGen.createTargetMachine());
164162

165163
// We want to bypass resilience.
166164
LoweringModeScope scope(IGM, TypeConverter::Mode::CompletelyFragile);

lib/Immediate/Immediate.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,9 @@ int swift::RunImmediately(CompilerInstance &CI,
246246
// IRGen the main module.
247247
auto *swiftModule = CI.getMainModule();
248248
const auto PSPs = CI.getPrimarySpecificPathsForAtMostOnePrimary();
249-
// FIXME: We shouldn't need to use the global context here, but
250-
// something is persisting across calls to performIRGeneration.
251-
auto ModuleCtx = std::make_unique<llvm::LLVMContext>();
252249
auto Module = performIRGeneration(
253250
IRGenOpts, swiftModule, std::move(SM), swiftModule->getName().str(),
254-
PSPs, *ModuleCtx, ArrayRef<std::string>());
251+
PSPs, ArrayRef<std::string>());
255252

256253
if (Context.hadError())
257254
return -1;
@@ -341,6 +338,7 @@ int swift::RunImmediately(CompilerInstance &CI,
341338
Module->dump());
342339

343340
{
341+
std::unique_ptr<llvm::LLVMContext> ModuleCtx(&Module->getContext());
344342
auto TSM = llvm::orc::ThreadSafeModule(std::move(Module), std::move(ModuleCtx));
345343
if (auto Err = JIT->addIRModule(std::move(TSM))) {
346344
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "");

lib/Immediate/REPL.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,9 @@ class REPLEnvironment {
891891
DumpSource += Line;
892892

893893
// IRGen the current line(s).
894-
// FIXME: We shouldn't need to use the global context here, but
895-
// something is persisting across calls to performIRGeneration.
896894
auto LineModule = performIRGeneration(
897895
IRGenOpts, M, std::move(sil), "REPLLine", PrimarySpecificPaths(),
898-
getGlobalLLVMContext(), /*parallelOutputFilenames*/{});
896+
/*parallelOutputFilenames*/{});
899897

900898
if (CI.getASTContext().hadError())
901899
return false;

0 commit comments

Comments
 (0)