1 //===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
17 #ifndef MCJIT_TEST_BASE_H
18 #define MCJIT_TEST_BASE_H
20 #include "llvm/Config/config.h"
21 #include "llvm/ExecutionEngine/ExecutionEngine.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/TypeBuilder.h"
28 #include "llvm/Support/CodeGen.h"
29 #include "MCJITTestAPICommon.h"
33 class MCJITTestBase : public MCJITTestAPICommon {
37 : OptLevel(CodeGenOpt::None)
38 , RelocModel(Reloc::Default)
39 , CodeModel(CodeModel::Default)
42 , MM(new SectionMemoryManager)
44 // The architectures below are known to be compatible with MCJIT as they
45 // are copied from test/ExecutionEngine/MCJIT/lit.local.cfg and should be
47 SupportedArchs.push_back(Triple::aarch64);
48 SupportedArchs.push_back(Triple::arm);
49 SupportedArchs.push_back(Triple::x86);
50 SupportedArchs.push_back(Triple::x86_64);
52 // Some architectures have sub-architectures in which tests will fail, like
53 // ARM. These two vectors will define if they do have sub-archs (to avoid
54 // extra work for those who don't), and if so, if they are listed to work
55 HasSubArchs.push_back(Triple::arm);
56 SupportedSubArchs.push_back("armv6");
57 SupportedSubArchs.push_back("armv7");
59 // The operating systems below are known to be incompatible with MCJIT as
60 // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and
61 // should be kept in sync.
62 UnsupportedOSs.push_back(Triple::Cygwin);
63 UnsupportedOSs.push_back(Triple::Darwin);
66 Module *createEmptyModule(StringRef Name) {
67 Module * M = new Module(Name, Context);
68 M->setTargetTriple(Triple::normalize(HostTriple));
72 template<typename FuncType>
73 Function *startFunction(Module *M, StringRef Name) {
74 Function *Result = Function::Create(
75 TypeBuilder<FuncType, false>::get(Context),
76 GlobalValue::ExternalLinkage, Name, M);
78 BasicBlock *BB = BasicBlock::Create(Context, Name, Result);
79 Builder.SetInsertPoint(BB);
84 void endFunctionWithRet(Function *Func, Value *RetValue) {
85 Builder.CreateRet(RetValue);
88 // Inserts a simple function that invokes Callee and takes the same arguments:
89 // int Caller(...) { return Callee(...); }
90 template<typename Signature>
91 Function *insertSimpleCallFunction(Module *M, Function *Callee) {
92 Function *Result = startFunction<Signature>(M, "caller");
94 SmallVector<Value*, 1> CallArgs;
96 Function::arg_iterator arg_iter = Result->arg_begin();
97 for(;arg_iter != Result->arg_end(); ++arg_iter)
98 CallArgs.push_back(arg_iter);
100 Value *ReturnCode = Builder.CreateCall(Callee, CallArgs);
101 Builder.CreateRet(ReturnCode);
105 // Inserts a function named 'main' that returns a uint32_t:
106 // int32_t main() { return X; }
107 // where X is given by returnCode
108 Function *insertMainFunction(Module *M, uint32_t returnCode) {
109 Function *Result = startFunction<int32_t(void)>(M, "main");
111 Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode));
112 endFunctionWithRet(Result, ReturnVal);
117 // Inserts a function
118 // int32_t add(int32_t a, int32_t b) { return a + b; }
119 // in the current module and returns a pointer to it.
120 Function *insertAddFunction(Module *M, StringRef Name = "add") {
121 Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name);
123 Function::arg_iterator args = Result->arg_begin();
125 Value *Arg2 = ++args;
126 Value *AddResult = Builder.CreateAdd(Arg1, Arg2);
128 endFunctionWithRet(Result, AddResult);
133 // Inserts an declaration to a function defined elsewhere
134 Function *insertExternalReferenceToFunction(Module *M, StringRef Name,
135 FunctionType *FuncTy) {
136 Function *Result = Function::Create(FuncTy,
137 GlobalValue::ExternalLinkage,
142 // Inserts an declaration to a function defined elsewhere
143 Function *insertExternalReferenceToFunction(Module *M, Function *Func) {
144 Function *Result = Function::Create(Func->getFunctionType(),
145 GlobalValue::AvailableExternallyLinkage,
150 // Inserts a global variable of type int32
151 GlobalVariable *insertGlobalInt32(Module *M,
153 int32_t InitialValue) {
154 Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context);
155 Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue));
156 GlobalVariable *Global = new GlobalVariable(*M,
159 GlobalValue::ExternalLinkage,
165 void createJIT(Module *M) {
167 // Due to the EngineBuilder constructor, it is required to have a Module
168 // in order to construct an ExecutionEngine (i.e. MCJIT)
169 assert(M != 0 && "a non-null Module must be provided to create MCJIT");
173 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
174 .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */
175 .setMCJITMemoryManager(MM)
177 .setOptLevel(CodeGenOpt::None)
178 .setAllocateGVsWithCode(false) /*does this do anything?*/
179 .setCodeModel(CodeModel::JITDefault)
180 .setRelocationModel(Reloc::Default)
182 .setMCPU(sys::getHostCPUName())
185 // At this point, we cannot modify the module any more.
186 assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder");
190 CodeGenOpt::Level OptLevel;
191 Reloc::Model RelocModel;
192 CodeModel::Model CodeModel;
194 SmallVector<std::string, 1> MAttrs;
195 OwningPtr<ExecutionEngine> TheJIT;
197 RTDyldMemoryManager *MM;
204 #endif // MCJIT_TEST_H