1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
16 #define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
18 #include "RuntimeDyld.h"
19 #include "llvm-c/ExecutionEngine.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/IR/ValueMap.h"
25 #include "llvm/MC/MCCodeGenInfo.h"
26 #include "llvm/Object/Binary.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Mutex.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
41 class ExecutionEngine;
45 class JITEventListener;
46 class MachineCodeInfo;
47 class MCJITMemoryManager;
50 class RTDyldMemoryManager;
59 /// \brief Helper class for helping synchronize access to the global address map
60 /// table. Access to this class should be serialized under a mutex.
61 class ExecutionEngineState {
63 typedef StringMap<uint64_t> GlobalAddressMapTy;
67 /// GlobalAddressMap - A mapping between LLVM global symbol names values and
68 /// their actualized version...
69 GlobalAddressMapTy GlobalAddressMap;
71 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
72 /// used to convert raw addresses into the LLVM global value that is emitted
73 /// at the address. This map is not computed unless getGlobalValueAtAddress
74 /// is called at some point.
75 std::map<uint64_t, std::string> GlobalAddressReverseMap;
79 GlobalAddressMapTy &getGlobalAddressMap() {
80 return GlobalAddressMap;
83 std::map<uint64_t, std::string> &getGlobalAddressReverseMap() {
84 return GlobalAddressReverseMap;
87 /// \brief Erase an entry from the mapping table.
89 /// \returns The address that \p ToUnmap was happed to.
90 uint64_t RemoveMapping(StringRef Name);
93 using FunctionCreator = std::function<void *(const std::string &)>;
95 /// \brief Abstract interface for implementation execution of LLVM modules,
96 /// designed to support both interpreter and just-in-time (JIT) compiler
98 class ExecutionEngine {
99 /// The state object holding the global address mapping, which must be
100 /// accessed synchronously.
102 // FIXME: There is no particular need the entire map needs to be
103 // synchronized. Wouldn't a reader-writer design be better here?
104 ExecutionEngineState EEState;
106 /// The target data for the platform for which execution is being performed.
108 /// Note: the DataLayout is LLVMContext specific because it has an
109 /// internal cache based on type pointers. It makes unsafe to reuse the
110 /// ExecutionEngine across context, we don't enforce this rule but undefined
111 /// behavior can occurs if the user tries to do it.
114 /// Whether lazy JIT compilation is enabled.
115 bool CompilingLazily;
117 /// Whether JIT compilation of external global variables is allowed.
118 bool GVCompilationDisabled;
120 /// Whether the JIT should perform lookups of external symbols (e.g.,
122 bool SymbolSearchingDisabled;
124 /// Whether the JIT should verify IR modules during compilation.
127 friend class EngineBuilder; // To allow access to JITCtor and InterpCtor.
130 /// The list of Modules that we are JIT'ing from. We use a SmallVector to
131 /// optimize for the case where there is only one module.
132 SmallVector<std::unique_ptr<Module>, 1> Modules;
134 /// getMemoryforGV - Allocate memory for a global variable.
135 virtual char *getMemoryForGV(const GlobalVariable *GV);
137 static ExecutionEngine *(*MCJITCtor)(
138 std::unique_ptr<Module> M,
139 std::string *ErrorStr,
140 std::shared_ptr<MCJITMemoryManager> MM,
141 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
142 std::unique_ptr<TargetMachine> TM);
144 static ExecutionEngine *(*OrcMCJITReplacementCtor)(
145 std::string *ErrorStr,
146 std::shared_ptr<MCJITMemoryManager> MM,
147 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
148 std::unique_ptr<TargetMachine> TM);
150 static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
151 std::string *ErrorStr);
153 /// LazyFunctionCreator - If an unknown function is needed, this function
154 /// pointer is invoked to create it. If this returns null, the JIT will
156 FunctionCreator LazyFunctionCreator;
158 /// getMangledName - Get mangled name.
159 std::string getMangledName(const GlobalValue *GV);
162 /// lock - This lock protects the ExecutionEngine and MCJIT classes. It must
163 /// be held while changing the internal state of any of those classes.
166 //===--------------------------------------------------------------------===//
167 // ExecutionEngine Startup
168 //===--------------------------------------------------------------------===//
170 virtual ~ExecutionEngine();
172 /// Add a Module to the list of modules that we can JIT from.
173 virtual void addModule(std::unique_ptr<Module> M) {
174 Modules.push_back(std::move(M));
177 /// addObjectFile - Add an ObjectFile to the execution engine.
179 /// This method is only supported by MCJIT. MCJIT will immediately load the
180 /// object into memory and adds its symbols to the list used to resolve
181 /// external symbols while preparing other objects for execution.
183 /// Objects added using this function will not be made executable until
184 /// needed by another object.
186 /// MCJIT will take ownership of the ObjectFile.
187 virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
188 virtual void addObjectFile(object::OwningBinary<object::ObjectFile> O);
190 /// addArchive - Add an Archive to the execution engine.
192 /// This method is only supported by MCJIT. MCJIT will use the archive to
193 /// resolve external symbols in objects it is loading. If a symbol is found
194 /// in the Archive the contained object file will be extracted (in memory)
195 /// and loaded for possible execution.
196 virtual void addArchive(object::OwningBinary<object::Archive> A);
198 //===--------------------------------------------------------------------===//
200 const DataLayout &getDataLayout() const { return DL; }
202 /// removeModule - Remove a Module from the list of modules. Returns true if
204 virtual bool removeModule(Module *M);
206 /// FindFunctionNamed - Search all of the active modules to find the function that
207 /// defines FnName. This is very slow operation and shouldn't be used for
209 virtual Function *FindFunctionNamed(const char *FnName);
211 /// FindGlobalVariableNamed - Search all of the active modules to find the global variable
212 /// that defines Name. This is very slow operation and shouldn't be used for
214 virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false);
216 /// runFunction - Execute the specified function with the specified arguments,
217 /// and return the result.
218 virtual GenericValue runFunction(Function *F,
219 ArrayRef<GenericValue> ArgValues) = 0;
221 /// getPointerToNamedFunction - This method returns the address of the
222 /// specified function by using the dlsym function call. As such it is only
223 /// useful for resolving library symbols, not code generated symbols.
225 /// If AbortOnFailure is false and no function with the given name is
226 /// found, this function silently returns a null pointer. Otherwise,
227 /// it prints a message to stderr and aborts.
229 /// This function is deprecated for the MCJIT execution engine.
230 virtual void *getPointerToNamedFunction(StringRef Name,
231 bool AbortOnFailure = true) = 0;
233 /// mapSectionAddress - map a section to its target address space value.
234 /// Map the address of a JIT section as returned from the memory manager
235 /// to the address in the target process as the running code will see it.
236 /// This is the address which will be used for relocation resolution.
237 virtual void mapSectionAddress(const void *LocalAddress,
238 uint64_t TargetAddress) {
239 llvm_unreachable("Re-mapping of section addresses not supported with this "
243 /// generateCodeForModule - Run code generation for the specified module and
244 /// load it into memory.
246 /// When this function has completed, all code and data for the specified
247 /// module, and any module on which this module depends, will be generated
248 /// and loaded into memory, but relocations will not yet have been applied
249 /// and all memory will be readable and writable but not executable.
251 /// This function is primarily useful when generating code for an external
252 /// target, allowing the client an opportunity to remap section addresses
253 /// before relocations are applied. Clients that intend to execute code
254 /// locally can use the getFunctionAddress call, which will generate code
255 /// and apply final preparations all in one step.
257 /// This method has no effect for the interpeter.
258 virtual void generateCodeForModule(Module *M) {}
260 /// finalizeObject - ensure the module is fully processed and is usable.
262 /// It is the user-level function for completing the process of making the
263 /// object usable for execution. It should be called after sections within an
264 /// object have been relocated using mapSectionAddress. When this method is
265 /// called the MCJIT execution engine will reapply relocations for a loaded
266 /// object. This method has no effect for the interpeter.
267 virtual void finalizeObject() {}
269 /// runStaticConstructorsDestructors - This method is used to execute all of
270 /// the static constructors or destructors for a program.
272 /// \param isDtors - Run the destructors instead of constructors.
273 virtual void runStaticConstructorsDestructors(bool isDtors);
275 /// This method is used to execute all of the static constructors or
276 /// destructors for a particular module.
278 /// \param isDtors - Run the destructors instead of constructors.
279 void runStaticConstructorsDestructors(Module &module, bool isDtors);
282 /// runFunctionAsMain - This is a helper function which wraps runFunction to
283 /// handle the common task of starting up main with the specified argc, argv,
284 /// and envp parameters.
285 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
286 const char * const * envp);
289 /// addGlobalMapping - Tell the execution engine that the specified global is
290 /// at the specified location. This is used internally as functions are JIT'd
291 /// and as global variables are laid out in memory. It can and should also be
292 /// used by clients of the EE that want to have an LLVM global overlay
293 /// existing data in memory. Mappings are automatically removed when their
294 /// GlobalValue is destroyed.
295 void addGlobalMapping(const GlobalValue *GV, void *Addr);
296 void addGlobalMapping(StringRef Name, uint64_t Addr);
298 /// clearAllGlobalMappings - Clear all global mappings and start over again,
299 /// for use in dynamic compilation scenarios to move globals.
300 void clearAllGlobalMappings();
302 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
303 /// particular module, because it has been removed from the JIT.
304 void clearGlobalMappingsFromModule(Module *M);
306 /// updateGlobalMapping - Replace an existing mapping for GV with a new
307 /// address. This updates both maps as required. If "Addr" is null, the
308 /// entry for the global is removed from the mappings. This returns the old
309 /// value of the pointer, or null if it was not in the map.
310 uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr);
311 uint64_t updateGlobalMapping(StringRef Name, uint64_t Addr);
313 /// getAddressToGlobalIfAvailable - This returns the address of the specified
315 uint64_t getAddressToGlobalIfAvailable(StringRef S);
317 /// getPointerToGlobalIfAvailable - This returns the address of the specified
318 /// global value if it is has already been codegen'd, otherwise it returns
320 void *getPointerToGlobalIfAvailable(StringRef S);
321 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
323 /// getPointerToGlobal - This returns the address of the specified global
324 /// value. This may involve code generation if it's a function.
326 /// This function is deprecated for the MCJIT execution engine. Use
327 /// getGlobalValueAddress instead.
328 void *getPointerToGlobal(const GlobalValue *GV);
330 /// getPointerToFunction - The different EE's represent function bodies in
331 /// different ways. They should each implement this to say what a function
332 /// pointer should look like. When F is destroyed, the ExecutionEngine will
333 /// remove its global mapping and free any machine code. Be sure no threads
334 /// are running inside F when that happens.
336 /// This function is deprecated for the MCJIT execution engine. Use
337 /// getFunctionAddress instead.
338 virtual void *getPointerToFunction(Function *F) = 0;
340 /// getPointerToFunctionOrStub - If the specified function has been
341 /// code-gen'd, return a pointer to the function. If not, compile it, or use
342 /// a stub to implement lazy compilation if available. See
343 /// getPointerToFunction for the requirements on destroying F.
345 /// This function is deprecated for the MCJIT execution engine. Use
346 /// getFunctionAddress instead.
347 virtual void *getPointerToFunctionOrStub(Function *F) {
348 // Default implementation, just codegen the function.
349 return getPointerToFunction(F);
352 /// getGlobalValueAddress - Return the address of the specified global
353 /// value. This may involve code generation.
355 /// This function should not be called with the interpreter engine.
356 virtual uint64_t getGlobalValueAddress(const std::string &Name) {
357 // Default implementation for the interpreter. MCJIT will override this.
358 // JIT and interpreter clients should use getPointerToGlobal instead.
362 /// getFunctionAddress - Return the address of the specified function.
363 /// This may involve code generation.
364 virtual uint64_t getFunctionAddress(const std::string &Name) {
365 // Default implementation for the interpreter. MCJIT will override this.
366 // Interpreter clients should use getPointerToFunction instead.
370 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
371 /// at the specified address.
373 const GlobalValue *getGlobalValueAtAddress(void *Addr);
375 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
376 /// Ptr is the address of the memory at which to store Val, cast to
377 /// GenericValue *. It is not a pointer to a GenericValue containing the
378 /// address at which to store Val.
379 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
382 void InitializeMemory(const Constant *Init, void *Addr);
384 /// getOrEmitGlobalVariable - Return the address of the specified global
385 /// variable, possibly emitting it to memory if needed. This is used by the
388 /// This function is deprecated for the MCJIT execution engine. Use
389 /// getGlobalValueAddress instead.
390 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
391 return getPointerToGlobal((const GlobalValue *)GV);
394 /// Registers a listener to be called back on various events within
395 /// the JIT. See JITEventListener.h for more details. Does not
396 /// take ownership of the argument. The argument may be NULL, in
397 /// which case these functions do nothing.
398 virtual void RegisterJITEventListener(JITEventListener *) {}
399 virtual void UnregisterJITEventListener(JITEventListener *) {}
401 /// Sets the pre-compiled object cache. The ownership of the ObjectCache is
402 /// not changed. Supported by MCJIT but not the interpreter.
403 virtual void setObjectCache(ObjectCache *) {
404 llvm_unreachable("No support for an object cache");
407 /// setProcessAllSections (MCJIT Only): By default, only sections that are
408 /// "required for execution" are passed to the RTDyldMemoryManager, and other
409 /// sections are discarded. Passing 'true' to this method will cause
410 /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
411 /// of whether they are "required to execute" in the usual sense.
413 /// Rationale: Some MCJIT clients want to be able to inspect metadata
414 /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
415 /// performance. Passing these sections to the memory manager allows the
416 /// client to make policy about the relevant sections, rather than having
418 virtual void setProcessAllSections(bool ProcessAllSections) {
419 llvm_unreachable("No support for ProcessAllSections option");
422 /// Return the target machine (if available).
423 virtual TargetMachine *getTargetMachine() { return nullptr; }
425 /// DisableLazyCompilation - When lazy compilation is off (the default), the
426 /// JIT will eagerly compile every function reachable from the argument to
427 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only
428 /// compile the one function and emit stubs to compile the rest when they're
429 /// first called. If lazy compilation is turned off again while some lazy
430 /// stubs are still around, and one of those stubs is called, the program will
433 /// In order to safely compile lazily in a threaded program, the user must
434 /// ensure that 1) only one thread at a time can call any particular lazy
435 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
436 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
437 /// lazy stub. See http://llvm.org/PR5184 for details.
438 void DisableLazyCompilation(bool Disabled = true) {
439 CompilingLazily = !Disabled;
441 bool isCompilingLazily() const {
442 return CompilingLazily;
445 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
446 /// allocate space and populate a GlobalVariable that is not internal to
448 void DisableGVCompilation(bool Disabled = true) {
449 GVCompilationDisabled = Disabled;
451 bool isGVCompilationDisabled() const {
452 return GVCompilationDisabled;
455 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
456 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
457 /// resolve symbols in a custom way.
458 void DisableSymbolSearching(bool Disabled = true) {
459 SymbolSearchingDisabled = Disabled;
461 bool isSymbolSearchingDisabled() const {
462 return SymbolSearchingDisabled;
465 /// Enable/Disable IR module verification.
467 /// Note: Module verification is enabled by default in Debug builds, and
468 /// disabled by default in Release. Use this method to override the default.
469 void setVerifyModules(bool Verify) {
470 VerifyModules = Verify;
472 bool getVerifyModules() const {
473 return VerifyModules;
476 /// InstallLazyFunctionCreator - If an unknown function is needed, the
477 /// specified function pointer is invoked to create it. If it returns null,
478 /// the JIT will abort.
479 void InstallLazyFunctionCreator(FunctionCreator C) {
480 LazyFunctionCreator = C;
484 ExecutionEngine(const DataLayout DL) : DL(std::move(DL)){}
485 explicit ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M);
486 explicit ExecutionEngine(std::unique_ptr<Module> M);
490 void EmitGlobalVariable(const GlobalVariable *GV);
492 GenericValue getConstantValue(const Constant *C);
493 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
497 void Init(std::unique_ptr<Module> M);
500 namespace EngineKind {
501 // These are actually bitmasks that get or-ed together.
506 const static Kind Either = (Kind)(JIT | Interpreter);
509 /// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
510 /// chaining the various set* methods, and terminating it with a .create()
512 class EngineBuilder {
514 std::unique_ptr<Module> M;
515 EngineKind::Kind WhichEngine;
516 std::string *ErrorStr;
517 CodeGenOpt::Level OptLevel;
518 std::shared_ptr<MCJITMemoryManager> MemMgr;
519 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver;
520 TargetOptions Options;
521 Reloc::Model RelocModel;
522 CodeModel::Model CMModel;
525 SmallVector<std::string, 4> MAttrs;
527 bool UseOrcMCJITReplacement;
530 /// Default constructor for EngineBuilder.
533 /// Constructor for EngineBuilder.
534 EngineBuilder(std::unique_ptr<Module> M);
536 // Out-of-line since we don't have the def'n of RTDyldMemoryManager here.
539 /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
540 /// or whichever engine works. This option defaults to EngineKind::Either.
541 EngineBuilder &setEngineKind(EngineKind::Kind w) {
546 /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
547 /// clients to customize their memory allocation policies for the MCJIT. This
548 /// is only appropriate for the MCJIT; setting this and configuring the builder
549 /// to create anything other than MCJIT will cause a runtime error. If create()
550 /// is called and is successful, the created engine takes ownership of the
551 /// memory manager. This option defaults to NULL.
552 EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
555 setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
558 setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
560 /// setErrorStr - Set the error string to write to on error. This option
561 /// defaults to NULL.
562 EngineBuilder &setErrorStr(std::string *e) {
567 /// setOptLevel - Set the optimization level for the JIT. This option
568 /// defaults to CodeGenOpt::Default.
569 EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
574 /// setTargetOptions - Set the target options that the ExecutionEngine
575 /// target is using. Defaults to TargetOptions().
576 EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
581 /// setRelocationModel - Set the relocation model that the ExecutionEngine
582 /// target is using. Defaults to target specific default "Reloc::Default".
583 EngineBuilder &setRelocationModel(Reloc::Model RM) {
588 /// setCodeModel - Set the CodeModel that the ExecutionEngine target
589 /// data is using. Defaults to target specific default
590 /// "CodeModel::JITDefault".
591 EngineBuilder &setCodeModel(CodeModel::Model M) {
596 /// setMArch - Override the architecture set by the Module's triple.
597 EngineBuilder &setMArch(StringRef march) {
598 MArch.assign(march.begin(), march.end());
602 /// setMCPU - Target a specific cpu type.
603 EngineBuilder &setMCPU(StringRef mcpu) {
604 MCPU.assign(mcpu.begin(), mcpu.end());
608 /// setVerifyModules - Set whether the JIT implementation should verify
609 /// IR modules during compilation.
610 EngineBuilder &setVerifyModules(bool Verify) {
611 VerifyModules = Verify;
615 /// setMAttrs - Set cpu-specific attributes.
616 template<typename StringSequence>
617 EngineBuilder &setMAttrs(const StringSequence &mattrs) {
619 MAttrs.append(mattrs.begin(), mattrs.end());
623 // \brief Use OrcMCJITReplacement instead of MCJIT. Off by default.
624 void setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement) {
625 this->UseOrcMCJITReplacement = UseOrcMCJITReplacement;
628 TargetMachine *selectTarget();
630 /// selectTarget - Pick a target either via -march or by guessing the native
631 /// arch. Add any CPU features specified via -mcpu or -mattr.
632 TargetMachine *selectTarget(const Triple &TargetTriple,
635 const SmallVectorImpl<std::string>& MAttrs);
637 ExecutionEngine *create() {
638 return create(selectTarget());
641 ExecutionEngine *create(TargetMachine *TM);
644 // Create wrappers for C Binding types (see CBindingWrapping.h).
645 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
647 } // End llvm namespace