当前位置:网站首页>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
边栏推荐
- Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
- ssh限制登录的四种手段
- DP - [noip2000] grid access
- JS progress bar, displaying the loading progress
- Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
- xx项目架构随记
- Preview CSV file
- mysql 5.1升级到5.611
- 微信小程序轮播图swiper
- json date时间日期格式化
猜你喜欢

xx项目架构随记

统信UOS PHP7.2.3升级至PHP7.2.24

TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别

API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance

政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移

在Clion中给主函数传入外部参数

MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍

Visio画拓扑图随记

redis数据库讲解二(redis高可用、持久化、性能管理)

sar命令详解
随机推荐
mysql 5.1升级到5.68
Five ways of using synchronized to remove clouds and fog are introduced
source insight via samba
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
MySQL数据库讲解(七)
json date时间日期格式化
Quickly understand the three ways of thread implementation
ActiveMQ Basics
ansible及常用模块的使用
利用json-server在本地创建服务器请求
VMware installation 64 bit XP Chinese tutorial
flannel 原理 之 TUN模式
Logback logger and root
KVM学习资源
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
预览CSV文件
IE8 browser prompts whether to block access to JS script
Uni app message push
Man man notes and @ reboot usage of crontab
翻牌效果