Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:54

0001 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the abstract interface that implements execution support
0010 // for LLVM.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
0015 #define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
0016 
0017 #include "llvm-c/ExecutionEngine.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 #include "llvm/ADT/StringMap.h"
0021 #include "llvm/ADT/StringRef.h"
0022 #include "llvm/ExecutionEngine/JITSymbol.h"
0023 #include "llvm/IR/DataLayout.h"
0024 #include "llvm/IR/Module.h"
0025 #include "llvm/Object/Binary.h"
0026 #include "llvm/Support/CBindingWrapping.h"
0027 #include "llvm/Support/CodeGen.h"
0028 #include "llvm/Support/ErrorHandling.h"
0029 #include "llvm/Support/Mutex.h"
0030 #include "llvm/Target/TargetMachine.h"
0031 #include "llvm/Target/TargetOptions.h"
0032 #include <algorithm>
0033 #include <cstdint>
0034 #include <functional>
0035 #include <map>
0036 #include <memory>
0037 #include <optional>
0038 #include <string>
0039 #include <vector>
0040 
0041 namespace llvm {
0042 
0043 class Constant;
0044 class Function;
0045 struct GenericValue;
0046 class GlobalValue;
0047 class GlobalVariable;
0048 class JITEventListener;
0049 class MCJITMemoryManager;
0050 class ObjectCache;
0051 class RTDyldMemoryManager;
0052 class Triple;
0053 class Type;
0054 
0055 namespace object {
0056 
0057 class Archive;
0058 class ObjectFile;
0059 
0060 } // end namespace object
0061 
0062 /// Helper class for helping synchronize access to the global address map
0063 /// table.  Access to this class should be serialized under a mutex.
0064 class ExecutionEngineState {
0065 public:
0066   using GlobalAddressMapTy = StringMap<uint64_t>;
0067 
0068 private:
0069   /// GlobalAddressMap - A mapping between LLVM global symbol names values and
0070   /// their actualized version...
0071   GlobalAddressMapTy GlobalAddressMap;
0072 
0073   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
0074   /// used to convert raw addresses into the LLVM global value that is emitted
0075   /// at the address.  This map is not computed unless getGlobalValueAtAddress
0076   /// is called at some point.
0077   std::map<uint64_t, std::string> GlobalAddressReverseMap;
0078 
0079 public:
0080   GlobalAddressMapTy &getGlobalAddressMap() {
0081     return GlobalAddressMap;
0082   }
0083 
0084   std::map<uint64_t, std::string> &getGlobalAddressReverseMap() {
0085     return GlobalAddressReverseMap;
0086   }
0087 
0088   /// Erase an entry from the mapping table.
0089   ///
0090   /// \returns The address that \p ToUnmap was mapped to.
0091   uint64_t RemoveMapping(StringRef Name);
0092 };
0093 
0094 using FunctionCreator = std::function<void *(const std::string &)>;
0095 
0096 /// Abstract interface for implementation execution of LLVM modules,
0097 /// designed to support both interpreter and just-in-time (JIT) compiler
0098 /// implementations.
0099 class ExecutionEngine {
0100   /// The state object holding the global address mapping, which must be
0101   /// accessed synchronously.
0102   //
0103   // FIXME: There is no particular need the entire map needs to be
0104   // synchronized.  Wouldn't a reader-writer design be better here?
0105   ExecutionEngineState EEState;
0106 
0107   /// The target data for the platform for which execution is being performed.
0108   ///
0109   /// Note: the DataLayout is LLVMContext specific because it has an
0110   /// internal cache based on type pointers. It makes unsafe to reuse the
0111   /// ExecutionEngine across context, we don't enforce this rule but undefined
0112   /// behavior can occurs if the user tries to do it.
0113   const DataLayout DL;
0114 
0115   /// Whether lazy JIT compilation is enabled.
0116   bool CompilingLazily;
0117 
0118   /// Whether JIT compilation of external global variables is allowed.
0119   bool GVCompilationDisabled;
0120 
0121   /// Whether the JIT should perform lookups of external symbols (e.g.,
0122   /// using dlsym).
0123   bool SymbolSearchingDisabled;
0124 
0125   /// Whether the JIT should verify IR modules during compilation.
0126   bool VerifyModules;
0127 
0128   friend class EngineBuilder;  // To allow access to JITCtor and InterpCtor.
0129 
0130 protected:
0131   /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
0132   /// optimize for the case where there is only one module.
0133   SmallVector<std::unique_ptr<Module>, 1> Modules;
0134 
0135   /// getMemoryforGV - Allocate memory for a global variable.
0136   virtual char *getMemoryForGV(const GlobalVariable *GV);
0137 
0138   static ExecutionEngine *(*MCJITCtor)(
0139       std::unique_ptr<Module> M, std::string *ErrorStr,
0140       std::shared_ptr<MCJITMemoryManager> MM,
0141       std::shared_ptr<LegacyJITSymbolResolver> SR,
0142       std::unique_ptr<TargetMachine> TM);
0143 
0144   static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
0145                                         std::string *ErrorStr);
0146 
0147   /// LazyFunctionCreator - If an unknown function is needed, this function
0148   /// pointer is invoked to create it.  If this returns null, the JIT will
0149   /// abort.
0150   FunctionCreator LazyFunctionCreator;
0151 
0152   /// getMangledName - Get mangled name.
0153   std::string getMangledName(const GlobalValue *GV);
0154 
0155   std::string ErrMsg;
0156 
0157 public:
0158   /// lock - This lock protects the ExecutionEngine and MCJIT classes. It must
0159   /// be held while changing the internal state of any of those classes.
0160   sys::Mutex lock;
0161 
0162   //===--------------------------------------------------------------------===//
0163   //  ExecutionEngine Startup
0164   //===--------------------------------------------------------------------===//
0165 
0166   virtual ~ExecutionEngine();
0167 
0168   /// Add a Module to the list of modules that we can JIT from.
0169   virtual void addModule(std::unique_ptr<Module> M) {
0170     Modules.push_back(std::move(M));
0171   }
0172 
0173   /// addObjectFile - Add an ObjectFile to the execution engine.
0174   ///
0175   /// This method is only supported by MCJIT.  MCJIT will immediately load the
0176   /// object into memory and adds its symbols to the list used to resolve
0177   /// external symbols while preparing other objects for execution.
0178   ///
0179   /// Objects added using this function will not be made executable until
0180   /// needed by another object.
0181   ///
0182   /// MCJIT will take ownership of the ObjectFile.
0183   virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
0184   virtual void addObjectFile(object::OwningBinary<object::ObjectFile> O);
0185 
0186   /// addArchive - Add an Archive to the execution engine.
0187   ///
0188   /// This method is only supported by MCJIT.  MCJIT will use the archive to
0189   /// resolve external symbols in objects it is loading.  If a symbol is found
0190   /// in the Archive the contained object file will be extracted (in memory)
0191   /// and loaded for possible execution.
0192   virtual void addArchive(object::OwningBinary<object::Archive> A);
0193 
0194   //===--------------------------------------------------------------------===//
0195 
0196   const DataLayout &getDataLayout() const { return DL; }
0197 
0198   /// removeModule - Removes a Module from the list of modules, but does not
0199   /// free the module's memory. Returns true if M is found, in which case the
0200   /// caller assumes responsibility for deleting the module.
0201   //
0202   // FIXME: This stealth ownership transfer is horrible. This will probably be
0203   //        fixed by deleting ExecutionEngine.
0204   virtual bool removeModule(Module *M);
0205 
0206   /// FindFunctionNamed - Search all of the active modules to find the function that
0207   /// defines FnName.  This is very slow operation and shouldn't be used for
0208   /// general code.
0209   virtual Function *FindFunctionNamed(StringRef FnName);
0210 
0211   /// FindGlobalVariableNamed - Search all of the active modules to find the global variable
0212   /// that defines Name.  This is very slow operation and shouldn't be used for
0213   /// general code.
0214   virtual GlobalVariable *FindGlobalVariableNamed(StringRef Name, bool AllowInternal = false);
0215 
0216   /// runFunction - Execute the specified function with the specified arguments,
0217   /// and return the result.
0218   ///
0219   /// For MCJIT execution engines, clients are encouraged to use the
0220   /// "GetFunctionAddress" method (rather than runFunction) and cast the
0221   /// returned uint64_t to the desired function pointer type. However, for
0222   /// backwards compatibility MCJIT's implementation can execute 'main-like'
0223   /// function (i.e. those returning void or int, and taking either no
0224   /// arguments or (int, char*[])).
0225   virtual GenericValue runFunction(Function *F,
0226                                    ArrayRef<GenericValue> ArgValues) = 0;
0227 
0228   /// getPointerToNamedFunction - This method returns the address of the
0229   /// specified function by using the dlsym function call.  As such it is only
0230   /// useful for resolving library symbols, not code generated symbols.
0231   ///
0232   /// If AbortOnFailure is false and no function with the given name is
0233   /// found, this function silently returns a null pointer. Otherwise,
0234   /// it prints a message to stderr and aborts.
0235   ///
0236   /// This function is deprecated for the MCJIT execution engine.
0237   virtual void *getPointerToNamedFunction(StringRef Name,
0238                                           bool AbortOnFailure = true) = 0;
0239 
0240   /// mapSectionAddress - map a section to its target address space value.
0241   /// Map the address of a JIT section as returned from the memory manager
0242   /// to the address in the target process as the running code will see it.
0243   /// This is the address which will be used for relocation resolution.
0244   virtual void mapSectionAddress(const void *LocalAddress,
0245                                  uint64_t TargetAddress) {
0246     llvm_unreachable("Re-mapping of section addresses not supported with this "
0247                      "EE!");
0248   }
0249 
0250   /// generateCodeForModule - Run code generation for the specified module and
0251   /// load it into memory.
0252   ///
0253   /// When this function has completed, all code and data for the specified
0254   /// module, and any module on which this module depends, will be generated
0255   /// and loaded into memory, but relocations will not yet have been applied
0256   /// and all memory will be readable and writable but not executable.
0257   ///
0258   /// This function is primarily useful when generating code for an external
0259   /// target, allowing the client an opportunity to remap section addresses
0260   /// before relocations are applied.  Clients that intend to execute code
0261   /// locally can use the getFunctionAddress call, which will generate code
0262   /// and apply final preparations all in one step.
0263   ///
0264   /// This method has no effect for the interpreter.
0265   virtual void generateCodeForModule(Module *M) {}
0266 
0267   /// finalizeObject - ensure the module is fully processed and is usable.
0268   ///
0269   /// It is the user-level function for completing the process of making the
0270   /// object usable for execution.  It should be called after sections within an
0271   /// object have been relocated using mapSectionAddress.  When this method is
0272   /// called the MCJIT execution engine will reapply relocations for a loaded
0273   /// object.  This method has no effect for the interpreter.
0274   ///
0275   /// Returns true on success, false on failure. Error messages can be retrieved
0276   /// by calling getError();
0277   virtual void finalizeObject() {}
0278 
0279   /// Returns true if an error has been recorded.
0280   bool hasError() const { return !ErrMsg.empty(); }
0281 
0282   /// Clear the error message.
0283   void clearErrorMessage() { ErrMsg.clear(); }
0284 
0285   /// Returns the most recent error message.
0286   const std::string &getErrorMessage() const { return ErrMsg; }
0287 
0288   /// runStaticConstructorsDestructors - This method is used to execute all of
0289   /// the static constructors or destructors for a program.
0290   ///
0291   /// \param isDtors - Run the destructors instead of constructors.
0292   virtual void runStaticConstructorsDestructors(bool isDtors);
0293 
0294   /// This method is used to execute all of the static constructors or
0295   /// destructors for a particular module.
0296   ///
0297   /// \param isDtors - Run the destructors instead of constructors.
0298   void runStaticConstructorsDestructors(Module &module, bool isDtors);
0299 
0300 
0301   /// runFunctionAsMain - This is a helper function which wraps runFunction to
0302   /// handle the common task of starting up main with the specified argc, argv,
0303   /// and envp parameters.
0304   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
0305                         const char * const * envp);
0306 
0307 
0308   /// addGlobalMapping - Tell the execution engine that the specified global is
0309   /// at the specified location.  This is used internally as functions are JIT'd
0310   /// and as global variables are laid out in memory.  It can and should also be
0311   /// used by clients of the EE that want to have an LLVM global overlay
0312   /// existing data in memory. Values to be mapped should be named, and have
0313   /// external or weak linkage. Mappings are automatically removed when their
0314   /// GlobalValue is destroyed.
0315   void addGlobalMapping(const GlobalValue *GV, void *Addr);
0316   void addGlobalMapping(StringRef Name, uint64_t Addr);
0317 
0318   /// clearAllGlobalMappings - Clear all global mappings and start over again,
0319   /// for use in dynamic compilation scenarios to move globals.
0320   void clearAllGlobalMappings();
0321 
0322   /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
0323   /// particular module, because it has been removed from the JIT.
0324   void clearGlobalMappingsFromModule(Module *M);
0325 
0326   /// updateGlobalMapping - Replace an existing mapping for GV with a new
0327   /// address.  This updates both maps as required.  If "Addr" is null, the
0328   /// entry for the global is removed from the mappings.  This returns the old
0329   /// value of the pointer, or null if it was not in the map.
0330   uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr);
0331   uint64_t updateGlobalMapping(StringRef Name, uint64_t Addr);
0332 
0333   /// getAddressToGlobalIfAvailable - This returns the address of the specified
0334   /// global symbol.
0335   uint64_t getAddressToGlobalIfAvailable(StringRef S);
0336 
0337   /// getPointerToGlobalIfAvailable - This returns the address of the specified
0338   /// global value if it is has already been codegen'd, otherwise it returns
0339   /// null.
0340   void *getPointerToGlobalIfAvailable(StringRef S);
0341   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
0342 
0343   /// getPointerToGlobal - This returns the address of the specified global
0344   /// value. This may involve code generation if it's a function.
0345   ///
0346   /// This function is deprecated for the MCJIT execution engine.  Use
0347   /// getGlobalValueAddress instead.
0348   void *getPointerToGlobal(const GlobalValue *GV);
0349 
0350   /// getPointerToFunction - The different EE's represent function bodies in
0351   /// different ways.  They should each implement this to say what a function
0352   /// pointer should look like.  When F is destroyed, the ExecutionEngine will
0353   /// remove its global mapping and free any machine code.  Be sure no threads
0354   /// are running inside F when that happens.
0355   ///
0356   /// This function is deprecated for the MCJIT execution engine.  Use
0357   /// getFunctionAddress instead.
0358   virtual void *getPointerToFunction(Function *F) = 0;
0359 
0360   /// getPointerToFunctionOrStub - If the specified function has been
0361   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
0362   /// a stub to implement lazy compilation if available.  See
0363   /// getPointerToFunction for the requirements on destroying F.
0364   ///
0365   /// This function is deprecated for the MCJIT execution engine.  Use
0366   /// getFunctionAddress instead.
0367   virtual void *getPointerToFunctionOrStub(Function *F) {
0368     // Default implementation, just codegen the function.
0369     return getPointerToFunction(F);
0370   }
0371 
0372   /// getGlobalValueAddress - Return the address of the specified global
0373   /// value. This may involve code generation.
0374   ///
0375   /// This function should not be called with the interpreter engine.
0376   virtual uint64_t getGlobalValueAddress(const std::string &Name) {
0377     // Default implementation for the interpreter.  MCJIT will override this.
0378     // JIT and interpreter clients should use getPointerToGlobal instead.
0379     return 0;
0380   }
0381 
0382   /// getFunctionAddress - Return the address of the specified function.
0383   /// This may involve code generation.
0384   virtual uint64_t getFunctionAddress(const std::string &Name) {
0385     // Default implementation for the interpreter.  MCJIT will override this.
0386     // Interpreter clients should use getPointerToFunction instead.
0387     return 0;
0388   }
0389 
0390   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
0391   /// at the specified address.
0392   ///
0393   const GlobalValue *getGlobalValueAtAddress(void *Addr);
0394 
0395   /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
0396   /// Ptr is the address of the memory at which to store Val, cast to
0397   /// GenericValue *.  It is not a pointer to a GenericValue containing the
0398   /// address at which to store Val.
0399   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
0400                           Type *Ty);
0401 
0402   void InitializeMemory(const Constant *Init, void *Addr);
0403 
0404   /// getOrEmitGlobalVariable - Return the address of the specified global
0405   /// variable, possibly emitting it to memory if needed.  This is used by the
0406   /// Emitter.
0407   ///
0408   /// This function is deprecated for the MCJIT execution engine.  Use
0409   /// getGlobalValueAddress instead.
0410   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
0411     return getPointerToGlobal((const GlobalValue *)GV);
0412   }
0413 
0414   /// Registers a listener to be called back on various events within
0415   /// the JIT.  See JITEventListener.h for more details.  Does not
0416   /// take ownership of the argument.  The argument may be NULL, in
0417   /// which case these functions do nothing.
0418   virtual void RegisterJITEventListener(JITEventListener *) {}
0419   virtual void UnregisterJITEventListener(JITEventListener *) {}
0420 
0421   /// Sets the pre-compiled object cache.  The ownership of the ObjectCache is
0422   /// not changed.  Supported by MCJIT but not the interpreter.
0423   virtual void setObjectCache(ObjectCache *) {
0424     llvm_unreachable("No support for an object cache");
0425   }
0426 
0427   /// setProcessAllSections (MCJIT Only): By default, only sections that are
0428   /// "required for execution" are passed to the RTDyldMemoryManager, and other
0429   /// sections are discarded. Passing 'true' to this method will cause
0430   /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
0431   /// of whether they are "required to execute" in the usual sense.
0432   ///
0433   /// Rationale: Some MCJIT clients want to be able to inspect metadata
0434   /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
0435   /// performance. Passing these sections to the memory manager allows the
0436   /// client to make policy about the relevant sections, rather than having
0437   /// MCJIT do it.
0438   virtual void setProcessAllSections(bool ProcessAllSections) {
0439     llvm_unreachable("No support for ProcessAllSections option");
0440   }
0441 
0442   /// Return the target machine (if available).
0443   virtual TargetMachine *getTargetMachine() { return nullptr; }
0444 
0445   /// DisableLazyCompilation - When lazy compilation is off (the default), the
0446   /// JIT will eagerly compile every function reachable from the argument to
0447   /// getPointerToFunction.  If lazy compilation is turned on, the JIT will only
0448   /// compile the one function and emit stubs to compile the rest when they're
0449   /// first called.  If lazy compilation is turned off again while some lazy
0450   /// stubs are still around, and one of those stubs is called, the program will
0451   /// abort.
0452   ///
0453   /// In order to safely compile lazily in a threaded program, the user must
0454   /// ensure that 1) only one thread at a time can call any particular lazy
0455   /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
0456   /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
0457   /// lazy stub.  See http://llvm.org/PR5184 for details.
0458   void DisableLazyCompilation(bool Disabled = true) {
0459     CompilingLazily = !Disabled;
0460   }
0461   bool isCompilingLazily() const {
0462     return CompilingLazily;
0463   }
0464 
0465   /// DisableGVCompilation - If called, the JIT will abort if it's asked to
0466   /// allocate space and populate a GlobalVariable that is not internal to
0467   /// the module.
0468   void DisableGVCompilation(bool Disabled = true) {
0469     GVCompilationDisabled = Disabled;
0470   }
0471   bool isGVCompilationDisabled() const {
0472     return GVCompilationDisabled;
0473   }
0474 
0475   /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
0476   /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
0477   /// resolve symbols in a custom way.
0478   void DisableSymbolSearching(bool Disabled = true) {
0479     SymbolSearchingDisabled = Disabled;
0480   }
0481   bool isSymbolSearchingDisabled() const {
0482     return SymbolSearchingDisabled;
0483   }
0484 
0485   /// Enable/Disable IR module verification.
0486   ///
0487   /// Note: Module verification is enabled by default in Debug builds, and
0488   /// disabled by default in Release. Use this method to override the default.
0489   void setVerifyModules(bool Verify) {
0490     VerifyModules = Verify;
0491   }
0492   bool getVerifyModules() const {
0493     return VerifyModules;
0494   }
0495 
0496   /// InstallLazyFunctionCreator - If an unknown function is needed, the
0497   /// specified function pointer is invoked to create it.  If it returns null,
0498   /// the JIT will abort.
0499   void InstallLazyFunctionCreator(FunctionCreator C) {
0500     LazyFunctionCreator = std::move(C);
0501   }
0502 
0503 protected:
0504   ExecutionEngine(DataLayout DL) : DL(std::move(DL)) {}
0505   explicit ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M);
0506   explicit ExecutionEngine(std::unique_ptr<Module> M);
0507 
0508   void emitGlobals();
0509 
0510   void emitGlobalVariable(const GlobalVariable *GV);
0511 
0512   GenericValue getConstantValue(const Constant *C);
0513   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
0514                            Type *Ty);
0515 
0516 private:
0517   void Init(std::unique_ptr<Module> M);
0518 };
0519 
0520 namespace EngineKind {
0521 
0522   // These are actually bitmasks that get or-ed together.
0523   enum Kind {
0524     JIT         = 0x1,
0525     Interpreter = 0x2
0526   };
0527   const static Kind Either = (Kind)(JIT | Interpreter);
0528 
0529 } // end namespace EngineKind
0530 
0531 /// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
0532 /// chaining the various set* methods, and terminating it with a .create()
0533 /// call.
0534 class EngineBuilder {
0535 private:
0536   std::unique_ptr<Module> M;
0537   EngineKind::Kind WhichEngine;
0538   std::string *ErrorStr;
0539   CodeGenOptLevel OptLevel;
0540   std::shared_ptr<MCJITMemoryManager> MemMgr;
0541   std::shared_ptr<LegacyJITSymbolResolver> Resolver;
0542   TargetOptions Options;
0543   std::optional<Reloc::Model> RelocModel;
0544   std::optional<CodeModel::Model> CMModel;
0545   std::string MArch;
0546   std::string MCPU;
0547   SmallVector<std::string, 4> MAttrs;
0548   bool VerifyModules;
0549   bool EmulatedTLS = true;
0550 
0551 public:
0552   /// Default constructor for EngineBuilder.
0553   EngineBuilder();
0554 
0555   /// Constructor for EngineBuilder.
0556   EngineBuilder(std::unique_ptr<Module> M);
0557 
0558   // Out-of-line since we don't have the def'n of RTDyldMemoryManager here.
0559   ~EngineBuilder();
0560 
0561   /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
0562   /// or whichever engine works.  This option defaults to EngineKind::Either.
0563   EngineBuilder &setEngineKind(EngineKind::Kind w) {
0564     WhichEngine = w;
0565     return *this;
0566   }
0567 
0568   /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
0569   /// clients to customize their memory allocation policies for the MCJIT. This
0570   /// is only appropriate for the MCJIT; setting this and configuring the builder
0571   /// to create anything other than MCJIT will cause a runtime error. If create()
0572   /// is called and is successful, the created engine takes ownership of the
0573   /// memory manager. This option defaults to NULL.
0574   EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
0575 
0576   EngineBuilder&
0577   setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
0578 
0579   EngineBuilder &setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR);
0580 
0581   /// setErrorStr - Set the error string to write to on error.  This option
0582   /// defaults to NULL.
0583   EngineBuilder &setErrorStr(std::string *e) {
0584     ErrorStr = e;
0585     return *this;
0586   }
0587 
0588   /// setOptLevel - Set the optimization level for the JIT.  This option
0589   /// defaults to CodeGenOptLevel::Default.
0590   EngineBuilder &setOptLevel(CodeGenOptLevel l) {
0591     OptLevel = l;
0592     return *this;
0593   }
0594 
0595   /// setTargetOptions - Set the target options that the ExecutionEngine
0596   /// target is using. Defaults to TargetOptions().
0597   EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
0598     Options = Opts;
0599     return *this;
0600   }
0601 
0602   /// setRelocationModel - Set the relocation model that the ExecutionEngine
0603   /// target is using. Defaults to target specific default "Reloc::Default".
0604   EngineBuilder &setRelocationModel(Reloc::Model RM) {
0605     RelocModel = RM;
0606     return *this;
0607   }
0608 
0609   /// setCodeModel - Set the CodeModel that the ExecutionEngine target
0610   /// data is using. Defaults to target specific default
0611   /// "CodeModel::JITDefault".
0612   EngineBuilder &setCodeModel(CodeModel::Model M) {
0613     CMModel = M;
0614     return *this;
0615   }
0616 
0617   /// setMArch - Override the architecture set by the Module's triple.
0618   EngineBuilder &setMArch(StringRef march) {
0619     MArch.assign(march.begin(), march.end());
0620     return *this;
0621   }
0622 
0623   /// setMCPU - Target a specific cpu type.
0624   EngineBuilder &setMCPU(StringRef mcpu) {
0625     MCPU.assign(mcpu.begin(), mcpu.end());
0626     return *this;
0627   }
0628 
0629   /// setVerifyModules - Set whether the JIT implementation should verify
0630   /// IR modules during compilation.
0631   EngineBuilder &setVerifyModules(bool Verify) {
0632     VerifyModules = Verify;
0633     return *this;
0634   }
0635 
0636   /// setMAttrs - Set cpu-specific attributes.
0637   template<typename StringSequence>
0638   EngineBuilder &setMAttrs(const StringSequence &mattrs) {
0639     MAttrs.clear();
0640     MAttrs.append(mattrs.begin(), mattrs.end());
0641     return *this;
0642   }
0643 
0644   void setEmulatedTLS(bool EmulatedTLS) {
0645     this->EmulatedTLS = EmulatedTLS;
0646   }
0647 
0648   TargetMachine *selectTarget();
0649 
0650   /// selectTarget - Pick a target either via -march or by guessing the native
0651   /// arch.  Add any CPU features specified via -mcpu or -mattr.
0652   TargetMachine *selectTarget(const Triple &TargetTriple,
0653                               StringRef MArch,
0654                               StringRef MCPU,
0655                               const SmallVectorImpl<std::string>& MAttrs);
0656 
0657   ExecutionEngine *create() {
0658     return create(selectTarget());
0659   }
0660 
0661   ExecutionEngine *create(TargetMachine *TM);
0662 };
0663 
0664 // Create wrappers for C Binding types (see CBindingWrapping.h).
0665 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
0666 
0667 } // end namespace llvm
0668 
0669 #endif // LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H