Skip to content

Commit d05bfa0

Browse files
committed
[IRGen] Lift call to performLLVM out of IRGenRequest
Remove the side-effecting call from IRGenRequest, instead requiring callers to handle the optimization and emission of the LLVM module.
1 parent 408b6fc commit d05bfa0

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

include/swift/Subsystems.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ namespace swift {
204204
std::string>
205205
getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx);
206206

207-
/// Turn the given Swift module into either LLVM IR or native code
208-
/// and return the generated LLVM IR module.
209-
/// If you set an outModuleHash, then you need to call performLLVM.
207+
/// Turn the given Swift module into LLVM IR and return the generated module.
208+
/// To compile and output the generated code, call \c performLLVM.
210209
GeneratedModule
211210
performIRGeneration(ModuleDecl *M, const IRGenOptions &Opts,
212211
const TBDGenOptions &TBDOpts,
@@ -215,9 +214,8 @@ namespace swift {
215214
ArrayRef<std::string> parallelOutputFilenames,
216215
llvm::GlobalVariable **outModuleHash = nullptr);
217216

218-
/// Turn the given Swift file into either LLVM IR or native code
219-
/// and return the generated LLVM IR module.
220-
/// If you set an outModuleHash, then you need to call performLLVM.
217+
/// Turn the given Swift file into LLVM IR and return the generated module.
218+
/// To compile and output the generated code, call \c performLLVM.
221219
GeneratedModule
222220
performIRGeneration(FileUnit *file, const IRGenOptions &Opts,
223221
const TBDGenOptions &TBDOpts,

lib/IRGen/IRGen.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -996,26 +996,18 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
996996
if (Ctx.hadError()) return GeneratedModule::null();
997997

998998
// Free the memory occupied by the SILModule.
999-
// Execute this task in parallel to the LLVM compilation.
999+
// Execute this task in parallel to the embedding of bitcode.
10001000
auto SILModuleRelease = [&SILMod]() { SILMod.reset(nullptr); };
10011001
auto Thread = std::thread(SILModuleRelease);
10021002
// Wait for the thread to terminate.
10031003
SWIFT_DEFER { Thread.join(); };
10041004

10051005
embedBitcode(IGM.getModule(), Opts);
10061006

1007+
// TODO: Turn the module hash into an actual output.
10071008
if (auto **outModuleHash = desc.outModuleHash) {
10081009
*outModuleHash = IGM.ModuleHash;
1009-
} else {
1010-
FrontendStatsTracer tracer(Ctx.Stats, "LLVM pipeline");
1011-
1012-
// Since no out module hash was set, we need to performLLVM.
1013-
if (performLLVM(Opts, IGM.Context.Diags, nullptr, IGM.ModuleHash,
1014-
IGM.getModule(), IGM.TargetMachine.get(),
1015-
IGM.OutputFilename, IGM.Context.Stats))
1016-
return GeneratedModule::null();
10171010
}
1018-
10191011
return std::move(IGM).intoGeneratedModule();
10201012
}
10211013

lib/Immediate/Immediate.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ int swift::RunImmediately(CompilerInstance &CI,
196196
const IRGenOptions &IRGenOpts,
197197
const SILOptions &SILOpts,
198198
std::unique_ptr<SILModule> &&SM) {
199+
// TODO: Use OptimizedIRRequest for this.
199200
ASTContext &Context = CI.getASTContext();
200201

201202
// IRGen the main module.
@@ -211,6 +212,13 @@ int swift::RunImmediately(CompilerInstance &CI,
211212

212213
assert(GenModule && "Emitted no diagnostics but IR generation failed?");
213214

215+
performLLVM(IRGenOpts, Context.Diags, /*diagMutex*/ nullptr, /*hash*/ nullptr,
216+
GenModule.getModule(), GenModule.getTargetMachine(),
217+
PSPs.OutputFilename, Context.Stats);
218+
219+
if (Context.hadError())
220+
return -1;
221+
214222
// Load libSwiftCore to setup process arguments.
215223
//
216224
// This must be done here, before any library loading has been done, to avoid

tools/sil-llvm-gen/SILLLVMGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,11 @@ int main(int argc, char **argv) {
201201
std::move(SILMod),
202202
CI.getMainModule()->getName().str(), PSPs,
203203
ArrayRef<std::string>());
204+
if (!Mod)
205+
return 1;
206+
207+
performLLVM(Opts, CI.getDiags(), /*diagMutex*/ nullptr, /*hash*/ nullptr,
208+
Mod.getModule(), Mod.getTargetMachine(), OutputFilename,
209+
CI.getASTContext().Stats);
204210
return CI.getASTContext().hadError();
205211
}

0 commit comments

Comments
 (0)