forked from skeru/libVersioningCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestJit.cpp
177 lines (147 loc) · 5.93 KB
/
TestJit.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
/* Copyright 2017 Politecnico di Milano.
* Developed by : Stefano Cherubin
* PhD student, Politecnico di Milano
* <first_name>.<family_name>@polimi.it
* Marco Festa
* Ms student, Politecnico di Milano
* <first_name>2.<family_name>@mail.polimi.it
* Nicole Gervasoni
* Ms student, Politecnico di Milano
* <first_name>annamaria.<family_name>@mail.polimi.it
*
* This file is part of libVersioningCompiler
*
* libVersioningCompiler is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libVersioningCompiler is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libVersioningCompiler. If not, see <http://www.gnu.org/licenses/>
*/
#include "versioningCompiler/Version.hpp"
#include "versioningCompiler/CompilerImpl/SystemCompiler.hpp"
#include "versioningCompiler/CompilerImpl/SystemCompilerOptimizer.hpp"
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>
// ----------------------------------------------------------------------
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
// ----------------------------------------------------------------
#if HAVE_CLANG_AS_LIB
#include "versioningCompiler/CompilerImpl/ClangLibCompiler.hpp"
#include <versioningCompiler/CompilerImpl/JITCompiler.hpp>
#endif
#ifndef FORCED_PATH_TO_TEST
#define FORCED_PATH_TO_TEST "../libVersioningCompiler/test_code"
#endif
#define PATH_TO_C_TEST_CODE FORCED_PATH_TO_TEST "/test_code.c"
#ifndef TEST_FUNCTION
#define TEST_FUNCTION "test_function"
#endif
#ifndef TEST_FUNCTION_LBL
#define TEST_FUNCTION_LBL "TEST_FUNCTION"
#endif
using namespace llvm;
using namespace orc;
// someone should provide the signature of the function now versioning
// in the form of function pointer type.
typedef int (*signature_t)(int);
int main(int argc, char const *argv[]) {
std::cout << "Setting up builder.." << std::endl;
vc::Version::Builder builder;
builder._functionName.push_back(TEST_FUNCTION);
builder._fileName_src.push_back(PATH_TO_C_TEST_CODE);
builder.addFunctionFlag(TEST_FUNCTION_LBL);
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
std::cout << "Setting up target machine.." << std::endl;
auto targetMachine = EngineBuilder().selectTarget();
// ---------- Compiler initialization ---------
std::cout << "Setting up compiler.." << std::endl;
vc::compiler_ptr_t jitCompiler = vc::make_compiler<vc::JITCompiler>(
"jitCompiler",
".",
"./test_jit.log",
*targetMachine
);
builder._compiler = jitCompiler;
builder._autoremoveFilesEnable = false;
builder._optOptionList = {
vc::Option("mem2reg", "-mem2reg"),
vc::Option("o", "-O", "3"),
};
std::cout << "Building version.." << std::endl;
vc::version_ptr_t myversion = builder.build();
std::cout << "Preparing IR for version.." << std::endl;
bool ok = myversion->prepareIR();
if (!ok) {
if (!jitCompiler->hasIRSupport()) {
std::cerr << "Error: something went wrong with the compiler." << std::endl;
} else if (!myversion->hasGeneratedIR()) {
std::cerr << "Error: generation of IR for myversion failed." << std::endl;
} else {
std::cerr << "Error: optimization of IR for myversion failed." << std::endl;
}
std::cerr << "\tPlease check the compiler/optimizer install path" << std::endl;
return -1;
}
std::cout << "IR prepared. Going for myversion compilation.." << std::endl;
ok = myversion->compile();
if (!ok) {
std::cerr << "Error: myversion compilation has failed." << std::endl;
if (myversion->hasGeneratedBin()) {
std::cerr << "\tmyversion has generated binary " << myversion->getFileName_bin() << std::endl;
}
if (myversion->hasLoadedSymbol()) {
std::cerr << "\tmyversion has loaded symbol" << std::endl;
}
return -1;
}
std::cout << "Object myversion compiled." << std::endl;
std::cout << "Executing myversion symbol." << std::endl;
std::vector<signature_t> f;
f.push_back((signature_t) myversion->getSymbol());
std::cout << "Result 1: " << f[0](42) << std::endl;
std::cout << "Result 2: " << f[0](12) << std::endl;
std::cout << "Result 3: " << f[0](128) << std::endl;
std::cout << "Folding myversion object." << std::endl;
myversion->fold();
std::cout << "Version folded, reloading it." << std::endl;
myversion->reload();
std::cout << "Executing myversion reloaded symbol." << std::endl;
signature_t reloaded = (signature_t) myversion->getSymbol();
if (reloaded) {
std::cout << "Result r1: " << reloaded(42) << std::endl;
std::cout << "Result r2: " << reloaded(12) << std::endl;
std::cout << "Result r3: " << reloaded(128) << std::endl;
} else {
std::cerr << "Error in folding and reloading v3" << std::endl;
}
myversion->fold();
return 0;
}