当前位置:网站首页>Llvm - generate addition
Llvm - generate addition
2022-04-23 15:04:00 【Mrpre】
In this case KaleidoscopeJIT stay ./llvm-8.0.1.src/examples/Kaleidoscope/include/KaleidoscopeJIT.
in
1、LLVM Generating function
2、LVVM Generating addition
3、LLVM jit function
#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://yzsam.com/2022/04/202204231409588187.html
边栏推荐
- Bingbing learning notes: take you step by step to realize the sequence table
- How to upload large files quickly?
- Nuxt project: Global get process Env information
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
- 脏读、不可重复读和幻读介绍
- QT Detailed explanation of pro file
- 分布式事务Seata介绍
- Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
- LeetCode149-直线上最多的点数-数学-哈希表
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
猜你喜欢
如何设计一个良好的API接口?
QT Detailed explanation of pro file
What is the role of the full connection layer?
[detailed explanation of factory mode] factory method mode
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
eolink 如何助力远程办公
Brute force of DVWA low -- > High
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Leetcode162 - find peak - dichotomy - array
随机推荐
OPPO数据湖统一存储技术实践
JUC学习记录(2022.4.22)
Mds55-16-asemi rectifier module mds55-16
epoll 的 ET,LT工作模式———实例程序
买卖股票的最佳时机系列问题
Leetcode149 - maximum number of points on a line - Math - hash table
For 22 years, you didn't know the file contained vulnerabilities?
How does eolink help telecommuting
Redis master-slave synchronization
Share 20 tips for ES6 that should not be missed
The win10 taskbar notification area icon is missing
[thymeleaf] handle null values and use safe operators
Async keyword
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
Frame synchronization implementation
[jz46 translate numbers into strings]
SQL中HAVING和WHERE的区别
How to design a good API interface?
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Chapter 7 of JVM series -- bytecode execution engine