blob: eea88bbe3f43104f91f9707dbba673c0ef36d08c [file] [log] [blame]
Andrew Kaylor5e7d7922012-10-04 20:29:44 +00001//===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements common functionality required by the MCJIT unit tests,
11// as well as logic to skip tests on unsupported architectures and operating
12// systems.
13//
14//===----------------------------------------------------------------------===//
15
16
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H
18#define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTBASE_H
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000019
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "MCJITTestAPICommon.h"
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000021#include "llvm/Config/config.h"
22#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylorab5ba512012-11-27 19:42:02 +000023#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/IRBuilder.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/TypeBuilder.h"
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000029#include "llvm/Support/CodeGen.h"
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000030
31namespace llvm {
32
Daniel Malea4146b042013-06-28 20:37:20 +000033/// Helper class that can build very simple Modules
34class TrivialModuleBuilder {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000035protected:
Daniel Malea4146b042013-06-28 20:37:20 +000036 LLVMContext Context;
37 IRBuilder<> Builder;
38 std::string BuilderTriple;
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000039
Daniel Malea4146b042013-06-28 20:37:20 +000040 TrivialModuleBuilder(const std::string &Triple)
41 : Builder(Context), BuilderTriple(Triple) {}
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000042
Daniel Malea4146b042013-06-28 20:37:20 +000043 Module *createEmptyModule(StringRef Name = StringRef()) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000044 Module * M = new Module(Name, Context);
Daniel Malea4146b042013-06-28 20:37:20 +000045 M->setTargetTriple(Triple::normalize(BuilderTriple));
Andrew Kaylor5e7d7922012-10-04 20:29:44 +000046 return M;
47 }
48
49 template<typename FuncType>
50 Function *startFunction(Module *M, StringRef Name) {
51 Function *Result = Function::Create(
52 TypeBuilder<FuncType, false>::get(Context),
53 GlobalValue::ExternalLinkage, Name, M);
54
55 BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
56 Builder.SetInsertPoint(BB);
57
58 return Result;
59 }
60
61 void endFunctionWithRet(Function *Func, Value *RetValue) {
62 Builder.CreateRet(RetValue);
63 }
64
65 // Inserts a simple function that invokes Callee and takes the same arguments:
66 // int Caller(...) { return Callee(...); }
67 template<typename Signature>
68 Function *insertSimpleCallFunction(Module *M, Function *Callee) {
69 Function *Result = startFunction<Signature>(M, "caller");
70
71 SmallVector<Value*, 1> CallArgs;
72
73 Function::arg_iterator arg_iter = Result->arg_begin();
74 for(;arg_iter != Result->arg_end(); ++arg_iter)
75 CallArgs.push_back(arg_iter);
76
77 Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
78 Builder.CreateRet(ReturnCode);
79 return Result;
80 }
81
82 // Inserts a function named 'main' that returns a uint32_t:
83 // int32_t main() { return X; }
84 // where X is given by returnCode
85 Function *insertMainFunction(Module *M, uint32_t returnCode) {
86 Function *Result = startFunction<int32_t(void)>(M, "main");
87
88 Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
89 endFunctionWithRet(Result, ReturnVal);
90
91 return Result;
92 }
93
94 // Inserts a function
95 // int32_t add(int32_t a, int32_t b) { return a + b; }
96 // in the current module and returns a pointer to it.
97 Function *insertAddFunction(Module *M, StringRef Name = "add") {
98 Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name);
99
100 Function::arg_iterator args = Result->arg_begin();
101 Value *Arg1 = args;
102 Value *Arg2 = ++args;
103 Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
104
105 endFunctionWithRet(Result, AddResult);
106
107 return Result;
108 }
109
Lang Hamesefe7e222014-10-22 23:18:42 +0000110 // Inserts a declaration to a function defined elsewhere
111 template <typename FuncType>
112 Function *insertExternalReferenceToFunction(Module *M, StringRef Name) {
113 Function *Result = Function::Create(
114 TypeBuilder<FuncType, false>::get(Context),
115 GlobalValue::ExternalLinkage, Name, M);
116 return Result;
117 }
118
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000119 // Inserts an declaration to a function defined elsewhere
120 Function *insertExternalReferenceToFunction(Module *M, StringRef Name,
121 FunctionType *FuncTy) {
122 Function *Result = Function::Create(FuncTy,
123 GlobalValue::ExternalLinkage,
124 Name, M);
125 return Result;
126 }
127
128 // Inserts an declaration to a function defined elsewhere
129 Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
130 Function *Result = Function::Create(Func->getFunctionType(),
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000131 GlobalValue::ExternalLinkage,
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000132 Func->getName(), M);
133 return Result;
134 }
135
136 // Inserts a global variable of type int32
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000137 // FIXME: make this a template function to support any type
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000138 GlobalVariable *insertGlobalInt32(Module *M,
139 StringRef name,
140 int32_t InitialValue) {
141 Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context);
142 Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
143 GlobalVariable *Global = new GlobalVariable(*M,
144 GlobalTy,
145 false,
146 GlobalValue::ExternalLinkage,
147 IV,
148 name);
149 return Global;
150 }
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000151
152 // Inserts a function
153 // int32_t recursive_add(int32_t num) {
154 // if (num == 0) {
155 // return num;
156 // } else {
157 // int32_t recursive_param = num - 1;
158 // return num + Helper(recursive_param);
159 // }
160 // }
161 // NOTE: if Helper is left as the default parameter, Helper == recursive_add.
162 Function *insertAccumulateFunction(Module *M,
163 Function *Helper = 0,
164 StringRef Name = "accumulate") {
165 Function *Result = startFunction<int32_t(int32_t)>(M, Name);
166 if (Helper == 0)
167 Helper = Result;
168
169 BasicBlock *BaseCase = BasicBlock::Create(Context, "", Result);
170 BasicBlock *RecursiveCase = BasicBlock::Create(Context, "", Result);
171
172 // if (num == 0)
173 Value *Param = Result->arg_begin();
174 Value *Zero = ConstantInt::get(Context, APInt(32, 0));
175 Builder.CreateCondBr(Builder.CreateICmpEQ(Param, Zero),
176 BaseCase, RecursiveCase);
177
178 // return num;
179 Builder.SetInsertPoint(BaseCase);
180 Builder.CreateRet(Param);
181
182 // int32_t recursive_param = num - 1;
183 // return Helper(recursive_param);
184 Builder.SetInsertPoint(RecursiveCase);
185 Value *One = ConstantInt::get(Context, APInt(32, 1));
186 Value *RecursiveParam = Builder.CreateSub(Param, One);
187 Value *RecursiveReturn = Builder.CreateCall(Helper, RecursiveParam);
188 Value *Accumulator = Builder.CreateAdd(Param, RecursiveReturn);
189 Builder.CreateRet(Accumulator);
190
191 return Result;
192 }
193
194 // Populates Modules A and B:
195 // Module A { Extern FB1, Function FA which calls FB1 },
196 // Module B { Extern FA, Function FB1, Function FB2 which calls FA },
Ahmed Charles56440fd2014-03-06 05:51:42 +0000197 void createCrossModuleRecursiveCase(std::unique_ptr<Module> &A, Function *&FA,
198 std::unique_ptr<Module> &B,
199 Function *&FB1, Function *&FB2) {
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000200 // Define FB1 in B.
201 B.reset(createEmptyModule("B"));
202 FB1 = insertAccumulateFunction(B.get(), 0, "FB1");
203
204 // Declare FB1 in A (as an external).
205 A.reset(createEmptyModule("A"));
206 Function *FB1Extern = insertExternalReferenceToFunction(A.get(), FB1);
207
208 // Define FA in A (with a call to FB1).
209 FA = insertAccumulateFunction(A.get(), FB1Extern, "FA");
210
211 // Declare FA in B (as an external)
212 Function *FAExtern = insertExternalReferenceToFunction(B.get(), FA);
213
214 // Define FB2 in B (with a call to FA)
215 FB2 = insertAccumulateFunction(B.get(), FAExtern, "FB2");
216 }
217
218 // Module A { Function FA },
219 // Module B { Extern FA, Function FB which calls FA },
220 // Module C { Extern FB, Function FC which calls FB },
Ahmed Charles56440fd2014-03-06 05:51:42 +0000221 void
222 createThreeModuleChainedCallsCase(std::unique_ptr<Module> &A, Function *&FA,
223 std::unique_ptr<Module> &B, Function *&FB,
224 std::unique_ptr<Module> &C, Function *&FC) {
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000225 A.reset(createEmptyModule("A"));
226 FA = insertAddFunction(A.get());
227
228 B.reset(createEmptyModule("B"));
229 Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
230 FB = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(B.get(), FAExtern_in_B);
231
232 C.reset(createEmptyModule("C"));
233 Function *FBExtern_in_C = insertExternalReferenceToFunction(C.get(), FB);
234 FC = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(C.get(), FBExtern_in_C);
235 }
236
237
238 // Module A { Function FA },
239 // Populates Modules A and B:
240 // Module B { Function FB }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000241 void createTwoModuleCase(std::unique_ptr<Module> &A, Function *&FA,
242 std::unique_ptr<Module> &B, Function *&FB) {
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000243 A.reset(createEmptyModule("A"));
244 FA = insertAddFunction(A.get());
245
246 B.reset(createEmptyModule("B"));
247 FB = insertAddFunction(B.get());
248 }
249
250 // Module A { Function FA },
251 // Module B { Extern FA, Function FB which calls FA }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000252 void createTwoModuleExternCase(std::unique_ptr<Module> &A, Function *&FA,
253 std::unique_ptr<Module> &B, Function *&FB) {
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000254 A.reset(createEmptyModule("A"));
255 FA = insertAddFunction(A.get());
256
257 B.reset(createEmptyModule("B"));
258 Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
259 FB = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(B.get(),
260 FAExtern_in_B);
261 }
262
263 // Module A { Function FA },
264 // Module B { Extern FA, Function FB which calls FA },
265 // Module C { Extern FB, Function FC which calls FA },
Ahmed Charles56440fd2014-03-06 05:51:42 +0000266 void createThreeModuleCase(std::unique_ptr<Module> &A, Function *&FA,
267 std::unique_ptr<Module> &B, Function *&FB,
268 std::unique_ptr<Module> &C, Function *&FC) {
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000269 A.reset(createEmptyModule("A"));
270 FA = insertAddFunction(A.get());
271
272 B.reset(createEmptyModule("B"));
273 Function *FAExtern_in_B = insertExternalReferenceToFunction(B.get(), FA);
274 FB = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(B.get(), FAExtern_in_B);
275
276 C.reset(createEmptyModule("C"));
277 Function *FAExtern_in_C = insertExternalReferenceToFunction(C.get(), FA);
278 FC = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(C.get(), FAExtern_in_C);
279 }
Daniel Malea4146b042013-06-28 20:37:20 +0000280};
281
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000282
Daniel Malea4146b042013-06-28 20:37:20 +0000283class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder {
284protected:
Andrew Kaylor6bbb2c92013-10-01 01:48:36 +0000285
Daniel Malea4146b042013-06-28 20:37:20 +0000286 MCJITTestBase()
287 : TrivialModuleBuilder(HostTriple)
288 , OptLevel(CodeGenOpt::None)
289 , RelocModel(Reloc::Default)
290 , CodeModel(CodeModel::Default)
291 , MArch("")
292 , MM(new SectionMemoryManager)
293 {
294 // The architectures below are known to be compatible with MCJIT as they
295 // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
296 // kept in sync.
297 SupportedArchs.push_back(Triple::aarch64);
298 SupportedArchs.push_back(Triple::arm);
Akira Hatanaka2e236242013-07-24 01:58:40 +0000299 SupportedArchs.push_back(Triple::mips);
300 SupportedArchs.push_back(Triple::mipsel);
Daniel Malea4146b042013-06-28 20:37:20 +0000301 SupportedArchs.push_back(Triple::x86);
302 SupportedArchs.push_back(Triple::x86_64);
303
304 // Some architectures have sub-architectures in which tests will fail, like
305 // ARM. These two vectors will define if they do have sub-archs (to avoid
306 // extra work for those who don't), and if so, if they are listed to work
307 HasSubArchs.push_back(Triple::arm);
308 SupportedSubArchs.push_back("armv6");
309 SupportedSubArchs.push_back("armv7");
310
311 // The operating systems below are known to be incompatible with MCJIT as
312 // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and
313 // should be kept in sync.
Daniel Malea4146b042013-06-28 20:37:20 +0000314 UnsupportedOSs.push_back(Triple::Darwin);
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +0000315
316 UnsupportedEnvironments.push_back(Triple::Cygnus);
Daniel Malea4146b042013-06-28 20:37:20 +0000317 }
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000318
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000319 void createJIT(std::unique_ptr<Module> M) {
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000320
321 // Due to the EngineBuilder constructor, it is required to have a Module
322 // in order to construct an ExecutionEngine (i.e. MCJIT)
323 assert(M != 0 && "a non-null Module must be provided to create MCJIT");
324
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000325 EngineBuilder EB(std::move(M));
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000326 std::string Error;
327 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000328 .setMCJITMemoryManager(MM)
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000329 .setErrorStr(&Error)
330 .setOptLevel(CodeGenOpt::None)
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000331 .setCodeModel(CodeModel::JITDefault)
332 .setRelocationModel(Reloc::Default)
333 .setMArch(MArch)
334 .setMCPU(sys::getHostCPUName())
335 //.setMAttrs(MAttrs)
336 .create());
337 // At this point, we cannot modify the module any more.
338 assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
339 }
340
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000341 CodeGenOpt::Level OptLevel;
342 Reloc::Model RelocModel;
343 CodeModel::Model CodeModel;
344 StringRef MArch;
345 SmallVector<std::string, 1> MAttrs;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000346 std::unique_ptr<ExecutionEngine> TheJIT;
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000347 RTDyldMemoryManager *MM;
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000348
Ahmed Charles56440fd2014-03-06 05:51:42 +0000349 std::unique_ptr<Module> M;
Andrew Kaylor5e7d7922012-10-04 20:29:44 +0000350};
351
352} // namespace llvm
353
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000354#endif