LLVM  3.7.0
lib/ExecutionEngine/MCJIT/MCJIT.h
Go to the documentation of this file.
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_MCJIT_H
11 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/Module.h"
22 
23 namespace llvm {
24 class MCJIT;
25 
26 // This is a helper class that the MCJIT execution engine uses for linking
27 // functions across modules that it owns. It aggregates the memory manager
28 // that is passed in to the MCJIT constructor and defers most functionality
29 // to that object.
31 public:
33  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
34  : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {}
35 
36  RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override;
37 
38  // MCJIT doesn't support logical dylibs.
40  findSymbolInLogicalDylib(const std::string &Name) override {
41  return nullptr;
42  }
43 
44 private:
45  MCJIT &ParentEngine;
46  std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
47 };
48 
49 // About Module states: added->loaded->finalized.
50 //
51 // The purpose of the "added" state is having modules in standby. (added=known
52 // but not compiled). The idea is that you can add a module to provide function
53 // definitions but if nothing in that module is referenced by a module in which
54 // a function is executed (note the wording here because it's not exactly the
55 // ideal case) then the module never gets compiled. This is sort of lazy
56 // compilation.
57 //
58 // The purpose of the "loaded" state (loaded=compiled and required sections
59 // copied into local memory but not yet ready for execution) is to have an
60 // intermediate state wherein clients can remap the addresses of sections, using
61 // MCJIT::mapSectionAddress, (in preparation for later copying to a new location
62 // or an external process) before relocations and page permissions are applied.
63 //
64 // It might not be obvious at first glance, but the "remote-mcjit" case in the
65 // lli tool does this. In that case, the intermediate action is taken by the
66 // RemoteMemoryManager in response to the notifyObjectLoaded function being
67 // called.
68 
69 class MCJIT : public ExecutionEngine {
70  MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
71  std::shared_ptr<MCJITMemoryManager> MemMgr,
72  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver);
73 
75 
76  class OwningModuleContainer {
77  public:
78  OwningModuleContainer() {
79  }
80  ~OwningModuleContainer() {
81  freeModulePtrSet(AddedModules);
82  freeModulePtrSet(LoadedModules);
83  freeModulePtrSet(FinalizedModules);
84  }
85 
86  ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
87  ModulePtrSet::iterator end_added() { return AddedModules.end(); }
89  return iterator_range<ModulePtrSet::iterator>(begin_added(), end_added());
90  }
91 
92  ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
93  ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }
94 
95  ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
96  ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
97 
98  void addModule(std::unique_ptr<Module> M) {
99  AddedModules.insert(M.release());
100  }
101 
102  bool removeModule(Module *M) {
103  return AddedModules.erase(M) || LoadedModules.erase(M) ||
104  FinalizedModules.erase(M);
105  }
106 
107  bool hasModuleBeenAddedButNotLoaded(Module *M) {
108  return AddedModules.count(M) != 0;
109  }
110 
111  bool hasModuleBeenLoaded(Module *M) {
112  // If the module is in either the "loaded" or "finalized" sections it
113  // has been loaded.
114  return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0);
115  }
116 
117  bool hasModuleBeenFinalized(Module *M) {
118  return FinalizedModules.count(M) != 0;
119  }
120 
121  bool ownsModule(Module* M) {
122  return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) ||
123  (FinalizedModules.count(M) != 0);
124  }
125 
126  void markModuleAsLoaded(Module *M) {
127  // This checks against logic errors in the MCJIT implementation.
128  // This function should never be called with either a Module that MCJIT
129  // does not own or a Module that has already been loaded and/or finalized.
130  assert(AddedModules.count(M) &&
131  "markModuleAsLoaded: Module not found in AddedModules");
132 
133  // Remove the module from the "Added" set.
134  AddedModules.erase(M);
135 
136  // Add the Module to the "Loaded" set.
137  LoadedModules.insert(M);
138  }
139 
140  void markModuleAsFinalized(Module *M) {
141  // This checks against logic errors in the MCJIT implementation.
142  // This function should never be called with either a Module that MCJIT
143  // does not own, a Module that has not been loaded or a Module that has
144  // already been finalized.
145  assert(LoadedModules.count(M) &&
146  "markModuleAsFinalized: Module not found in LoadedModules");
147 
148  // Remove the module from the "Loaded" section of the list.
149  LoadedModules.erase(M);
150 
151  // Add the Module to the "Finalized" section of the list by inserting it
152  // before the 'end' iterator.
153  FinalizedModules.insert(M);
154  }
155 
156  void markAllLoadedModulesAsFinalized() {
157  for (ModulePtrSet::iterator I = LoadedModules.begin(),
158  E = LoadedModules.end();
159  I != E; ++I) {
160  Module *M = *I;
161  FinalizedModules.insert(M);
162  }
163  LoadedModules.clear();
164  }
165 
166  private:
167  ModulePtrSet AddedModules;
168  ModulePtrSet LoadedModules;
169  ModulePtrSet FinalizedModules;
170 
171  void freeModulePtrSet(ModulePtrSet& MPS) {
172  // Go through the module set and delete everything.
173  for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) {
174  Module *M = *I;
175  delete M;
176  }
177  MPS.clear();
178  }
179  };
180 
181  std::unique_ptr<TargetMachine> TM;
182  MCContext *Ctx;
183  std::shared_ptr<MCJITMemoryManager> MemMgr;
184  LinkingSymbolResolver Resolver;
185  RuntimeDyld Dyld;
186  std::vector<JITEventListener*> EventListeners;
187 
188  OwningModuleContainer OwnedModules;
189 
192 
194 
195  // An optional ObjectCache to be notified of compiled objects and used to
196  // perform lookup of pre-compiled code to avoid re-compilation.
197  ObjectCache *ObjCache;
198 
199  Function *FindFunctionNamedInModulePtrSet(const char *FnName,
202 
203  GlobalVariable *FindGlobalVariableNamedInModulePtrSet(const char *Name,
204  bool AllowInternal,
207 
208  void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,
211 
212 public:
213  ~MCJIT() override;
214 
215  /// @name ExecutionEngine interface implementation
216  /// @{
217  void addModule(std::unique_ptr<Module> M) override;
218  void addObjectFile(std::unique_ptr<object::ObjectFile> O) override;
221  bool removeModule(Module *M) override;
222 
223  /// FindFunctionNamed - Search all of the active modules to find the function that
224  /// defines FnName. This is very slow operation and shouldn't be used for
225  /// general code.
226  virtual Function *FindFunctionNamed(const char *FnName) override;
227 
228  /// FindGlobalVariableNamed - Search all of the active modules to find the global variable
229  /// that defines Name. This is very slow operation and shouldn't be used for
230  /// general code.
231  virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false) override;
232 
233  /// Sets the object manager that MCJIT should use to avoid compilation.
234  void setObjectCache(ObjectCache *manager) override;
235 
236  void setProcessAllSections(bool ProcessAllSections) override {
237  Dyld.setProcessAllSections(ProcessAllSections);
238  }
239 
240  void generateCodeForModule(Module *M) override;
241 
242  /// finalizeObject - ensure the module is fully processed and is usable.
243  ///
244  /// It is the user-level function for completing the process of making the
245  /// object usable for execution. It should be called after sections within an
246  /// object have been relocated using mapSectionAddress. When this method is
247  /// called the MCJIT execution engine will reapply relocations for a loaded
248  /// object.
249  /// Is it OK to finalize a set of modules, add modules and finalize again.
250  // FIXME: Do we really need both of these?
251  void finalizeObject() override;
252  virtual void finalizeModule(Module *);
253  void finalizeLoadedModules();
254 
255  /// runStaticConstructorsDestructors - This method is used to execute all of
256  /// the static constructors or destructors for a program.
257  ///
258  /// \param isDtors - Run the destructors instead of constructors.
259  void runStaticConstructorsDestructors(bool isDtors) override;
260 
261  void *getPointerToFunction(Function *F) override;
262 
264  ArrayRef<GenericValue> ArgValues) override;
265 
266  /// getPointerToNamedFunction - This method returns the address of the
267  /// specified function by using the dlsym function call. As such it is only
268  /// useful for resolving library symbols, not code generated symbols.
269  ///
270  /// If AbortOnFailure is false and no function with the given name is
271  /// found, this function silently returns a null pointer. Otherwise,
272  /// it prints a message to stderr and aborts.
273  ///
275  bool AbortOnFailure = true) override;
276 
277  /// mapSectionAddress - map a section to its target address space value.
278  /// Map the address of a JIT section as returned from the memory manager
279  /// to the address in the target process as the running code will see it.
280  /// This is the address which will be used for relocation resolution.
281  void mapSectionAddress(const void *LocalAddress,
282  uint64_t TargetAddress) override {
283  Dyld.mapSectionAddress(LocalAddress, TargetAddress);
284  }
285  void RegisterJITEventListener(JITEventListener *L) override;
287 
288  // If successful, these function will implicitly finalize all loaded objects.
289  // To get a function address within MCJIT without causing a finalize, use
290  // getSymbolAddress.
291  uint64_t getGlobalValueAddress(const std::string &Name) override;
292  uint64_t getFunctionAddress(const std::string &Name) override;
293 
294  TargetMachine *getTargetMachine() override { return TM.get(); }
295 
296  /// @}
297  /// @name (Private) Registration Interfaces
298  /// @{
299 
300  static void Register() {
302  }
303 
304  static ExecutionEngine*
305  createJIT(std::unique_ptr<Module> M,
306  std::string *ErrorStr,
307  std::shared_ptr<MCJITMemoryManager> MemMgr,
308  std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
309  std::unique_ptr<TargetMachine> TM);
310 
311  // @}
312 
313  RuntimeDyld::SymbolInfo findSymbol(const std::string &Name,
314  bool CheckFunctionsOnly);
315  // DEPRECATED - Please use findSymbol instead.
316  // This is not directly exposed via the ExecutionEngine API, but it is
317  // used by the LinkingMemoryManager.
318  uint64_t getSymbolAddress(const std::string &Name,
319  bool CheckFunctionsOnly);
320 
321 protected:
322  /// emitObject -- Generate a JITed object in memory from the specified module
323  /// Currently, MCJIT only supports a single module and the module passed to
324  /// this function call is expected to be the contained module. The module
325  /// is passed as a parameter here to prepare for multiple module support in
326  /// the future.
327  std::unique_ptr<MemoryBuffer> emitObject(Module *M);
328 
329  void NotifyObjectEmitted(const object::ObjectFile& Obj,
331  void NotifyFreeingObject(const object::ObjectFile& Obj);
332 
333  RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name);
334  Module *findModuleForSymbol(const std::string &Name,
335  bool CheckFunctionsOnly);
336 };
337 
338 } // End llvm namespace
339 
340 #endif
Information about the loaded object.
Definition: RuntimeDyld.h:59
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
Definition: MCJIT.cpp:136
virtual Function * FindFunctionNamed(const char *FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
Definition: MCJIT.cpp:450
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
This class is the base class for all object file types.
Definition: ObjectFile.h:176
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory...
Definition: MCJIT.cpp:179
F(f)
void UnregisterJITEventListener(JITEventListener *L) override
Definition: MCJIT.cpp:601
static ExecutionEngine *(* MCJITCtor)(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< RuntimeDyld::SymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: MCJIT.cpp:416
Context object for machine code objects.
Definition: MCContext.h:48
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Definition: MCJIT.cpp:357
virtual void finalizeModule(Module *)
Definition: MCJIT.cpp:253
RuntimeDyld::SymbolInfo findSymbolInLogicalDylib(const std::string &Name) override
This method returns the address of the specified symbol if it exists within the logical dynamic libra...
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:306
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
Definition: MCJIT.cpp:374
void setProcessAllSections(bool ProcessAllSections)
By default, only sections that are "required for execution" are passed to the RTDyldMemoryManager, and other sections are discarded.
Definition: RuntimeDyld.h:230
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Definition: MCJIT.cpp:238
static ExecutionEngine * createJIT(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< RuntimeDyld::SymbolResolver > Resolver, std::unique_ptr< TargetMachine > TM)
Definition: MCJIT.cpp:46
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
Definition: MCJIT.cpp:104
void setProcessAllSections(bool ProcessAllSections) override
setProcessAllSections (MCJIT Only): By default, only sections that are "required for execution" are p...
bool removeModule(Module *M) override
removeModule - Remove a Module from the list of modules.
Definition: MCJIT.cpp:109
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
Definition: MCJIT.cpp:573
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Definition: MCJIT.cpp:365
iterator begin() const
Definition: SmallPtrSet.h:286
TargetMachine * getTargetMachine() override
Return the target machine (if available).
virtual GlobalVariable * FindGlobalVariableNamed(const char *Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Definition: MCJIT.cpp:462
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Definition: MCJIT.cpp:132
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
Definition: SmallPtrSet.h:179
RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name)
Definition: MCJIT.cpp:266
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:301
uint64_t TargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:26
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Definition: MCJIT.cpp:594
A range adaptor for a pair of iterators.
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:278
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
Definition: MCJIT.cpp:474
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently, MCJIT only supports a single module and the module passed to this function call is expected to be the contained module.
Definition: MCJIT.cpp:141
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
Definition: MCJIT.cpp:628
iterator end() const
Definition: SmallPtrSet.h:289
#define I(x, y, z)
Definition: MD5.cpp:54
Information about a named symbol.
Definition: RuntimeDyld.h:47
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:22
LinkingSymbolResolver(MCJIT &Parent, std::shared_ptr< RuntimeDyld::SymbolResolver > Resolver)
~MCJIT() override
Definition: MCJIT.cpp:92
void NotifyFreeingObject(const object::ObjectFile &Obj)
Definition: MCJIT.cpp:621
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void NotifyObjectEmitted(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
Definition: MCJIT.cpp:612
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
Map a section to its target address space value.
void finalizeLoadedModules()
Definition: MCJIT.cpp:222
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
Definition: MCJIT.cpp:114