1193323Sed//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This file defines the abstract interface that implements execution support 10193323Sed// for LLVM. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14249423Sdim#ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 15249423Sdim#define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 16193323Sed 17251662Sdim#include "llvm-c/ExecutionEngine.h" 18321369Sdim#include "llvm/ADT/ArrayRef.h" 19321369Sdim#include "llvm/ADT/Optional.h" 20193323Sed#include "llvm/ADT/SmallVector.h" 21321369Sdim#include "llvm/ADT/StringMap.h" 22203954Srdivacky#include "llvm/ADT/StringRef.h" 23321369Sdim#include "llvm/ExecutionEngine/JITSymbol.h" 24353358Sdim#include "llvm/ExecutionEngine/OrcV1Deprecation.h" 25321369Sdim#include "llvm/IR/DataLayout.h" 26280031Sdim#include "llvm/IR/Module.h" 27280031Sdim#include "llvm/Object/Binary.h" 28321369Sdim#include "llvm/Support/CBindingWrapping.h" 29321369Sdim#include "llvm/Support/CodeGen.h" 30234353Sdim#include "llvm/Support/ErrorHandling.h" 31249423Sdim#include "llvm/Support/Mutex.h" 32193323Sed#include "llvm/Target/TargetMachine.h" 33234353Sdim#include "llvm/Target/TargetOptions.h" 34321369Sdim#include <algorithm> 35321369Sdim#include <cstdint> 36321369Sdim#include <functional> 37234353Sdim#include <map> 38321369Sdim#include <memory> 39234353Sdim#include <string> 40249423Sdim#include <vector> 41193323Sed 42193323Sednamespace llvm { 43193323Sed 44193323Sedclass Constant; 45193323Sedclass Function; 46321369Sdimstruct GenericValue; 47321369Sdimclass GlobalValue; 48193323Sedclass GlobalVariable; 49195098Sedclass JITEventListener; 50288943Sdimclass MCJITMemoryManager; 51251662Sdimclass ObjectCache; 52261991Sdimclass RTDyldMemoryManager; 53234353Sdimclass Triple; 54193323Sedclass Type; 55193323Sed 56276479Sdimnamespace object { 57276479Sdim 58321369Sdimclass Archive; 59321369Sdimclass ObjectFile; 60321369Sdim 61321369Sdim} // end namespace object 62321369Sdim 63341825Sdim/// Helper class for helping synchronize access to the global address map 64276479Sdim/// table. Access to this class should be serialized under a mutex. 65193323Sedclass ExecutionEngineState { 66198090Srdivackypublic: 67321369Sdim using GlobalAddressMapTy = StringMap<uint64_t>; 68198090Srdivacky 69193323Sedprivate: 70288943Sdim /// GlobalAddressMap - A mapping between LLVM global symbol names values and 71288943Sdim /// their actualized version... 72198892Srdivacky GlobalAddressMapTy GlobalAddressMap; 73193323Sed 74193323Sed /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 75193323Sed /// used to convert raw addresses into the LLVM global value that is emitted 76193323Sed /// at the address. This map is not computed unless getGlobalValueAtAddress 77193323Sed /// is called at some point. 78288943Sdim std::map<uint64_t, std::string> GlobalAddressReverseMap; 79193323Sed 80193323Sedpublic: 81276479Sdim GlobalAddressMapTy &getGlobalAddressMap() { 82193323Sed return GlobalAddressMap; 83193323Sed } 84193323Sed 85288943Sdim std::map<uint64_t, std::string> &getGlobalAddressReverseMap() { 86193323Sed return GlobalAddressReverseMap; 87193323Sed } 88198090Srdivacky 89341825Sdim /// Erase an entry from the mapping table. 90218893Sdim /// 91243830Sdim /// \returns The address that \p ToUnmap was happed to. 92288943Sdim uint64_t RemoveMapping(StringRef Name); 93193323Sed}; 94193323Sed 95288943Sdimusing FunctionCreator = std::function<void *(const std::string &)>; 96288943Sdim 97341825Sdim/// Abstract interface for implementation execution of LLVM modules, 98218893Sdim/// designed to support both interpreter and just-in-time (JIT) compiler 99218893Sdim/// implementations. 100218893Sdimclass ExecutionEngine { 101218893Sdim /// The state object holding the global address mapping, which must be 102218893Sdim /// accessed synchronously. 103218893Sdim // 104218893Sdim // FIXME: There is no particular need the entire map needs to be 105218893Sdim // synchronized. Wouldn't a reader-writer design be better here? 106218893Sdim ExecutionEngineState EEState; 107193323Sed 108218893Sdim /// The target data for the platform for which execution is being performed. 109296417Sdim /// 110296417Sdim /// Note: the DataLayout is LLVMContext specific because it has an 111296417Sdim /// internal cache based on type pointers. It makes unsafe to reuse the 112296417Sdim /// ExecutionEngine across context, we don't enforce this rule but undefined 113296417Sdim /// behavior can occurs if the user tries to do it. 114296417Sdim const DataLayout DL; 115218893Sdim 116218893Sdim /// Whether lazy JIT compilation is enabled. 117198892Srdivacky bool CompilingLazily; 118218893Sdim 119218893Sdim /// Whether JIT compilation of external global variables is allowed. 120193323Sed bool GVCompilationDisabled; 121218893Sdim 122218893Sdim /// Whether the JIT should perform lookups of external symbols (e.g., 123218893Sdim /// using dlsym). 124193323Sed bool SymbolSearchingDisabled; 125193323Sed 126276479Sdim /// Whether the JIT should verify IR modules during compilation. 127276479Sdim bool VerifyModules; 128276479Sdim 129198090Srdivacky friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 130198090Srdivacky 131193323Sedprotected: 132218893Sdim /// The list of Modules that we are JIT'ing from. We use a SmallVector to 133218893Sdim /// optimize for the case where there is only one module. 134280031Sdim SmallVector<std::unique_ptr<Module>, 1> Modules; 135221345Sdim 136193323Sed /// getMemoryforGV - Allocate memory for a global variable. 137218893Sdim virtual char *getMemoryForGV(const GlobalVariable *GV); 138193323Sed 139218893Sdim static ExecutionEngine *(*MCJITCtor)( 140341825Sdim std::unique_ptr<Module> M, std::string *ErrorStr, 141341825Sdim std::shared_ptr<MCJITMemoryManager> MM, 142341825Sdim std::shared_ptr<LegacyJITSymbolResolver> SR, 143341825Sdim std::unique_ptr<TargetMachine> TM); 144288943Sdim 145288943Sdim static ExecutionEngine *(*OrcMCJITReplacementCtor)( 146341825Sdim std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MM, 147341825Sdim std::shared_ptr<LegacyJITSymbolResolver> SR, 148341825Sdim std::unique_ptr<TargetMachine> TM); 149288943Sdim 150280031Sdim static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M, 151280031Sdim std::string *ErrorStr); 152193323Sed 153193323Sed /// LazyFunctionCreator - If an unknown function is needed, this function 154218893Sdim /// pointer is invoked to create it. If this returns null, the JIT will 155218893Sdim /// abort. 156288943Sdim FunctionCreator LazyFunctionCreator; 157221345Sdim 158288943Sdim /// getMangledName - Get mangled name. 159288943Sdim std::string getMangledName(const GlobalValue *GV); 160288943Sdim 161193323Sedpublic: 162280031Sdim /// lock - This lock protects the ExecutionEngine and MCJIT classes. It must 163280031Sdim /// be held while changing the internal state of any of those classes. 164218893Sdim sys::Mutex lock; 165193323Sed 166193323Sed //===--------------------------------------------------------------------===// 167193323Sed // ExecutionEngine Startup 168193323Sed //===--------------------------------------------------------------------===// 169193323Sed 170193323Sed virtual ~ExecutionEngine(); 171193323Sed 172280031Sdim /// Add a Module to the list of modules that we can JIT from. 173280031Sdim virtual void addModule(std::unique_ptr<Module> M) { 174280031Sdim Modules.push_back(std::move(M)); 175193323Sed } 176221345Sdim 177276479Sdim /// addObjectFile - Add an ObjectFile to the execution engine. 178276479Sdim /// 179276479Sdim /// This method is only supported by MCJIT. MCJIT will immediately load the 180276479Sdim /// object into memory and adds its symbols to the list used to resolve 181276479Sdim /// external symbols while preparing other objects for execution. 182276479Sdim /// 183276479Sdim /// Objects added using this function will not be made executable until 184276479Sdim /// needed by another object. 185276479Sdim /// 186276479Sdim /// MCJIT will take ownership of the ObjectFile. 187276479Sdim virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O); 188280031Sdim virtual void addObjectFile(object::OwningBinary<object::ObjectFile> O); 189276479Sdim 190276479Sdim /// addArchive - Add an Archive to the execution engine. 191276479Sdim /// 192276479Sdim /// This method is only supported by MCJIT. MCJIT will use the archive to 193276479Sdim /// resolve external symbols in objects it is loading. If a symbol is found 194276479Sdim /// in the Archive the contained object file will be extracted (in memory) 195276479Sdim /// and loaded for possible execution. 196280031Sdim virtual void addArchive(object::OwningBinary<object::Archive> A); 197276479Sdim 198218893Sdim //===--------------------------------------------------------------------===// 199193323Sed 200296417Sdim const DataLayout &getDataLayout() const { return DL; } 201193323Sed 202314564Sdim /// removeModule - Removes a Module from the list of modules, but does not 203314564Sdim /// free the module's memory. Returns true if M is found, in which case the 204314564Sdim /// caller assumes responsibility for deleting the module. 205314564Sdim // 206314564Sdim // FIXME: This stealth ownership transfer is horrible. This will probably be 207314564Sdim // fixed by deleting ExecutionEngine. 208203954Srdivacky virtual bool removeModule(Module *M); 209193323Sed 210288943Sdim /// FindFunctionNamed - Search all of the active modules to find the function that 211193323Sed /// defines FnName. This is very slow operation and shouldn't be used for 212193323Sed /// general code. 213314564Sdim virtual Function *FindFunctionNamed(StringRef FnName); 214221345Sdim 215288943Sdim /// FindGlobalVariableNamed - Search all of the active modules to find the global variable 216288943Sdim /// that defines Name. This is very slow operation and shouldn't be used for 217288943Sdim /// general code. 218314564Sdim virtual GlobalVariable *FindGlobalVariableNamed(StringRef Name, bool AllowInternal = false); 219288943Sdim 220193323Sed /// runFunction - Execute the specified function with the specified arguments, 221193323Sed /// and return the result. 222314564Sdim /// 223314564Sdim /// For MCJIT execution engines, clients are encouraged to use the 224314564Sdim /// "GetFunctionAddress" method (rather than runFunction) and cast the 225314564Sdim /// returned uint64_t to the desired function pointer type. However, for 226314564Sdim /// backwards compatibility MCJIT's implementation can execute 'main-like' 227314564Sdim /// function (i.e. those returning void or int, and taking either no 228314564Sdim /// arguments or (int, char*[])). 229193323Sed virtual GenericValue runFunction(Function *F, 230288943Sdim ArrayRef<GenericValue> ArgValues) = 0; 231193323Sed 232234353Sdim /// getPointerToNamedFunction - This method returns the address of the 233234353Sdim /// specified function by using the dlsym function call. As such it is only 234234353Sdim /// useful for resolving library symbols, not code generated symbols. 235234353Sdim /// 236234353Sdim /// If AbortOnFailure is false and no function with the given name is 237234353Sdim /// found, this function silently returns a null pointer. Otherwise, 238234353Sdim /// it prints a message to stderr and aborts. 239234353Sdim /// 240261991Sdim /// This function is deprecated for the MCJIT execution engine. 241280031Sdim virtual void *getPointerToNamedFunction(StringRef Name, 242234353Sdim bool AbortOnFailure = true) = 0; 243234353Sdim 244234353Sdim /// mapSectionAddress - map a section to its target address space value. 245234353Sdim /// Map the address of a JIT section as returned from the memory manager 246234353Sdim /// to the address in the target process as the running code will see it. 247234353Sdim /// This is the address which will be used for relocation resolution. 248288943Sdim virtual void mapSectionAddress(const void *LocalAddress, 249288943Sdim uint64_t TargetAddress) { 250234353Sdim llvm_unreachable("Re-mapping of section addresses not supported with this " 251234353Sdim "EE!"); 252234353Sdim } 253234353Sdim 254280031Sdim /// generateCodeForModule - Run code generation for the specified module and 255261991Sdim /// load it into memory. 256261991Sdim /// 257261991Sdim /// When this function has completed, all code and data for the specified 258261991Sdim /// module, and any module on which this module depends, will be generated 259261991Sdim /// and loaded into memory, but relocations will not yet have been applied 260261991Sdim /// and all memory will be readable and writable but not executable. 261261991Sdim /// 262261991Sdim /// This function is primarily useful when generating code for an external 263261991Sdim /// target, allowing the client an opportunity to remap section addresses 264261991Sdim /// before relocations are applied. Clients that intend to execute code 265261991Sdim /// locally can use the getFunctionAddress call, which will generate code 266261991Sdim /// and apply final preparations all in one step. 267261991Sdim /// 268280031Sdim /// This method has no effect for the interpeter. 269261991Sdim virtual void generateCodeForModule(Module *M) {} 270261991Sdim 271261991Sdim /// finalizeObject - ensure the module is fully processed and is usable. 272261991Sdim /// 273261991Sdim /// It is the user-level function for completing the process of making the 274261991Sdim /// object usable for execution. It should be called after sections within an 275261991Sdim /// object have been relocated using mapSectionAddress. When this method is 276261991Sdim /// called the MCJIT execution engine will reapply relocations for a loaded 277280031Sdim /// object. This method has no effect for the interpeter. 278243830Sdim virtual void finalizeObject() {} 279243830Sdim 280193323Sed /// runStaticConstructorsDestructors - This method is used to execute all of 281218893Sdim /// the static constructors or destructors for a program. 282218893Sdim /// 283218893Sdim /// \param isDtors - Run the destructors instead of constructors. 284261991Sdim virtual void runStaticConstructorsDestructors(bool isDtors); 285218893Sdim 286280031Sdim /// This method is used to execute all of the static constructors or 287280031Sdim /// destructors for a particular module. 288218893Sdim /// 289218893Sdim /// \param isDtors - Run the destructors instead of constructors. 290280031Sdim void runStaticConstructorsDestructors(Module &module, bool isDtors); 291221345Sdim 292221345Sdim 293193323Sed /// runFunctionAsMain - This is a helper function which wraps runFunction to 294193323Sed /// handle the common task of starting up main with the specified argc, argv, 295193323Sed /// and envp parameters. 296193323Sed int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 297193323Sed const char * const * envp); 298193323Sed 299193323Sed 300193323Sed /// addGlobalMapping - Tell the execution engine that the specified global is 301193323Sed /// at the specified location. This is used internally as functions are JIT'd 302193323Sed /// and as global variables are laid out in memory. It can and should also be 303193323Sed /// used by clients of the EE that want to have an LLVM global overlay 304309124Sdim /// existing data in memory. Values to be mapped should be named, and have 305309124Sdim /// external or weak linkage. Mappings are automatically removed when their 306198090Srdivacky /// GlobalValue is destroyed. 307193323Sed void addGlobalMapping(const GlobalValue *GV, void *Addr); 308288943Sdim void addGlobalMapping(StringRef Name, uint64_t Addr); 309221345Sdim 310218893Sdim /// clearAllGlobalMappings - Clear all global mappings and start over again, 311218893Sdim /// for use in dynamic compilation scenarios to move globals. 312193323Sed void clearAllGlobalMappings(); 313221345Sdim 314193323Sed /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 315193323Sed /// particular module, because it has been removed from the JIT. 316193323Sed void clearGlobalMappingsFromModule(Module *M); 317221345Sdim 318193323Sed /// updateGlobalMapping - Replace an existing mapping for GV with a new 319193323Sed /// address. This updates both maps as required. If "Addr" is null, the 320193323Sed /// entry for the global is removed from the mappings. This returns the old 321193323Sed /// value of the pointer, or null if it was not in the map. 322288943Sdim uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr); 323288943Sdim uint64_t updateGlobalMapping(StringRef Name, uint64_t Addr); 324221345Sdim 325288943Sdim /// getAddressToGlobalIfAvailable - This returns the address of the specified 326288943Sdim /// global symbol. 327288943Sdim uint64_t getAddressToGlobalIfAvailable(StringRef S); 328288943Sdim 329193323Sed /// getPointerToGlobalIfAvailable - This returns the address of the specified 330193323Sed /// global value if it is has already been codegen'd, otherwise it returns 331193323Sed /// null. 332288943Sdim void *getPointerToGlobalIfAvailable(StringRef S); 333193323Sed void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 334193323Sed 335193323Sed /// getPointerToGlobal - This returns the address of the specified global 336218893Sdim /// value. This may involve code generation if it's a function. 337261991Sdim /// 338261991Sdim /// This function is deprecated for the MCJIT execution engine. Use 339261991Sdim /// getGlobalValueAddress instead. 340193323Sed void *getPointerToGlobal(const GlobalValue *GV); 341193323Sed 342193323Sed /// getPointerToFunction - The different EE's represent function bodies in 343193323Sed /// different ways. They should each implement this to say what a function 344198090Srdivacky /// pointer should look like. When F is destroyed, the ExecutionEngine will 345198892Srdivacky /// remove its global mapping and free any machine code. Be sure no threads 346198892Srdivacky /// are running inside F when that happens. 347261991Sdim /// 348261991Sdim /// This function is deprecated for the MCJIT execution engine. Use 349261991Sdim /// getFunctionAddress instead. 350193323Sed virtual void *getPointerToFunction(Function *F) = 0; 351193323Sed 352193323Sed /// getPointerToFunctionOrStub - If the specified function has been 353193323Sed /// code-gen'd, return a pointer to the function. If not, compile it, or use 354198090Srdivacky /// a stub to implement lazy compilation if available. See 355198090Srdivacky /// getPointerToFunction for the requirements on destroying F. 356261991Sdim /// 357261991Sdim /// This function is deprecated for the MCJIT execution engine. Use 358261991Sdim /// getFunctionAddress instead. 359193323Sed virtual void *getPointerToFunctionOrStub(Function *F) { 360193323Sed // Default implementation, just codegen the function. 361193323Sed return getPointerToFunction(F); 362193323Sed } 363193323Sed 364261991Sdim /// getGlobalValueAddress - Return the address of the specified global 365261991Sdim /// value. This may involve code generation. 366261991Sdim /// 367280031Sdim /// This function should not be called with the interpreter engine. 368261991Sdim virtual uint64_t getGlobalValueAddress(const std::string &Name) { 369280031Sdim // Default implementation for the interpreter. MCJIT will override this. 370261991Sdim // JIT and interpreter clients should use getPointerToGlobal instead. 371261991Sdim return 0; 372261991Sdim } 373261991Sdim 374261991Sdim /// getFunctionAddress - Return the address of the specified function. 375261991Sdim /// This may involve code generation. 376261991Sdim virtual uint64_t getFunctionAddress(const std::string &Name) { 377280031Sdim // Default implementation for the interpreter. MCJIT will override this. 378280031Sdim // Interpreter clients should use getPointerToFunction instead. 379261991Sdim return 0; 380261991Sdim } 381261991Sdim 382193323Sed /// getGlobalValueAtAddress - Return the LLVM global value object that starts 383193323Sed /// at the specified address. 384193323Sed /// 385193323Sed const GlobalValue *getGlobalValueAtAddress(void *Addr); 386193323Sed 387218893Sdim /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 388218893Sdim /// Ptr is the address of the memory at which to store Val, cast to 389218893Sdim /// GenericValue *. It is not a pointer to a GenericValue containing the 390218893Sdim /// address at which to store Val. 391193323Sed void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 392226633Sdim Type *Ty); 393218893Sdim 394193323Sed void InitializeMemory(const Constant *Init, void *Addr); 395193323Sed 396193323Sed /// getOrEmitGlobalVariable - Return the address of the specified global 397193323Sed /// variable, possibly emitting it to memory if needed. This is used by the 398198090Srdivacky /// Emitter. 399261991Sdim /// 400261991Sdim /// This function is deprecated for the MCJIT execution engine. Use 401261991Sdim /// getGlobalValueAddress instead. 402193323Sed virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 403239462Sdim return getPointerToGlobal((const GlobalValue *)GV); 404193323Sed } 405195098Sed 406195098Sed /// Registers a listener to be called back on various events within 407195098Sed /// the JIT. See JITEventListener.h for more details. Does not 408195098Sed /// take ownership of the argument. The argument may be NULL, in 409195098Sed /// which case these functions do nothing. 410198090Srdivacky virtual void RegisterJITEventListener(JITEventListener *) {} 411198090Srdivacky virtual void UnregisterJITEventListener(JITEventListener *) {} 412195098Sed 413251662Sdim /// Sets the pre-compiled object cache. The ownership of the ObjectCache is 414280031Sdim /// not changed. Supported by MCJIT but not the interpreter. 415251662Sdim virtual void setObjectCache(ObjectCache *) { 416251662Sdim llvm_unreachable("No support for an object cache"); 417251662Sdim } 418251662Sdim 419276479Sdim /// setProcessAllSections (MCJIT Only): By default, only sections that are 420276479Sdim /// "required for execution" are passed to the RTDyldMemoryManager, and other 421276479Sdim /// sections are discarded. Passing 'true' to this method will cause 422276479Sdim /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless 423276479Sdim /// of whether they are "required to execute" in the usual sense. 424276479Sdim /// 425276479Sdim /// Rationale: Some MCJIT clients want to be able to inspect metadata 426276479Sdim /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze 427276479Sdim /// performance. Passing these sections to the memory manager allows the 428276479Sdim /// client to make policy about the relevant sections, rather than having 429276479Sdim /// MCJIT do it. 430276479Sdim virtual void setProcessAllSections(bool ProcessAllSections) { 431276479Sdim llvm_unreachable("No support for ProcessAllSections option"); 432276479Sdim } 433276479Sdim 434276479Sdim /// Return the target machine (if available). 435276479Sdim virtual TargetMachine *getTargetMachine() { return nullptr; } 436276479Sdim 437198892Srdivacky /// DisableLazyCompilation - When lazy compilation is off (the default), the 438198892Srdivacky /// JIT will eagerly compile every function reachable from the argument to 439198892Srdivacky /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 440198892Srdivacky /// compile the one function and emit stubs to compile the rest when they're 441198892Srdivacky /// first called. If lazy compilation is turned off again while some lazy 442198892Srdivacky /// stubs are still around, and one of those stubs is called, the program will 443198892Srdivacky /// abort. 444198892Srdivacky /// 445198892Srdivacky /// In order to safely compile lazily in a threaded program, the user must 446198892Srdivacky /// ensure that 1) only one thread at a time can call any particular lazy 447198892Srdivacky /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 448198892Srdivacky /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 449198892Srdivacky /// lazy stub. See http://llvm.org/PR5184 for details. 450193323Sed void DisableLazyCompilation(bool Disabled = true) { 451198892Srdivacky CompilingLazily = !Disabled; 452193323Sed } 453198892Srdivacky bool isCompilingLazily() const { 454198892Srdivacky return CompilingLazily; 455198892Srdivacky } 456193323Sed 457193323Sed /// DisableGVCompilation - If called, the JIT will abort if it's asked to 458193323Sed /// allocate space and populate a GlobalVariable that is not internal to 459193323Sed /// the module. 460193323Sed void DisableGVCompilation(bool Disabled = true) { 461193323Sed GVCompilationDisabled = Disabled; 462193323Sed } 463193323Sed bool isGVCompilationDisabled() const { 464193323Sed return GVCompilationDisabled; 465193323Sed } 466193323Sed 467193323Sed /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 468193323Sed /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 469193323Sed /// resolve symbols in a custom way. 470193323Sed void DisableSymbolSearching(bool Disabled = true) { 471193323Sed SymbolSearchingDisabled = Disabled; 472193323Sed } 473193323Sed bool isSymbolSearchingDisabled() const { 474193323Sed return SymbolSearchingDisabled; 475193323Sed } 476199481Srdivacky 477276479Sdim /// Enable/Disable IR module verification. 478276479Sdim /// 479276479Sdim /// Note: Module verification is enabled by default in Debug builds, and 480276479Sdim /// disabled by default in Release. Use this method to override the default. 481276479Sdim void setVerifyModules(bool Verify) { 482276479Sdim VerifyModules = Verify; 483276479Sdim } 484276479Sdim bool getVerifyModules() const { 485276479Sdim return VerifyModules; 486276479Sdim } 487276479Sdim 488193323Sed /// InstallLazyFunctionCreator - If an unknown function is needed, the 489193323Sed /// specified function pointer is invoked to create it. If it returns null, 490193323Sed /// the JIT will abort. 491288943Sdim void InstallLazyFunctionCreator(FunctionCreator C) { 492309124Sdim LazyFunctionCreator = std::move(C); 493193323Sed } 494221345Sdim 495193323Sedprotected: 496309124Sdim ExecutionEngine(DataLayout DL) : DL(std::move(DL)) {} 497296417Sdim explicit ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M); 498280031Sdim explicit ExecutionEngine(std::unique_ptr<Module> M); 499193323Sed 500193323Sed void emitGlobals(); 501193323Sed 502193323Sed void EmitGlobalVariable(const GlobalVariable *GV); 503193323Sed 504193323Sed GenericValue getConstantValue(const Constant *C); 505221345Sdim void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 506226633Sdim Type *Ty); 507296417Sdim 508296417Sdimprivate: 509296417Sdim void Init(std::unique_ptr<Module> M); 510193323Sed}; 511193323Sed 512198090Srdivackynamespace EngineKind { 513321369Sdim 514198090Srdivacky // These are actually bitmasks that get or-ed together. 515198090Srdivacky enum Kind { 516198090Srdivacky JIT = 0x1, 517198090Srdivacky Interpreter = 0x2 518198090Srdivacky }; 519198090Srdivacky const static Kind Either = (Kind)(JIT | Interpreter); 520198090Srdivacky 521321369Sdim} // end namespace EngineKind 522321369Sdim 523280031Sdim/// Builder class for ExecutionEngines. Use this by stack-allocating a builder, 524280031Sdim/// chaining the various set* methods, and terminating it with a .create() 525280031Sdim/// call. 526198090Srdivackyclass EngineBuilder { 527218893Sdimprivate: 528280031Sdim std::unique_ptr<Module> M; 529198090Srdivacky EngineKind::Kind WhichEngine; 530198090Srdivacky std::string *ErrorStr; 531198090Srdivacky CodeGenOpt::Level OptLevel; 532288943Sdim std::shared_ptr<MCJITMemoryManager> MemMgr; 533341825Sdim std::shared_ptr<LegacyJITSymbolResolver> Resolver; 534234353Sdim TargetOptions Options; 535309124Sdim Optional<Reloc::Model> RelocModel; 536327952Sdim Optional<CodeModel::Model> CMModel; 537203954Srdivacky std::string MArch; 538203954Srdivacky std::string MCPU; 539203954Srdivacky SmallVector<std::string, 4> MAttrs; 540276479Sdim bool VerifyModules; 541288943Sdim bool UseOrcMCJITReplacement; 542327952Sdim bool EmulatedTLS = true; 543198090Srdivacky 544288943Sdimpublic: 545288943Sdim /// Default constructor for EngineBuilder. 546288943Sdim EngineBuilder(); 547198090Srdivacky 548280031Sdim /// Constructor for EngineBuilder. 549280031Sdim EngineBuilder(std::unique_ptr<Module> M); 550198090Srdivacky 551280031Sdim // Out-of-line since we don't have the def'n of RTDyldMemoryManager here. 552280031Sdim ~EngineBuilder(); 553280031Sdim 554198090Srdivacky /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 555198090Srdivacky /// or whichever engine works. This option defaults to EngineKind::Either. 556198090Srdivacky EngineBuilder &setEngineKind(EngineKind::Kind w) { 557198090Srdivacky WhichEngine = w; 558198090Srdivacky return *this; 559198090Srdivacky } 560276479Sdim 561261991Sdim /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows 562261991Sdim /// clients to customize their memory allocation policies for the MCJIT. This 563261991Sdim /// is only appropriate for the MCJIT; setting this and configuring the builder 564261991Sdim /// to create anything other than MCJIT will cause a runtime error. If create() 565261991Sdim /// is called and is successful, the created engine takes ownership of the 566280031Sdim /// memory manager. This option defaults to NULL. 567280031Sdim EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm); 568198090Srdivacky 569288943Sdim EngineBuilder& 570288943Sdim setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM); 571288943Sdim 572341825Sdim EngineBuilder &setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR); 573288943Sdim 574198090Srdivacky /// setErrorStr - Set the error string to write to on error. This option 575198090Srdivacky /// defaults to NULL. 576198090Srdivacky EngineBuilder &setErrorStr(std::string *e) { 577198090Srdivacky ErrorStr = e; 578198090Srdivacky return *this; 579198090Srdivacky } 580198090Srdivacky 581198090Srdivacky /// setOptLevel - Set the optimization level for the JIT. This option 582198090Srdivacky /// defaults to CodeGenOpt::Default. 583198090Srdivacky EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 584198090Srdivacky OptLevel = l; 585198090Srdivacky return *this; 586198090Srdivacky } 587198090Srdivacky 588234353Sdim /// setTargetOptions - Set the target options that the ExecutionEngine 589234353Sdim /// target is using. Defaults to TargetOptions(). 590234353Sdim EngineBuilder &setTargetOptions(const TargetOptions &Opts) { 591234353Sdim Options = Opts; 592234353Sdim return *this; 593234353Sdim } 594234353Sdim 595226633Sdim /// setRelocationModel - Set the relocation model that the ExecutionEngine 596226633Sdim /// target is using. Defaults to target specific default "Reloc::Default". 597226633Sdim EngineBuilder &setRelocationModel(Reloc::Model RM) { 598226633Sdim RelocModel = RM; 599226633Sdim return *this; 600226633Sdim } 601226633Sdim 602199481Srdivacky /// setCodeModel - Set the CodeModel that the ExecutionEngine target 603226633Sdim /// data is using. Defaults to target specific default 604226633Sdim /// "CodeModel::JITDefault". 605199481Srdivacky EngineBuilder &setCodeModel(CodeModel::Model M) { 606199481Srdivacky CMModel = M; 607199481Srdivacky return *this; 608199481Srdivacky } 609199481Srdivacky 610203954Srdivacky /// setMArch - Override the architecture set by the Module's triple. 611203954Srdivacky EngineBuilder &setMArch(StringRef march) { 612203954Srdivacky MArch.assign(march.begin(), march.end()); 613203954Srdivacky return *this; 614203954Srdivacky } 615203954Srdivacky 616203954Srdivacky /// setMCPU - Target a specific cpu type. 617203954Srdivacky EngineBuilder &setMCPU(StringRef mcpu) { 618203954Srdivacky MCPU.assign(mcpu.begin(), mcpu.end()); 619203954Srdivacky return *this; 620203954Srdivacky } 621203954Srdivacky 622276479Sdim /// setVerifyModules - Set whether the JIT implementation should verify 623276479Sdim /// IR modules during compilation. 624276479Sdim EngineBuilder &setVerifyModules(bool Verify) { 625276479Sdim VerifyModules = Verify; 626276479Sdim return *this; 627276479Sdim } 628276479Sdim 629203954Srdivacky /// setMAttrs - Set cpu-specific attributes. 630203954Srdivacky template<typename StringSequence> 631203954Srdivacky EngineBuilder &setMAttrs(const StringSequence &mattrs) { 632203954Srdivacky MAttrs.clear(); 633203954Srdivacky MAttrs.append(mattrs.begin(), mattrs.end()); 634203954Srdivacky return *this; 635203954Srdivacky } 636203954Srdivacky 637341825Sdim // Use OrcMCJITReplacement instead of MCJIT. Off by default. 638353358Sdim LLVM_ATTRIBUTE_DEPRECATED( 639353358Sdim inline void setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement), 640353358Sdim "ORCv1 utilities (including OrcMCJITReplacement) are deprecated. Please " 641353358Sdim "use ORCv2/LLJIT instead (see docs/ORCv2.rst)"); 642353358Sdim 643353358Sdim void setUseOrcMCJITReplacement(ORCv1DeprecationAcknowledgement, 644353358Sdim bool UseOrcMCJITReplacement) { 645288943Sdim this->UseOrcMCJITReplacement = UseOrcMCJITReplacement; 646288943Sdim } 647288943Sdim 648327952Sdim void setEmulatedTLS(bool EmulatedTLS) { 649327952Sdim this->EmulatedTLS = EmulatedTLS; 650327952Sdim } 651341825Sdim 652234353Sdim TargetMachine *selectTarget(); 653234353Sdim 654223017Sdim /// selectTarget - Pick a target either via -march or by guessing the native 655223017Sdim /// arch. Add any CPU features specified via -mcpu or -mattr. 656234353Sdim TargetMachine *selectTarget(const Triple &TargetTriple, 657234353Sdim StringRef MArch, 658234353Sdim StringRef MCPU, 659234353Sdim const SmallVectorImpl<std::string>& MAttrs); 660223017Sdim 661234353Sdim ExecutionEngine *create() { 662234353Sdim return create(selectTarget()); 663234353Sdim } 664234353Sdim 665234353Sdim ExecutionEngine *create(TargetMachine *TM); 666198090Srdivacky}; 667198090Srdivacky 668353358Sdimvoid EngineBuilder::setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement) { 669353358Sdim this->UseOrcMCJITReplacement = UseOrcMCJITReplacement; 670353358Sdim} 671353358Sdim 672251662Sdim// Create wrappers for C Binding types (see CBindingWrapping.h). 673251662SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef) 674251662Sdim 675321369Sdim} // end namespace llvm 676193323Sed 677321369Sdim#endif // LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 678