Skip to content

Instantly share code, notes, and snippets.

@seven1m
Last active November 11, 2024 07:25
Show Gist options
  • Save seven1m/2ca74265cca9ef6f493ef1de87e9252d to your computer and use it in GitHub Desktop.
Save seven1m/2ca74265cca9ef6f493ef1de87e9252d to your computer and use it in GitHub Desktop.
Simple 'hello world' example in LLVM IRBuilder
// % clang++ $(llvm-config --cxxflags --ldflags --system-libs --libs core) -o hello_world_llvm hello_world_llvm.cpp
// % ./hello_world_llvm
// hello world
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/IR/IRBuilder.h>
#include <stdio.h>
using namespace llvm;
int main() {
auto context = std::make_unique<LLVMContext>();
IRBuilder<> builder(*context);
auto module = std::make_unique<Module>("hello", *context);
// build a 'main' function
auto i32 = builder.getInt32Ty();
auto prototype = FunctionType::get(i32, false);
Function *main_fn = Function::Create(prototype, Function::ExternalLinkage, "main", module.get());
BasicBlock *body = BasicBlock::Create(*context, "body", main_fn);
builder.SetInsertPoint(body);
// use libc's printf function
auto i8p = builder.getInt8PtrTy();
auto printf_prototype = FunctionType::get(i8p, true);
auto printf_fn = Function::Create(printf_prototype, Function::ExternalLinkage, "printf", module.get());
// call printf with our string
auto format_str = builder.CreateGlobalStringPtr("hello world\n");
builder.CreateCall(printf_fn, { format_str });
// return 0 from main
auto ret = ConstantInt::get(i32, 0);
builder.CreateRet(ret);
// if you want to print the LLVM IR:
//module->print(llvm::outs(), nullptr);
// execute it!
ExecutionEngine *executionEngine = EngineBuilder(std::move(module)).setEngineKind(llvm::EngineKind::Interpreter).create();
Function *main = executionEngine->FindFunctionNamed(StringRef("main"));
auto result = executionEngine->runFunction(main, {});
// return the result
return result.IntVal.getLimitedValue();
}
@basavesh
Copy link

basavesh commented Nov 4, 2024

An update is needed to work with llvm-18.

// use libc's printf function
auto i8p = builder.getInt8Ty()->getPointerTo();

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