ExecutionEngine.h revision 280031
1//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- 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// This file defines the abstract interface that implements execution support
11// for LLVM.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
16#define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
17
18#include "llvm-c/ExecutionEngine.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/ValueHandle.h"
23#include "llvm/IR/ValueMap.h"
24#include "llvm/MC/MCCodeGenInfo.h"
25#include "llvm/Object/Binary.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/Mutex.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetOptions.h"
30#include <map>
31#include <string>
32#include <vector>
33
34namespace llvm {
35
36struct GenericValue;
37class Constant;
38class DataLayout;
39class ExecutionEngine;
40class Function;
41class GlobalVariable;
42class GlobalValue;
43class JITEventListener;
44class MachineCodeInfo;
45class MutexGuard;
46class ObjectCache;
47class RTDyldMemoryManager;
48class Triple;
49class Type;
50
51namespace object {
52  class Archive;
53  class ObjectFile;
54}
55
56/// \brief Helper class for helping synchronize access to the global address map
57/// table.  Access to this class should be serialized under a mutex.
58class ExecutionEngineState {
59public:
60  struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
61    typedef ExecutionEngineState *ExtraData;
62    static sys::Mutex *getMutex(ExecutionEngineState *EES);
63    static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
64    static void onRAUW(ExecutionEngineState *, const GlobalValue *,
65                       const GlobalValue *);
66  };
67
68  typedef ValueMap<const GlobalValue *, void *, AddressMapConfig>
69      GlobalAddressMapTy;
70
71private:
72  ExecutionEngine &EE;
73
74  /// GlobalAddressMap - A mapping between LLVM global values and their
75  /// actualized version...
76  GlobalAddressMapTy GlobalAddressMap;
77
78  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
79  /// used to convert raw addresses into the LLVM global value that is emitted
80  /// at the address.  This map is not computed unless getGlobalValueAtAddress
81  /// is called at some point.
82  std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap;
83
84public:
85  ExecutionEngineState(ExecutionEngine &EE);
86
87  GlobalAddressMapTy &getGlobalAddressMap() {
88    return GlobalAddressMap;
89  }
90
91  std::map<void*, AssertingVH<const GlobalValue> > &
92  getGlobalAddressReverseMap() {
93    return GlobalAddressReverseMap;
94  }
95
96  /// \brief Erase an entry from the mapping table.
97  ///
98  /// \returns The address that \p ToUnmap was happed to.
99  void *RemoveMapping(const GlobalValue *ToUnmap);
100};
101
102/// \brief Abstract interface for implementation execution of LLVM modules,
103/// designed to support both interpreter and just-in-time (JIT) compiler
104/// implementations.
105class ExecutionEngine {
106  /// The state object holding the global address mapping, which must be
107  /// accessed synchronously.
108  //
109  // FIXME: There is no particular need the entire map needs to be
110  // synchronized.  Wouldn't a reader-writer design be better here?
111  ExecutionEngineState EEState;
112
113  /// The target data for the platform for which execution is being performed.
114  const DataLayout *DL;
115
116  /// Whether lazy JIT compilation is enabled.
117  bool CompilingLazily;
118
119  /// Whether JIT compilation of external global variables is allowed.
120  bool GVCompilationDisabled;
121
122  /// Whether the JIT should perform lookups of external symbols (e.g.,
123  /// using dlsym).
124  bool SymbolSearchingDisabled;
125
126  /// Whether the JIT should verify IR modules during compilation.
127  bool VerifyModules;
128
129  friend class EngineBuilder;  // To allow access to JITCtor and InterpCtor.
130
131protected:
132  /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
133  /// optimize for the case where there is only one module.
134  SmallVector<std::unique_ptr<Module>, 1> Modules;
135
136  void setDataLayout(const DataLayout *Val) { DL = Val; }
137
138  /// getMemoryforGV - Allocate memory for a global variable.
139  virtual char *getMemoryForGV(const GlobalVariable *GV);
140
141  static ExecutionEngine *(*MCJITCtor)(
142                                     std::unique_ptr<Module> M,
143                                     std::string *ErrorStr,
144                                     std::unique_ptr<RTDyldMemoryManager> MCJMM,
145                                     std::unique_ptr<TargetMachine> TM);
146  static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
147                                        std::string *ErrorStr);
148
149  /// LazyFunctionCreator - If an unknown function is needed, this function
150  /// pointer is invoked to create it.  If this returns null, the JIT will
151  /// abort.
152  void *(*LazyFunctionCreator)(const std::string &);
153
154public:
155  /// lock - This lock protects the ExecutionEngine and MCJIT classes. It must
156  /// be held while changing the internal state of any of those classes.
157  sys::Mutex lock;
158
159  //===--------------------------------------------------------------------===//
160  //  ExecutionEngine Startup
161  //===--------------------------------------------------------------------===//
162
163  virtual ~ExecutionEngine();
164
165  /// Add a Module to the list of modules that we can JIT from.
166  virtual void addModule(std::unique_ptr<Module> M) {
167    Modules.push_back(std::move(M));
168  }
169
170  /// addObjectFile - Add an ObjectFile to the execution engine.
171  ///
172  /// This method is only supported by MCJIT.  MCJIT will immediately load the
173  /// object into memory and adds its symbols to the list used to resolve
174  /// external symbols while preparing other objects for execution.
175  ///
176  /// Objects added using this function will not be made executable until
177  /// needed by another object.
178  ///
179  /// MCJIT will take ownership of the ObjectFile.
180  virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
181  virtual void addObjectFile(object::OwningBinary<object::ObjectFile> O);
182
183  /// addArchive - Add an Archive to the execution engine.
184  ///
185  /// This method is only supported by MCJIT.  MCJIT will use the archive to
186  /// resolve external symbols in objects it is loading.  If a symbol is found
187  /// in the Archive the contained object file will be extracted (in memory)
188  /// and loaded for possible execution.
189  virtual void addArchive(object::OwningBinary<object::Archive> A);
190
191  //===--------------------------------------------------------------------===//
192
193  const DataLayout *getDataLayout() const { return DL; }
194
195  /// removeModule - Remove a Module from the list of modules.  Returns true if
196  /// M is found.
197  virtual bool removeModule(Module *M);
198
199  /// FindFunctionNamed - Search all of the active modules to find the one that
200  /// defines FnName.  This is very slow operation and shouldn't be used for
201  /// general code.
202  virtual Function *FindFunctionNamed(const char *FnName);
203
204  /// runFunction - Execute the specified function with the specified arguments,
205  /// and return the result.
206  virtual GenericValue runFunction(Function *F,
207                                const std::vector<GenericValue> &ArgValues) = 0;
208
209  /// getPointerToNamedFunction - This method returns the address of the
210  /// specified function by using the dlsym function call.  As such it is only
211  /// useful for resolving library symbols, not code generated symbols.
212  ///
213  /// If AbortOnFailure is false and no function with the given name is
214  /// found, this function silently returns a null pointer. Otherwise,
215  /// it prints a message to stderr and aborts.
216  ///
217  /// This function is deprecated for the MCJIT execution engine.
218  virtual void *getPointerToNamedFunction(StringRef Name,
219                                          bool AbortOnFailure = true) = 0;
220
221  /// mapSectionAddress - map a section to its target address space value.
222  /// Map the address of a JIT section as returned from the memory manager
223  /// to the address in the target process as the running code will see it.
224  /// This is the address which will be used for relocation resolution.
225  virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) {
226    llvm_unreachable("Re-mapping of section addresses not supported with this "
227                     "EE!");
228  }
229
230  /// generateCodeForModule - Run code generation for the specified module and
231  /// load it into memory.
232  ///
233  /// When this function has completed, all code and data for the specified
234  /// module, and any module on which this module depends, will be generated
235  /// and loaded into memory, but relocations will not yet have been applied
236  /// and all memory will be readable and writable but not executable.
237  ///
238  /// This function is primarily useful when generating code for an external
239  /// target, allowing the client an opportunity to remap section addresses
240  /// before relocations are applied.  Clients that intend to execute code
241  /// locally can use the getFunctionAddress call, which will generate code
242  /// and apply final preparations all in one step.
243  ///
244  /// This method has no effect for the interpeter.
245  virtual void generateCodeForModule(Module *M) {}
246
247  /// finalizeObject - ensure the module is fully processed and is usable.
248  ///
249  /// It is the user-level function for completing the process of making the
250  /// object usable for execution.  It should be called after sections within an
251  /// object have been relocated using mapSectionAddress.  When this method is
252  /// called the MCJIT execution engine will reapply relocations for a loaded
253  /// object.  This method has no effect for the interpeter.
254  virtual void finalizeObject() {}
255
256  /// runStaticConstructorsDestructors - This method is used to execute all of
257  /// the static constructors or destructors for a program.
258  ///
259  /// \param isDtors - Run the destructors instead of constructors.
260  virtual void runStaticConstructorsDestructors(bool isDtors);
261
262  /// This method is used to execute all of the static constructors or
263  /// destructors for a particular module.
264  ///
265  /// \param isDtors - Run the destructors instead of constructors.
266  void runStaticConstructorsDestructors(Module &module, bool isDtors);
267
268
269  /// runFunctionAsMain - This is a helper function which wraps runFunction to
270  /// handle the common task of starting up main with the specified argc, argv,
271  /// and envp parameters.
272  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
273                        const char * const * envp);
274
275
276  /// addGlobalMapping - Tell the execution engine that the specified global is
277  /// at the specified location.  This is used internally as functions are JIT'd
278  /// and as global variables are laid out in memory.  It can and should also be
279  /// used by clients of the EE that want to have an LLVM global overlay
280  /// existing data in memory.  Mappings are automatically removed when their
281  /// GlobalValue is destroyed.
282  void addGlobalMapping(const GlobalValue *GV, void *Addr);
283
284  /// clearAllGlobalMappings - Clear all global mappings and start over again,
285  /// for use in dynamic compilation scenarios to move globals.
286  void clearAllGlobalMappings();
287
288  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
289  /// particular module, because it has been removed from the JIT.
290  void clearGlobalMappingsFromModule(Module *M);
291
292  /// updateGlobalMapping - Replace an existing mapping for GV with a new
293  /// address.  This updates both maps as required.  If "Addr" is null, the
294  /// entry for the global is removed from the mappings.  This returns the old
295  /// value of the pointer, or null if it was not in the map.
296  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
297
298  /// getPointerToGlobalIfAvailable - This returns the address of the specified
299  /// global value if it is has already been codegen'd, otherwise it returns
300  /// null.
301  ///
302  /// This function is deprecated for the MCJIT execution engine.  It doesn't
303  /// seem to be needed in that case, but an equivalent can be added if it is.
304  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
305
306  /// getPointerToGlobal - This returns the address of the specified global
307  /// value. This may involve code generation if it's a function.
308  ///
309  /// This function is deprecated for the MCJIT execution engine.  Use
310  /// getGlobalValueAddress instead.
311  void *getPointerToGlobal(const GlobalValue *GV);
312
313  /// getPointerToFunction - The different EE's represent function bodies in
314  /// different ways.  They should each implement this to say what a function
315  /// pointer should look like.  When F is destroyed, the ExecutionEngine will
316  /// remove its global mapping and free any machine code.  Be sure no threads
317  /// are running inside F when that happens.
318  ///
319  /// This function is deprecated for the MCJIT execution engine.  Use
320  /// getFunctionAddress instead.
321  virtual void *getPointerToFunction(Function *F) = 0;
322
323  /// getPointerToFunctionOrStub - If the specified function has been
324  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
325  /// a stub to implement lazy compilation if available.  See
326  /// getPointerToFunction for the requirements on destroying F.
327  ///
328  /// This function is deprecated for the MCJIT execution engine.  Use
329  /// getFunctionAddress instead.
330  virtual void *getPointerToFunctionOrStub(Function *F) {
331    // Default implementation, just codegen the function.
332    return getPointerToFunction(F);
333  }
334
335  /// getGlobalValueAddress - Return the address of the specified global
336  /// value. This may involve code generation.
337  ///
338  /// This function should not be called with the interpreter engine.
339  virtual uint64_t getGlobalValueAddress(const std::string &Name) {
340    // Default implementation for the interpreter.  MCJIT will override this.
341    // JIT and interpreter clients should use getPointerToGlobal instead.
342    return 0;
343  }
344
345  /// getFunctionAddress - Return the address of the specified function.
346  /// This may involve code generation.
347  virtual uint64_t getFunctionAddress(const std::string &Name) {
348    // Default implementation for the interpreter.  MCJIT will override this.
349    // Interpreter clients should use getPointerToFunction instead.
350    return 0;
351  }
352
353  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
354  /// at the specified address.
355  ///
356  const GlobalValue *getGlobalValueAtAddress(void *Addr);
357
358  /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
359  /// Ptr is the address of the memory at which to store Val, cast to
360  /// GenericValue *.  It is not a pointer to a GenericValue containing the
361  /// address at which to store Val.
362  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
363                          Type *Ty);
364
365  void InitializeMemory(const Constant *Init, void *Addr);
366
367  /// getOrEmitGlobalVariable - Return the address of the specified global
368  /// variable, possibly emitting it to memory if needed.  This is used by the
369  /// Emitter.
370  ///
371  /// This function is deprecated for the MCJIT execution engine.  Use
372  /// getGlobalValueAddress instead.
373  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
374    return getPointerToGlobal((const GlobalValue *)GV);
375  }
376
377  /// Registers a listener to be called back on various events within
378  /// the JIT.  See JITEventListener.h for more details.  Does not
379  /// take ownership of the argument.  The argument may be NULL, in
380  /// which case these functions do nothing.
381  virtual void RegisterJITEventListener(JITEventListener *) {}
382  virtual void UnregisterJITEventListener(JITEventListener *) {}
383
384  /// Sets the pre-compiled object cache.  The ownership of the ObjectCache is
385  /// not changed.  Supported by MCJIT but not the interpreter.
386  virtual void setObjectCache(ObjectCache *) {
387    llvm_unreachable("No support for an object cache");
388  }
389
390  /// setProcessAllSections (MCJIT Only): By default, only sections that are
391  /// "required for execution" are passed to the RTDyldMemoryManager, and other
392  /// sections are discarded. Passing 'true' to this method will cause
393  /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
394  /// of whether they are "required to execute" in the usual sense.
395  ///
396  /// Rationale: Some MCJIT clients want to be able to inspect metadata
397  /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
398  /// performance. Passing these sections to the memory manager allows the
399  /// client to make policy about the relevant sections, rather than having
400  /// MCJIT do it.
401  virtual void setProcessAllSections(bool ProcessAllSections) {
402    llvm_unreachable("No support for ProcessAllSections option");
403  }
404
405  /// Return the target machine (if available).
406  virtual TargetMachine *getTargetMachine() { return nullptr; }
407
408  /// DisableLazyCompilation - When lazy compilation is off (the default), the
409  /// JIT will eagerly compile every function reachable from the argument to
410  /// getPointerToFunction.  If lazy compilation is turned on, the JIT will only
411  /// compile the one function and emit stubs to compile the rest when they're
412  /// first called.  If lazy compilation is turned off again while some lazy
413  /// stubs are still around, and one of those stubs is called, the program will
414  /// abort.
415  ///
416  /// In order to safely compile lazily in a threaded program, the user must
417  /// ensure that 1) only one thread at a time can call any particular lazy
418  /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
419  /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
420  /// lazy stub.  See http://llvm.org/PR5184 for details.
421  void DisableLazyCompilation(bool Disabled = true) {
422    CompilingLazily = !Disabled;
423  }
424  bool isCompilingLazily() const {
425    return CompilingLazily;
426  }
427
428  /// DisableGVCompilation - If called, the JIT will abort if it's asked to
429  /// allocate space and populate a GlobalVariable that is not internal to
430  /// the module.
431  void DisableGVCompilation(bool Disabled = true) {
432    GVCompilationDisabled = Disabled;
433  }
434  bool isGVCompilationDisabled() const {
435    return GVCompilationDisabled;
436  }
437
438  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
439  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
440  /// resolve symbols in a custom way.
441  void DisableSymbolSearching(bool Disabled = true) {
442    SymbolSearchingDisabled = Disabled;
443  }
444  bool isSymbolSearchingDisabled() const {
445    return SymbolSearchingDisabled;
446  }
447
448  /// Enable/Disable IR module verification.
449  ///
450  /// Note: Module verification is enabled by default in Debug builds, and
451  /// disabled by default in Release. Use this method to override the default.
452  void setVerifyModules(bool Verify) {
453    VerifyModules = Verify;
454  }
455  bool getVerifyModules() const {
456    return VerifyModules;
457  }
458
459  /// InstallLazyFunctionCreator - If an unknown function is needed, the
460  /// specified function pointer is invoked to create it.  If it returns null,
461  /// the JIT will abort.
462  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
463    LazyFunctionCreator = P;
464  }
465
466protected:
467  explicit ExecutionEngine(std::unique_ptr<Module> M);
468
469  void emitGlobals();
470
471  void EmitGlobalVariable(const GlobalVariable *GV);
472
473  GenericValue getConstantValue(const Constant *C);
474  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
475                           Type *Ty);
476};
477
478namespace EngineKind {
479  // These are actually bitmasks that get or-ed together.
480  enum Kind {
481    JIT         = 0x1,
482    Interpreter = 0x2
483  };
484  const static Kind Either = (Kind)(JIT | Interpreter);
485}
486
487/// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
488/// chaining the various set* methods, and terminating it with a .create()
489/// call.
490class EngineBuilder {
491private:
492  std::unique_ptr<Module> M;
493  EngineKind::Kind WhichEngine;
494  std::string *ErrorStr;
495  CodeGenOpt::Level OptLevel;
496  std::unique_ptr<RTDyldMemoryManager> MCJMM;
497  TargetOptions Options;
498  Reloc::Model RelocModel;
499  CodeModel::Model CMModel;
500  std::string MArch;
501  std::string MCPU;
502  SmallVector<std::string, 4> MAttrs;
503  bool VerifyModules;
504
505  /// InitEngine - Does the common initialization of default options.
506  void InitEngine();
507
508public:
509  /// Constructor for EngineBuilder.
510  EngineBuilder(std::unique_ptr<Module> M);
511
512  // Out-of-line since we don't have the def'n of RTDyldMemoryManager here.
513  ~EngineBuilder();
514
515  /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
516  /// or whichever engine works.  This option defaults to EngineKind::Either.
517  EngineBuilder &setEngineKind(EngineKind::Kind w) {
518    WhichEngine = w;
519    return *this;
520  }
521
522  /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
523  /// clients to customize their memory allocation policies for the MCJIT. This
524  /// is only appropriate for the MCJIT; setting this and configuring the builder
525  /// to create anything other than MCJIT will cause a runtime error. If create()
526  /// is called and is successful, the created engine takes ownership of the
527  /// memory manager. This option defaults to NULL.
528  EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
529
530  /// setErrorStr - Set the error string to write to on error.  This option
531  /// defaults to NULL.
532  EngineBuilder &setErrorStr(std::string *e) {
533    ErrorStr = e;
534    return *this;
535  }
536
537  /// setOptLevel - Set the optimization level for the JIT.  This option
538  /// defaults to CodeGenOpt::Default.
539  EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
540    OptLevel = l;
541    return *this;
542  }
543
544  /// setTargetOptions - Set the target options that the ExecutionEngine
545  /// target is using. Defaults to TargetOptions().
546  EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
547    Options = Opts;
548    return *this;
549  }
550
551  /// setRelocationModel - Set the relocation model that the ExecutionEngine
552  /// target is using. Defaults to target specific default "Reloc::Default".
553  EngineBuilder &setRelocationModel(Reloc::Model RM) {
554    RelocModel = RM;
555    return *this;
556  }
557
558  /// setCodeModel - Set the CodeModel that the ExecutionEngine target
559  /// data is using. Defaults to target specific default
560  /// "CodeModel::JITDefault".
561  EngineBuilder &setCodeModel(CodeModel::Model M) {
562    CMModel = M;
563    return *this;
564  }
565
566  /// setMArch - Override the architecture set by the Module's triple.
567  EngineBuilder &setMArch(StringRef march) {
568    MArch.assign(march.begin(), march.end());
569    return *this;
570  }
571
572  /// setMCPU - Target a specific cpu type.
573  EngineBuilder &setMCPU(StringRef mcpu) {
574    MCPU.assign(mcpu.begin(), mcpu.end());
575    return *this;
576  }
577
578  /// setVerifyModules - Set whether the JIT implementation should verify
579  /// IR modules during compilation.
580  EngineBuilder &setVerifyModules(bool Verify) {
581    VerifyModules = Verify;
582    return *this;
583  }
584
585  /// setMAttrs - Set cpu-specific attributes.
586  template<typename StringSequence>
587  EngineBuilder &setMAttrs(const StringSequence &mattrs) {
588    MAttrs.clear();
589    MAttrs.append(mattrs.begin(), mattrs.end());
590    return *this;
591  }
592
593  TargetMachine *selectTarget();
594
595  /// selectTarget - Pick a target either via -march or by guessing the native
596  /// arch.  Add any CPU features specified via -mcpu or -mattr.
597  TargetMachine *selectTarget(const Triple &TargetTriple,
598                              StringRef MArch,
599                              StringRef MCPU,
600                              const SmallVectorImpl<std::string>& MAttrs);
601
602  ExecutionEngine *create() {
603    return create(selectTarget());
604  }
605
606  ExecutionEngine *create(TargetMachine *TM);
607};
608
609// Create wrappers for C Binding types (see CBindingWrapping.h).
610DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
611
612} // End llvm namespace
613
614#endif
615