32
32
#include " llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
33
33
#include " llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
34
34
#include " llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
35
+ #include " llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
35
36
#include " llvm/Support/Error.h"
36
37
37
38
#include " swift/AST/IRGenRequests.h"
43
44
#include " swift/SILOptimizer/PassManager/Passes.h"
44
45
#include " swift/Subsystems.h"
45
46
47
+ #define DEBUG_TYPE " swift-immediate"
48
+
46
49
using namespace swift ;
47
50
48
51
// / The suffix appended to function bodies when creating lazy reexports
@@ -92,6 +95,30 @@ SwiftJIT::~SwiftJIT() {
92
95
J->getExecutionSession ().reportError (std::move (Err));
93
96
}
94
97
98
+ llvm::Expected<int > SwiftJIT::runMain (llvm::ArrayRef<std::string> Args) {
99
+ if (auto Err = J->initialize (J->getMainJITDylib ())) {
100
+ return Err;
101
+ }
102
+
103
+ auto MainSym = J->lookup (" main" );
104
+ if (!MainSym) {
105
+ return MainSym.takeError ();
106
+ }
107
+
108
+ using MainFnTy = int (*)(int , char *[]);
109
+ MainFnTy JITMain = MainSym->toPtr <MainFnTy>();
110
+
111
+ LLVM_DEBUG (llvm::dbgs () << " Running main\n " );
112
+ int Result = llvm::orc::runAsMain (JITMain, Args);
113
+
114
+ LLVM_DEBUG (llvm::dbgs () << " Running static destructors\n " );
115
+ if (auto Err = J->deinitialize (J->getMainJITDylib ())) {
116
+ return Err;
117
+ }
118
+
119
+ return Result;
120
+ }
121
+
95
122
llvm::orc::LLJIT &SwiftJIT::getJIT () { return *J; }
96
123
97
124
llvm::orc::JITDylib &SwiftJIT::getMainJITDylib () {
@@ -277,8 +304,8 @@ static void logError(llvm::Error Err) {
277
304
logAllUnhandledErrors (std::move (Err), llvm::errs (), " " );
278
305
}
279
306
280
- std::unique_ptr<SwiftMaterializationUnit >
281
- SwiftMaterializationUnit ::Create (SwiftJIT &JIT, CompilerInstance &CI) {
307
+ std::unique_ptr<LazySwiftMaterializationUnit >
308
+ LazySwiftMaterializationUnit ::Create (SwiftJIT &JIT, CompilerInstance &CI) {
282
309
auto *M = CI.getMainModule ();
283
310
TBDGenOptions Opts;
284
311
Opts.PublicSymbolsOnly = false ;
@@ -301,21 +328,22 @@ SwiftMaterializationUnit::Create(SwiftJIT &JIT, CompilerInstance &CI) {
301
328
auto MangledName = mangle (SymbolName);
302
329
PublicInterface[JIT.intern (MangledName)] = Flags;
303
330
}
304
- return std::unique_ptr<SwiftMaterializationUnit>(new SwiftMaterializationUnit (
305
- JIT, CI, std::move (Sources), std::move (PublicInterface)));
331
+ return std::unique_ptr<LazySwiftMaterializationUnit>(
332
+ new LazySwiftMaterializationUnit (JIT, CI, std::move (Sources),
333
+ std::move (PublicInterface)));
306
334
}
307
335
308
- StringRef SwiftMaterializationUnit ::getName () const {
336
+ StringRef LazySwiftMaterializationUnit ::getName () const {
309
337
return " SwiftMaterializationUnit" ;
310
338
}
311
339
312
- SwiftMaterializationUnit::SwiftMaterializationUnit (
340
+ LazySwiftMaterializationUnit::LazySwiftMaterializationUnit (
313
341
SwiftJIT &JIT, CompilerInstance &CI, SymbolSourceMap Sources,
314
342
llvm::orc::SymbolFlagsMap Symbols)
315
343
: MaterializationUnit({std::move (Symbols), nullptr }),
316
344
Sources(std::move(Sources)), JIT(JIT), CI(CI) {}
317
345
318
- void SwiftMaterializationUnit ::materialize (
346
+ void LazySwiftMaterializationUnit ::materialize (
319
347
std::unique_ptr<llvm::orc::MaterializationResponsibility> MR) {
320
348
SILRefsToEmit Refs;
321
349
const auto &RS = MR->getRequestedSymbols ();
@@ -379,8 +407,8 @@ void SwiftMaterializationUnit::materialize(
379
407
}
380
408
}
381
409
std::unique_ptr<MaterializationUnit> UnrequestedMU (
382
- new SwiftMaterializationUnit (JIT, CI, std::move (Sources),
383
- std::move (UnrequestedSymbols)));
410
+ new LazySwiftMaterializationUnit (JIT, CI, std::move (Sources),
411
+ std::move (UnrequestedSymbols)));
384
412
if (auto Err = MR->replace (std::move (UnrequestedMU))) {
385
413
logError (std::move (Err));
386
414
MR->failMaterialization ();
@@ -417,5 +445,95 @@ SwiftJIT::addSwift(llvm::orc::JITDylib &JD,
417
445
return JD.define (std::move (MU));
418
446
}
419
447
420
- void SwiftMaterializationUnit::discard (const llvm::orc::JITDylib &JD,
421
- const llvm::orc::SymbolStringPtr &Sym) {}
448
+ void LazySwiftMaterializationUnit::discard (
449
+ const llvm::orc::JITDylib &JD, const llvm::orc::SymbolStringPtr &Sym) {}
450
+
451
+ EagerSwiftMaterializationUnit::EagerSwiftMaterializationUnit (
452
+ SwiftJIT &JIT, const CompilerInstance &CI, const IRGenOptions &IRGenOpts,
453
+ std::unique_ptr<SILModule> SM)
454
+ : MaterializationUnit(getInterface(JIT, CI)), JIT(JIT), CI(CI),
455
+ IRGenOpts(IRGenOpts), SM(std::move(SM)) {}
456
+
457
+ StringRef EagerSwiftMaterializationUnit::getName () const {
458
+ return " EagerSwiftMaterializationUnit" ;
459
+ }
460
+
461
+ void EagerSwiftMaterializationUnit::materialize (
462
+ std::unique_ptr<llvm::orc::MaterializationResponsibility> R) {
463
+
464
+ auto GenModule = generateModule (CI, std::move (SM));
465
+
466
+ if (!GenModule) {
467
+ R->failMaterialization ();
468
+ return ;
469
+ }
470
+
471
+ auto *Module = GenModule->getModule ();
472
+
473
+ // Dump IR if requested
474
+ dumpJIT (*Module);
475
+
476
+ // Now we must register all other public symbols defined by
477
+ // the module with the JIT
478
+ llvm::orc::SymbolFlagsMap Symbols;
479
+ // Register all global values, including global
480
+ // variables and functions
481
+ for (const auto &GV : Module->global_values ()) {
482
+ // Ignore all symbols that will not appear in symbol table
483
+ if (GV.hasLocalLinkage () || GV.isDeclaration () || GV.hasAppendingLinkage ())
484
+ continue ;
485
+ auto Name = GV.getName ();
486
+ // The entry point is already registered up front with the
487
+ // interface, so ignore it as well
488
+ if (Name == CI.getASTContext ().getEntryPointFunctionName ())
489
+ continue ;
490
+ auto MangledName = JIT.mangleAndIntern (Name);
491
+ // Register this symbol with the proper flags
492
+ Symbols[MangledName] = llvm::JITSymbolFlags::fromGlobalValue (GV);
493
+ }
494
+ // Register the symbols we have discovered with the JIT
495
+ if (auto Err = R->defineMaterializing (Symbols)) {
496
+ logError (std::move (Err));
497
+ }
498
+ auto TSM = std::move (*GenModule).intoThreadSafeContext ();
499
+ JIT.getIRCompileLayer ().emit (std::move (R), std::move (TSM));
500
+ }
501
+
502
+ llvm::orc::MaterializationUnit::Interface
503
+ EagerSwiftMaterializationUnit::getInterface (SwiftJIT &JIT,
504
+ const CompilerInstance &CI) {
505
+ const auto &EntryPoint = CI.getASTContext ().getEntryPointFunctionName ();
506
+ auto MangledEntryPoint = JIT.mangleAndIntern (EntryPoint);
507
+ auto Flags = llvm::JITSymbolFlags::Callable | llvm::JITSymbolFlags::Exported;
508
+ llvm::orc::SymbolFlagsMap Symbols{{MangledEntryPoint, Flags}};
509
+ return {std::move (Symbols), nullptr };
510
+ }
511
+
512
+ void EagerSwiftMaterializationUnit::discard (
513
+ const llvm::orc::JITDylib &JD, const llvm::orc::SymbolStringPtr &Sym) {}
514
+
515
+ static void DumpLLVMIR (const llvm::Module &M) {
516
+ std::string path = (M.getName () + " .ll" ).str ();
517
+ for (size_t count = 0 ; llvm::sys::fs::exists (path);)
518
+ path = (M.getName () + llvm::utostr (count++) + " .ll" ).str ();
519
+
520
+ std::error_code error;
521
+ llvm::raw_fd_ostream stream (path, error);
522
+ if (error)
523
+ return ;
524
+ M.print (stream, /* AssemblyAnnotationWriter=*/ nullptr );
525
+ }
526
+
527
+ void EagerSwiftMaterializationUnit::dumpJIT (const llvm::Module &M) {
528
+ LLVM_DEBUG (llvm::dbgs () << " Module to be executed:\n " ; M.dump ());
529
+ switch (IRGenOpts.DumpJIT ) {
530
+ case JITDebugArtifact::None:
531
+ break ;
532
+ case JITDebugArtifact::LLVMIR:
533
+ DumpLLVMIR (M);
534
+ break ;
535
+ case JITDebugArtifact::Object:
536
+ JIT.getObjTransformLayer ().setTransform (llvm::orc::DumpObjects ());
537
+ break ;
538
+ }
539
+ }
0 commit comments