From ef1d0ad694a15000106b23eb9dcb2969e0545316 Mon Sep 17 00:00:00 2001 From: Henning Thielemann Date: Thu, 11 Aug 2016 11:19:30 +0200 Subject: [PATCH 1/2] switch from JIT to MCJIT This will allow us to use LLVM-3.6 and later. Unfortunately, LLVMRunFunction does not work anymore in MCJT. Thus we must use LLVMGetPointerToGlobal instead. --- Makefile | 2 +- sum.c | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 90af0dd..9f15a6d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=clang CFLAGS=-g `llvm-config --cflags` LD=clang++ -LDFLAGS=`llvm-config --cxxflags --ldflags --libs core executionengine jit interpreter analysis native bitwriter --system-libs` +LDFLAGS=`llvm-config --cxxflags --ldflags --libs core executionengine mcjit interpreter analysis native bitwriter --system-libs` all: sum diff --git a/sum.c b/sum.c index 7fce959..d025507 100644 --- a/sum.c +++ b/sum.c @@ -36,7 +36,7 @@ int main(int argc, char const *argv[]) { LLVMExecutionEngineRef engine; error = NULL; - LLVMLinkInJIT(); + LLVMLinkInMCJIT(); LLVMInitializeNativeTarget(); if (LLVMCreateExecutionEngineForModule(&engine, mod, &error) != 0) { fprintf(stderr, "failed to create execution engine\n"); @@ -52,15 +52,14 @@ int main(int argc, char const *argv[]) { fprintf(stderr, "usage: %s x y\n", argv[0]); exit(EXIT_FAILURE); } - long long x = strtoll(argv[1], NULL, 10); - long long y = strtoll(argv[2], NULL, 10); + int32_t x = strtoll(argv[1], NULL, 10); + int32_t y = strtoll(argv[2], NULL, 10); - LLVMGenericValueRef args[] = { - LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0), - LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0) - }; - LLVMGenericValueRef res = LLVMRunFunction(engine, sum, 2, args); - printf("%d\n", (int)LLVMGenericValueToInt(res, 0)); + { + int32_t (*funcPtr) (int32_t, int32_t) + = LLVMGetPointerToGlobal(engine, sum); + printf("%d\n", funcPtr(x,y)); + } // Write out bitcode to file if (LLVMWriteBitcodeToFile(mod, "sum.bc") != 0) { From 976875136d00b4754672366d9219e8e23ec9506b Mon Sep 17 00:00:00 2001 From: Henning Thielemann Date: Thu, 11 Aug 2016 11:22:08 +0200 Subject: [PATCH 2/2] Also initialize AsmPrinter and AsmParser. MCJIT seems to require that. --- sum.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sum.c b/sum.c index d025507..1f8a2c6 100644 --- a/sum.c +++ b/sum.c @@ -38,6 +38,8 @@ int main(int argc, char const *argv[]) { error = NULL; LLVMLinkInMCJIT(); LLVMInitializeNativeTarget(); + LLVMInitializeNativeAsmPrinter(); + LLVMInitializeNativeAsmParser(); if (LLVMCreateExecutionEngineForModule(&engine, mod, &error) != 0) { fprintf(stderr, "failed to create execution engine\n"); abort();