Search in sources :

Example 1 with LLVMExecutionEngineRef

use of org.bytedeco.llvm.LLVM.LLVMExecutionEngineRef in project javacpp-presets by bytedeco.

the class EmitBitcode method EvaluateBitcode.

/**
 * Sample code for importing a LLVM bitcode file and running a function
 * inside of the imported module
 * <p>
 * This sample depends on EmitBitcode to produce the bitcode file. Make sure
 * you've ran the EmitBitcode sample and have the 'sum.bc' bitcode file.
 * <p>
 * This sample contains code for the following steps:
 * <p>
 * 1. Initializing required LLVM components
 * 2. Load and parse the bitcode
 * 3. Run the 'sum' function inside the module
 * 4. Dispose of the allocated resources
 */
public static void EvaluateBitcode() {
    // Stage 1: Initialize LLVM components
    LLVMInitializeCore(LLVMGetGlobalPassRegistry());
    LLVMInitializeNativeAsmPrinter();
    LLVMInitializeNativeAsmParser();
    LLVMInitializeNativeTarget();
    // Stage 2: Load and parse bitcode
    LLVMContextRef context = LLVMContextCreate();
    LLVMTypeRef i32Type = LLVMInt32TypeInContext(context);
    LLVMModuleRef module = new LLVMModuleRef();
    LLVMMemoryBufferRef membuf = new LLVMMemoryBufferRef();
    BytePointer inputFile = new BytePointer("./sum.bc");
    if (LLVMCreateMemoryBufferWithContentsOfFile(inputFile, membuf, error) != 0) {
        System.err.println("Failed to read file into memory buffer: " + error.getString());
        LLVMDisposeMessage(error);
        return;
    }
    if (LLVMParseBitcodeInContext2(context, membuf, module) != 0) {
        System.err.println("Failed to parser module from bitcode");
        return;
    }
    LLVMExecutionEngineRef engine = new LLVMExecutionEngineRef();
    if (LLVMCreateInterpreterForModule(engine, module, error) != 0) {
        System.err.println("Failed to create LLVM interpreter: " + error.getString());
        LLVMDisposeMessage(error);
        return;
    }
    LLVMValueRef sum = LLVMGetNamedFunction(module, "sum");
    PointerPointer<Pointer> arguments = new PointerPointer<>(2).put(0, LLVMCreateGenericValueOfInt(i32Type, 42, /* signExtend */
    0)).put(1, LLVMCreateGenericValueOfInt(i32Type, 30, /* signExtend */
    0));
    LLVMGenericValueRef result = LLVMRunFunction(engine, sum, 2, arguments);
    System.out.println();
    System.out.print("The result of add(42, 30) imported from bitcode and executed with LLVM interpreter is: ");
    System.out.println(LLVMGenericValueToInt(result, /* signExtend */
    0));
    // Stage 4: Dispose of the allocated resources
    LLVMDisposeModule(module);
    LLVMContextDispose(context);
}
Also used : LLVMExecutionEngineRef(org.bytedeco.llvm.LLVM.LLVMExecutionEngineRef) LLVMContextRef(org.bytedeco.llvm.LLVM.LLVMContextRef) LLVMMemoryBufferRef(org.bytedeco.llvm.LLVM.LLVMMemoryBufferRef) PointerPointer(org.bytedeco.javacpp.PointerPointer) BytePointer(org.bytedeco.javacpp.BytePointer) LLVMValueRef(org.bytedeco.llvm.LLVM.LLVMValueRef) BytePointer(org.bytedeco.javacpp.BytePointer) Pointer(org.bytedeco.javacpp.Pointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) LLVMTypeRef(org.bytedeco.llvm.LLVM.LLVMTypeRef) LLVMModuleRef(org.bytedeco.llvm.LLVM.LLVMModuleRef) LLVMGenericValueRef(org.bytedeco.llvm.LLVM.LLVMGenericValueRef)

Example 2 with LLVMExecutionEngineRef

use of org.bytedeco.llvm.LLVM.LLVMExecutionEngineRef in project javacpp-presets by bytedeco.

the class Factorial method main.

public static void main(String[] args) {
    // Stage 1: Initialize LLVM components
    LLVMInitializeCore(LLVMGetGlobalPassRegistry());
    LLVMLinkInMCJIT();
    LLVMInitializeNativeAsmPrinter();
    LLVMInitializeNativeAsmParser();
    LLVMInitializeNativeTarget();
    // Stage 2: Build the factorial function.
    LLVMContextRef context = LLVMContextCreate();
    LLVMModuleRef module = LLVMModuleCreateWithNameInContext("factorial", context);
    LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
    LLVMTypeRef i32Type = LLVMInt32TypeInContext(context);
    LLVMTypeRef factorialType = LLVMFunctionType(i32Type, i32Type, /* argumentCount */
    1, /* isVariadic */
    0);
    LLVMValueRef factorial = LLVMAddFunction(module, "factorial", factorialType);
    LLVMSetFunctionCallConv(factorial, LLVMCCallConv);
    LLVMValueRef n = LLVMGetParam(factorial, /* parameterIndex */
    0);
    LLVMValueRef zero = LLVMConstInt(i32Type, 0, /* signExtend */
    0);
    LLVMValueRef one = LLVMConstInt(i32Type, 1, /* signExtend */
    0);
    LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(context, factorial, "entry");
    LLVMBasicBlockRef ifFalse = LLVMAppendBasicBlockInContext(context, factorial, "if_false");
    LLVMBasicBlockRef exit = LLVMAppendBasicBlockInContext(context, factorial, "exit");
    LLVMPositionBuilderAtEnd(builder, entry);
    LLVMValueRef condition = LLVMBuildICmp(builder, LLVMIntEQ, n, zero, "condition = n == 0");
    LLVMBuildCondBr(builder, condition, exit, ifFalse);
    LLVMPositionBuilderAtEnd(builder, ifFalse);
    LLVMValueRef nMinusOne = LLVMBuildSub(builder, n, one, "nMinusOne = n - 1");
    PointerPointer<Pointer> arguments = new PointerPointer<>(1).put(0, nMinusOne);
    LLVMValueRef factorialResult = LLVMBuildCall(builder, factorial, arguments, 1, "factorialResult = factorial(nMinusOne)");
    LLVMValueRef resultIfFalse = LLVMBuildMul(builder, n, factorialResult, "resultIfFalse = n * factorialResult");
    LLVMBuildBr(builder, exit);
    LLVMPositionBuilderAtEnd(builder, exit);
    LLVMValueRef phi = LLVMBuildPhi(builder, i32Type, "result");
    PointerPointer<Pointer> phiValues = new PointerPointer<>(2).put(0, one).put(1, resultIfFalse);
    PointerPointer<Pointer> phiBlocks = new PointerPointer<>(2).put(0, entry).put(1, ifFalse);
    LLVMAddIncoming(phi, phiValues, phiBlocks, /* pairCount */
    2);
    LLVMBuildRet(builder, phi);
    // Stage 3: Verify the module using LLVMVerifier
    if (LLVMVerifyModule(module, LLVMPrintMessageAction, error) != 0) {
        LLVMDisposeMessage(error);
        return;
    }
    // Stage 4: Create a pass pipeline using the legacy pass manager
    LLVMPassManagerRef pm = LLVMCreatePassManager();
    LLVMAddAggressiveInstCombinerPass(pm);
    LLVMAddNewGVNPass(pm);
    LLVMAddCFGSimplificationPass(pm);
    LLVMRunPassManager(pm, module);
    LLVMDumpModule(module);
    // Stage 5: Execute the code using MCJIT
    LLVMExecutionEngineRef engine = new LLVMExecutionEngineRef();
    LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions();
    if (LLVMCreateMCJITCompilerForModule(engine, module, options, 3, error) != 0) {
        System.err.println("Failed to create JIT compiler: " + error.getString());
        LLVMDisposeMessage(error);
        return;
    }
    LLVMGenericValueRef argument = LLVMCreateGenericValueOfInt(i32Type, 10, /* signExtend */
    0);
    LLVMGenericValueRef result = LLVMRunFunction(engine, factorial, /* argumentCount */
    1, argument);
    System.out.println();
    System.out.println("; Running factorial(10) with MCJIT...");
    System.out.println("; Result: " + LLVMGenericValueToInt(result, /* signExtend */
    0));
    // Stage 6: Dispose of the allocated resources
    LLVMDisposeExecutionEngine(engine);
    LLVMDisposePassManager(pm);
    LLVMDisposeBuilder(builder);
    LLVMContextDispose(context);
}
Also used : LLVMContextRef(org.bytedeco.llvm.LLVM.LLVMContextRef) PointerPointer(org.bytedeco.javacpp.PointerPointer) LLVMValueRef(org.bytedeco.llvm.LLVM.LLVMValueRef) BytePointer(org.bytedeco.javacpp.BytePointer) Pointer(org.bytedeco.javacpp.Pointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) LLVMPassManagerRef(org.bytedeco.llvm.LLVM.LLVMPassManagerRef) LLVMTypeRef(org.bytedeco.llvm.LLVM.LLVMTypeRef) LLVMModuleRef(org.bytedeco.llvm.LLVM.LLVMModuleRef) LLVMGenericValueRef(org.bytedeco.llvm.LLVM.LLVMGenericValueRef) LLVMExecutionEngineRef(org.bytedeco.llvm.LLVM.LLVMExecutionEngineRef) LLVMBasicBlockRef(org.bytedeco.llvm.LLVM.LLVMBasicBlockRef) LLVMMCJITCompilerOptions(org.bytedeco.llvm.LLVM.LLVMMCJITCompilerOptions) LLVMBuilderRef(org.bytedeco.llvm.LLVM.LLVMBuilderRef)

Aggregations

BytePointer (org.bytedeco.javacpp.BytePointer)2 Pointer (org.bytedeco.javacpp.Pointer)2 PointerPointer (org.bytedeco.javacpp.PointerPointer)2 LLVMContextRef (org.bytedeco.llvm.LLVM.LLVMContextRef)2 LLVMExecutionEngineRef (org.bytedeco.llvm.LLVM.LLVMExecutionEngineRef)2 LLVMGenericValueRef (org.bytedeco.llvm.LLVM.LLVMGenericValueRef)2 LLVMModuleRef (org.bytedeco.llvm.LLVM.LLVMModuleRef)2 LLVMTypeRef (org.bytedeco.llvm.LLVM.LLVMTypeRef)2 LLVMValueRef (org.bytedeco.llvm.LLVM.LLVMValueRef)2 LLVMBasicBlockRef (org.bytedeco.llvm.LLVM.LLVMBasicBlockRef)1 LLVMBuilderRef (org.bytedeco.llvm.LLVM.LLVMBuilderRef)1 LLVMMCJITCompilerOptions (org.bytedeco.llvm.LLVM.LLVMMCJITCompilerOptions)1 LLVMMemoryBufferRef (org.bytedeco.llvm.LLVM.LLVMMemoryBufferRef)1 LLVMPassManagerRef (org.bytedeco.llvm.LLVM.LLVMPassManagerRef)1