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