LLVM: lib/ExecutionEngine/ExecutionEngineBindings.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
22#include
23#include
24
25using namespace llvm;
26
27#define DEBUG_TYPE "jit"
28
29
31
32
37
38
39
41 unsigned long long N,
45 return wrap(GenVal);
46}
47
51 return wrap(GenVal);
52}
53
59 break;
62 break;
63 default:
64 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
65 }
66 return wrap(GenVal);
67}
68
70 return unwrap(GenValRef)->IntVal.getBitWidth();
71}
72
76 if (IsSigned)
78 else
80}
81
83 return unwrap(GenVal)->PointerVal;
84}
85
89 return unwrap(GenVal)->FloatVal;
91 return unwrap(GenVal)->DoubleVal;
92 default:
93 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
94 }
95}
96
100
101
102
105 char **OutError) {
106 std::string Error;
111 *OutEE = wrap(EE);
112 return 0;
113 }
114 *OutError = strdup(Error.c_str());
115 return 1;
116}
117
120 char **OutError) {
121 std::string Error;
126 *OutInterp = wrap(Interp);
127 return 0;
128 }
129 *OutError = strdup(Error.c_str());
130 return 1;
131}
132
135 unsigned OptLevel,
136 char **OutError) {
137 std::string Error;
143 *OutJIT = wrap(JIT);
144 return 0;
145 }
146 *OutError = strdup(Error.c_str());
147 return 1;
148}
149
151 size_t SizeOfPassedOptions) {
153 memset(&options, 0, sizeof(options));
155
156 memcpy(PassedOptions, &options,
157 std::min(sizeof(options), SizeOfPassedOptions));
158}
159
163 char **OutError) {
165
166
167 if (SizeOfPassedOptions > sizeof(options)) {
168 *OutError = strdup(
169 "Refusing to use options struct that is larger than my own; assuming "
170 "LLVM library mismatch.");
171 return 1;
172 }
173
174
175
176
177
179 memcpy(&options, PassedOptions, SizeOfPassedOptions);
180
182 targetOptions.EnableFastISel = options.EnableFastISel;
183 std::unique_ptr Mod(unwrap(M));
184
186
187
189 auto Attrs = F.getAttributes();
190 StringRef Value = options.NoFramePointerElim ? "all" : "none";
191 Attrs = Attrs.addFnAttribute(F.getContext(), "frame-pointer", Value);
192 F.setAttributes(Attrs);
193 }
194
195 std::string Error;
201 bool JIT;
202 if (std::optionalCodeModel::Model CM = unwrap(options.CodeModel, JIT))
204 if (options.MCJMM)
206 std::unique_ptr(unwrap(options.MCJMM)));
208 *OutJIT = wrap(JIT);
209 return 0;
210 }
211 *OutError = strdup(Error.c_str());
212 return 1;
213}
214
218
220 unwrap(EE)->finalizeObject();
221 unwrap(EE)->runStaticConstructorsDestructors(false);
222}
223
225 unwrap(EE)->finalizeObject();
226 unwrap(EE)->runStaticConstructorsDestructors(true);
227}
228
230 unsigned ArgC, const char * const *ArgV,
231 const char * const *EnvP) {
232 unwrap(EE)->finalizeObject();
233
234 std::vectorstd::string ArgVec(ArgV, ArgV + ArgC);
236}
237
239 unsigned NumArgs,
241 unwrap(EE)->finalizeObject();
242
243 std::vector ArgVec;
244 ArgVec.reserve(NumArgs);
245 for (unsigned I = 0; I != NumArgs; ++I)
246 ArgVec.push_back(*unwrap(Args[I]));
247
250 return wrap(Result);
251}
252
255
257 unwrap(EE)->addModule(std::unique_ptr(unwrap(M)));
258}
259
267
272 return 0;
273 }
274 return 1;
275}
276
281
283 return wrap(&unwrap(EE)->getDataLayout());
284}
285
288 return wrap(unwrap(EE)->getTargetMachine());
289}
290
295
297 unwrap(EE)->finalizeObject();
298
300}
301
303 return unwrap(EE)->getGlobalValueAddress(Name);
304}
305
307 return unwrap(EE)->getFunctionAddress(Name);
308}
309
311 char **OutError) {
312 assert(OutError && "OutError must be non-null");
313 auto *ExecEngine = unwrap(EE);
314 if (ExecEngine->hasError()) {
315 *OutError = strdup(ExecEngine->getErrorMessage().c_str());
316 ExecEngine->clearErrorMessage();
317 return true;
318 }
319 return false;
320}
321
322
323
324namespace {
325
326struct SimpleBindingMMFunctions {
331};
332
334public:
335 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
336 void *Opaque);
337 ~SimpleBindingMemoryManager() override;
338
339 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
340 unsigned SectionID,
341 StringRef SectionName) override;
342
343 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
344 unsigned SectionID, StringRef SectionName,
345 bool isReadOnly) override;
346
347 bool finalizeMemory(std::string *ErrMsg) override;
348
349private:
350 SimpleBindingMMFunctions Functions;
351 void *Opaque;
352};
353
354SimpleBindingMemoryManager::SimpleBindingMemoryManager(
355 const SimpleBindingMMFunctions& Functions,
356 void *Opaque)
357 : Functions(Functions), Opaque(Opaque) {
358 assert(Functions.AllocateCodeSection &&
359 "No AllocateCodeSection function provided!");
360 assert(Functions.AllocateDataSection &&
361 "No AllocateDataSection function provided!");
362 assert(Functions.FinalizeMemory &&
363 "No FinalizeMemory function provided!");
364 assert(Functions.Destroy &&
365 "No Destroy function provided!");
366}
367
368SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
369 Functions.Destroy(Opaque);
370}
371
372uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
373 uintptr_t Size, unsigned Alignment, unsigned SectionID,
374 StringRef SectionName) {
375 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
377}
378
379uint8_t *SimpleBindingMemoryManager::allocateDataSection(
380 uintptr_t Size, unsigned Alignment, unsigned SectionID,
381 StringRef SectionName, bool isReadOnly) {
382 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
384 isReadOnly);
385}
386
387bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
388 char *errMsgCString = nullptr;
389 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
390 assert((result || !errMsgCString) &&
391 "Did not expect an error message if FinalizeMemory succeeded");
392 if (errMsgCString) {
393 if (ErrMsg)
394 *ErrMsg = errMsgCString;
395 free(errMsgCString);
396 }
397 return result;
398}
399
400}
401
403 void *Opaque,
408
409 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
410 !Destroy)
411 return nullptr;
412
413 SimpleBindingMMFunctions functions;
414 functions.AllocateCodeSection = AllocateCodeSection;
415 functions.AllocateDataSection = AllocateDataSection;
416 functions.FinalizeMemory = FinalizeMemory;
418 return wrap(new SimpleBindingMemoryManager(functions, Opaque));
419}
420
424
425
426
427
428#if !LLVM_USE_INTEL_JITEVENTS
430{
431 return nullptr;
432}
433#endif
434
435#if !LLVM_USE_OPROFILE
437{
438 return nullptr;
439}
440#endif
441
442#if !LLVM_USE_PERF
444{
445 return nullptr;
446}
447#endif
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...
A Module instance is used to store all the information related to an LLVM module.
StringRef - Represent a constant reference to a string, i.e.
Primary interface to the complete machine description for the target machine.
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)
Definition ExecutionEngineBindings.cpp:302
void * LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn)
Definition ExecutionEngineBindings.cpp:277
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, void *Addr)
Definition ExecutionEngineBindings.cpp:291
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.
Definition ExecutionEngineBindings.cpp:160
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateOProfileJITEventListener(void)
Definition ExecutionEngineBindings.cpp:436
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned NumArgs, LLVMGenericValueRef *Args)
Definition ExecutionEngineBindings.cpp:238
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreateIntelJITEventListener(void)
Definition ExecutionEngineBindings.cpp:429
LLVM_C_ABI LLVMJITEventListenerRef LLVMCreatePerfJITEventListener(void)
Definition ExecutionEngineBindings.cpp:443
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, LLVMBool IsSigned)
Definition ExecutionEngineBindings.cpp:73
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F)
Definition ExecutionEngineBindings.cpp:253
LLVM_C_ABI void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM)
Definition ExecutionEngineBindings.cpp:421
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError)
Definition ExecutionEngineBindings.cpp:103
struct LLVMOpaqueMCJITMemoryManager * LLVMMCJITMemoryManagerRef
void * LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
Definition ExecutionEngineBindings.cpp:296
void(* LLVMMemoryManagerDestroyCallback)(void *Opaque)
uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name)
Definition ExecutionEngineBindings.cpp:306
struct LLVMOpaqueExecutionEngine * LLVMExecutionEngineRef
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, unsigned long long N, LLVMBool IsSigned)
Definition ExecutionEngineBindings.cpp:40
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned ArgC, const char *const *ArgV, const char *const *EnvP)
Definition ExecutionEngineBindings.cpp:229
LLVMTargetMachineRef LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE)
Definition ExecutionEngineBindings.cpp:287
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE)
Definition ExecutionEngineBindings.cpp:215
double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal)
Definition ExecutionEngineBindings.cpp:86
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, LLVMModuleRef *OutMod, char **OutError)
Definition ExecutionEngineBindings.cpp:260
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE)
Definition ExecutionEngineBindings.cpp:282
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal)
Definition ExecutionEngineBindings.cpp:97
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError)
Definition ExecutionEngineBindings.cpp:118
struct LLVMOpaqueGenericValue * LLVMGenericValueRef
LLVMBool(* LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg)
LLVMBool LLVMExecutionEngineGetErrMsg(LLVMExecutionEngineRef EE, char **OutError)
Returns true on error, false on success.
Definition ExecutionEngineBindings.cpp:310
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE)
Definition ExecutionEngineBindings.cpp:219
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M)
Definition ExecutionEngineBindings.cpp:256
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions)
Definition ExecutionEngineBindings.cpp:150
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, LLVMValueRef *OutFn)
Definition ExecutionEngineBindings.cpp:268
uint8_t *(* LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName, LLVMBool IsReadOnly)
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P)
Definition ExecutionEngineBindings.cpp:48
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef)
Definition ExecutionEngineBindings.cpp:69
void * LLVMGenericValueToPointer(LLVMGenericValueRef GenVal)
Definition ExecutionEngineBindings.cpp:82
LLVM_C_ABI LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(void *Opaque, LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, LLVMMemoryManagerDestroyCallback Destroy)
Create a simple custom MCJIT memory manager.
Definition ExecutionEngineBindings.cpp:402
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)
Definition ExecutionEngineBindings.cpp:133
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE)
Definition ExecutionEngineBindings.cpp:224
LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N)
Definition ExecutionEngineBindings.cpp:54
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
struct LLVMOpaqueTargetData * LLVMTargetDataRef
@ LLVMCodeModelJITDefault
#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)