Last active
June 12, 2022 10:17
-
-
Save kuasha/5e0525ca784ed0075fb46301d52fbaaf to your computer and use it in GitHub Desktop.
LLVM Tutorial 1: A First Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FROM: https://releases.llvm.org/2.2/docs/tutorial/JITTutorial1.html | |
#include <llvm/IR/Module.h> | |
#include <llvm/IR/Function.h> | |
#include <llvm/IR/CallingConv.h> | |
#include "llvm/IR/IRBuilder.h" | |
#include "llvm/IR/BasicBlock.h" | |
int main(int argc, char **argv) | |
{ | |
auto context = std::make_unique<llvm::LLVMContext>(); | |
auto module = std::make_unique<llvm::Module>("test.gt", *context); | |
auto ptype = llvm::IntegerType::get(*context, 32); | |
std::vector<llvm::Type *> parameter_types; | |
parameter_types.push_back(llvm::IntegerType::get(*context, 32)); | |
parameter_types.push_back(llvm::IntegerType::get(*context, 32)); | |
parameter_types.push_back(llvm::IntegerType::get(*context, 32)); | |
auto ft = llvm::FunctionType::get(ptype, parameter_types, false); | |
llvm::FunctionCallee c = module->getOrInsertFunction("mul_add", ft); | |
llvm::Function *mul_add = llvm::cast<llvm::Function>(c.getCallee()); | |
mul_add->setCallingConv(llvm::CallingConv::C); | |
llvm::Function::arg_iterator args = mul_add->arg_begin(); | |
llvm::Value *x = args++; | |
x->setName("x"); | |
llvm::Value *y = args++; | |
y->setName("y"); | |
llvm::Value *z = args++; | |
z->setName("z"); | |
llvm::BasicBlock *block = llvm::BasicBlock::Create(*context, "entry", mul_add); | |
llvm::IRBuilder<> builder(block); | |
llvm::Value *tmp = builder.CreateBinOp(llvm::Instruction::Mul, x, y, "tmp"); | |
llvm::Value *tmp2 = builder.CreateBinOp(llvm::Instruction::Add, tmp, z, "tmp2"); | |
builder.CreateRet(tmp2); | |
module->print(llvm::errs(), nullptr); | |
return 0; | |
} | |
/* | |
CMAKE_MINIMUM_REQUIRED(VERSION 3.16) | |
SET(CMAKE_CXX_STANDARD 14) | |
project(fntest) | |
find_package(LLVM REQUIRED CONFIG) | |
llvm_map_components_to_libnames(LLVM_LIBS core native orcjit support) | |
add_executable(fntest | |
fntest.cpp | |
) | |
target_link_libraries(fntest PRIVATE ${LLVM_LIBS}) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment