Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 1 | //===-- MCJIT.h - Class definition for the MCJIT ----------------*- 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 | #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H |
| 11 | #define LLVM_LIB_EXECUTIONENGINE_MCJIT_H |
| 12 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/ObjectImage.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 19 | #include "llvm/PassManager.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 23 | class MCJIT; |
| 24 | |
| 25 | // This is a helper class that the MCJIT execution engine uses for linking |
| 26 | // functions across modules that it owns. It aggregates the memory manager |
| 27 | // that is passed in to the MCJIT constructor and defers most functionality |
| 28 | // to that object. |
| 29 | class LinkingMemoryManager : public RTDyldMemoryManager { |
| 30 | public: |
| 31 | LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM) |
| 32 | : ParentEngine(Parent), ClientMM(MM) {} |
| 33 | |
| 34 | virtual uint64_t getSymbolAddress(const std::string &Name); |
| 35 | |
| 36 | // Functions deferred to client memory manager |
| 37 | virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 38 | unsigned SectionID, StringRef SectionName) { |
| 39 | return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 43 | unsigned SectionID, StringRef SectionName, |
| 44 | bool IsReadOnly) { |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 45 | return ClientMM->allocateDataSection(Size, Alignment, |
Filip Pizlo | 7aa695e0 | 2013-10-02 00:59:25 +0000 | [diff] [blame] | 46 | SectionID, SectionName, IsReadOnly); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Andrew Kaylor | 1b2cfb6 | 2013-10-04 00:49:38 +0000 | [diff] [blame] | 49 | virtual void notifyObjectLoaded(ExecutionEngine *EE, |
| 50 | const ObjectImage *Obj) { |
| 51 | ClientMM->notifyObjectLoaded(EE, Obj); |
| 52 | } |
| 53 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 54 | virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) { |
| 55 | ClientMM->registerEHFrames(Addr, LoadAddr, Size); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Andrew Kaylor | c442a76 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 58 | virtual void deregisterEHFrames(uint8_t *Addr, |
| 59 | uint64_t LoadAddr, |
| 60 | size_t Size) { |
| 61 | ClientMM->deregisterEHFrames(Addr, LoadAddr, Size); |
| 62 | } |
| 63 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 64 | virtual bool finalizeMemory(std::string *ErrMsg = 0) { |
| 65 | return ClientMM->finalizeMemory(ErrMsg); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | MCJIT *ParentEngine; |
| 70 | OwningPtr<RTDyldMemoryManager> ClientMM; |
| 71 | }; |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 72 | |
Yaron Keren | fb95582 | 2013-10-19 09:03:20 +0000 | [diff] [blame] | 73 | // About Module states: |
| 74 | // |
| 75 | // The purpose of the "added" state is having modules in standby. (added=known |
| 76 | // but not compiled). The idea is that you can add a module to provide function |
| 77 | // definitions but if nothing in that module is referenced by a module in which |
| 78 | // a function is executed (note the wording here because itÂ’s not exactly the |
| 79 | // ideal case) then the module never gets compiled. This is sort of lazy |
| 80 | // compilation. |
| 81 | // |
| 82 | // The purpose of the "loaded" state (loaded=compiled and required sections |
| 83 | // copied into local memory but not yet ready for execution) is to have an |
| 84 | // intermediate state wherein clients can remap the addresses of sections, using |
| 85 | // MCJIT::mapSectionAddress, (in preparation for later copying to a new location |
| 86 | // or an external process) before relocations and page permissions are applied. |
| 87 | // |
| 88 | // It might not be obvious at first glance, but the "remote-mcjit" case in the |
| 89 | // lli tool does this. In that case, the intermediate action is taken by the |
| 90 | // RemoteMemoryManager in response to the notifyObjectLoaded function being |
| 91 | // called. |
| 92 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 93 | class MCJIT : public ExecutionEngine { |
Jim Grosbach | bea6753 | 2012-08-21 15:42:49 +0000 | [diff] [blame] | 94 | MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr, |
| 95 | bool AllocateGVsWithCode); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 96 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 97 | enum ModuleState { |
| 98 | ModuleAdded, |
| 99 | ModuleEmitted, |
| 100 | ModuleLoading, |
| 101 | ModuleLoaded, |
| 102 | ModuleFinalizing, |
| 103 | ModuleFinalized |
| 104 | }; |
| 105 | |
| 106 | class MCJITModuleState { |
| 107 | public: |
| 108 | MCJITModuleState() : State(ModuleAdded) {} |
| 109 | |
| 110 | MCJITModuleState & operator=(ModuleState s) { State = s; return *this; } |
| 111 | bool hasBeenEmitted() { return State != ModuleAdded; } |
| 112 | bool hasBeenLoaded() { return State != ModuleAdded && |
| 113 | State != ModuleEmitted; } |
| 114 | bool hasBeenFinalized() { return State == ModuleFinalized; } |
| 115 | |
| 116 | private: |
| 117 | ModuleState State; |
| 118 | }; |
| 119 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 120 | TargetMachine *TM; |
| 121 | MCContext *Ctx; |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 122 | LinkingMemoryManager MemMgr; |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 123 | RuntimeDyld Dyld; |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 124 | SmallVector<JITEventListener*, 2> EventListeners; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 125 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 126 | typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap; |
| 127 | ModuleStateMap ModuleStates; |
| 128 | |
| 129 | typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap; |
| 130 | LoadedObjectMap LoadedObjects; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 131 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 132 | // An optional ObjectCache to be notified of compiled objects and used to |
| 133 | // perform lookup of pre-compiled code to avoid re-compilation. |
| 134 | ObjectCache *ObjCache; |
| 135 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 136 | public: |
| 137 | ~MCJIT(); |
| 138 | |
| 139 | /// @name ExecutionEngine interface implementation |
| 140 | /// @{ |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 141 | virtual void addModule(Module *M); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 142 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 143 | /// Sets the object manager that MCJIT should use to avoid compilation. |
| 144 | virtual void setObjectCache(ObjectCache *manager); |
| 145 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 146 | virtual void generateCodeForModule(Module *M); |
| 147 | |
David Tweed | 2e7efed | 2013-05-17 10:01:46 +0000 | [diff] [blame] | 148 | /// finalizeObject - ensure the module is fully processed and is usable. |
| 149 | /// |
| 150 | /// It is the user-level function for completing the process of making the |
| 151 | /// object usable for execution. It should be called after sections within an |
| 152 | /// object have been relocated using mapSectionAddress. When this method is |
| 153 | /// called the MCJIT execution engine will reapply relocations for a loaded |
| 154 | /// object. |
Yaron Keren | fb95582 | 2013-10-19 09:03:20 +0000 | [diff] [blame] | 155 | /// Is it OK to finalize a set of modules, add modules and finalize again. |
Andrew Kaylor | 2fb40ce | 2013-10-21 23:27:02 +0000 | [diff] [blame^] | 156 | // FIXME: Do we really need both of these? |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 157 | virtual void finalizeObject(); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 158 | virtual void finalizeModule(Module *); |
| 159 | void finalizeLoadedModules(); |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 161 | virtual void *getPointerToBasicBlock(BasicBlock *BB); |
| 162 | |
| 163 | virtual void *getPointerToFunction(Function *F); |
| 164 | |
| 165 | virtual void *recompileAndRelinkFunction(Function *F); |
| 166 | |
| 167 | virtual void freeMachineCodeForFunction(Function *F); |
| 168 | |
| 169 | virtual GenericValue runFunction(Function *F, |
| 170 | const std::vector<GenericValue> &ArgValues); |
| 171 | |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 172 | /// getPointerToNamedFunction - This method returns the address of the |
| 173 | /// specified function by using the dlsym function call. As such it is only |
| 174 | /// useful for resolving library symbols, not code generated symbols. |
| 175 | /// |
| 176 | /// If AbortOnFailure is false and no function with the given name is |
| 177 | /// found, this function silently returns a null pointer. Otherwise, |
| 178 | /// it prints a message to stderr and aborts. |
| 179 | /// |
Danil Malyshev | 7e32578 | 2012-01-05 21:16:14 +0000 | [diff] [blame] | 180 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 181 | bool AbortOnFailure = true); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 182 | |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 183 | /// mapSectionAddress - map a section to its target address space value. |
| 184 | /// Map the address of a JIT section as returned from the memory manager |
| 185 | /// to the address in the target process as the running code will see it. |
| 186 | /// This is the address which will be used for relocation resolution. |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 187 | virtual void mapSectionAddress(const void *LocalAddress, |
| 188 | uint64_t TargetAddress) { |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 189 | Dyld.mapSectionAddress(LocalAddress, TargetAddress); |
| 190 | } |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 191 | virtual void RegisterJITEventListener(JITEventListener *L); |
| 192 | virtual void UnregisterJITEventListener(JITEventListener *L); |
| 193 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 194 | // If successful, these function will implicitly finalize all loaded objects. |
| 195 | // To get a function address within MCJIT without causing a finalize, use |
| 196 | // getSymbolAddress. |
| 197 | virtual uint64_t getGlobalValueAddress(const std::string &Name); |
| 198 | virtual uint64_t getFunctionAddress(const std::string &Name); |
| 199 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 200 | /// @} |
| 201 | /// @name (Private) Registration Interfaces |
| 202 | /// @{ |
| 203 | |
| 204 | static void Register() { |
| 205 | MCJITCtor = createJIT; |
| 206 | } |
| 207 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 208 | static ExecutionEngine *createJIT(Module *M, |
| 209 | std::string *ErrorStr, |
Filip Pizlo | 9bc53e8 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 210 | RTDyldMemoryManager *MemMgr, |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 211 | bool GVsWithCode, |
Dylan Noblesmith | 8418fdc | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 212 | TargetMachine *TM); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 213 | |
| 214 | // @} |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 215 | |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 216 | // This is not directly exposed via the ExecutionEngine API, but it is |
| 217 | // used by the LinkingMemoryManager. |
| 218 | uint64_t getSymbolAddress(const std::string &Name, |
| 219 | bool CheckFunctionsOnly); |
| 220 | |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 221 | protected: |
| 222 | /// emitObject -- Generate a JITed object in memory from the specified module |
| 223 | /// Currently, MCJIT only supports a single module and the module passed to |
| 224 | /// this function call is expected to be the contained module. The module |
| 225 | /// is passed as a parameter here to prepare for multiple module support in |
| 226 | /// the future. |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 227 | ObjectBufferStream* emitObject(Module *M); |
| 228 | |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 229 | void NotifyObjectEmitted(const ObjectImage& Obj); |
| 230 | void NotifyFreeingObject(const ObjectImage& Obj); |
Andrew Kaylor | ea39592 | 2013-10-01 01:47:35 +0000 | [diff] [blame] | 231 | |
| 232 | uint64_t getExistingSymbolAddress(const std::string &Name); |
| 233 | Module *findModuleForSymbol(const std::string &Name, |
| 234 | bool CheckFunctionsOnly); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | } // End llvm namespace |
| 238 | |
| 239 | #endif |