Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 17 | #ifndef MCJIT_TEST_BASE_H |
| 18 | #define MCJIT_TEST_BASE_H |
| 19 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 20 | #include "llvm/Config/config.h" |
| 21 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Andrew Kaylor | 927ba6a | 2012-11-27 19:42:02 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 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" |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CodeGen.h" |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 29 | #include "MCJITTestAPICommon.h" |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 30 | |
| 31 | namespace llvm { |
| 32 | |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 33 | class MCJITTestBase : public MCJITTestAPICommon { |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 34 | protected: |
| 35 | |
| 36 | MCJITTestBase() |
| 37 | : OptLevel(CodeGenOpt::None) |
| 38 | , RelocModel(Reloc::Default) |
| 39 | , CodeModel(CodeModel::Default) |
| 40 | , MArch("") |
| 41 | , Builder(Context) |
| 42 | , MM(new SectionMemoryManager) |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 43 | { |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 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 |
| 46 | // kept in sync. |
Tim Northover | 233a4d7 | 2013-05-19 19:44:56 +0000 | [diff] [blame^] | 47 | SupportedArchs.push_back(Triple::aarch64); |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 48 | SupportedArchs.push_back(Triple::arm); |
| 49 | SupportedArchs.push_back(Triple::mips); |
| 50 | SupportedArchs.push_back(Triple::x86); |
| 51 | SupportedArchs.push_back(Triple::x86_64); |
| 52 | |
| 53 | // The operating systems below are known to be incompatible with MCJIT as |
| 54 | // they are copied from the test/ExecutionEngine/MCJIT/lit.local.cfg and |
| 55 | // should be kept in sync. |
| 56 | UnsupportedOSs.push_back(Triple::Cygwin); |
| 57 | UnsupportedOSs.push_back(Triple::Darwin); |
| 58 | } |
| 59 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 60 | Module *createEmptyModule(StringRef Name) { |
| 61 | Module * M = new Module(Name, Context); |
| 62 | M->setTargetTriple(Triple::normalize(HostTriple)); |
| 63 | return M; |
| 64 | } |
| 65 | |
| 66 | template<typename FuncType> |
| 67 | Function *startFunction(Module *M, StringRef Name) { |
| 68 | Function *Result = Function::Create( |
| 69 | TypeBuilder<FuncType, false>::get(Context), |
| 70 | GlobalValue::ExternalLinkage, Name, M); |
| 71 | |
| 72 | BasicBlock *BB = BasicBlock::Create(Context, Name, Result); |
| 73 | Builder.SetInsertPoint(BB); |
| 74 | |
| 75 | return Result; |
| 76 | } |
| 77 | |
| 78 | void endFunctionWithRet(Function *Func, Value *RetValue) { |
| 79 | Builder.CreateRet(RetValue); |
| 80 | } |
| 81 | |
| 82 | // Inserts a simple function that invokes Callee and takes the same arguments: |
| 83 | // int Caller(...) { return Callee(...); } |
| 84 | template<typename Signature> |
| 85 | Function *insertSimpleCallFunction(Module *M, Function *Callee) { |
| 86 | Function *Result = startFunction<Signature>(M, "caller"); |
| 87 | |
| 88 | SmallVector<Value*, 1> CallArgs; |
| 89 | |
| 90 | Function::arg_iterator arg_iter = Result->arg_begin(); |
| 91 | for(;arg_iter != Result->arg_end(); ++arg_iter) |
| 92 | CallArgs.push_back(arg_iter); |
| 93 | |
| 94 | Value *ReturnCode = Builder.CreateCall(Callee, CallArgs); |
| 95 | Builder.CreateRet(ReturnCode); |
| 96 | return Result; |
| 97 | } |
| 98 | |
| 99 | // Inserts a function named 'main' that returns a uint32_t: |
| 100 | // int32_t main() { return X; } |
| 101 | // where X is given by returnCode |
| 102 | Function *insertMainFunction(Module *M, uint32_t returnCode) { |
| 103 | Function *Result = startFunction<int32_t(void)>(M, "main"); |
| 104 | |
| 105 | Value *ReturnVal = ConstantInt::get(Context, APInt(32, returnCode)); |
| 106 | endFunctionWithRet(Result, ReturnVal); |
| 107 | |
| 108 | return Result; |
| 109 | } |
| 110 | |
| 111 | // Inserts a function |
| 112 | // int32_t add(int32_t a, int32_t b) { return a + b; } |
| 113 | // in the current module and returns a pointer to it. |
| 114 | Function *insertAddFunction(Module *M, StringRef Name = "add") { |
| 115 | Function *Result = startFunction<int32_t(int32_t, int32_t)>(M, Name); |
| 116 | |
| 117 | Function::arg_iterator args = Result->arg_begin(); |
| 118 | Value *Arg1 = args; |
| 119 | Value *Arg2 = ++args; |
| 120 | Value *AddResult = Builder.CreateAdd(Arg1, Arg2); |
| 121 | |
| 122 | endFunctionWithRet(Result, AddResult); |
| 123 | |
| 124 | return Result; |
| 125 | } |
| 126 | |
| 127 | // Inserts an declaration to a function defined elsewhere |
| 128 | Function *insertExternalReferenceToFunction(Module *M, StringRef Name, |
| 129 | FunctionType *FuncTy) { |
| 130 | Function *Result = Function::Create(FuncTy, |
| 131 | GlobalValue::ExternalLinkage, |
| 132 | Name, M); |
| 133 | return Result; |
| 134 | } |
| 135 | |
| 136 | // Inserts an declaration to a function defined elsewhere |
| 137 | Function *insertExternalReferenceToFunction(Module *M, Function *Func) { |
| 138 | Function *Result = Function::Create(Func->getFunctionType(), |
| 139 | GlobalValue::AvailableExternallyLinkage, |
| 140 | Func->getName(), M); |
| 141 | return Result; |
| 142 | } |
| 143 | |
| 144 | // Inserts a global variable of type int32 |
| 145 | GlobalVariable *insertGlobalInt32(Module *M, |
| 146 | StringRef name, |
| 147 | int32_t InitialValue) { |
| 148 | Type *GlobalTy = TypeBuilder<types::i<32>, true>::get(Context); |
| 149 | Constant *IV = ConstantInt::get(Context, APInt(32, InitialValue)); |
| 150 | GlobalVariable *Global = new GlobalVariable(*M, |
| 151 | GlobalTy, |
| 152 | false, |
| 153 | GlobalValue::ExternalLinkage, |
| 154 | IV, |
| 155 | name); |
| 156 | return Global; |
| 157 | } |
| 158 | |
| 159 | void createJIT(Module *M) { |
| 160 | |
| 161 | // Due to the EngineBuilder constructor, it is required to have a Module |
| 162 | // in order to construct an ExecutionEngine (i.e. MCJIT) |
| 163 | assert(M != 0 && "a non-null Module must be provided to create MCJIT"); |
| 164 | |
| 165 | EngineBuilder EB(M); |
| 166 | std::string Error; |
| 167 | TheJIT.reset(EB.setEngineKind(EngineKind::JIT) |
| 168 | .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */ |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 169 | .setMCJITMemoryManager(MM) |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 170 | .setErrorStr(&Error) |
| 171 | .setOptLevel(CodeGenOpt::None) |
| 172 | .setAllocateGVsWithCode(false) /*does this do anything?*/ |
| 173 | .setCodeModel(CodeModel::JITDefault) |
| 174 | .setRelocationModel(Reloc::Default) |
| 175 | .setMArch(MArch) |
| 176 | .setMCPU(sys::getHostCPUName()) |
| 177 | //.setMAttrs(MAttrs) |
| 178 | .create()); |
| 179 | // At this point, we cannot modify the module any more. |
| 180 | assert(TheJIT.get() != NULL && "error creating MCJIT with EngineBuilder"); |
| 181 | } |
| 182 | |
| 183 | LLVMContext Context; |
| 184 | CodeGenOpt::Level OptLevel; |
| 185 | Reloc::Model RelocModel; |
| 186 | CodeModel::Model CodeModel; |
| 187 | StringRef MArch; |
| 188 | SmallVector<std::string, 1> MAttrs; |
| 189 | OwningPtr<TargetMachine> TM; |
| 190 | OwningPtr<ExecutionEngine> TheJIT; |
| 191 | IRBuilder<> Builder; |
Filip Pizlo | 13a3cf1 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 192 | RTDyldMemoryManager *MM; |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 193 | |
Andrew Kaylor | 2d6d585 | 2012-10-04 20:29:44 +0000 | [diff] [blame] | 194 | OwningPtr<Module> M; |
| 195 | }; |
| 196 | |
| 197 | } // namespace llvm |
| 198 | |
| 199 | #endif // MCJIT_TEST_H |