From ef1d0ad694a15000106b23eb9dcb2969e0545316 Mon Sep 17 00:00:00 2001 From: Henning Thielemann Date: Thu, 11 Aug 2016 11:19:30 +0200 Subject: [PATCH 1/3] 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/3] 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(); From d26915c855fa1d7fb3eedf06952e0625e1f864b7 Mon Sep 17 00:00:00 2001 From: Wink Saville Date: Tue, 16 Jan 2018 20:37:01 -0800 Subject: [PATCH 3/3] Update for LLVM 5.0 and add .gitignore This builds on top of @thielema's change for 3.6 https://github.com/paulsmith/getting-started-llvm-c-api/pull/1 and adds funcPtr_t to sum.c and adds the .gitignore to ignore the generated files. --- .gitignore | 3 +++ sum.c | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dce254 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +sum +*.o +*.bc diff --git a/sum.c b/sum.c index 1f8a2c6..25b6fb6 100644 --- a/sum.c +++ b/sum.c @@ -16,6 +16,8 @@ #include #include +typedef int32_t (*funcPtr_t) (int32_t, int32_t); + int main(int argc, char const *argv[]) { LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); @@ -58,8 +60,7 @@ int main(int argc, char const *argv[]) { int32_t y = strtoll(argv[2], NULL, 10); { - int32_t (*funcPtr) (int32_t, int32_t) - = LLVMGetPointerToGlobal(engine, sum); + funcPtr_t funcPtr = (funcPtr_t)LLVMGetPointerToGlobal(engine, sum); printf("%d\n", funcPtr(x,y)); }