Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 1 | //===---- OrcMCJITReplacement.h - Orc based MCJIT replacement ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Orc based MCJIT replacement. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H |
| 15 | #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 18 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 19 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 20 | #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h" |
| 21 | #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" |
| 22 | #include "llvm/Object/Archive.h" |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class OrcMCJITReplacement : public ExecutionEngine { |
| 27 | |
| 28 | class ForwardingRTDyldMM : public RTDyldMemoryManager { |
| 29 | public: |
| 30 | ForwardingRTDyldMM(OrcMCJITReplacement &M) : M(M) {} |
| 31 | |
| 32 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 33 | unsigned SectionID, |
| 34 | StringRef SectionName) override { |
| 35 | uint8_t *Addr = |
| 36 | M.MM->allocateCodeSection(Size, Alignment, SectionID, SectionName); |
| 37 | M.SectionsAllocatedSinceLastLoad.insert(Addr); |
| 38 | return Addr; |
| 39 | } |
| 40 | |
| 41 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 42 | unsigned SectionID, StringRef SectionName, |
| 43 | bool IsReadOnly) override { |
| 44 | uint8_t *Addr = M.MM->allocateDataSection(Size, Alignment, SectionID, |
| 45 | SectionName, IsReadOnly); |
| 46 | M.SectionsAllocatedSinceLastLoad.insert(Addr); |
| 47 | return Addr; |
| 48 | } |
| 49 | |
| 50 | void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO, |
| 51 | uintptr_t DataSizeRW) override { |
| 52 | return M.MM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW); |
| 53 | } |
| 54 | |
| 55 | bool needsToReserveAllocationSpace() override { |
| 56 | return M.MM->needsToReserveAllocationSpace(); |
| 57 | } |
| 58 | |
| 59 | void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 60 | size_t Size) override { |
| 61 | return M.MM->registerEHFrames(Addr, LoadAddr, Size); |
| 62 | } |
| 63 | |
| 64 | void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, |
| 65 | size_t Size) override { |
| 66 | return M.MM->deregisterEHFrames(Addr, LoadAddr, Size); |
| 67 | } |
| 68 | |
| 69 | uint64_t getSymbolAddress(const std::string &Name) override { |
| 70 | return M.getSymbolAddressWithoutMangling(Name); |
| 71 | } |
| 72 | |
| 73 | void *getPointerToNamedFunction(const std::string &Name, |
| 74 | bool AbortOnFailure = true) override { |
| 75 | return M.MM->getPointerToNamedFunction(Name, AbortOnFailure); |
| 76 | } |
| 77 | |
| 78 | void notifyObjectLoaded(ExecutionEngine *EE, |
| 79 | const object::ObjectFile &O) override { |
| 80 | return M.MM->notifyObjectLoaded(EE, O); |
| 81 | } |
| 82 | |
| 83 | bool finalizeMemory(std::string *ErrMsg = nullptr) override { |
| 84 | // Each set of objects loaded will be finalized exactly once, but since |
| 85 | // symbol lookup during relocation may recursively trigger the |
| 86 | // loading/relocation of other modules, and since we're forwarding all |
| 87 | // finalizeMemory calls to a single underlying memory manager, we need to |
| 88 | // defer forwarding the call on until all necessary objects have been |
| 89 | // loaded. Otherwise, during the relocation of a leaf object, we will end |
| 90 | // up finalizing memory, causing a crash further up the stack when we |
| 91 | // attempt to apply relocations to finalized memory. |
| 92 | // To avoid finalizing too early, look at how many objects have been |
| 93 | // loaded but not yet finalized. This is a bit of a hack that relies on |
| 94 | // the fact that we're lazily emitting object files: The only way you can |
| 95 | // get more than one set of objects loaded but not yet finalized is if |
| 96 | // they were loaded during relocation of another set. |
| 97 | if (M.UnfinalizedSections.size() == 1) |
| 98 | return M.MM->finalizeMemory(ErrMsg); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | OrcMCJITReplacement &M; |
| 104 | }; |
| 105 | |
| 106 | private: |
| 107 | static ExecutionEngine * |
| 108 | createOrcMCJITReplacement(std::string *ErrorMsg, |
| 109 | std::unique_ptr<RTDyldMemoryManager> OrcJMM, |
| 110 | std::unique_ptr<llvm::TargetMachine> TM) { |
| 111 | return new llvm::OrcMCJITReplacement(std::move(OrcJMM), std::move(TM)); |
| 112 | } |
| 113 | |
| 114 | public: |
| 115 | static void Register() { |
| 116 | OrcMCJITReplacementCtor = createOrcMCJITReplacement; |
| 117 | } |
| 118 | |
| 119 | OrcMCJITReplacement(std::unique_ptr<RTDyldMemoryManager> MM, |
| 120 | std::unique_ptr<TargetMachine> TM) |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 121 | : TM(std::move(TM)), MM(std::move(MM)), Mang(this->TM->getDataLayout()), |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 122 | NotifyObjectLoaded(*this), NotifyFinalized(*this), |
Lang Hames | 28452d8 | 2015-01-23 22:11:07 +0000 | [diff] [blame] | 123 | ObjectLayer(ObjectLayerT::CreateRTDyldMMFtor(), NotifyObjectLoaded, |
| 124 | NotifyFinalized), |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 125 | CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)), |
| 126 | LazyEmitLayer(CompileLayer) { |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 127 | setDataLayout(this->TM->getDataLayout()); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Reid Kleckner | 7b23b43 | 2015-01-23 22:25:47 +0000 | [diff] [blame] | 130 | void addModule(std::unique_ptr<Module> M) override { |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 131 | |
| 132 | // If this module doesn't have a DataLayout attached then attach the |
| 133 | // default. |
| 134 | if (!M->getDataLayout()) |
| 135 | M->setDataLayout(getDataLayout()); |
| 136 | |
Lang Hames | a3e710d | 2015-02-02 04:34:02 +0000 | [diff] [blame] | 137 | Modules.push_back(std::move(M)); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 138 | std::vector<Module *> Ms; |
Lang Hames | a3e710d | 2015-02-02 04:34:02 +0000 | [diff] [blame] | 139 | Ms.push_back(&*Modules.back()); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 140 | LazyEmitLayer.addModuleSet(std::move(Ms), |
| 141 | llvm::make_unique<ForwardingRTDyldMM>(*this)); |
| 142 | } |
| 143 | |
| 144 | void addObjectFile(std::unique_ptr<object::ObjectFile> O) override { |
| 145 | std::vector<std::unique_ptr<object::ObjectFile>> Objs; |
| 146 | Objs.push_back(std::move(O)); |
| 147 | ObjectLayer.addObjectSet(std::move(Objs), |
| 148 | llvm::make_unique<ForwardingRTDyldMM>(*this)); |
| 149 | } |
| 150 | |
| 151 | void addObjectFile(object::OwningBinary<object::ObjectFile> O) override { |
| 152 | std::unique_ptr<object::ObjectFile> Obj; |
| 153 | std::unique_ptr<MemoryBuffer> Buf; |
| 154 | std::tie(Obj, Buf) = O.takeBinary(); |
| 155 | std::vector<std::unique_ptr<object::ObjectFile>> Objs; |
| 156 | Objs.push_back(std::move(Obj)); |
Lang Hames | 35a514d | 2015-02-02 19:51:18 +0000 | [diff] [blame^] | 157 | auto H = |
| 158 | ObjectLayer.addObjectSet(std::move(Objs), |
| 159 | llvm::make_unique<ForwardingRTDyldMM>(*this)); |
| 160 | |
| 161 | std::vector<std::unique_ptr<MemoryBuffer>> Bufs; |
| 162 | Bufs.push_back(std::move(Buf)); |
| 163 | ObjectLayer.takeOwnershipOfBuffers(H, std::move(Bufs)); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void addArchive(object::OwningBinary<object::Archive> A) override { |
| 167 | Archives.push_back(std::move(A)); |
| 168 | } |
| 169 | |
| 170 | uint64_t getSymbolAddress(StringRef Name) { |
| 171 | return getSymbolAddressWithoutMangling(Mangle(Name)); |
| 172 | } |
| 173 | |
| 174 | void finalizeObject() override { |
| 175 | // This is deprecated - Aim to remove in ExecutionEngine. |
| 176 | // REMOVE IF POSSIBLE - Doesn't make sense for New JIT. |
| 177 | } |
| 178 | |
| 179 | void mapSectionAddress(const void *LocalAddress, |
| 180 | uint64_t TargetAddress) override { |
| 181 | for (auto &P : UnfinalizedSections) |
| 182 | if (P.second.count(LocalAddress)) |
| 183 | ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress); |
| 184 | } |
| 185 | |
| 186 | uint64_t getGlobalValueAddress(const std::string &Name) override { |
| 187 | return getSymbolAddress(Name); |
| 188 | } |
| 189 | |
| 190 | uint64_t getFunctionAddress(const std::string &Name) override { |
| 191 | return getSymbolAddress(Name); |
| 192 | } |
| 193 | |
| 194 | void *getPointerToFunction(Function *F) override { |
| 195 | uint64_t FAddr = getSymbolAddress(F->getName()); |
| 196 | return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr)); |
| 197 | } |
| 198 | |
| 199 | void *getPointerToNamedFunction(StringRef Name, |
| 200 | bool AbortOnFailure = true) override { |
| 201 | uint64_t Addr = getSymbolAddress(Name); |
| 202 | if (!Addr && AbortOnFailure) |
| 203 | llvm_unreachable("Missing symbol!"); |
| 204 | return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr)); |
| 205 | } |
| 206 | |
| 207 | GenericValue runFunction(Function *F, |
| 208 | const std::vector<GenericValue> &ArgValues) override; |
| 209 | |
| 210 | void setObjectCache(ObjectCache *NewCache) override { |
| 211 | CompileLayer.setObjectCache(NewCache); |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | uint64_t getSymbolAddressWithoutMangling(StringRef Name) { |
| 216 | if (uint64_t Addr = LazyEmitLayer.getSymbolAddress(Name, false)) |
| 217 | return Addr; |
| 218 | if (uint64_t Addr = MM->getSymbolAddress(Name)) |
| 219 | return Addr; |
| 220 | if (uint64_t Addr = scanArchives(Name)) |
| 221 | return Addr; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | uint64_t scanArchives(StringRef Name) { |
| 227 | for (object::OwningBinary<object::Archive> &OB : Archives) { |
| 228 | object::Archive *A = OB.getBinary(); |
| 229 | // Look for our symbols in each Archive |
| 230 | object::Archive::child_iterator ChildIt = A->findSym(Name); |
| 231 | if (ChildIt != A->child_end()) { |
| 232 | // FIXME: Support nested archives? |
| 233 | ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr = |
| 234 | ChildIt->getAsBinary(); |
| 235 | if (ChildBinOrErr.getError()) |
| 236 | continue; |
| 237 | std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); |
| 238 | if (ChildBin->isObject()) { |
| 239 | std::vector<std::unique_ptr<object::ObjectFile>> ObjSet; |
| 240 | ObjSet.push_back(std::unique_ptr<object::ObjectFile>( |
| 241 | static_cast<object::ObjectFile *>(ChildBin.release()))); |
| 242 | ObjectLayer.addObjectSet( |
| 243 | std::move(ObjSet), llvm::make_unique<ForwardingRTDyldMM>(*this)); |
| 244 | if (uint64_t Addr = ObjectLayer.getSymbolAddress(Name, true)) |
| 245 | return Addr; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | class NotifyObjectLoadedT { |
| 253 | public: |
| 254 | typedef std::vector<std::unique_ptr<object::ObjectFile>> ObjListT; |
| 255 | typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>> |
| 256 | LoadedObjInfoListT; |
| 257 | |
| 258 | NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {} |
| 259 | |
| 260 | void operator()(ObjectLinkingLayerBase::ObjSetHandleT H, |
| 261 | const ObjListT &Objects, |
| 262 | const LoadedObjInfoListT &Infos) const { |
| 263 | M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad); |
NAKAMURA Takumi | 2fb9a523 | 2015-01-25 11:41:49 +0000 | [diff] [blame] | 264 | M.SectionsAllocatedSinceLastLoad = SectionAddrSet(); |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 265 | assert(Objects.size() == Infos.size() && |
| 266 | "Incorrect number of Infos for Objects."); |
| 267 | for (unsigned I = 0; I < Objects.size(); ++I) |
| 268 | M.MM->notifyObjectLoaded(&M, *Objects[I]); |
| 269 | }; |
| 270 | |
| 271 | private: |
| 272 | OrcMCJITReplacement &M; |
| 273 | }; |
| 274 | |
| 275 | class NotifyFinalizedT { |
| 276 | public: |
| 277 | NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {} |
| 278 | void operator()(ObjectLinkingLayerBase::ObjSetHandleT H) { |
| 279 | M.UnfinalizedSections.erase(H); |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | OrcMCJITReplacement &M; |
| 284 | }; |
| 285 | |
| 286 | std::string Mangle(StringRef Name) { |
| 287 | std::string MangledName; |
| 288 | { |
| 289 | raw_string_ostream MangledNameStream(MangledName); |
| 290 | Mang.getNameWithPrefix(MangledNameStream, Name); |
| 291 | } |
| 292 | return MangledName; |
| 293 | } |
| 294 | |
| 295 | typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT; |
| 296 | typedef IRCompileLayer<ObjectLayerT> CompileLayerT; |
| 297 | typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT; |
| 298 | |
| 299 | std::unique_ptr<TargetMachine> TM; |
| 300 | std::unique_ptr<RTDyldMemoryManager> MM; |
| 301 | Mangler Mang; |
| 302 | |
| 303 | NotifyObjectLoadedT NotifyObjectLoaded; |
| 304 | NotifyFinalizedT NotifyFinalized; |
| 305 | |
| 306 | ObjectLayerT ObjectLayer; |
| 307 | CompileLayerT CompileLayer; |
| 308 | LazyEmitLayerT LazyEmitLayer; |
| 309 | |
Lang Hames | 93de2a1 | 2015-01-23 21:25:00 +0000 | [diff] [blame] | 310 | // We need to store ObjLayerT::ObjSetHandles for each of the object sets |
| 311 | // that have been emitted but not yet finalized so that we can forward the |
| 312 | // mapSectionAddress calls appropriately. |
| 313 | typedef std::set<const void *> SectionAddrSet; |
| 314 | struct ObjSetHandleCompare { |
| 315 | bool operator()(ObjectLayerT::ObjSetHandleT H1, |
| 316 | ObjectLayerT::ObjSetHandleT H2) const { |
| 317 | return &*H1 < &*H2; |
| 318 | } |
| 319 | }; |
| 320 | SectionAddrSet SectionsAllocatedSinceLastLoad; |
| 321 | std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare> |
| 322 | UnfinalizedSections; |
| 323 | |
| 324 | std::vector<object::OwningBinary<object::Archive>> Archives; |
| 325 | }; |
| 326 | } |
| 327 | |
| 328 | #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H |