27#define DEBUG_TYPE "jit"
70 return unwrap(GenValRef)->IntVal.getBitWidth();
83 return unwrap(GenVal)->PointerVal;
89 return unwrap(GenVal)->FloatVal;
91 return unwrap(GenVal)->DoubleVal;
117 *OutError = strdup(
Error.c_str());
129 *OutInterp =
wrap(Interp);
132 *OutError = strdup(
Error.c_str());
152 *OutError = strdup(
Error.c_str());
157 size_t SizeOfPassedOptions) {
159 memset(&options, 0,
sizeof(options));
162 memcpy(PassedOptions, &options,
163 std::min(
sizeof(options), SizeOfPassedOptions));
173 if (SizeOfPassedOptions >
sizeof(options)) {
175 "Refusing to use options struct that is larger than my own; assuming "
176 "LLVM library mismatch.");
185 memcpy(&options, PassedOptions, SizeOfPassedOptions);
190 &
unwrap(M)->getContext().getLibraryOptions();
196 for (
auto &
F : *
Mod) {
197 auto Attrs =
F.getAttributes();
199 Attrs = Attrs.addFnAttribute(
F.getContext(),
"frame-pointer",
Value);
200 F.setAttributes(Attrs);
210 if (std::optional<CodeModel::Model> CM =
unwrap(options.CodeModel, JIT))
214 std::unique_ptr<RTDyldMemoryManager>(
unwrap(options.MCJMM)));
219 *OutError = strdup(
Error.c_str());
228 unwrap(EE)->finalizeObject();
229 unwrap(EE)->runStaticConstructorsDestructors(
false);
233 unwrap(EE)->finalizeObject();
234 unwrap(EE)->runStaticConstructorsDestructors(
true);
238 unsigned ArgC,
const char *
const *ArgV,
239 const char *
const *EnvP) {
240 unwrap(EE)->finalizeObject();
242 std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
249 unwrap(EE)->finalizeObject();
251 std::vector<GenericValue> ArgVec;
252 ArgVec.reserve(NumArgs);
253 for (
unsigned I = 0;
I != NumArgs; ++
I)
254 ArgVec.push_back(*
unwrap(Args[
I]));
265 unwrap(EE)->addModule(std::unique_ptr<Module>(
unwrap(M)));
305 unwrap(EE)->finalizeObject();
311 return unwrap(EE)->getGlobalValueAddress(Name);
315 return unwrap(EE)->getFunctionAddress(Name);
320 assert(OutError &&
"OutError must be non-null");
321 auto *ExecEngine =
unwrap(EE);
322 if (ExecEngine->hasError()) {
323 *OutError = strdup(ExecEngine->getErrorMessage().c_str());
324 ExecEngine->clearErrorMessage();
334struct SimpleBindingMMFunctions {
343 SimpleBindingMemoryManager(
const SimpleBindingMMFunctions& Functions,
345 ~SimpleBindingMemoryManager()
override;
347 uint8_t *allocateCodeSection(uintptr_t
Size,
unsigned Alignment,
349 StringRef SectionName)
override;
351 uint8_t *allocateDataSection(uintptr_t
Size,
unsigned Alignment,
352 unsigned SectionID, StringRef SectionName,
353 bool isReadOnly)
override;
355 bool finalizeMemory(std::string *ErrMsg)
override;
358 SimpleBindingMMFunctions Functions;
362SimpleBindingMemoryManager::SimpleBindingMemoryManager(
363 const SimpleBindingMMFunctions& Functions,
365 : Functions(Functions), Opaque(Opaque) {
366 assert(Functions.AllocateCodeSection &&
367 "No AllocateCodeSection function provided!");
368 assert(Functions.AllocateDataSection &&
369 "No AllocateDataSection function provided!");
370 assert(Functions.FinalizeMemory &&
371 "No FinalizeMemory function provided!");
372 assert(Functions.Destroy &&
373 "No Destroy function provided!");
376SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
377 Functions.Destroy(Opaque);
380uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
381 uintptr_t
Size,
unsigned Alignment,
unsigned SectionID,
382 StringRef SectionName) {
383 return Functions.AllocateCodeSection(Opaque,
Size, Alignment, SectionID,
387uint8_t *SimpleBindingMemoryManager::allocateDataSection(
388 uintptr_t
Size,
unsigned Alignment,
unsigned SectionID,
389 StringRef SectionName,
bool isReadOnly) {
390 return Functions.AllocateDataSection(Opaque,
Size, Alignment, SectionID,
395bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
396 char *errMsgCString =
nullptr;
397 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
398 assert((result || !errMsgCString) &&
399 "Did not expect an error message if FinalizeMemory succeeded");
402 *ErrMsg = errMsgCString;
417 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
422 functions.AllocateCodeSection = AllocateCodeSection;
423 functions.AllocateDataSection = AllocateDataSection;
424 functions.FinalizeMemory = FinalizeMemory;
426 return wrap(
new SimpleBindingMemoryManager(
functions, Opaque));
436#if !LLVM_USE_INTEL_JITEVENTS
443#if !LLVM_USE_OPROFILE
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Lower uses of LDS variables from non kernel functions
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
static char getTypeID(Type *Ty)
Module.h This file contains the declarations for the Module class.
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
int64_t getSExtValue() const
Get sign extended value.
Builder class for ExecutionEngines.
EngineBuilder & setTargetOptions(const TargetOptions &Opts)
setTargetOptions - Set the target options that the ExecutionEngine target is using.
LLVM_ABI EngineBuilder & setMCJITMemoryManager(std::unique_ptr< RTDyldMemoryManager > mcjmm)
setMCJITMemoryManager - Sets the MCJIT memory manager to use.
EngineBuilder & setCodeModel(CodeModel::Model M)
setCodeModel - Set the CodeModel that the ExecutionEngine target data is using.
EngineBuilder & setOptLevel(CodeGenOptLevel l)
setOptLevel - Set the optimization level for the JIT.
EngineBuilder & setErrorStr(std::string *e)
setErrorStr - Set the error string to write to on error.
EngineBuilder & setEngineKind(EngineKind::Kind w)
setEngineKind - Controls whether the user wants the interpreter, the JIT, or whichever engine works.
ExecutionEngine * create()
Lightweight error class with error context and mandatory checking.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
const cl::LibraryOptionsContext * LibraryOptions
A Module instance is used to store all the information related to an LLVM module.
Represent a constant reference to a string, i.e.
Primary interface to the complete machine description for the target machine.
MCTargetOptions MCOptions
Machine level options.
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
@ FloatTyID
32-bit floating point type
@ DoubleTyID
64-bit floating point type
LLVM Value Representation.
uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name)
void * LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn)
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, void *Addr)
LLVMBool LLVMCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, char **OutError)
Create an MCJIT execution engine for a module, with the given options.
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateOProfileJITEventListener(void)
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned NumArgs, LLVMGenericValueRef *Args)
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateIntelJITEventListener(void)
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreatePerfJITEventListener(void)
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, LLVMBool IsSigned)
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F)
LLVM_C_ABI void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM)
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError)
struct LLVMOpaqueMCJITMemoryManager * LLVMMCJITMemoryManagerRef
void * LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
void(* LLVMMemoryManagerDestroyCallback)(void *Opaque)
uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name)
struct LLVMOpaqueExecutionEngine * LLVMExecutionEngineRef
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, unsigned long long N, LLVMBool IsSigned)
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned ArgC, const char *const *ArgV, const char *const *EnvP)
LLVMTargetMachineRef LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE)
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE)
double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal)
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, LLVMModuleRef *OutMod, char **OutError)
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE)
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal)
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError)
struct LLVMOpaqueGenericValue * LLVMGenericValueRef
LLVMBool(* LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg)
LLVMBool LLVMExecutionEngineGetErrMsg(LLVMExecutionEngineRef EE, char **OutError)
Returns true on error, false on success.
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE)
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M)
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions)
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, LLVMValueRef *OutFn)
uint8_t *(* LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName, LLVMBool IsReadOnly)
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P)
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef)
void * LLVMGenericValueToPointer(LLVMGenericValueRef GenVal)
LLVM_C_ABI LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(void *Opaque, LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, LLVMMemoryManagerDestroyCallback Destroy)
Create a simple custom MCJIT memory manager.
uint8_t *(* LLVMMemoryManagerAllocateCodeSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName)
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, unsigned OptLevel, char **OutError)
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE)
LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N)
struct LLVMOpaqueValue * LLVMValueRef
Represents an individual value in LLVM IR.
struct LLVMOpaqueType * LLVMTypeRef
Each value in the LLVM IR has a type, an LLVMTypeRef.
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
struct LLVMOpaqueJITEventListener * LLVMJITEventListenerRef
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
@ LLVMCodeModelJITDefault
struct LLVMOpaqueTargetData * LLVMTargetDataRef
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
CodeGenOptLevel
Code generation optimization level.
@ Global
Append to llvm.global_dtors.
@ Mod
The access may modify the value stored in memory.
Attribute unwrap(LLVMAttributeRef Attr)
LLVMAttributeRef wrap(Attribute Attr)