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,76 @@ 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
+ .setCodeGenOptLevel (llvm::CodeGenOpt::Default))
320
+ .create ();
321
+
322
+ if (!JITOrErr) {
323
+ llvm::logAllUnhandledErrors (JITOrErr.takeError (), llvm::errs (), " " );
324
+ return -1 ;
325
+ } else
326
+ JIT = std::move (*JITOrErr);
327
+ }
328
+
329
+ {
330
+ // Get a generator for the process symbols and attach it to the main
331
+ // JITDylib.
332
+ if (auto G = llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess (Module->getDataLayout ().getGlobalPrefix ()))
333
+ JIT->getMainJITDylib ().addGenerator (std::move (*G));
334
+ else {
335
+ logAllUnhandledErrors (G.takeError (), llvm::errs (), " " );
336
+ return -1 ;
337
+ }
318
338
}
319
339
320
340
LLVM_DEBUG (llvm::dbgs () << " Module to be executed:\n " ;
321
341
Module->dump ());
322
342
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 );
343
+ {
344
+ auto TSM = llvm::orc::ThreadSafeModule (std::move (Module), std::move (ModuleCtx));
345
+ if (auto Err = JIT->addIRModule (std::move (TSM))) {
346
+ llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
347
+ return -1 ;
348
+ }
349
+ }
350
+
351
+ using MainFnTy = int (*)(int , char *[]);
352
+
353
+ if (auto Err = JIT->runConstructors ()) {
354
+ llvm::logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
355
+ return -1 ;
356
+ }
357
+
358
+ MainFnTy JITMain = nullptr ;
359
+ if (auto MainFnOrErr = JIT->lookup (" main" ))
360
+ JITMain = llvm::jitTargetAddressToFunction<MainFnTy>(MainFnOrErr->getAddress ());
361
+ else {
362
+ logAllUnhandledErrors (MainFnOrErr.takeError (), llvm::errs (), " " );
363
+ return -1 ;
330
364
}
331
365
332
366
LLVM_DEBUG (llvm::dbgs () << " Running static constructors\n " );
333
- EE->runStaticConstructorsDestructors (false );
367
+ if (auto Err = JIT->runConstructors ()) {
368
+ logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
369
+ return -1 ;
370
+ }
334
371
LLVM_DEBUG (llvm::dbgs () << " Running main\n " );
335
- llvm::Function *EntryFn = Module->getFunction (" main" );
336
- return EE->runFunctionAsMain (EntryFn, CmdLine, nullptr );
372
+ return llvm::orc::runAsMain (JITMain, CmdLine);
337
373
}
0 commit comments