Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
LLVM [2.6] JIT Tutorial 1 (OS X, 2015)
// A modified version of the "LLVM JIT Tutorial 1" code from
// which actually works with the latest versions of LLVM as of Feb 2015.
// To compile if you're on OS X using 'llvm' [libs, etc.] from Homebrew:
// clang++ -g test.cpp `/usr/local/opt/llvm/bin/llvm-config --cxxflags --ldflags --libs core --system-libs` -I/usr/local/opt/llvm/include -o test
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/PassManager.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
Module* makeLLVMModule() {
LLVMContext &context = getGlobalContext();
// Module Construction
Module* mod = new Module("test", context);
Constant* c = mod->getOrInsertFunction("mul_add",
/*ret type*/ IntegerType::get(context, 32),
/*args*/ IntegerType::get(context, 32),
IntegerType::get(context, 32),
IntegerType::get(context, 32),
/*varargs terminated with null*/ NULL);
Function* mul_add = cast<Function>(c);
Function::arg_iterator args = mul_add->arg_begin();
Value* x = args++;
Value* y = args++;
Value* z = args++;
BasicBlock* block = BasicBlock::Create(context, "entry", mul_add);
IRBuilder<> builder(block);
Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y, "tmp");
Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z, "tmp2");
return mod;
int main(int argc, char**argv) {
Module* Mod = makeLLVMModule();
verifyModule(*Mod, &outs());
PassManager PM;
delete Mod;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.