LCOV - code coverage report
Current view: top level - lib/ExecutionEngine/Orc - OrcMCJITReplacement.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 115 161 71.4 %
Date: 2018-02-23 15:42:53 Functions: 31 40 77.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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/ADT/ArrayRef.h"
      18             : #include "llvm/ADT/STLExtras.h"
      19             : #include "llvm/ADT/StringRef.h"
      20             : #include "llvm/ExecutionEngine/ExecutionEngine.h"
      21             : #include "llvm/ExecutionEngine/GenericValue.h"
      22             : #include "llvm/ExecutionEngine/JITSymbol.h"
      23             : #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
      24             : #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
      25             : #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
      26             : #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
      27             : #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
      28             : #include "llvm/ExecutionEngine/RuntimeDyld.h"
      29             : #include "llvm/IR/DataLayout.h"
      30             : #include "llvm/IR/Function.h"
      31             : #include "llvm/IR/Mangler.h"
      32             : #include "llvm/IR/Module.h"
      33             : #include "llvm/Object/Archive.h"
      34             : #include "llvm/Object/Binary.h"
      35             : #include "llvm/Object/ObjectFile.h"
      36             : #include "llvm/Support/Error.h"
      37             : #include "llvm/Support/ErrorHandling.h"
      38             : #include "llvm/Support/raw_ostream.h"
      39             : #include "llvm/Target/TargetMachine.h"
      40             : #include <algorithm>
      41             : #include <cassert>
      42             : #include <cstddef>
      43             : #include <cstdint>
      44             : #include <map>
      45             : #include <memory>
      46             : #include <set>
      47             : #include <string>
      48             : #include <vector>
      49             : 
      50             : namespace llvm {
      51             : 
      52             : class ObjectCache;
      53             : 
      54             : namespace orc {
      55             : 
      56          44 : class OrcMCJITReplacement : public ExecutionEngine {
      57             :   // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
      58             :   // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
      59             :   // expecting - see finalizeMemory.
      60          22 :   class MCJITReplacementMemMgr : public MCJITMemoryManager {
      61             :   public:
      62             :     MCJITReplacementMemMgr(OrcMCJITReplacement &M,
      63             :                            std::shared_ptr<MCJITMemoryManager> ClientMM)
      64         154 :       : M(M), ClientMM(std::move(ClientMM)) {}
      65             : 
      66          94 :     uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
      67             :                                  unsigned SectionID,
      68             :                                  StringRef SectionName) override {
      69             :       uint8_t *Addr =
      70          94 :           ClientMM->allocateCodeSection(Size, Alignment, SectionID,
      71          94 :                                         SectionName);
      72         188 :       M.SectionsAllocatedSinceLastLoad.insert(Addr);
      73          94 :       return Addr;
      74             :     }
      75             : 
      76         134 :     uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
      77             :                                  unsigned SectionID, StringRef SectionName,
      78             :                                  bool IsReadOnly) override {
      79         268 :       uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
      80         268 :                                                     SectionName, IsReadOnly);
      81         268 :       M.SectionsAllocatedSinceLastLoad.insert(Addr);
      82         134 :       return Addr;
      83             :     }
      84             : 
      85          14 :     void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
      86             :                                 uintptr_t RODataSize, uint32_t RODataAlign,
      87             :                                 uintptr_t RWDataSize,
      88             :                                 uint32_t RWDataAlign) override {
      89          28 :       return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
      90             :                                               RODataSize, RODataAlign,
      91          28 :                                               RWDataSize, RWDataAlign);
      92             :     }
      93             : 
      94          93 :     bool needsToReserveAllocationSpace() override {
      95          93 :       return ClientMM->needsToReserveAllocationSpace();
      96             :     }
      97             : 
      98          73 :     void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
      99             :                           size_t Size) override {
     100          73 :       return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
     101             :     }
     102             : 
     103          14 :     void deregisterEHFrames() override {
     104          14 :       return ClientMM->deregisterEHFrames();
     105             :     }
     106             : 
     107          93 :     void notifyObjectLoaded(RuntimeDyld &RTDyld,
     108             :                             const object::ObjectFile &O) override {
     109          93 :       return ClientMM->notifyObjectLoaded(RTDyld, O);
     110             :     }
     111             : 
     112          93 :     void notifyObjectLoaded(ExecutionEngine *EE,
     113             :                             const object::ObjectFile &O) override {
     114          93 :       return ClientMM->notifyObjectLoaded(EE, O);
     115             :     }
     116             : 
     117          76 :     bool finalizeMemory(std::string *ErrMsg = nullptr) override {
     118             :       // Each set of objects loaded will be finalized exactly once, but since
     119             :       // symbol lookup during relocation may recursively trigger the
     120             :       // loading/relocation of other modules, and since we're forwarding all
     121             :       // finalizeMemory calls to a single underlying memory manager, we need to
     122             :       // defer forwarding the call on until all necessary objects have been
     123             :       // loaded. Otherwise, during the relocation of a leaf object, we will end
     124             :       // up finalizing memory, causing a crash further up the stack when we
     125             :       // attempt to apply relocations to finalized memory.
     126             :       // To avoid finalizing too early, look at how many objects have been
     127             :       // loaded but not yet finalized. This is a bit of a hack that relies on
     128             :       // the fact that we're lazily emitting object files: The only way you can
     129             :       // get more than one set of objects loaded but not yet finalized is if
     130             :       // they were loaded during relocation of another set.
     131         152 :       if (M.UnfinalizedSections.size() == 1)
     132          76 :         return ClientMM->finalizeMemory(ErrMsg);
     133             :       return false;
     134             :     }
     135             : 
     136             :   private:
     137             :     OrcMCJITReplacement &M;
     138             :     std::shared_ptr<MCJITMemoryManager> ClientMM;
     139             :   };
     140             : 
     141          11 :   class LinkingORCResolver : public orc::SymbolResolver {
     142             :   public:
     143          77 :     LinkingORCResolver(OrcMCJITReplacement &M) : M(M) {}
     144             : 
     145          93 :     SymbolNameSet lookupFlags(SymbolFlagsMap &SymbolFlags,
     146             :                               const SymbolNameSet &Symbols) override {
     147             :       SymbolNameSet UnresolvedSymbols;
     148             : 
     149         112 :       for (auto &S : Symbols) {
     150          57 :         if (auto Sym = M.findMangledSymbol(*S)) {
     151           3 :           SymbolFlags[S] = Sym.getFlags();
     152          16 :         } else if (auto Err = Sym.takeError()) {
     153           0 :           M.reportError(std::move(Err));
     154           0 :           return SymbolNameSet();
     155             :         } else {
     156          80 :           if (auto Sym2 = M.ClientResolver->findSymbolInLogicalDylib(*S)) {
     157           0 :             SymbolFlags[S] = Sym2.getFlags();
     158          16 :           } else if (auto Err = Sym2.takeError()) {
     159           0 :             M.reportError(std::move(Err));
     160           0 :             return SymbolNameSet();
     161             :           } else
     162             :             UnresolvedSymbols.insert(S);
     163             :         }
     164             :       }
     165             : 
     166             :       return UnresolvedSymbols;
     167             :     }
     168             : 
     169          35 :     SymbolNameSet lookup(std::shared_ptr<AsynchronousSymbolQuery> Query,
     170             :                          SymbolNameSet Symbols) override {
     171             :       SymbolNameSet UnresolvedSymbols;
     172             : 
     173          95 :       for (auto &S : Symbols) {
     174         180 :         if (auto Sym = M.findMangledSymbol(*S)) {
     175         120 :           if (auto Addr = Sym.getAddress())
     176         180 :             Query->setDefinition(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
     177             :           else {
     178           0 :             Query->setFailed(Addr.takeError());
     179           0 :             return SymbolNameSet();
     180             :           }
     181           0 :         } else if (auto Err = Sym.takeError()) {
     182           0 :           Query->setFailed(std::move(Err));
     183           0 :           return SymbolNameSet();
     184             :         } else {
     185           0 :           if (auto Sym2 = M.ClientResolver->findSymbol(*S)) {
     186           0 :             if (auto Addr = Sym2.getAddress())
     187           0 :               Query->setDefinition(S,
     188             :                                    JITEvaluatedSymbol(*Addr, Sym2.getFlags()));
     189             :             else {
     190           0 :               Query->setFailed(Addr.takeError());
     191           0 :               return SymbolNameSet();
     192             :             }
     193           0 :           } else if (auto Err = Sym2.takeError()) {
     194           0 :             Query->setFailed(std::move(Err));
     195           0 :             return SymbolNameSet();
     196             :           } else
     197             :             UnresolvedSymbols.insert(S);
     198             :         }
     199             :       }
     200             : 
     201             :       return UnresolvedSymbols;
     202             :     }
     203             : 
     204             :   private:
     205             :     OrcMCJITReplacement &M;
     206             :   };
     207             : 
     208             : private:
     209             :   static ExecutionEngine *
     210          77 :   createOrcMCJITReplacement(std::string *ErrorMsg,
     211             :                             std::shared_ptr<MCJITMemoryManager> MemMgr,
     212             :                             std::shared_ptr<LegacyJITSymbolResolver> Resolver,
     213             :                             std::unique_ptr<TargetMachine> TM) {
     214             :     return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver),
     215         308 :                                    std::move(TM));
     216             :   }
     217             : 
     218           0 :   void reportError(Error Err) {
     219           0 :     logAllUnhandledErrors(std::move(Err), errs(), "MCJIT error: ");
     220           0 :   }
     221             : 
     222             : public:
     223          77 :   OrcMCJITReplacement(std::shared_ptr<MCJITMemoryManager> MemMgr,
     224             :                       std::shared_ptr<LegacyJITSymbolResolver> ClientResolver,
     225             :                       std::unique_ptr<TargetMachine> TM)
     226         154 :       : ExecutionEngine(TM->createDataLayout()), ES(SSP), TM(std::move(TM)),
     227             :         MemMgr(
     228             :             std::make_shared<MCJITReplacementMemMgr>(*this, std::move(MemMgr))),
     229             :         Resolver(std::make_shared<LinkingORCResolver>(*this)),
     230             :         ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
     231             :         NotifyFinalized(*this),
     232             :         ObjectLayer(
     233             :             ES,
     234         186 :             [this](VModuleKey K) {
     235             :               return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
     236             :             },
     237             :             NotifyObjectLoaded, NotifyFinalized),
     238             :         CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
     239         770 :         LazyEmitLayer(CompileLayer) {}
     240             : 
     241             :   static void Register() {
     242       81686 :     OrcMCJITReplacementCtor = createOrcMCJITReplacement;
     243             :   }
     244             : 
     245          90 :   void addModule(std::unique_ptr<Module> M) override {
     246             :     // If this module doesn't have a DataLayout attached then attach the
     247             :     // default.
     248         180 :     if (M->getDataLayout().isDefault()) {
     249          90 :       M->setDataLayout(getDataLayout());
     250             :     } else {
     251             :       assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
     252             :     }
     253          90 :     auto *MPtr = M.release();
     254          90 :     ShouldDelete[MPtr] = true;
     255          14 :     auto Deleter = [this](Module *Mod) {
     256             :       auto I = ShouldDelete.find(Mod);
     257          14 :       if (I != ShouldDelete.end() && I->second)
     258          14 :         delete Mod;
     259          14 :     };
     260         180 :     LocalModules.push_back(std::shared_ptr<Module>(MPtr, std::move(Deleter)));
     261             :     cantFail(
     262         360 :         LazyEmitLayer.addModule(ES.allocateVModule(), LocalModules.back()));
     263          90 :   }
     264             : 
     265           0 :   void addObjectFile(std::unique_ptr<object::ObjectFile> O) override {
     266           0 :     cantFail(ObjectLayer.addObject(
     267           0 :         ES.allocateVModule(), MemoryBuffer::getMemBufferCopy(O->getData())));
     268           0 :   }
     269             : 
     270           2 :   void addObjectFile(object::OwningBinary<object::ObjectFile> O) override {
     271           2 :     std::unique_ptr<object::ObjectFile> Obj;
     272           2 :     std::unique_ptr<MemoryBuffer> ObjBuffer;
     273           2 :     std::tie(Obj, ObjBuffer) = O.takeBinary();
     274           8 :     cantFail(ObjectLayer.addObject(ES.allocateVModule(), std::move(ObjBuffer)));
     275           2 :   }
     276             : 
     277           1 :   void addArchive(object::OwningBinary<object::Archive> A) override {
     278           1 :     Archives.push_back(std::move(A));
     279           1 :   }
     280             :   
     281           0 :   bool removeModule(Module *M) override {
     282           0 :     for (auto I = LocalModules.begin(), E = LocalModules.end(); I != E; ++I) {
     283           0 :       if (I->get() == M) {
     284           0 :         ShouldDelete[M] = false;
     285           0 :         LocalModules.erase(I);
     286             :         return true;
     287             :       }
     288             :     }
     289             :     return false;
     290             :   }
     291             : 
     292         207 :   uint64_t getSymbolAddress(StringRef Name) {
     293         414 :     return cantFail(findSymbol(Name).getAddress());
     294             :   }
     295             : 
     296         207 :   JITSymbol findSymbol(StringRef Name) {
     297         621 :     return findMangledSymbol(Mangle(Name));
     298             :   }
     299             : 
     300          76 :   void finalizeObject() override {
     301             :     // This is deprecated - Aim to remove in ExecutionEngine.
     302             :     // REMOVE IF POSSIBLE - Doesn't make sense for New JIT.
     303          76 :   }
     304             : 
     305           0 :   void mapSectionAddress(const void *LocalAddress,
     306             :                          uint64_t TargetAddress) override {
     307           0 :     for (auto &P : UnfinalizedSections)
     308             :       if (P.second.count(LocalAddress))
     309           0 :         ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress);
     310           0 :   }
     311             : 
     312           0 :   uint64_t getGlobalValueAddress(const std::string &Name) override {
     313           0 :     return getSymbolAddress(Name);
     314             :   }
     315             : 
     316          11 :   uint64_t getFunctionAddress(const std::string &Name) override {
     317          11 :     return getSymbolAddress(Name);
     318             :   }
     319             : 
     320         196 :   void *getPointerToFunction(Function *F) override {
     321         196 :     uint64_t FAddr = getSymbolAddress(F->getName());
     322         196 :     return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr));
     323             :   }
     324             : 
     325           0 :   void *getPointerToNamedFunction(StringRef Name,
     326             :                                   bool AbortOnFailure = true) override {
     327           0 :     uint64_t Addr = getSymbolAddress(Name);
     328           0 :     if (!Addr && AbortOnFailure)
     329           0 :       llvm_unreachable("Missing symbol!");
     330           0 :     return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr));
     331             :   }
     332             : 
     333             :   GenericValue runFunction(Function *F,
     334             :                            ArrayRef<GenericValue> ArgValues) override;
     335             : 
     336           1 :   void setObjectCache(ObjectCache *NewCache) override {
     337             :     CompileLayer.getCompiler().setObjectCache(NewCache);
     338           1 :   }
     339             : 
     340           0 :   void setProcessAllSections(bool ProcessAllSections) override {
     341             :     ObjectLayer.setProcessAllSections(ProcessAllSections);
     342           0 :   }
     343             : 
     344             :   void runStaticConstructorsDestructors(bool isDtors) override;
     345             : 
     346             : private:
     347         286 :   JITSymbol findMangledSymbol(StringRef Name) {
     348         980 :     if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
     349         164 :       return Sym;
     350         384 :     if (auto Sym = ClientResolver->findSymbol(Name))
     351         104 :       return Sym;
     352          34 :     if (auto Sym = scanArchives(Name))
     353           2 :       return Sym;
     354             : 
     355             :     return nullptr;
     356             :   }
     357             : 
     358          18 :   JITSymbol scanArchives(StringRef Name) {
     359          18 :     for (object::OwningBinary<object::Archive> &OB : Archives) {
     360             :       object::Archive *A = OB.getBinary();
     361             :       // Look for our symbols in each Archive
     362           2 :       auto OptionalChildOrErr = A->findSym(Name);
     363           2 :       if (!OptionalChildOrErr)
     364           0 :         report_fatal_error(OptionalChildOrErr.takeError());
     365             :       auto &OptionalChild = *OptionalChildOrErr;
     366           2 :       if (OptionalChild) {
     367             :         // FIXME: Support nested archives?
     368             :         Expected<std::unique_ptr<object::Binary>> ChildBinOrErr =
     369           2 :             OptionalChild->getAsBinary();
     370           2 :         if (!ChildBinOrErr) {
     371             :           // TODO: Actually report errors helpfully.
     372           0 :           consumeError(ChildBinOrErr.takeError());
     373           0 :           continue;
     374             :         }
     375             :         std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
     376           4 :         if (ChildBin->isObject()) {
     377           6 :           cantFail(ObjectLayer.addObject(
     378             :               ES.allocateVModule(),
     379           4 :               MemoryBuffer::getMemBufferCopy(ChildBin->getData())));
     380           2 :           if (auto Sym = ObjectLayer.findSymbol(Name, true))
     381           2 :             return Sym;
     382             :         }
     383             :       }
     384             :     }
     385             :     return nullptr;
     386             :   }
     387             : 
     388             :   class NotifyObjectLoadedT {
     389             :   public:
     390             :     using LoadedObjInfoListT =
     391             :         std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
     392             : 
     393          77 :     NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {}
     394             : 
     395          93 :     void operator()(VModuleKey K, const object::ObjectFile &Obj,
     396             :                     const RuntimeDyld::LoadedObjectInfo &Info) const {
     397          93 :       M.UnfinalizedSections[K] = std::move(M.SectionsAllocatedSinceLastLoad);
     398         186 :       M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
     399         186 :       M.MemMgr->notifyObjectLoaded(&M, Obj);
     400          93 :     }
     401             :   private:
     402             :     OrcMCJITReplacement &M;
     403             :   };
     404             : 
     405             :   class NotifyFinalizedT {
     406             :   public:
     407          77 :     NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {}
     408             : 
     409          93 :     void operator()(VModuleKey K) { M.UnfinalizedSections.erase(K); }
     410             : 
     411             :   private:
     412             :     OrcMCJITReplacement &M;
     413             :   };
     414             : 
     415         207 :   std::string Mangle(StringRef Name) {
     416             :     std::string MangledName;
     417             :     {
     418         207 :       raw_string_ostream MangledNameStream(MangledName);
     419         207 :       Mang.getNameWithPrefix(MangledNameStream, Name, getDataLayout());
     420             :     }
     421         207 :     return MangledName;
     422             :   }
     423             : 
     424             :   using ObjectLayerT = RTDyldObjectLinkingLayer;
     425             :   using CompileLayerT = IRCompileLayer<ObjectLayerT, orc::SimpleCompiler>;
     426             :   using LazyEmitLayerT = LazyEmittingLayer<CompileLayerT>;
     427             : 
     428             :   SymbolStringPool SSP;
     429             :   ExecutionSession ES;
     430             : 
     431             :   std::unique_ptr<TargetMachine> TM;
     432             :   std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
     433             :   std::shared_ptr<LinkingORCResolver> Resolver;
     434             :   std::shared_ptr<LegacyJITSymbolResolver> ClientResolver;
     435             :   Mangler Mang;
     436             : 
     437             :   // IMPORTANT: ShouldDelete *must* come before LocalModules: The shared_ptr
     438             :   // delete blocks in LocalModules refer to the ShouldDelete map, so
     439             :   // LocalModules needs to be destructed before ShouldDelete.
     440             :   std::map<Module*, bool> ShouldDelete;
     441             :   std::vector<std::shared_ptr<Module>> LocalModules;
     442             : 
     443             :   NotifyObjectLoadedT NotifyObjectLoaded;
     444             :   NotifyFinalizedT NotifyFinalized;
     445             : 
     446             :   ObjectLayerT ObjectLayer;
     447             :   CompileLayerT CompileLayer;
     448             :   LazyEmitLayerT LazyEmitLayer;
     449             : 
     450             :   // We need to store ObjLayerT::ObjSetHandles for each of the object sets
     451             :   // that have been emitted but not yet finalized so that we can forward the
     452             :   // mapSectionAddress calls appropriately.
     453             :   using SectionAddrSet = std::set<const void *>;
     454             :   SectionAddrSet SectionsAllocatedSinceLastLoad;
     455             :   std::map<VModuleKey, SectionAddrSet> UnfinalizedSections;
     456             : 
     457             :   std::vector<object::OwningBinary<object::Archive>> Archives;
     458             : };
     459             : 
     460             : } // end namespace orc
     461             : 
     462             : } // end namespace llvm
     463             : 
     464             : #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H

Generated by: LCOV version 1.13