[llvm-dev] How to use LLVM-C to JIT-compile the floating-point identity function

Jan Wedekind via llvm-dev llvm-dev at lists.llvm.org
Fri Dec 8 14:27:04 PST 2017


On Fri, 8 Dec 2017, Jan Wedekind via llvm-dev wrote:

> Hi,
> I am trying to JIT compile code using floating-point method arguments using 
> the LLVM-C API.

Ok, I am answering it myself.
Instead of LLVMRunFunction one can use LLVMGetFunctionAddress and call the 
function directly using typecasting in C (or a FFI library):


     #include <stdlib.h>
     #include <stdio.h>
     #include <llvm-c/Core.h>
     #include <llvm-c/Analysis.h>
     #include <llvm-c/ExecutionEngine.h>
     #include <llvm-c/Target.h>
     #include <llvm-c/Transforms/Scalar.h>


     int main (int argc, char const *argv[])
     {
       char *error = NULL;
       LLVMLinkInMCJIT();
       LLVMInitializeNativeTarget();
       LLVMInitializeNativeAsmPrinter();
       LLVMInitializeNativeAsmParser();

       LLVMModuleRef mod = LLVMModuleCreateWithName("minimal_module");
       LLVMTypeRef identity_args[] = { LLVMInt32Type() };
       LLVMValueRef identity = LLVMAddFunction(mod, "identity", LLVMFunctionType(LLVMInt32Type(), identity_args, 1, 0));
       LLVMSetFunctionCallConv(identity, LLVMCCallConv);
       LLVMValueRef n = LLVMGetParam(identity, 0);

       LLVMBasicBlockRef entry = LLVMAppendBasicBlock(identity, "entry");
       LLVMBuilderRef builder = LLVMCreateBuilder();
       LLVMPositionBuilderAtEnd(builder, entry);
       LLVMBuildRet(builder, n);

       LLVMVerifyModule(mod, LLVMAbortProcessAction, &error);
       LLVMDisposeMessage(error);

       LLVMExecutionEngineRef engine;
       error = NULL;
       if(LLVMCreateJITCompilerForModule(&engine, mod, 2, &error) != 0) {
         fprintf(stderr, "%s\n", error);
         LLVMDisposeMessage(error);
         abort();
       }
       int (*fun)(int) = (int (*)(int))LLVMGetFunctionAddress(engine, "identity");

       LLVMDumpModule(mod);

       fprintf(stderr, "\n");
       fprintf(stderr, "; Running identity(42) with JIT...\n");
       fprintf(stderr, "; Result: %d\n", (*fun)(42));

       LLVMRemoveModule(engine, mod, &mod, &error);
       LLVMDisposeModule(mod);
       LLVMDisposeExecutionEngine(engine);
       LLVMDisposeBuilder(builder);
       return 0;
     }


More information about the llvm-dev mailing list