Skip to content

Instantly share code, notes, and snippets.

@JacquesLucke
Created November 30, 2018 14:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacquesLucke/1bddc9aa24fe684d1b19d4bf51a5eb47 to your computer and use it in GitHub Desktop.
Save JacquesLucke/1bddc9aa24fe684d1b19d4bf51a5eb47 to your computer and use it in GitHub Desktop.
Compile simple add function using the LLVM C++ API.
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include <vector>
#include <memory>
void test_llvm() {
llvm::LLVMContext *context = new llvm::LLVMContext();
llvm::Module *module = new llvm::Module("test", *context);
std::vector<llvm::Type *> arg_types = {
llvm::Type::getInt32Ty(*context),
llvm::Type::getInt32Ty(*context)};
llvm::FunctionType *ftype = llvm::FunctionType::get(
llvm::Type::getInt32Ty(*context), arg_types, false);
llvm::Function *func = llvm::Function::Create(
ftype, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "my_func", module);
llvm::BasicBlock *bb = llvm::BasicBlock::Create(*context, "entry", func);
llvm::IRBuilder<> builder(*context);
builder.SetInsertPoint(bb);
llvm::Argument *arg0 = func->arg_begin() + 0;
llvm::Argument *arg1 = func->arg_begin() + 1;
llvm::Value *result = builder.CreateAdd(arg0, arg1, "result");
builder.CreateRet(result);
llvm::verifyFunction(*func, &llvm::outs());
llvm::verifyModule(*module, &llvm::outs());
module->print(llvm::outs(), nullptr);
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
llvm::ExecutionEngine *ee = llvm::EngineBuilder(std::unique_ptr<llvm::Module>(module)).create();
ee->finalizeObject();
int (*add)(int, int) = (int (*)(int, int))ee->getFunctionAddress("my_func");
printf("Pointer: %p\n", add);
printf("Result: %d\n", add(43, 10));
}
@sqward
Copy link

sqward commented Jan 3, 2019

Could that lead to Animation Nodes switching LLVM as an execution backend? I was thinking about it some time ago and it would make perfect sense from performance point of view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment