当前位置:网站首页>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
边栏推荐
- ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- 小红书 timestamp2 (2022/04/22)
- 你還不知道責任鏈模式的使用場景嗎?
- The difference between having and where in SQL
- async关键字
- 你还不知道责任链模式的使用场景吗?
- Daily question - leetcode396 - rotation function - recursion
- 8.3 language model and data set
- epoll 的EPOLLONESHOT 事件———实例程序
- eolink 如何助力遠程辦公
猜你喜欢

1 - first knowledge of go language

Explain TCP's three handshakes in detail

What is the role of the full connection layer?

Share 20 tips for ES6 that should not be missed
![Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]](/img/c6/5241de0d670da3dae136a3047c6160.jpg)
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]

8.5 concise implementation of cyclic neural network

Daily question - leetcode396 - rotation function - recursion

大文件如何快速上传?

Don't you know the usage scenario of the responsibility chain model?

22年了你还不知道文件包含漏洞?
随机推荐
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
LeetCode 练习——396. 旋转函数
8.4 realization of recurrent neural network from zero
Svn detailed use tutorial
冰冰学习笔记:一步一步带你实现顺序表
8.2 text preprocessing
Lotus DB design and Implementation - 1 Basic Concepts
分布式事务Seata介绍
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
2-Go变量操作
[jz46 translate numbers into strings]
每日一题-LeetCode396-旋转函数-递推
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Don't you know the usage scenario of the responsibility chain model?
LeetCode167-两数之和II-双指针-二分-数组-查找
分享3个使用工具,在家剪辑5个作品挣了400多
Difference between like and regexp
3、 Gradient descent solution θ
Introduction to distributed transaction Seata