|
| 1 | +//===--- SwiftMaterializationUnit.h - JIT Swift ASTs ------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Defines the `SwiftMaterializationUnit` class, which allows you to JIT |
| 14 | +// individual Swift AST declarations. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_IMMEDIATE_SWIFTMATERIALIZATIONUNIT_H |
| 19 | +#define SWIFT_IMMEDIATE_SWIFTMATERIALIZATIONUNIT_H |
| 20 | + |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +#include "llvm/ADT/StringRef.h" |
| 24 | +#include "llvm/ExecutionEngine/JITLink/JITLink.h" |
| 25 | +#include "llvm/ExecutionEngine/Orc/Core.h" |
| 26 | +#include "llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h" |
| 27 | +#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" |
| 28 | +#include "llvm/ExecutionEngine/Orc/LLJIT.h" |
| 29 | +#include "llvm/ExecutionEngine/Orc/LazyReexports.h" |
| 30 | +#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h" |
| 31 | + |
| 32 | +#include "swift/AST/TBDGenRequests.h" |
| 33 | + |
| 34 | +namespace swift { |
| 35 | + |
| 36 | +class CompilerInstance; |
| 37 | + |
| 38 | +class SwiftJIT { |
| 39 | +public: |
| 40 | + SwiftJIT(const SwiftJIT &) = delete; |
| 41 | + SwiftJIT(SwiftJIT &&) = delete; |
| 42 | + SwiftJIT &operator=(const SwiftJIT &) = delete; |
| 43 | + SwiftJIT &operator=(SwiftJIT &&) = delete; |
| 44 | + |
| 45 | + /// Attempt to create and initialize a new `SwiftJIT` with lazy compilation |
| 46 | + /// enabled and an attached generator to search for symbols defined in the |
| 47 | + /// current process. |
| 48 | + static llvm::Expected<std::unique_ptr<SwiftJIT>> Create(CompilerInstance &CI); |
| 49 | + |
| 50 | + ~SwiftJIT(); |
| 51 | + llvm::orc::LLJIT &getJIT(); |
| 52 | + |
| 53 | + llvm::orc::JITDylib &getMainJITDylib(); |
| 54 | + |
| 55 | + /// Register a the materialization unit `MU` with the `JITDylib``JD` and |
| 56 | + /// create lazy reexports for all functions defined in the interface of `MU` |
| 57 | + llvm::Error addSwift(llvm::orc::JITDylib &JD, |
| 58 | + std::unique_ptr<llvm::orc::MaterializationUnit> MU); |
| 59 | + |
| 60 | + llvm::Error initialize(llvm::orc::JITDylib &JD); |
| 61 | + |
| 62 | + llvm::Error deinitialize(llvm::orc::JITDylib &JD); |
| 63 | + |
| 64 | + llvm::Expected<llvm::orc::ExecutorAddr> lookup(llvm::StringRef Name); |
| 65 | + |
| 66 | + llvm::Expected<llvm::orc::ExecutorAddr> |
| 67 | + lookupLinkerMangled(llvm::StringRef Name); |
| 68 | + |
| 69 | + llvm::orc::SymbolStringPtr mangleAndIntern(llvm::StringRef Name); |
| 70 | + |
| 71 | + llvm::orc::SymbolStringPtr intern(llvm::StringRef Name); |
| 72 | + |
| 73 | + llvm::orc::IRCompileLayer &getIRCompileLayer(); |
| 74 | + |
| 75 | + llvm::orc::ObjectTransformLayer &getObjTransformLayer(); |
| 76 | + |
| 77 | +private: |
| 78 | + static llvm::Expected<std::unique_ptr<llvm::orc::LLJIT>> |
| 79 | + CreateLLJIT(CompilerInstance &CI); |
| 80 | + |
| 81 | + /// An ORC layer to rename the names of function bodies to support lazy |
| 82 | + /// reexports |
| 83 | + class Plugin : public llvm::orc::ObjectLinkingLayer::Plugin { |
| 84 | + void |
| 85 | + modifyPassConfig(llvm::orc::MaterializationResponsibility &MR, |
| 86 | + llvm::jitlink::LinkGraph &G, |
| 87 | + llvm::jitlink::PassConfiguration &PassConfig) override; |
| 88 | + |
| 89 | + llvm::Error |
| 90 | + notifyFailed(llvm::orc::MaterializationResponsibility &MR) override; |
| 91 | + |
| 92 | + llvm::Error notifyRemovingResources(llvm::orc::ResourceKey K) override; |
| 93 | + |
| 94 | + void notifyTransferringResources(llvm::orc::ResourceKey DstKey, |
| 95 | + llvm::orc::ResourceKey SrcKey) override; |
| 96 | + }; |
| 97 | + |
| 98 | + static void handleLazyCompilationFailure(); |
| 99 | + |
| 100 | + SwiftJIT(std::unique_ptr<llvm::orc::LLJIT> J, |
| 101 | + std::unique_ptr<llvm::orc::EPCIndirectionUtils> EPCIU); |
| 102 | + |
| 103 | + std::unique_ptr<llvm::orc::LLJIT> J; |
| 104 | + std::unique_ptr<llvm::orc::EPCIndirectionUtils> EPCIU; |
| 105 | + llvm::orc::LazyCallThroughManager &LCTM; |
| 106 | + std::unique_ptr<llvm::orc::IndirectStubsManager> ISM; |
| 107 | +}; |
| 108 | + |
| 109 | +class SwiftMaterializationUnit : public llvm::orc::MaterializationUnit { |
| 110 | +public: |
| 111 | + static std::unique_ptr<SwiftMaterializationUnit> Create(SwiftJIT &JIT, |
| 112 | + CompilerInstance &CI); |
| 113 | + llvm::StringRef getName() const override; |
| 114 | + |
| 115 | +private: |
| 116 | + SwiftMaterializationUnit(SwiftJIT &JIT, CompilerInstance &CI, |
| 117 | + SymbolSourceMap Sources, |
| 118 | + llvm::orc::SymbolFlagsMap Symbols); |
| 119 | + void materialize( |
| 120 | + std::unique_ptr<llvm::orc::MaterializationResponsibility> MR) override; |
| 121 | + void discard(const llvm::orc::JITDylib &JD, |
| 122 | + const llvm::orc::SymbolStringPtr &Sym) override; |
| 123 | + SymbolSourceMap Sources; |
| 124 | + SwiftJIT &JIT; |
| 125 | + CompilerInstance &CI; |
| 126 | +}; |
| 127 | +} // namespace swift |
| 128 | + |
| 129 | +#endif // SWIFT_IMMEDIATE_SWIFTMATERIALIZATIONUNIT_H |
0 commit comments