当前位置:网站首页>LLVM - 生成加法
LLVM - 生成加法
2022-04-23 14:11:00 【Mrpre】
本例中KaleidoscopeJIT在 ./llvm-8.0.1.src/examples/Kaleidoscope/include/KaleidoscopeJIT.
中
1、LLVM生成函数
2、LVVM生成加法
3、LLVM jit运行
#include <sys/time.h>
#include "./llvm-8.0.1.src/examples/Kaleidoscope/include/KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
using namespace llvm;
using namespace llvm::orc;
//LLVM items
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::unique_ptr<Module> TheModule;
//JIT
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
/* *double fuckadd(double a, double b) *{ * return a + b; *} * */
int main()
{
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
//init module
TheModule = llvm::make_unique<Module>("myjit", TheContext);
//used to be runned by jit later
TheJIT = llvm::make_unique<KaleidoscopeJIT>();
TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
//define the args
vector<std::string> ArgNames;
//fuckadd has 2 args
ArgNames.push_back(string("a"));
ArgNames.push_back(string("b"));
//make the 2 args attach to LLVM Type::double
std::vector<Type *> Doubles(ArgNames.size(), Type::getDoubleTy(TheContext));
//generate llvm function type
FunctionType *FT = FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
//Create function whose FunctionType is FT
Function *F = Function::Create(FT, Function::ExternalLinkage, "fuckadd", TheModule.get());
//give the name of Function args and save the args as innerargs
unsigned Idx = 0;
std::vector<Value *>innerargs;
for (auto &Arg : F->args()) {
Arg.setName(ArgNames[Idx++]);
innerargs.push_back(&Arg);
}
//generate the function body
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", F);
Builder.SetInsertPoint(BB);
//generate llvm function body:a+b
Value *ret = Builder.CreateFAdd(innerargs[0], innerargs[1], "addtmp");
Builder.CreateRet(ret);
//print LLVM IR
F->print(errs());
//using jit to run this code
auto H = TheJIT->addModule(std::move(TheModule));
auto ExprSymbol = TheJIT->findSymbol("fuckadd");
double (*fuckadd)(double, double) = (double (*)(double, double))(intptr_t)cantFail(ExprSymbol.getAddress());
std::cout<< fuckadd(1, 3) <<std::endl;
}
版权声明
本文为[Mrpre]所创,转载请带上原文链接,感谢
https://wonderful.blog.csdn.net/article/details/106900546
边栏推荐
- setcontext getcontext makecontext swapcontext
- 线程间控制之Semaphore使用介绍
- Pass in external parameters to the main function in clion
- JS format time
- MySQL数据库讲解(七)
- redis数据库讲解(三)redis数据类型
- source insight via samba
- A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
- tcp_diag 内核相关实现 1 调用层次
- mysql 5.1升级到5.69
猜你喜欢
redis数据库讲解二(redis高可用、持久化、性能管理)
About the configuration and use of json5 in nodejs
01-nio basic ByteBuffer and filechannel
剑指offer刷题(2)--面向华为
Pass in external parameters to the main function in clion
Use the executors class to quickly create a thread pool
剑指offer刷题(1)--面向华为
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
Visio画拓扑图随记
关于云容灾,你需要知道这些
随机推荐
OpenSSH的升级、版本号的修改
VMware installation 64 bit XP Chinese tutorial
Wechat applet rotation map swiper
利用json-server在本地创建服务器请求
mysql 5.1升级到5.611
DP energy Necklace
关于UDP接收icmp端口不可达(port unreachable)
Redis cluster 原理
线程间控制之Semaphore使用介绍
Mysql的安装过程(已经安装成功的步骤说明)
mysql锁数据库锁
setcontext getcontext makecontext swapcontext
获取线程返回值Future接口与FutureTask类使用介绍
MySQL基础知识
文字组合,不重复,做搜索或查询关键字匹配
grep无法重定向到文件的问题
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
某政务云项目业务系统迁移调研实践
剑指offer刷题(1)--面向华为
错误:无法远程查找到密钥 “428F7ECC7117F726“