15
15
//
16
16
// ===----------------------------------------------------------------------===//
17
17
18
- #define DEBUG_TYPE " swift-immediate"
19
18
#include " swift/Immediate/Immediate.h"
20
19
#include " ImmediateImpl.h"
21
20
31
30
#include " swift/SILOptimizer/PassManager/Passes.h"
32
31
#include " llvm/ADT/SmallString.h"
33
32
#include " llvm/Config/config.h"
34
- #include " llvm/ExecutionEngine/MCJIT.h"
33
+ #include " llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
34
+ #include " llvm/ExecutionEngine/Orc/LLJIT.h"
35
35
#include " llvm/IR/DiagnosticPrinter.h"
36
36
#include " llvm/IR/DiagnosticInfo.h"
37
37
#include " llvm/IR/LLVMContext.h"
40
40
#include " llvm/Transforms/IPO/PassManagerBuilder.h"
41
41
#include " llvm/Support/Path.h"
42
42
43
+ #define DEBUG_TYPE " swift-immediate"
44
+
43
45
#if defined(_WIN32)
44
46
#define WIN32_LEAN_AND_MEAN
45
47
#define NOMINMAX
@@ -246,10 +248,10 @@ int swift::RunImmediately(CompilerInstance &CI,
246
248
const auto PSPs = CI.getPrimarySpecificPathsForAtMostOnePrimary ();
247
249
// FIXME: We shouldn't need to use the global context here, but
248
250
// something is persisting across calls to performIRGeneration.
249
- auto ModuleOwner = performIRGeneration (
251
+ auto ModuleCtx = std::make_unique<llvm::LLVMContext>();
252
+ auto Module = performIRGeneration (
250
253
IRGenOpts, swiftModule, std::move (SM), swiftModule->getName ().str (),
251
- PSPs, getGlobalLLVMContext (), ArrayRef<std::string>());
252
- auto *Module = ModuleOwner.get ();
254
+ PSPs, *ModuleCtx, ArrayRef<std::string>());
253
255
254
256
if (Context.hadError ())
255
257
return -1 ;
@@ -288,7 +290,6 @@ int swift::RunImmediately(CompilerInstance &CI,
288
290
289
291
(*emplaceProcessArgs)(argBuf.data (), CmdLine.size ());
290
292
291
- SmallVector<llvm::Function*, 8 > InitFns;
292
293
if (autolinkImportedModules (swiftModule, IRGenOpts))
293
294
return -1 ;
294
295
@@ -297,41 +298,75 @@ int swift::RunImmediately(CompilerInstance &CI,
297
298
PMBuilder.Inliner = llvm::createFunctionInliningPass (200 );
298
299
299
300
// Build the ExecutionEngine.
300
- llvm::EngineBuilder builder (std::move (ModuleOwner));
301
- std::string ErrorMsg;
302
301
llvm::TargetOptions TargetOpt;
303
302
std::string CPU;
304
303
std::string Triple;
305
304
std::vector<std::string> Features;
306
305
std::tie (TargetOpt, CPU, Features, Triple)
307
306
= getIRTargetOptions (IRGenOpts, swiftModule->getASTContext ());
308
- builder.setRelocationModel (llvm::Reloc::PIC_);
309
- builder.setTargetOptions (TargetOpt);
310
- builder.setMCPU (CPU);
311
- builder.setMAttrs (Features);
312
- builder.setErrorStr (&ErrorMsg);
313
- builder.setEngineKind (llvm::EngineKind::JIT);
314
- llvm::ExecutionEngine *EE = builder.create ();
315
- if (!EE) {
316
- llvm::errs () << " Error loading JIT: " << ErrorMsg;
317
- return -1 ;
307
+
308
+ std::unique_ptr<llvm::orc::LLJIT> JIT;
309
+
310
+ {
311
+ auto JITOrErr =
312
+ llvm::orc::LLJITBuilder ()
313
+ .setJITTargetMachineBuilder (
314
+ llvm::orc::JITTargetMachineBuilder (llvm::Triple (Triple))
315
+ .setRelocationModel (llvm::Reloc::PIC_)
316
+ .setOptions (std::move (TargetOpt))
317
+ .setCPU (std::move (CPU))
318
+ .addFeatures (Features))
319
+ .create ();
320
+
321
+ if (!JITOrErr) {
322
+ llvm::logAllUnhandledErrors (JITOrErr.takeError (), llvm::errs (), " " );
323
+ return -1 ;
324
+ } else
325
+ JIT = std::move (*JITOrErr);
326
+ }
327
+
328
+ {
329
+ // Get a generator for the process symbols and attach it to the main
330
+ // JITDylib.
331
+ if (auto G = llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess (Module->getDataLayout ().getGlobalPrefix ()))
332
+ JIT->getMainJITDylib ().addGenerator (std::move (*G));
333
+ else {
334
+ logAllUnhandledErrors (G.takeError (), llvm::errs (), " " );
335
+ return -1 ;
336
+ }
318
337
}
319
338
320
339
LLVM_DEBUG (llvm::dbgs () << " Module to be executed:\n " ;
321
340
Module->dump ());
322
341
323
- EE->finalizeObject ();
324
-
325
- // Run the generated program.
326
- for (auto InitFn : InitFns) {
327
- LLVM_DEBUG (llvm::dbgs () << " Running initialization function "
328
- << InitFn->getName () << ' \n ' );
329
- EE->runFunctionAsMain (InitFn, CmdLine, nullptr );
342
+ {
343
+ auto TSM = llvm::orc::ThreadSafeModule (std::move (Module), std::move (ModuleCtx));
344
+ if (auto Err = JIT->addIRModule (std::move (TSM))) {
345
+ llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
346
+ return -1 ;
347
+ }
348
+ }
349
+
350
+ using MainFnTy = int (*)(int , char *[]);
351
+
352
+ if (auto Err = JIT->runConstructors ()) {
353
+ llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
354
+ return -1 ;
355
+ }
356
+
357
+ MainFnTy JITMain = nullptr ;
358
+ if (auto MainFnOrErr = JIT->lookup (" main" ))
359
+ JITMain = llvm::jitTargetAddressToFunction<MainFnTy>(MainFnOrErr->getAddress ());
360
+ else {
361
+ logAllUnhandledErrors (MainFnOrErr.takeError (), llvm::errs (), " " );
362
+ return -1 ;
330
363
}
331
364
332
365
LLVM_DEBUG (llvm::dbgs () << " Running static constructors\n " );
333
- EE->runStaticConstructorsDestructors (false );
366
+ if (auto Err = JIT->runConstructors ()) {
367
+ logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
368
+ return -1 ;
369
+ }
334
370
LLVM_DEBUG (llvm::dbgs () << " Running main\n " );
335
- llvm::Function *EntryFn = Module->getFunction (" main" );
336
- return EE->runFunctionAsMain (EntryFn, CmdLine, nullptr );
371
+ return llvm::orc::runAsMain (JITMain, CmdLine);
337
372
}
0 commit comments