-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfibonacci.cpp
192 lines (168 loc) · 5.44 KB
/
fibonacci.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <coat/Function.h>
#include <coat/ControlFlow.h>
uint32_t fib(uint32_t index){
if(index < 2){
return index;
}else{
return fib(index-1) + fib(index-2);
}
}
template<class Fn>
void assemble_selfcall(Fn &fn){
//auto [index] = fn.getArguments("index"); // clang does not like it, bindings cannot be used in lambda captures
auto args = fn.getArguments("index");
auto &index = std::get<0>(args);
coat::if_then_else(fn, index < 2, [&]{
coat::ret(fn, index);
}, [&]{
auto ret = coat::FunctionCall(fn, fn, index-1);
ret += coat::FunctionCall(fn, fn, index-2);
coat::ret(fn, ret);
});
}
template<class Fn, typename Fnptr>
void assemble_crosscall(Fn &fn, Fnptr fnptr, const char *funcname){
auto [index] = fn.getArguments("index");
//FIXME: needs symbol name of function for LLVM, that is not exposed...
auto ret = coat::FunctionCall(fn, fnptr, funcname, index);
coat::ret(fn, ret);
}
template<class Fn>
void assemble_allinone(Fn &fn){
// signature of internal function inside generated code
using internalfunc_t = uint32_t (*)(uint32_t index);
// create internal function handle to call it and insert code into it later
auto internalCall = fn.template addFunction<internalfunc_t>("rec2");
// EDSL of outer function first
{
auto [index] = fn.getArguments("index");
auto ret = coat::FunctionCall(fn, internalCall, index);
coat::ret(fn, ret);
}
// end outer function and start internal function
fn.startNextFunction(internalCall);
// EDSL for internal function
{
//auto [index] = internalCall.getArguments("index");
auto args = internalCall.getArguments("index");
auto &index = std::get<0>(args);
coat::if_then_else(internalCall, index < 2, [&]{
coat::ret(internalCall, index);
}, [&]{
auto ret = coat::FunctionCall(internalCall, internalCall, index-1);
ret += coat::FunctionCall(internalCall, internalCall, index-2);
coat::ret(internalCall, ret);
});
}
}
#ifdef ENABLE_LLVMJIT
template<typename F>
static void verifyAndOptimize(F &function, const char *fname1, const char *fname2){
function.printIR(fname1);
if(!function.verify()){
puts("verification failed. aborting.");
exit(EXIT_FAILURE); //FIXME: better error handling
}
function.optimize(2);
function.printIR(fname2);
}
#endif
int main(int argc, char *argv[]){
if(argc < 2){
puts("argument required: index in fibonacci sequence");
return -1;
}
uint32_t index = atoi(argv[1]);
uint32_t expected = fib(index);
// init JIT backends
#ifdef ENABLE_ASMJIT
coat::runtimeasmjit asmrt;
#endif
#ifdef ENABLE_LLVMJIT
coat::runtimellvmjit::initTarget();
coat::runtimellvmjit llvmrt;
#endif
// signature of the generated function
using func_t = uint32_t (*)(uint32_t index);
#ifdef ENABLE_ASMJIT
{
// context object representing the generated function
coat::Function<coat::runtimeasmjit,func_t> fn(asmrt);
assemble_selfcall(fn);
// finalize code generation and get function pointer to the generated function
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("selfcall asmjit:\nresult: %u; expected: %u\n", result, expected);
}
#endif
#ifdef ENABLE_LLVMJIT
{
// context object representing the generated function
coat::Function<coat::runtimellvmjit,func_t> fn(llvmrt);
assemble_selfcall(fn);
verifyAndOptimize(fn, "selfcall.ll", "selfcall_opt.ll");
// finalize code generation and get function pointer to the generated function
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("selfcall llvm:\nresult: %u; expected: %u\n", result, expected);
}
#endif
#ifdef ENABLE_ASMJIT
{
coat::Function<coat::runtimeasmjit,func_t> fnrec(asmrt);
assemble_selfcall(fnrec);
func_t foorec = fnrec.finalize();
coat::Function<coat::runtimeasmjit,func_t> fn(asmrt);
assemble_crosscall(fn, foorec, "");
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("crosscall asmjit:\nresult: %u; expected: %u\n", result, expected);
}
#endif
#ifdef ENABLE_LLVMJIT
{
coat::Function<coat::runtimellvmjit,func_t> fnrec(llvmrt, "rec");
assemble_selfcall(fnrec);
func_t foorec = fnrec.finalize();
coat::Function<coat::runtimellvmjit,func_t> fn(llvmrt, "caller");
assemble_crosscall(fn, foorec, "rec");
verifyAndOptimize(fn, "crosscall.ll", "crosscall_opt.ll");
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("crosscall llvm:\nresult: %u; expected: %u\n", result, expected);
}
#endif
#ifdef ENABLE_ASMJIT
{
// context object representing the generated function
coat::Function<coat::runtimeasmjit,func_t> fn(asmrt);
assemble_allinone(fn);
// finalize code generation and get function pointer to the generated function
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("allinone asmjit:\nresult: %u; expected: %u\n", result, expected);
}
#endif
#ifdef ENABLE_LLVMJIT
{
// context object representing the generated function
coat::Function<coat::runtimellvmjit,func_t> fn(llvmrt, "caller2");
assemble_allinone(fn);
verifyAndOptimize(fn, "allinone.ll", "allinone_opt.ll");
// finalize code generation and get function pointer to the generated function
func_t foo = fn.finalize();
// execute the generated function
uint32_t result = foo(index);
printf("allinone llvmjit:\nresult: %u; expected: %u\n", result, expected);
}
#endif
return 0;
}