Search in sources :

Example 1 with LLVMOrcJITDylibRef

use of org.bytedeco.llvm.LLVM.LLVMOrcJITDylibRef 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();
}
Also used : LLVMOrcThreadSafeContextRef(org.bytedeco.llvm.LLVM.LLVMOrcThreadSafeContextRef) LLVMContextRef(org.bytedeco.llvm.LLVM.LLVMContextRef) PointerPointer(org.bytedeco.javacpp.PointerPointer) LLVMValueRef(org.bytedeco.llvm.LLVM.LLVMValueRef) LongPointer(org.bytedeco.javacpp.LongPointer) IntPointer(org.bytedeco.javacpp.IntPointer) Pointer(org.bytedeco.javacpp.Pointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) LLVMTypeRef(org.bytedeco.llvm.LLVM.LLVMTypeRef) LLVMModuleRef(org.bytedeco.llvm.LLVM.LLVMModuleRef) LLVMOrcLLJITBuilderRef(org.bytedeco.llvm.LLVM.LLVMOrcLLJITBuilderRef) LLVMOrcJITDylibRef(org.bytedeco.llvm.LLVM.LLVMOrcJITDylibRef) LLVMBasicBlockRef(org.bytedeco.llvm.LLVM.LLVMBasicBlockRef) LongPointer(org.bytedeco.javacpp.LongPointer) org.bytedeco.libffi.ffi_cif(org.bytedeco.libffi.ffi_cif) LLVMOrcLLJITRef(org.bytedeco.llvm.LLVM.LLVMOrcLLJITRef) IntPointer(org.bytedeco.javacpp.IntPointer) LLVMOrcThreadSafeModuleRef(org.bytedeco.llvm.LLVM.LLVMOrcThreadSafeModuleRef) LLVMBuilderRef(org.bytedeco.llvm.LLVM.LLVMBuilderRef)

Aggregations

IntPointer (org.bytedeco.javacpp.IntPointer)1 LongPointer (org.bytedeco.javacpp.LongPointer)1 Pointer (org.bytedeco.javacpp.Pointer)1 PointerPointer (org.bytedeco.javacpp.PointerPointer)1 org.bytedeco.libffi.ffi_cif (org.bytedeco.libffi.ffi_cif)1 LLVMBasicBlockRef (org.bytedeco.llvm.LLVM.LLVMBasicBlockRef)1 LLVMBuilderRef (org.bytedeco.llvm.LLVM.LLVMBuilderRef)1 LLVMContextRef (org.bytedeco.llvm.LLVM.LLVMContextRef)1 LLVMModuleRef (org.bytedeco.llvm.LLVM.LLVMModuleRef)1 LLVMOrcJITDylibRef (org.bytedeco.llvm.LLVM.LLVMOrcJITDylibRef)1 LLVMOrcLLJITBuilderRef (org.bytedeco.llvm.LLVM.LLVMOrcLLJITBuilderRef)1 LLVMOrcLLJITRef (org.bytedeco.llvm.LLVM.LLVMOrcLLJITRef)1 LLVMOrcThreadSafeContextRef (org.bytedeco.llvm.LLVM.LLVMOrcThreadSafeContextRef)1 LLVMOrcThreadSafeModuleRef (org.bytedeco.llvm.LLVM.LLVMOrcThreadSafeModuleRef)1 LLVMTypeRef (org.bytedeco.llvm.LLVM.LLVMTypeRef)1 LLVMValueRef (org.bytedeco.llvm.LLVM.LLVMValueRef)1