ExecutionEngine.h 25 KB

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