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