-
Notifications
You must be signed in to change notification settings - Fork 2
/
llvm101.cpp
113 lines (99 loc) · 2.96 KB
/
llvm101.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/IR/Argument.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/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <memory>
#include <vector>
#include <random>
#include <iostream>
using namespace llvm;
using std::vector;
vector<int>
generate_vec(int size) {
static std::default_random_engine e(67);
vector<int> vec(size);
for (int i = 0; i < size; ++i) {
vec[i] = e() % 10000 + 1;
}
return vec;
}
class LLVM_Environment {
public:
LLVM_Environment() {
InitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
}
~LLVM_Environment() {
// free all resources
llvm_shutdown();
}
};
enum class Op { Plus, Minus, Multiply, Divide, Modular };
using func_t = void (*)(int size, const int* va, const int* vb, const int* vc, int* vd);
class Engine {
public:
Engine()
: Owner(make_unique<Module>("llvm101", ctx)), mod(Owner.get()), builder(ctx) {}
func_t get_function(Op op1, Op op2);
private:
LLVMContext ctx;
std::unique_ptr<Module> Owner;
Module* mod;
IRBuilder<> builder;
std::unique_ptr<ExecutionEngine> EE;
};
// excise now!
func_t
Engine::get_function(Op op1, Op op2) {
static int counter = 0;
auto func_name = "func_" + std::to_string(counter++);
// TODO: insert code for funcion here!!
// TODO: notice that builder, ctx, M is ready at "this" pointer
EE = std::unique_ptr<ExecutionEngine>(EngineBuilder(std::move(Owner)).create());
outs() << *mod;
auto fptr_ = EE->getFunctionAddress(func_name);
return reinterpret_cast<func_t>(fptr_);
}
int
main() {
LLVM_Environment llvm_environment;
int size = 1000;
vector<int> vec_a = generate_vec(size);
vector<int> vec_b = generate_vec(size);
vector<int> vec_c = generate_vec(size);
Engine eng;
func_t func_multiply_plus = eng.get_function(Op::Multiply, Op::Plus);
vector<int> vec_d(size);
func_multiply_plus(size, vec_a.data(), vec_b.data(), vec_c.data(), vec_d.data());
//
for (int i = 0; i < size; ++i) {
auto ans = vec_d[i];
auto ref = vec_a[i] * vec_b[i] + vec_c[i];
if (ref != ans) {
std::cout << "error at i=" << i << " ans=" << ans << " ref=" << ref
<< " a=" << vec_a[i]
<< " b=" << vec_b[i]
<< " c=" << vec_c[i]
<< std::endl;
exit(-1);
}
}
std::cout << "all is ok" << std::endl;
return 0;
}