use of org.bytedeco.llvm.LLVM.LLVMOrcLLJITBuilderRef in project javacpp-presets by bytedeco.
the class OrcJit method main.
public static void main(String[] args) {
// Stage 1: Initialize LLVM components
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
// Stage 2: Generate LLVM IR
LLVMOrcThreadSafeContextRef threadContext = LLVMOrcCreateNewThreadSafeContext();
LLVMContextRef context = LLVMOrcThreadSafeContextGetContext(threadContext);
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("sum", context);
LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
LLVMTypeRef i32Type = LLVMInt32TypeInContext(context);
PointerPointer<Pointer> sumArgumentTypes = new PointerPointer<>(2).put(0, i32Type).put(1, i32Type);
LLVMTypeRef sumType = LLVMFunctionType(i32Type, sumArgumentTypes, /* argumentCount */
2, /* isVariadic */
0);
LLVMValueRef sum = LLVMAddFunction(module, "sum", sumType);
LLVMSetFunctionCallConv(sum, LLVMCCallConv);
LLVMValueRef lhs = LLVMGetParam(sum, 0);
LLVMValueRef rhs = LLVMGetParam(sum, 1);
LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(context, sum, "entry");
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef result = LLVMBuildAdd(builder, lhs, rhs, "result = lhs + rhs");
LLVMBuildRet(builder, result);
LLVMDumpModule(module);
LLVMOrcThreadSafeModuleRef threadModule = LLVMOrcCreateNewThreadSafeModule(module, threadContext);
// Stage 3: Execute using OrcJIT
LLVMOrcLLJITRef jit = new LLVMOrcLLJITRef();
LLVMOrcLLJITBuilderRef jitBuilder = LLVMOrcCreateLLJITBuilder();
if ((err = LLVMOrcCreateLLJIT(jit, jitBuilder)) != null) {
System.err.println("Failed to create LLJIT: " + LLVMGetErrorMessage(err));
LLVMConsumeError(err);
return;
}
LLVMOrcJITDylibRef mainDylib = LLVMOrcLLJITGetMainJITDylib(jit);
if ((err = LLVMOrcLLJITAddLLVMIRModule(jit, mainDylib, threadModule)) != null) {
System.err.println("Failed to add LLVM IR module: " + LLVMGetErrorMessage(err));
LLVMConsumeError(err);
return;
}
final LongPointer res = new LongPointer(1);
if ((err = LLVMOrcLLJITLookup(jit, res, "sum")) != null) {
System.err.println("Failed to look up 'sum' symbol: " + LLVMGetErrorMessage(err));
LLVMConsumeError(err);
return;
}
// Stage 4: Call the function with libffi
ffi_cif cif = new ffi_cif();
PointerPointer<Pointer> arguments = new PointerPointer<>(2).put(0, ffi_type_sint()).put(1, ffi_type_sint());
PointerPointer<Pointer> values = new PointerPointer<>(2).put(0, new IntPointer(1).put(42)).put(1, new IntPointer(1).put(30));
IntPointer returns = new IntPointer(1);
if (ffi_prep_cif(cif, FFI_DEFAULT_ABI(), 2, ffi_type_sint(), arguments) != FFI_OK) {
System.err.println("Failed to prepare the libffi cif");
return;
}
Pointer function = new Pointer() {
{
address = res.get();
}
};
ffi_call(cif, function, returns, values);
System.out.println("Evaluating sum(42, 30) through OrcJIT results in: " + returns.get());
// Stage 5: Dispose of the allocated resources
LLVMOrcDisposeLLJIT(jit);
LLVMShutdown();
}
Aggregations