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 "llvm-c/ExecutionEngine.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/ValueHandle.h"
22 #include "llvm/IR/ValueMap.h"
23 #include "llvm/MC/MCCodeGenInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
37 class ExecutionEngine;
41 class JITEventListener;
42 class JITMemoryManager;
43 class MachineCodeInfo;
47 class RTDyldMemoryManager;
56 /// \brief Helper class for helping synchronize access to the global address map
57 /// table. Access to this class should be serialized under a mutex.
58 class ExecutionEngineState {
60 struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
61 typedef ExecutionEngineState *ExtraData;
62 static sys::Mutex *getMutex(ExecutionEngineState *EES);
63 static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
64 static void onRAUW(ExecutionEngineState *, const GlobalValue *,
68 typedef ValueMap<const GlobalValue *, void *, AddressMapConfig>
74 /// GlobalAddressMap - A mapping between LLVM global values and their
75 /// actualized version...
76 GlobalAddressMapTy GlobalAddressMap;
78 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
79 /// used to convert raw addresses into the LLVM global value that is emitted
80 /// at the address. This map is not computed unless getGlobalValueAtAddress
81 /// is called at some point.
82 std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap;
85 ExecutionEngineState(ExecutionEngine &EE);
87 GlobalAddressMapTy &getGlobalAddressMap() {
88 return GlobalAddressMap;
91 std::map<void*, AssertingVH<const GlobalValue> > &
92 getGlobalAddressReverseMap() {
93 return GlobalAddressReverseMap;
96 /// \brief Erase an entry from the mapping table.
98 /// \returns The address that \p ToUnmap was happed to.
99 void *RemoveMapping(const GlobalValue *ToUnmap);
102 /// \brief Abstract interface for implementation execution of LLVM modules,
103 /// designed to support both interpreter and just-in-time (JIT) compiler
105 class ExecutionEngine {
106 /// The state object holding the global address mapping, which must be
107 /// accessed synchronously.
109 // FIXME: There is no particular need the entire map needs to be
110 // synchronized. Wouldn't a reader-writer design be better here?
111 ExecutionEngineState EEState;
113 /// The target data for the platform for which execution is being performed.
114 const DataLayout *DL;
116 /// Whether lazy JIT compilation is enabled.
117 bool CompilingLazily;
119 /// Whether JIT compilation of external global variables is allowed.
120 bool GVCompilationDisabled;
122 /// Whether the JIT should perform lookups of external symbols (e.g.,
124 bool SymbolSearchingDisabled;
126 /// Whether the JIT should verify IR modules during compilation.
129 friend class EngineBuilder; // To allow access to JITCtor and InterpCtor.
132 /// The list of Modules that we are JIT'ing from. We use a SmallVector to
133 /// optimize for the case where there is only one module.
134 SmallVector<Module*, 1> Modules;
136 void setDataLayout(const DataLayout *Val) { DL = Val; }
138 /// getMemoryforGV - Allocate memory for a global variable.
139 virtual char *getMemoryForGV(const GlobalVariable *GV);
141 // To avoid having libexecutionengine depend on the JIT and interpreter
142 // libraries, the execution engine implementations set these functions to ctor
143 // pointers at startup time if they are linked in.
144 static ExecutionEngine *(*JITCtor)(
146 std::string *ErrorStr,
147 JITMemoryManager *JMM,
150 static ExecutionEngine *(*MCJITCtor)(
152 std::string *ErrorStr,
153 RTDyldMemoryManager *MCJMM,
156 static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
158 /// LazyFunctionCreator - If an unknown function is needed, this function
159 /// pointer is invoked to create it. If this returns null, the JIT will
161 void *(*LazyFunctionCreator)(const std::string &);
164 /// lock - This lock protects the ExecutionEngine, MCJIT, JIT, JITResolver and
165 /// JITEmitter classes. It must be held while changing the internal state of
166 /// any of those classes.
169 //===--------------------------------------------------------------------===//
170 // ExecutionEngine Startup
171 //===--------------------------------------------------------------------===//
173 virtual ~ExecutionEngine();
175 /// addModule - Add a Module to the list of modules that we can JIT from.
176 /// Note that this takes ownership of the Module: when the ExecutionEngine is
177 /// destroyed, it destroys the Module as well.
178 virtual void addModule(Module *M) {
179 Modules.push_back(M);
182 /// addObjectFile - Add an ObjectFile to the execution engine.
184 /// This method is only supported by MCJIT. MCJIT will immediately load the
185 /// object into memory and adds its symbols to the list used to resolve
186 /// external symbols while preparing other objects for execution.
188 /// Objects added using this function will not be made executable until
189 /// needed by another object.
191 /// MCJIT will take ownership of the ObjectFile.
192 virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
194 /// addArchive - Add an Archive to the execution engine.
196 /// This method is only supported by MCJIT. MCJIT will use the archive to
197 /// resolve external symbols in objects it is loading. If a symbol is found
198 /// in the Archive the contained object file will be extracted (in memory)
199 /// and loaded for possible execution.
201 /// MCJIT will take ownership of the Archive.
202 virtual void addArchive(object::Archive *A) {
203 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
206 //===--------------------------------------------------------------------===//
208 const DataLayout *getDataLayout() const { return DL; }
210 /// removeModule - Remove a Module from the list of modules. Returns true if
212 virtual bool removeModule(Module *M);
214 /// FindFunctionNamed - Search all of the active modules to find the one that
215 /// defines FnName. This is very slow operation and shouldn't be used for
217 virtual Function *FindFunctionNamed(const char *FnName);
219 /// runFunction - Execute the specified function with the specified arguments,
220 /// and return the result.
221 virtual GenericValue runFunction(Function *F,
222 const std::vector<GenericValue> &ArgValues) = 0;
224 /// getPointerToNamedFunction - This method returns the address of the
225 /// specified function by using the dlsym function call. As such it is only
226 /// useful for resolving library symbols, not code generated symbols.
228 /// If AbortOnFailure is false and no function with the given name is
229 /// found, this function silently returns a null pointer. Otherwise,
230 /// it prints a message to stderr and aborts.
232 /// This function is deprecated for the MCJIT execution engine.
234 /// FIXME: the JIT and MCJIT interfaces should be disentangled or united
235 /// again, if possible.
237 virtual void *getPointerToNamedFunction(const std::string &Name,
238 bool AbortOnFailure = true) = 0;
240 /// mapSectionAddress - map a section to its target address space value.
241 /// Map the address of a JIT section as returned from the memory manager
242 /// to the address in the target process as the running code will see it.
243 /// This is the address which will be used for relocation resolution.
244 virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) {
245 llvm_unreachable("Re-mapping of section addresses not supported with this "
249 /// generateCodeForModule - Run code generationen for the specified module and
250 /// load it into memory.
252 /// When this function has completed, all code and data for the specified
253 /// module, and any module on which this module depends, will be generated
254 /// and loaded into memory, but relocations will not yet have been applied
255 /// and all memory will be readable and writable but not executable.
257 /// This function is primarily useful when generating code for an external
258 /// target, allowing the client an opportunity to remap section addresses
259 /// before relocations are applied. Clients that intend to execute code
260 /// locally can use the getFunctionAddress call, which will generate code
261 /// and apply final preparations all in one step.
263 /// This method has no effect for the legacy JIT engine or the interpeter.
264 virtual void generateCodeForModule(Module *M) {}
266 /// finalizeObject - ensure the module is fully processed and is usable.
268 /// It is the user-level function for completing the process of making the
269 /// object usable for execution. It should be called after sections within an
270 /// object have been relocated using mapSectionAddress. When this method is
271 /// called the MCJIT execution engine will reapply relocations for a loaded
272 /// object. This method has no effect for the legacy JIT engine or the
274 virtual void finalizeObject() {}
276 /// runStaticConstructorsDestructors - This method is used to execute all of
277 /// the static constructors or destructors for a program.
279 /// \param isDtors - Run the destructors instead of constructors.
280 virtual void runStaticConstructorsDestructors(bool isDtors);
282 /// runStaticConstructorsDestructors - This method is used to execute all of
283 /// the static constructors or destructors for a particular module.
285 /// \param isDtors - Run the destructors instead of constructors.
286 void runStaticConstructorsDestructors(Module *module, bool isDtors);
289 /// runFunctionAsMain - This is a helper function which wraps runFunction to
290 /// handle the common task of starting up main with the specified argc, argv,
291 /// and envp parameters.
292 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
293 const char * const * envp);
296 /// addGlobalMapping - Tell the execution engine that the specified global is
297 /// at the specified location. This is used internally as functions are JIT'd
298 /// and as global variables are laid out in memory. It can and should also be
299 /// used by clients of the EE that want to have an LLVM global overlay
300 /// existing data in memory. Mappings are automatically removed when their
301 /// GlobalValue is destroyed.
302 void addGlobalMapping(const GlobalValue *GV, void *Addr);
304 /// clearAllGlobalMappings - Clear all global mappings and start over again,
305 /// for use in dynamic compilation scenarios to move globals.
306 void clearAllGlobalMappings();
308 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
309 /// particular module, because it has been removed from the JIT.
310 void clearGlobalMappingsFromModule(Module *M);
312 /// updateGlobalMapping - Replace an existing mapping for GV with a new
313 /// address. This updates both maps as required. If "Addr" is null, the
314 /// entry for the global is removed from the mappings. This returns the old
315 /// value of the pointer, or null if it was not in the map.
316 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
318 /// getPointerToGlobalIfAvailable - This returns the address of the specified
319 /// global value if it is has already been codegen'd, otherwise it returns
322 /// This function is deprecated for the MCJIT execution engine. It doesn't
323 /// seem to be needed in that case, but an equivalent can be added if it is.
324 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
326 /// getPointerToGlobal - This returns the address of the specified global
327 /// value. This may involve code generation if it's a function.
329 /// This function is deprecated for the MCJIT execution engine. Use
330 /// getGlobalValueAddress instead.
331 void *getPointerToGlobal(const GlobalValue *GV);
333 /// getPointerToFunction - The different EE's represent function bodies in
334 /// different ways. They should each implement this to say what a function
335 /// pointer should look like. When F is destroyed, the ExecutionEngine will
336 /// remove its global mapping and free any machine code. Be sure no threads
337 /// are running inside F when that happens.
339 /// This function is deprecated for the MCJIT execution engine. Use
340 /// getFunctionAddress instead.
341 virtual void *getPointerToFunction(Function *F) = 0;
343 /// getPointerToBasicBlock - The different EE's represent basic blocks in
344 /// different ways. Return the representation for a blockaddress of the
347 /// This function will not be implemented for the MCJIT execution engine.
348 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
350 /// getPointerToFunctionOrStub - If the specified function has been
351 /// code-gen'd, return a pointer to the function. If not, compile it, or use
352 /// a stub to implement lazy compilation if available. See
353 /// getPointerToFunction for the requirements on destroying F.
355 /// This function is deprecated for the MCJIT execution engine. Use
356 /// getFunctionAddress instead.
357 virtual void *getPointerToFunctionOrStub(Function *F) {
358 // Default implementation, just codegen the function.
359 return getPointerToFunction(F);
362 /// getGlobalValueAddress - Return the address of the specified global
363 /// value. This may involve code generation.
365 /// This function should not be called with the JIT or interpreter engines.
366 virtual uint64_t getGlobalValueAddress(const std::string &Name) {
367 // Default implementation for JIT and interpreter. MCJIT will override this.
368 // JIT and interpreter clients should use getPointerToGlobal instead.
372 /// getFunctionAddress - Return the address of the specified function.
373 /// This may involve code generation.
374 virtual uint64_t getFunctionAddress(const std::string &Name) {
375 // Default implementation for JIT and interpreter. MCJIT will override this.
376 // JIT and interpreter clients should use getPointerToFunction instead.
380 // The JIT overrides a version that actually does this.
381 virtual void runJITOnFunction(Function *, MachineCodeInfo * = nullptr) { }
383 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
384 /// at the specified address.
386 const GlobalValue *getGlobalValueAtAddress(void *Addr);
388 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
389 /// Ptr is the address of the memory at which to store Val, cast to
390 /// GenericValue *. It is not a pointer to a GenericValue containing the
391 /// address at which to store Val.
392 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
395 void InitializeMemory(const Constant *Init, void *Addr);
397 /// recompileAndRelinkFunction - This method is used to force a function which
398 /// has already been compiled to be compiled again, possibly after it has been
399 /// modified. Then the entry to the old copy is overwritten with a branch to
400 /// the new copy. If there was no old copy, this acts just like
401 /// VM::getPointerToFunction().
402 virtual void *recompileAndRelinkFunction(Function *F) = 0;
404 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
405 /// corresponding to the machine code emitted to execute this function, useful
406 /// for garbage-collecting generated code.
407 virtual void freeMachineCodeForFunction(Function *F) = 0;
409 /// getOrEmitGlobalVariable - Return the address of the specified global
410 /// variable, possibly emitting it to memory if needed. This is used by the
413 /// This function is deprecated for the MCJIT execution engine. Use
414 /// getGlobalValueAddress instead.
415 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
416 return getPointerToGlobal((const GlobalValue *)GV);
419 /// Registers a listener to be called back on various events within
420 /// the JIT. See JITEventListener.h for more details. Does not
421 /// take ownership of the argument. The argument may be NULL, in
422 /// which case these functions do nothing.
423 virtual void RegisterJITEventListener(JITEventListener *) {}
424 virtual void UnregisterJITEventListener(JITEventListener *) {}
426 /// Sets the pre-compiled object cache. The ownership of the ObjectCache is
427 /// not changed. Supported by MCJIT but not JIT.
428 virtual void setObjectCache(ObjectCache *) {
429 llvm_unreachable("No support for an object cache");
432 /// setProcessAllSections (MCJIT Only): By default, only sections that are
433 /// "required for execution" are passed to the RTDyldMemoryManager, and other
434 /// sections are discarded. Passing 'true' to this method will cause
435 /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
436 /// of whether they are "required to execute" in the usual sense.
438 /// Rationale: Some MCJIT clients want to be able to inspect metadata
439 /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
440 /// performance. Passing these sections to the memory manager allows the
441 /// client to make policy about the relevant sections, rather than having
443 virtual void setProcessAllSections(bool ProcessAllSections) {
444 llvm_unreachable("No support for ProcessAllSections option");
447 /// Return the target machine (if available).
448 virtual TargetMachine *getTargetMachine() { return nullptr; }
450 /// DisableLazyCompilation - When lazy compilation is off (the default), the
451 /// JIT will eagerly compile every function reachable from the argument to
452 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only
453 /// compile the one function and emit stubs to compile the rest when they're
454 /// first called. If lazy compilation is turned off again while some lazy
455 /// stubs are still around, and one of those stubs is called, the program will
458 /// In order to safely compile lazily in a threaded program, the user must
459 /// ensure that 1) only one thread at a time can call any particular lazy
460 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
461 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
462 /// lazy stub. See http://llvm.org/PR5184 for details.
463 void DisableLazyCompilation(bool Disabled = true) {
464 CompilingLazily = !Disabled;
466 bool isCompilingLazily() const {
467 return CompilingLazily;
469 // Deprecated in favor of isCompilingLazily (to reduce double-negatives).
470 // Remove this in LLVM 2.8.
471 bool isLazyCompilationDisabled() const {
472 return !CompilingLazily;
475 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
476 /// allocate space and populate a GlobalVariable that is not internal to
478 void DisableGVCompilation(bool Disabled = true) {
479 GVCompilationDisabled = Disabled;
481 bool isGVCompilationDisabled() const {
482 return GVCompilationDisabled;
485 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
486 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
487 /// resolve symbols in a custom way.
488 void DisableSymbolSearching(bool Disabled = true) {
489 SymbolSearchingDisabled = Disabled;
491 bool isSymbolSearchingDisabled() const {
492 return SymbolSearchingDisabled;
495 /// Enable/Disable IR module verification.
497 /// Note: Module verification is enabled by default in Debug builds, and
498 /// disabled by default in Release. Use this method to override the default.
499 void setVerifyModules(bool Verify) {
500 VerifyModules = Verify;
502 bool getVerifyModules() const {
503 return VerifyModules;
506 /// InstallLazyFunctionCreator - If an unknown function is needed, the
507 /// specified function pointer is invoked to create it. If it returns null,
508 /// the JIT will abort.
509 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
510 LazyFunctionCreator = P;
514 explicit ExecutionEngine(Module *M);
518 void EmitGlobalVariable(const GlobalVariable *GV);
520 GenericValue getConstantValue(const Constant *C);
521 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
525 namespace EngineKind {
526 // These are actually bitmasks that get or-ed together.
531 const static Kind Either = (Kind)(JIT | Interpreter);
534 /// EngineBuilder - Builder class for ExecutionEngines. Use this by
535 /// stack-allocating a builder, chaining the various set* methods, and
536 /// terminating it with a .create() call.
537 class EngineBuilder {
540 EngineKind::Kind WhichEngine;
541 std::string *ErrorStr;
542 CodeGenOpt::Level OptLevel;
543 RTDyldMemoryManager *MCJMM;
544 JITMemoryManager *JMM;
545 bool AllocateGVsWithCode;
546 TargetOptions Options;
547 Reloc::Model RelocModel;
548 CodeModel::Model CMModel;
551 SmallVector<std::string, 4> MAttrs;
555 /// InitEngine - Does the common initialization of default options.
559 /// EngineBuilder - Constructor for EngineBuilder. If create() is called and
560 /// is successful, the created engine takes ownership of the module.
561 EngineBuilder(Module *m) : M(m) {
565 /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
566 /// or whichever engine works. This option defaults to EngineKind::Either.
567 EngineBuilder &setEngineKind(EngineKind::Kind w) {
572 /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
573 /// clients to customize their memory allocation policies for the MCJIT. This
574 /// is only appropriate for the MCJIT; setting this and configuring the builder
575 /// to create anything other than MCJIT will cause a runtime error. If create()
576 /// is called and is successful, the created engine takes ownership of the
577 /// memory manager. This option defaults to NULL. Using this option nullifies
578 /// the setJITMemoryManager() option.
579 EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) {
585 /// setJITMemoryManager - Sets the JIT memory manager to use. This allows
586 /// clients to customize their memory allocation policies. This is only
587 /// appropriate for either JIT or MCJIT; setting this and configuring the
588 /// builder to create an interpreter will cause a runtime error. If create()
589 /// is called and is successful, the created engine takes ownership of the
590 /// memory manager. This option defaults to NULL. This option overrides
591 /// setMCJITMemoryManager() as well.
592 EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) {
598 /// setErrorStr - Set the error string to write to on error. This option
599 /// defaults to NULL.
600 EngineBuilder &setErrorStr(std::string *e) {
605 /// setOptLevel - Set the optimization level for the JIT. This option
606 /// defaults to CodeGenOpt::Default.
607 EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
612 /// setTargetOptions - Set the target options that the ExecutionEngine
613 /// target is using. Defaults to TargetOptions().
614 EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
619 /// setRelocationModel - Set the relocation model that the ExecutionEngine
620 /// target is using. Defaults to target specific default "Reloc::Default".
621 EngineBuilder &setRelocationModel(Reloc::Model RM) {
626 /// setCodeModel - Set the CodeModel that the ExecutionEngine target
627 /// data is using. Defaults to target specific default
628 /// "CodeModel::JITDefault".
629 EngineBuilder &setCodeModel(CodeModel::Model M) {
634 /// setAllocateGVsWithCode - Sets whether global values should be allocated
635 /// into the same buffer as code. For most applications this should be set
636 /// to false. Allocating globals with code breaks freeMachineCodeForFunction
637 /// and is probably unsafe and bad for performance. However, we have clients
638 /// who depend on this behavior, so we must support it. This option defaults
639 /// to false so that users of the new API can safely use the new memory
640 /// manager and free machine code.
641 EngineBuilder &setAllocateGVsWithCode(bool a) {
642 AllocateGVsWithCode = a;
646 /// setMArch - Override the architecture set by the Module's triple.
647 EngineBuilder &setMArch(StringRef march) {
648 MArch.assign(march.begin(), march.end());
652 /// setMCPU - Target a specific cpu type.
653 EngineBuilder &setMCPU(StringRef mcpu) {
654 MCPU.assign(mcpu.begin(), mcpu.end());
658 /// setUseMCJIT - Set whether the MC-JIT implementation should be used
660 EngineBuilder &setUseMCJIT(bool Value) {
665 /// setVerifyModules - Set whether the JIT implementation should verify
666 /// IR modules during compilation.
667 EngineBuilder &setVerifyModules(bool Verify) {
668 VerifyModules = Verify;
672 /// setMAttrs - Set cpu-specific attributes.
673 template<typename StringSequence>
674 EngineBuilder &setMAttrs(const StringSequence &mattrs) {
676 MAttrs.append(mattrs.begin(), mattrs.end());
680 TargetMachine *selectTarget();
682 /// selectTarget - Pick a target either via -march or by guessing the native
683 /// arch. Add any CPU features specified via -mcpu or -mattr.
684 TargetMachine *selectTarget(const Triple &TargetTriple,
687 const SmallVectorImpl<std::string>& MAttrs);
689 ExecutionEngine *create() {
690 return create(selectTarget());
693 ExecutionEngine *create(TargetMachine *TM);
696 // Create wrappers for C Binding types (see CBindingWrapping.h).
697 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
699 } // End llvm namespace