LLVM API Documentation

lib/ExecutionEngine/MCJIT/MCJIT.h
Go to the documentation of this file.
00001 //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H
00011 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H
00012 
00013 #include "llvm/ADT/DenseMap.h"
00014 #include "llvm/ADT/SmallPtrSet.h"
00015 #include "llvm/ADT/SmallVector.h"
00016 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00017 #include "llvm/ExecutionEngine/ObjectCache.h"
00018 #include "llvm/ExecutionEngine/ObjectImage.h"
00019 #include "llvm/ExecutionEngine/RuntimeDyld.h"
00020 #include "llvm/IR/Module.h"
00021 
00022 namespace llvm {
00023 class MCJIT;
00024 
00025 // This is a helper class that the MCJIT execution engine uses for linking
00026 // functions across modules that it owns.  It aggregates the memory manager
00027 // that is passed in to the MCJIT constructor and defers most functionality
00028 // to that object.
00029 class LinkingMemoryManager : public RTDyldMemoryManager {
00030 public:
00031   LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
00032     : ParentEngine(Parent), ClientMM(MM) {}
00033 
00034   uint64_t getSymbolAddress(const std::string &Name) override;
00035 
00036   // Functions deferred to client memory manager
00037   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
00038                                unsigned SectionID,
00039                                StringRef SectionName) override {
00040     return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
00041   }
00042 
00043   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
00044                                unsigned SectionID, StringRef SectionName,
00045                                bool IsReadOnly) override {
00046     return ClientMM->allocateDataSection(Size, Alignment,
00047                                          SectionID, SectionName, IsReadOnly);
00048   }
00049 
00050   void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
00051                               uintptr_t DataSizeRW) override {
00052     return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
00053   }
00054 
00055   bool needsToReserveAllocationSpace() override {
00056     return ClientMM->needsToReserveAllocationSpace();
00057   }
00058 
00059   void notifyObjectLoaded(ExecutionEngine *EE,
00060                           const ObjectImage *Obj) override {
00061     ClientMM->notifyObjectLoaded(EE, Obj);
00062   }
00063 
00064   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
00065                         size_t Size) override {
00066     ClientMM->registerEHFrames(Addr, LoadAddr, Size);
00067   }
00068 
00069   void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
00070                           size_t Size) override {
00071     ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
00072   }
00073 
00074   bool finalizeMemory(std::string *ErrMsg = nullptr) override {
00075     return ClientMM->finalizeMemory(ErrMsg);
00076   }
00077 
00078 private:
00079   MCJIT *ParentEngine;
00080   std::unique_ptr<RTDyldMemoryManager> ClientMM;
00081 };
00082 
00083 // About Module states: added->loaded->finalized.
00084 //
00085 // The purpose of the "added" state is having modules in standby. (added=known
00086 // but not compiled). The idea is that you can add a module to provide function
00087 // definitions but if nothing in that module is referenced by a module in which
00088 // a function is executed (note the wording here because it's not exactly the
00089 // ideal case) then the module never gets compiled. This is sort of lazy
00090 // compilation.
00091 //
00092 // The purpose of the "loaded" state (loaded=compiled and required sections
00093 // copied into local memory but not yet ready for execution) is to have an
00094 // intermediate state wherein clients can remap the addresses of sections, using
00095 // MCJIT::mapSectionAddress, (in preparation for later copying to a new location
00096 // or an external process) before relocations and page permissions are applied.
00097 //
00098 // It might not be obvious at first glance, but the "remote-mcjit" case in the
00099 // lli tool does this.  In that case, the intermediate action is taken by the
00100 // RemoteMemoryManager in response to the notifyObjectLoaded function being
00101 // called.
00102 
00103 class MCJIT : public ExecutionEngine {
00104   MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
00105         RTDyldMemoryManager *MemMgr);
00106 
00107   typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
00108 
00109   class OwningModuleContainer {
00110   public:
00111     OwningModuleContainer() {
00112     }
00113     ~OwningModuleContainer() {
00114       freeModulePtrSet(AddedModules);
00115       freeModulePtrSet(LoadedModules);
00116       freeModulePtrSet(FinalizedModules);
00117     }
00118 
00119     ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
00120     ModulePtrSet::iterator end_added() { return AddedModules.end(); }
00121     iterator_range<ModulePtrSet::iterator> added() {
00122       return iterator_range<ModulePtrSet::iterator>(begin_added(), end_added());
00123     }
00124 
00125     ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
00126     ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }
00127 
00128     ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
00129     ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
00130 
00131     void addModule(std::unique_ptr<Module> M) {
00132       AddedModules.insert(M.release());
00133     }
00134 
00135     bool removeModule(Module *M) {
00136       return AddedModules.erase(M) || LoadedModules.erase(M) ||
00137              FinalizedModules.erase(M);
00138     }
00139 
00140     bool hasModuleBeenAddedButNotLoaded(Module *M) {
00141       return AddedModules.count(M) != 0;
00142     }
00143 
00144     bool hasModuleBeenLoaded(Module *M) {
00145       // If the module is in either the "loaded" or "finalized" sections it
00146       // has been loaded.
00147       return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0);
00148     }
00149 
00150     bool hasModuleBeenFinalized(Module *M) {
00151       return FinalizedModules.count(M) != 0;
00152     }
00153 
00154     bool ownsModule(Module* M) {
00155       return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) ||
00156              (FinalizedModules.count(M) != 0);
00157     }
00158 
00159     void markModuleAsLoaded(Module *M) {
00160       // This checks against logic errors in the MCJIT implementation.
00161       // This function should never be called with either a Module that MCJIT
00162       // does not own or a Module that has already been loaded and/or finalized.
00163       assert(AddedModules.count(M) &&
00164              "markModuleAsLoaded: Module not found in AddedModules");
00165 
00166       // Remove the module from the "Added" set.
00167       AddedModules.erase(M);
00168 
00169       // Add the Module to the "Loaded" set.
00170       LoadedModules.insert(M);
00171     }
00172 
00173     void markModuleAsFinalized(Module *M) {
00174       // This checks against logic errors in the MCJIT implementation.
00175       // This function should never be called with either a Module that MCJIT
00176       // does not own, a Module that has not been loaded or a Module that has
00177       // already been finalized.
00178       assert(LoadedModules.count(M) &&
00179              "markModuleAsFinalized: Module not found in LoadedModules");
00180 
00181       // Remove the module from the "Loaded" section of the list.
00182       LoadedModules.erase(M);
00183 
00184       // Add the Module to the "Finalized" section of the list by inserting it
00185       // before the 'end' iterator.
00186       FinalizedModules.insert(M);
00187     }
00188 
00189     void markAllLoadedModulesAsFinalized() {
00190       for (ModulePtrSet::iterator I = LoadedModules.begin(),
00191                                   E = LoadedModules.end();
00192            I != E; ++I) {
00193         Module *M = *I;
00194         FinalizedModules.insert(M);
00195       }
00196       LoadedModules.clear();
00197     }
00198 
00199   private:
00200     ModulePtrSet AddedModules;
00201     ModulePtrSet LoadedModules;
00202     ModulePtrSet FinalizedModules;
00203 
00204     void freeModulePtrSet(ModulePtrSet& MPS) {
00205       // Go through the module set and delete everything.
00206       for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) {
00207         Module *M = *I;
00208         delete M;
00209       }
00210       MPS.clear();
00211     }
00212   };
00213 
00214   std::unique_ptr<TargetMachine> TM;
00215   MCContext *Ctx;
00216   LinkingMemoryManager MemMgr;
00217   RuntimeDyld Dyld;
00218   std::vector<JITEventListener*> EventListeners;
00219 
00220   OwningModuleContainer OwnedModules;
00221 
00222   SmallVector<object::OwningBinary<object::Archive>, 2> Archives;
00223   SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers;
00224 
00225   SmallVector<std::unique_ptr<ObjectImage>, 2> LoadedObjects;
00226 
00227   // An optional ObjectCache to be notified of compiled objects and used to
00228   // perform lookup of pre-compiled code to avoid re-compilation.
00229   ObjectCache *ObjCache;
00230 
00231   Function *FindFunctionNamedInModulePtrSet(const char *FnName,
00232                                             ModulePtrSet::iterator I,
00233                                             ModulePtrSet::iterator E);
00234 
00235   void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,
00236                                                       ModulePtrSet::iterator I,
00237                                                       ModulePtrSet::iterator E);
00238 
00239 public:
00240   ~MCJIT();
00241 
00242   /// @name ExecutionEngine interface implementation
00243   /// @{
00244   void addModule(std::unique_ptr<Module> M) override;
00245   void addObjectFile(std::unique_ptr<object::ObjectFile> O) override;
00246   void addObjectFile(object::OwningBinary<object::ObjectFile> O) override;
00247   void addArchive(object::OwningBinary<object::Archive> O) override;
00248   bool removeModule(Module *M) override;
00249 
00250   /// FindFunctionNamed - Search all of the active modules to find the one that
00251   /// defines FnName.  This is very slow operation and shouldn't be used for
00252   /// general code.
00253   Function *FindFunctionNamed(const char *FnName) override;
00254 
00255   /// Sets the object manager that MCJIT should use to avoid compilation.
00256   void setObjectCache(ObjectCache *manager) override;
00257 
00258   void setProcessAllSections(bool ProcessAllSections) override {
00259     Dyld.setProcessAllSections(ProcessAllSections);
00260   }
00261 
00262   void generateCodeForModule(Module *M) override;
00263 
00264   /// finalizeObject - ensure the module is fully processed and is usable.
00265   ///
00266   /// It is the user-level function for completing the process of making the
00267   /// object usable for execution. It should be called after sections within an
00268   /// object have been relocated using mapSectionAddress.  When this method is
00269   /// called the MCJIT execution engine will reapply relocations for a loaded
00270   /// object.
00271   /// Is it OK to finalize a set of modules, add modules and finalize again.
00272   // FIXME: Do we really need both of these?
00273   void finalizeObject() override;
00274   virtual void finalizeModule(Module *);
00275   void finalizeLoadedModules();
00276 
00277   /// runStaticConstructorsDestructors - This method is used to execute all of
00278   /// the static constructors or destructors for a program.
00279   ///
00280   /// \param isDtors - Run the destructors instead of constructors.
00281   void runStaticConstructorsDestructors(bool isDtors) override;
00282 
00283   void *getPointerToFunction(Function *F) override;
00284 
00285   GenericValue runFunction(Function *F,
00286                            const std::vector<GenericValue> &ArgValues) override;
00287 
00288   /// getPointerToNamedFunction - This method returns the address of the
00289   /// specified function by using the dlsym function call.  As such it is only
00290   /// useful for resolving library symbols, not code generated symbols.
00291   ///
00292   /// If AbortOnFailure is false and no function with the given name is
00293   /// found, this function silently returns a null pointer. Otherwise,
00294   /// it prints a message to stderr and aborts.
00295   ///
00296   void *getPointerToNamedFunction(StringRef Name,
00297                                   bool AbortOnFailure = true) override;
00298 
00299   /// mapSectionAddress - map a section to its target address space value.
00300   /// Map the address of a JIT section as returned from the memory manager
00301   /// to the address in the target process as the running code will see it.
00302   /// This is the address which will be used for relocation resolution.
00303   void mapSectionAddress(const void *LocalAddress,
00304                          uint64_t TargetAddress) override {
00305     Dyld.mapSectionAddress(LocalAddress, TargetAddress);
00306   }
00307   void RegisterJITEventListener(JITEventListener *L) override;
00308   void UnregisterJITEventListener(JITEventListener *L) override;
00309 
00310   // If successful, these function will implicitly finalize all loaded objects.
00311   // To get a function address within MCJIT without causing a finalize, use
00312   // getSymbolAddress.
00313   uint64_t getGlobalValueAddress(const std::string &Name) override;
00314   uint64_t getFunctionAddress(const std::string &Name) override;
00315 
00316   TargetMachine *getTargetMachine() override { return TM.get(); }
00317 
00318   /// @}
00319   /// @name (Private) Registration Interfaces
00320   /// @{
00321 
00322   static void Register() {
00323     MCJITCtor = createJIT;
00324   }
00325 
00326   static ExecutionEngine *createJIT(std::unique_ptr<Module> M,
00327                                     std::string *ErrorStr,
00328                                     RTDyldMemoryManager *MemMgr,
00329                                     std::unique_ptr<TargetMachine> TM);
00330 
00331   // @}
00332 
00333   // This is not directly exposed via the ExecutionEngine API, but it is
00334   // used by the LinkingMemoryManager.
00335   uint64_t getSymbolAddress(const std::string &Name,
00336                           bool CheckFunctionsOnly);
00337 
00338 protected:
00339   /// emitObject -- Generate a JITed object in memory from the specified module
00340   /// Currently, MCJIT only supports a single module and the module passed to
00341   /// this function call is expected to be the contained module.  The module
00342   /// is passed as a parameter here to prepare for multiple module support in
00343   /// the future.
00344   std::unique_ptr<ObjectBufferStream> emitObject(Module *M);
00345 
00346   void NotifyObjectEmitted(const ObjectImage& Obj);
00347   void NotifyFreeingObject(const ObjectImage& Obj);
00348 
00349   uint64_t getExistingSymbolAddress(const std::string &Name);
00350   Module *findModuleForSymbol(const std::string &Name,
00351                               bool CheckFunctionsOnly);
00352 };
00353 
00354 } // End llvm namespace
00355 
00356 #endif