Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 1 | //===- JITTest.cpp - Unit tests for the JIT -------------------------------===// |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 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 | #include "gtest/gtest.h" |
| 11 | #include "llvm/ADT/OwningPtr.h" |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallPtrSet.h" |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 13 | #include "llvm/Assembly/Parser.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 14 | #include "llvm/BasicBlock.h" |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/ReaderWriter.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 16 | #include "llvm/Constant.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/ExecutionEngine/JIT.h" |
| 20 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/GlobalValue.h" |
| 23 | #include "llvm/GlobalVariable.h" |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 24 | #include "llvm/LLVMContext.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 26 | #include "llvm/Support/IRBuilder.h" |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SourceMgr.h" |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TypeBuilder.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetSelect.h" |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 31 | #include "llvm/Type.h" |
| 32 | |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) { |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 40 | std::vector<Type*> params; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 41 | FunctionType *FTy = FunctionType::get(G->getType()->getElementType(), |
Dan Gohman | c6f40b6 | 2009-07-11 13:56:14 +0000 | [diff] [blame] | 42 | params, false); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 43 | Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 44 | BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 45 | IRBuilder<> builder(Entry); |
| 46 | Value *Load = builder.CreateLoad(G); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 47 | Type *GTy = G->getType()->getElementType(); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 48 | Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 49 | builder.CreateStore(Add, G); |
| 50 | builder.CreateRet(Add); |
| 51 | return F; |
| 52 | } |
| 53 | |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 54 | std::string DumpFunction(const Function *F) { |
| 55 | std::string Result; |
| 56 | raw_string_ostream(Result) << "" << *F; |
| 57 | return Result; |
| 58 | } |
| 59 | |
| 60 | class RecordingJITMemoryManager : public JITMemoryManager { |
| 61 | const OwningPtr<JITMemoryManager> Base; |
| 62 | public: |
| 63 | RecordingJITMemoryManager() |
| 64 | : Base(JITMemoryManager::CreateDefaultMemManager()) { |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 65 | stubsAllocated = 0; |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | virtual void setMemoryWritable() { Base->setMemoryWritable(); } |
| 69 | virtual void setMemoryExecutable() { Base->setMemoryExecutable(); } |
| 70 | virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); } |
| 71 | virtual void AllocateGOT() { Base->AllocateGOT(); } |
| 72 | virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); } |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 73 | struct StartFunctionBodyCall { |
| 74 | StartFunctionBodyCall(uint8_t *Result, const Function *F, |
| 75 | uintptr_t ActualSize, uintptr_t ActualSizeResult) |
| 76 | : Result(Result), F(F), F_dump(DumpFunction(F)), |
| 77 | ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {} |
| 78 | uint8_t *Result; |
| 79 | const Function *F; |
| 80 | std::string F_dump; |
| 81 | uintptr_t ActualSize; |
| 82 | uintptr_t ActualSizeResult; |
| 83 | }; |
| 84 | std::vector<StartFunctionBodyCall> startFunctionBodyCalls; |
| 85 | virtual uint8_t *startFunctionBody(const Function *F, |
| 86 | uintptr_t &ActualSize) { |
| 87 | uintptr_t InitialActualSize = ActualSize; |
| 88 | uint8_t *Result = Base->startFunctionBody(F, ActualSize); |
| 89 | startFunctionBodyCalls.push_back( |
| 90 | StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize)); |
| 91 | return Result; |
| 92 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 93 | int stubsAllocated; |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 94 | virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, |
| 95 | unsigned Alignment) { |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 96 | stubsAllocated++; |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 97 | return Base->allocateStub(F, StubSize, Alignment); |
| 98 | } |
| 99 | struct EndFunctionBodyCall { |
| 100 | EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart, |
| 101 | uint8_t *FunctionEnd) |
| 102 | : F(F), F_dump(DumpFunction(F)), |
| 103 | FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {} |
| 104 | const Function *F; |
| 105 | std::string F_dump; |
| 106 | uint8_t *FunctionStart; |
| 107 | uint8_t *FunctionEnd; |
| 108 | }; |
| 109 | std::vector<EndFunctionBodyCall> endFunctionBodyCalls; |
| 110 | virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart, |
| 111 | uint8_t *FunctionEnd) { |
| 112 | endFunctionBodyCalls.push_back( |
| 113 | EndFunctionBodyCall(F, FunctionStart, FunctionEnd)); |
| 114 | Base->endFunctionBody(F, FunctionStart, FunctionEnd); |
| 115 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 116 | virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 117 | unsigned SectionID) { |
| 118 | return Base->allocateDataSection(Size, Alignment, SectionID); |
| 119 | } |
| 120 | virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 121 | unsigned SectionID) { |
| 122 | return Base->allocateCodeSection(Size, Alignment, SectionID); |
| 123 | } |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 124 | virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { |
| 125 | return Base->allocateSpace(Size, Alignment); |
| 126 | } |
| 127 | virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
| 128 | return Base->allocateGlobal(Size, Alignment); |
| 129 | } |
| 130 | struct DeallocateFunctionBodyCall { |
| 131 | DeallocateFunctionBodyCall(const void *Body) : Body(Body) {} |
| 132 | const void *Body; |
| 133 | }; |
| 134 | std::vector<DeallocateFunctionBodyCall> deallocateFunctionBodyCalls; |
| 135 | virtual void deallocateFunctionBody(void *Body) { |
| 136 | deallocateFunctionBodyCalls.push_back(DeallocateFunctionBodyCall(Body)); |
| 137 | Base->deallocateFunctionBody(Body); |
| 138 | } |
| 139 | struct DeallocateExceptionTableCall { |
| 140 | DeallocateExceptionTableCall(const void *ET) : ET(ET) {} |
| 141 | const void *ET; |
| 142 | }; |
| 143 | std::vector<DeallocateExceptionTableCall> deallocateExceptionTableCalls; |
| 144 | virtual void deallocateExceptionTable(void *ET) { |
| 145 | deallocateExceptionTableCalls.push_back(DeallocateExceptionTableCall(ET)); |
| 146 | Base->deallocateExceptionTable(ET); |
| 147 | } |
| 148 | struct StartExceptionTableCall { |
| 149 | StartExceptionTableCall(uint8_t *Result, const Function *F, |
| 150 | uintptr_t ActualSize, uintptr_t ActualSizeResult) |
| 151 | : Result(Result), F(F), F_dump(DumpFunction(F)), |
| 152 | ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {} |
| 153 | uint8_t *Result; |
| 154 | const Function *F; |
| 155 | std::string F_dump; |
| 156 | uintptr_t ActualSize; |
| 157 | uintptr_t ActualSizeResult; |
| 158 | }; |
| 159 | std::vector<StartExceptionTableCall> startExceptionTableCalls; |
| 160 | virtual uint8_t* startExceptionTable(const Function* F, |
| 161 | uintptr_t &ActualSize) { |
| 162 | uintptr_t InitialActualSize = ActualSize; |
| 163 | uint8_t *Result = Base->startExceptionTable(F, ActualSize); |
| 164 | startExceptionTableCalls.push_back( |
| 165 | StartExceptionTableCall(Result, F, InitialActualSize, ActualSize)); |
| 166 | return Result; |
| 167 | } |
| 168 | struct EndExceptionTableCall { |
| 169 | EndExceptionTableCall(const Function *F, uint8_t *TableStart, |
| 170 | uint8_t *TableEnd, uint8_t* FrameRegister) |
| 171 | : F(F), F_dump(DumpFunction(F)), |
| 172 | TableStart(TableStart), TableEnd(TableEnd), |
| 173 | FrameRegister(FrameRegister) {} |
| 174 | const Function *F; |
| 175 | std::string F_dump; |
| 176 | uint8_t *TableStart; |
| 177 | uint8_t *TableEnd; |
| 178 | uint8_t *FrameRegister; |
| 179 | }; |
| 180 | std::vector<EndExceptionTableCall> endExceptionTableCalls; |
| 181 | virtual void endExceptionTable(const Function *F, uint8_t *TableStart, |
| 182 | uint8_t *TableEnd, uint8_t* FrameRegister) { |
| 183 | endExceptionTableCalls.push_back( |
| 184 | EndExceptionTableCall(F, TableStart, TableEnd, FrameRegister)); |
| 185 | return Base->endExceptionTable(F, TableStart, TableEnd, FrameRegister); |
| 186 | } |
| 187 | }; |
| 188 | |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 189 | bool LoadAssemblyInto(Module *M, const char *assembly) { |
| 190 | SMDiagnostic Error; |
| 191 | bool success = |
| 192 | NULL != ParseAssemblyString(assembly, M, Error, M->getContext()); |
| 193 | std::string errMsg; |
| 194 | raw_string_ostream os(errMsg); |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 195 | Error.print("", os); |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 196 | EXPECT_TRUE(success) << os.str(); |
| 197 | return success; |
| 198 | } |
| 199 | |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 200 | class JITTest : public testing::Test { |
| 201 | protected: |
| 202 | virtual void SetUp() { |
| 203 | M = new Module("<main>", Context); |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 204 | RJMM = new RecordingJITMemoryManager; |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 205 | RJMM->setPoisonMemory(true); |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 206 | std::string Error; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 207 | TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT) |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 208 | .setJITMemoryManager(RJMM) |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 209 | .setErrorStr(&Error).create()); |
| 210 | ASSERT_TRUE(TheJIT.get() != NULL) << Error; |
| 211 | } |
| 212 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 213 | void LoadAssembly(const char *assembly) { |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 214 | LoadAssemblyInto(M, assembly); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 217 | LLVMContext Context; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 218 | Module *M; // Owned by ExecutionEngine. |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 219 | RecordingJITMemoryManager *RJMM; |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 220 | OwningPtr<ExecutionEngine> TheJIT; |
| 221 | }; |
| 222 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 223 | // Regression test for a bug. The JIT used to allocate globals inside the same |
| 224 | // memory block used for the function, and when the function code was freed, |
| 225 | // the global was left in the same place. This test allocates a function |
| 226 | // that uses and global, deallocates it, and then makes sure that the global |
| 227 | // stays alive after that. |
| 228 | TEST(JIT, GlobalInFunction) { |
| 229 | LLVMContext context; |
| 230 | Module *M = new Module("<main>", context); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 231 | |
| 232 | JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager(); |
| 233 | // Tell the memory manager to poison freed memory so that accessing freed |
| 234 | // memory is more easily tested. |
| 235 | MemMgr->setPoisonMemory(true); |
| 236 | std::string Error; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 237 | OwningPtr<ExecutionEngine> JIT(EngineBuilder(M) |
Daniel Dunbar | d370d77 | 2009-07-18 06:08:49 +0000 | [diff] [blame] | 238 | .setEngineKind(EngineKind::JIT) |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 239 | .setErrorStr(&Error) |
| 240 | .setJITMemoryManager(MemMgr) |
| 241 | // The next line enables the fix: |
| 242 | .setAllocateGVsWithCode(false) |
| 243 | .create()); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 244 | ASSERT_EQ(Error, ""); |
| 245 | |
| 246 | // Create a global variable. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 247 | Type *GTy = Type::getInt32Ty(context); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 248 | GlobalVariable *G = new GlobalVariable( |
| 249 | *M, |
| 250 | GTy, |
| 251 | false, // Not constant. |
| 252 | GlobalValue::InternalLinkage, |
Benjamin Kramer | feba756 | 2009-07-31 20:56:31 +0000 | [diff] [blame] | 253 | Constant::getNullValue(GTy), |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 254 | "myglobal"); |
| 255 | |
| 256 | // Make a function that points to a global. |
| 257 | Function *F1 = makeReturnGlobal("F1", G, M); |
| 258 | |
| 259 | // Get the pointer to the native code to force it to JIT the function and |
| 260 | // allocate space for the global. |
Jeffrey Yasskin | 0f2ba78 | 2009-10-06 19:06:16 +0000 | [diff] [blame] | 261 | void (*F1Ptr)() = |
| 262 | reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 263 | |
| 264 | // Since F1 was codegen'd, a pointer to G should be available. |
| 265 | int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G); |
| 266 | ASSERT_NE((int32_t*)NULL, GPtr); |
| 267 | EXPECT_EQ(0, *GPtr); |
| 268 | |
| 269 | // F1() should increment G. |
| 270 | F1Ptr(); |
| 271 | EXPECT_EQ(1, *GPtr); |
| 272 | |
| 273 | // Make a second function identical to the first, referring to the same |
| 274 | // global. |
| 275 | Function *F2 = makeReturnGlobal("F2", G, M); |
Jeffrey Yasskin | 0f2ba78 | 2009-10-06 19:06:16 +0000 | [diff] [blame] | 276 | void (*F2Ptr)() = |
| 277 | reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2)); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 278 | |
| 279 | // F2() should increment G. |
| 280 | F2Ptr(); |
| 281 | EXPECT_EQ(2, *GPtr); |
| 282 | |
| 283 | // Deallocate F1. |
| 284 | JIT->freeMachineCodeForFunction(F1); |
| 285 | |
| 286 | // F2() should *still* increment G. |
| 287 | F2Ptr(); |
| 288 | EXPECT_EQ(3, *GPtr); |
| 289 | } |
| 290 | |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 291 | int PlusOne(int arg) { |
| 292 | return arg + 1; |
| 293 | } |
| 294 | |
Andrew Trick | 0005cc7 | 2011-08-26 23:39:30 +0000 | [diff] [blame] | 295 | // ARM tests disabled pending fix for PR10783. |
| 296 | #if !defined(__arm__) |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 297 | TEST_F(JITTest, FarCallToKnownFunction) { |
| 298 | // x86-64 can only make direct calls to functions within 32 bits of |
| 299 | // the current PC. To call anything farther away, we have to load |
| 300 | // the address into a register and call through the register. The |
| 301 | // current JIT does this by allocating a stub for any far call. |
| 302 | // There was a bug in which the JIT tried to emit a direct call when |
| 303 | // the target was already in the JIT's global mappings and lazy |
| 304 | // compilation was disabled. |
| 305 | |
| 306 | Function *KnownFunction = Function::Create( |
| 307 | TypeBuilder<int(int), false>::get(Context), |
| 308 | GlobalValue::ExternalLinkage, "known", M); |
| 309 | TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne); |
| 310 | |
| 311 | // int test() { return known(7); } |
| 312 | Function *TestFunction = Function::Create( |
| 313 | TypeBuilder<int(), false>::get(Context), |
| 314 | GlobalValue::ExternalLinkage, "test", M); |
| 315 | BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction); |
| 316 | IRBuilder<> Builder(Entry); |
| 317 | Value *result = Builder.CreateCall( |
| 318 | KnownFunction, |
| 319 | ConstantInt::get(TypeBuilder<int, false>::get(Context), 7)); |
| 320 | Builder.CreateRet(result); |
| 321 | |
Jeffrey Yasskin | 18fec73 | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 322 | TheJIT->DisableLazyCompilation(true); |
Jeffrey Yasskin | ea5ed00 | 2009-10-06 00:35:55 +0000 | [diff] [blame] | 323 | int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>( |
| 324 | (intptr_t)TheJIT->getPointerToFunction(TestFunction)); |
| 325 | // This used to crash in trying to call PlusOne(). |
| 326 | EXPECT_EQ(8, TestFunctionPtr()); |
| 327 | } |
| 328 | |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 329 | // Test a function C which calls A and B which call each other. |
| 330 | TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) { |
Jeffrey Yasskin | 18fec73 | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 331 | TheJIT->DisableLazyCompilation(true); |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 332 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 333 | FunctionType *Func1Ty = |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 334 | cast<FunctionType>(TypeBuilder<void(void), false>::get(Context)); |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 335 | std::vector<Type*> arg_types; |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 336 | arg_types.push_back(Type::getInt1Ty(Context)); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 337 | FunctionType *FuncTy = FunctionType::get( |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 338 | Type::getVoidTy(Context), arg_types, false); |
| 339 | Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage, |
| 340 | "func1", M); |
| 341 | Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage, |
| 342 | "func2", M); |
| 343 | Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage, |
| 344 | "func3", M); |
| 345 | BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1); |
| 346 | BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2); |
| 347 | BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2); |
| 348 | BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2); |
| 349 | BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3); |
| 350 | BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3); |
| 351 | BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3); |
| 352 | |
| 353 | // Make Func1 call Func2(0) and Func3(0). |
| 354 | IRBuilder<> Builder(Block1); |
| 355 | Builder.CreateCall(Func2, ConstantInt::getTrue(Context)); |
| 356 | Builder.CreateCall(Func3, ConstantInt::getTrue(Context)); |
| 357 | Builder.CreateRetVoid(); |
| 358 | |
| 359 | // void Func2(bool b) { if (b) { Func3(false); return; } return; } |
| 360 | Builder.SetInsertPoint(Block2); |
| 361 | Builder.CreateCondBr(Func2->arg_begin(), True2, False2); |
| 362 | Builder.SetInsertPoint(True2); |
| 363 | Builder.CreateCall(Func3, ConstantInt::getFalse(Context)); |
| 364 | Builder.CreateRetVoid(); |
| 365 | Builder.SetInsertPoint(False2); |
| 366 | Builder.CreateRetVoid(); |
| 367 | |
| 368 | // void Func3(bool b) { if (b) { Func2(false); return; } return; } |
| 369 | Builder.SetInsertPoint(Block3); |
| 370 | Builder.CreateCondBr(Func3->arg_begin(), True3, False3); |
| 371 | Builder.SetInsertPoint(True3); |
| 372 | Builder.CreateCall(Func2, ConstantInt::getFalse(Context)); |
| 373 | Builder.CreateRetVoid(); |
| 374 | Builder.SetInsertPoint(False3); |
| 375 | Builder.CreateRetVoid(); |
| 376 | |
| 377 | // Compile the function to native code |
| 378 | void (*F1Ptr)() = |
| 379 | reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1)); |
| 380 | |
| 381 | F1Ptr(); |
| 382 | } |
| 383 | |
| 384 | // Regression test for PR5162. This used to trigger an AssertingVH inside the |
| 385 | // JIT's Function to stub mapping. |
| 386 | TEST_F(JITTest, NonLazyLeaksNoStubs) { |
Jeffrey Yasskin | 18fec73 | 2009-10-27 22:39:42 +0000 | [diff] [blame] | 387 | TheJIT->DisableLazyCompilation(true); |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 388 | |
| 389 | // Create two functions with a single basic block each. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 390 | FunctionType *FuncTy = |
Jeffrey Yasskin | e5f8798 | 2009-10-13 21:32:57 +0000 | [diff] [blame] | 391 | cast<FunctionType>(TypeBuilder<int(), false>::get(Context)); |
| 392 | Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage, |
| 393 | "func1", M); |
| 394 | Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage, |
| 395 | "func2", M); |
| 396 | BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1); |
| 397 | BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2); |
| 398 | |
| 399 | // The first function calls the second and returns the result |
| 400 | IRBuilder<> Builder(Block1); |
| 401 | Value *Result = Builder.CreateCall(Func2); |
| 402 | Builder.CreateRet(Result); |
| 403 | |
| 404 | // The second function just returns a constant |
| 405 | Builder.SetInsertPoint(Block2); |
| 406 | Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42)); |
| 407 | |
| 408 | // Compile the function to native code |
| 409 | (void)TheJIT->getPointerToFunction(Func1); |
| 410 | |
| 411 | // Free the JIT state for the functions |
| 412 | TheJIT->freeMachineCodeForFunction(Func1); |
| 413 | TheJIT->freeMachineCodeForFunction(Func2); |
| 414 | |
| 415 | // Delete the first function (and show that is has no users) |
| 416 | EXPECT_EQ(Func1->getNumUses(), 0u); |
| 417 | Func1->eraseFromParent(); |
| 418 | |
| 419 | // Delete the second function (and show that it has no users - it had one, |
| 420 | // func1 but that's gone now) |
| 421 | EXPECT_EQ(Func2->getNumUses(), 0u); |
| 422 | Func2->eraseFromParent(); |
| 423 | } |
| 424 | |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 425 | TEST_F(JITTest, ModuleDeletion) { |
Jeffrey Yasskin | b235224 | 2009-10-28 00:28:31 +0000 | [diff] [blame] | 426 | TheJIT->DisableLazyCompilation(false); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 427 | LoadAssembly("define void @main() { " |
| 428 | " call i32 @computeVal() " |
| 429 | " ret void " |
| 430 | "} " |
| 431 | " " |
| 432 | "define internal i32 @computeVal() { " |
| 433 | " ret i32 0 " |
| 434 | "} "); |
| 435 | Function *func = M->getFunction("main"); |
| 436 | TheJIT->getPointerToFunction(func); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 437 | TheJIT->removeModule(M); |
| 438 | delete M; |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 439 | |
| 440 | SmallPtrSet<const void*, 2> FunctionsDeallocated; |
| 441 | for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size(); |
| 442 | i != e; ++i) { |
| 443 | FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body); |
| 444 | } |
| 445 | for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) { |
| 446 | EXPECT_TRUE(FunctionsDeallocated.count( |
| 447 | RJMM->startFunctionBodyCalls[i].Result)) |
| 448 | << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump; |
| 449 | } |
| 450 | EXPECT_EQ(RJMM->startFunctionBodyCalls.size(), |
| 451 | RJMM->deallocateFunctionBodyCalls.size()); |
| 452 | |
| 453 | SmallPtrSet<const void*, 2> ExceptionTablesDeallocated; |
Jeffrey Yasskin | b069c91 | 2009-11-11 05:30:02 +0000 | [diff] [blame] | 454 | unsigned NumTablesDeallocated = 0; |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 455 | for (unsigned i = 0, e = RJMM->deallocateExceptionTableCalls.size(); |
| 456 | i != e; ++i) { |
| 457 | ExceptionTablesDeallocated.insert( |
| 458 | RJMM->deallocateExceptionTableCalls[i].ET); |
Jeffrey Yasskin | b069c91 | 2009-11-11 05:30:02 +0000 | [diff] [blame] | 459 | if (RJMM->deallocateExceptionTableCalls[i].ET != NULL) { |
| 460 | // If JITEmitDebugInfo is off, we'll "deallocate" NULL, which doesn't |
| 461 | // appear in startExceptionTableCalls. |
| 462 | NumTablesDeallocated++; |
| 463 | } |
Jeffrey Yasskin | 7a9034c | 2009-10-27 00:03:05 +0000 | [diff] [blame] | 464 | } |
| 465 | for (unsigned i = 0, e = RJMM->startExceptionTableCalls.size(); i != e; ++i) { |
| 466 | EXPECT_TRUE(ExceptionTablesDeallocated.count( |
| 467 | RJMM->startExceptionTableCalls[i].Result)) |
| 468 | << "Function's exception table leaked: \n" |
| 469 | << RJMM->startExceptionTableCalls[i].F_dump; |
| 470 | } |
| 471 | EXPECT_EQ(RJMM->startExceptionTableCalls.size(), |
Jeffrey Yasskin | b069c91 | 2009-11-11 05:30:02 +0000 | [diff] [blame] | 472 | NumTablesDeallocated); |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 473 | } |
Andrew Trick | 0005cc7 | 2011-08-26 23:39:30 +0000 | [diff] [blame] | 474 | #endif // !defined(__arm__) |
Jeffrey Yasskin | 23e5fcf | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 475 | |
Jeffrey Yasskin | 630382a | 2009-11-24 02:11:14 +0000 | [diff] [blame] | 476 | // ARM and PPC still emit stubs for calls since the target may be too far away |
| 477 | // to call directly. This #if can probably be removed when |
| 478 | // http://llvm.org/PR5201 is fixed. |
| 479 | #if !defined(__arm__) && !defined(__powerpc__) && !defined(__ppc__) |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 480 | typedef int (*FooPtr) (); |
| 481 | |
| 482 | TEST_F(JITTest, NoStubs) { |
| 483 | LoadAssembly("define void @bar() {" |
| 484 | "entry: " |
| 485 | "ret void" |
| 486 | "}" |
| 487 | " " |
| 488 | "define i32 @foo() {" |
| 489 | "entry:" |
| 490 | "call void @bar()" |
| 491 | "ret i32 undef" |
| 492 | "}" |
| 493 | " " |
| 494 | "define i32 @main() {" |
| 495 | "entry:" |
| 496 | "%0 = call i32 @foo()" |
| 497 | "call void @bar()" |
| 498 | "ret i32 undef" |
| 499 | "}"); |
| 500 | Function *foo = M->getFunction("foo"); |
| 501 | uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo)); |
| 502 | FooPtr ptr = (FooPtr)(tmp); |
| 503 | |
| 504 | (ptr)(); |
| 505 | |
| 506 | // We should now allocate no more stubs, we have the code to foo |
| 507 | // and the existing stub for bar. |
| 508 | int stubsBefore = RJMM->stubsAllocated; |
| 509 | Function *func = M->getFunction("main"); |
| 510 | TheJIT->getPointerToFunction(func); |
| 511 | |
| 512 | Function *bar = M->getFunction("bar"); |
| 513 | TheJIT->getPointerToFunction(bar); |
| 514 | |
| 515 | ASSERT_EQ(stubsBefore, RJMM->stubsAllocated); |
| 516 | } |
Jeffrey Yasskin | 630382a | 2009-11-24 02:11:14 +0000 | [diff] [blame] | 517 | #endif // !ARM && !PPC |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 518 | |
| 519 | TEST_F(JITTest, FunctionPointersOutliveTheirCreator) { |
| 520 | TheJIT->DisableLazyCompilation(true); |
| 521 | LoadAssembly("define i8()* @get_foo_addr() { " |
| 522 | " ret i8()* @foo " |
| 523 | "} " |
| 524 | " " |
| 525 | "define i8 @foo() { " |
| 526 | " ret i8 42 " |
| 527 | "} "); |
| 528 | Function *F_get_foo_addr = M->getFunction("get_foo_addr"); |
| 529 | |
| 530 | typedef char(*fooT)(); |
| 531 | fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>( |
| 532 | (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr)); |
| 533 | fooT foo_addr = get_foo_addr(); |
| 534 | |
| 535 | // Now free get_foo_addr. This should not free the machine code for foo or |
| 536 | // any call stub returned as foo's canonical address. |
| 537 | TheJIT->freeMachineCodeForFunction(F_get_foo_addr); |
| 538 | |
| 539 | // Check by calling the reported address of foo. |
| 540 | EXPECT_EQ(42, foo_addr()); |
| 541 | |
| 542 | // The reported address should also be the same as the result of a subsequent |
| 543 | // getPointerToFunction(foo). |
| 544 | #if 0 |
| 545 | // Fails until PR5126 is fixed: |
| 546 | Function *F_foo = M->getFunction("foo"); |
| 547 | fooT foo = reinterpret_cast<fooT>( |
| 548 | (intptr_t)TheJIT->getPointerToFunction(F_foo)); |
| 549 | EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr); |
Bill Wendling | 0c2749f | 2009-11-13 21:58:54 +0000 | [diff] [blame] | 550 | #endif |
Jeffrey Yasskin | 108c838 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 551 | } |
Eric Christopher | 116664a | 2009-11-12 03:12:18 +0000 | [diff] [blame] | 552 | |
Jeffrey Yasskin | b9b88ea | 2009-12-23 00:58:02 +0000 | [diff] [blame] | 553 | // ARM doesn't have an implementation of replaceMachineCodeForFunction(), so |
| 554 | // recompileAndRelinkFunction doesn't work. |
| 555 | #if !defined(__arm__) |
Jeffrey Yasskin | 92fdf45 | 2009-12-22 23:18:18 +0000 | [diff] [blame] | 556 | TEST_F(JITTest, FunctionIsRecompiledAndRelinked) { |
| 557 | Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context), |
| 558 | GlobalValue::ExternalLinkage, "test", M); |
| 559 | BasicBlock *Entry = BasicBlock::Create(Context, "entry", F); |
| 560 | IRBuilder<> Builder(Entry); |
| 561 | Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1); |
| 562 | Builder.CreateRet(Val); |
| 563 | |
| 564 | TheJIT->DisableLazyCompilation(true); |
| 565 | // Compile the function once, and make sure it works. |
| 566 | int (*OrigFPtr)() = reinterpret_cast<int(*)()>( |
| 567 | (intptr_t)TheJIT->recompileAndRelinkFunction(F)); |
| 568 | EXPECT_EQ(1, OrigFPtr()); |
| 569 | |
| 570 | // Now change the function to return a different value. |
| 571 | Entry->eraseFromParent(); |
| 572 | BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F); |
| 573 | Builder.SetInsertPoint(NewEntry); |
| 574 | Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2); |
| 575 | Builder.CreateRet(Val); |
| 576 | // Recompile it, which should produce a new function pointer _and_ update the |
| 577 | // old one. |
| 578 | int (*NewFPtr)() = reinterpret_cast<int(*)()>( |
| 579 | (intptr_t)TheJIT->recompileAndRelinkFunction(F)); |
| 580 | |
| 581 | EXPECT_EQ(2, NewFPtr()) |
| 582 | << "The new pointer should call the new version of the function"; |
| 583 | EXPECT_EQ(2, OrigFPtr()) |
| 584 | << "The old pointer's target should now jump to the new version"; |
| 585 | } |
Jeffrey Yasskin | b9b88ea | 2009-12-23 00:58:02 +0000 | [diff] [blame] | 586 | #endif // !defined(__arm__) |
Jeffrey Yasskin | 92fdf45 | 2009-12-22 23:18:18 +0000 | [diff] [blame] | 587 | |
Jeffrey Yasskin | 898e9df | 2009-12-13 20:30:32 +0000 | [diff] [blame] | 588 | } // anonymous namespace |
| 589 | // This variable is intentionally defined differently in the statically-compiled |
| 590 | // program from the IR input to the JIT to assert that the JIT doesn't use its |
| 591 | // definition. |
| 592 | extern "C" int32_t JITTest_AvailableExternallyGlobal; |
| 593 | int32_t JITTest_AvailableExternallyGlobal = 42; |
| 594 | namespace { |
| 595 | |
| 596 | TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) { |
| 597 | TheJIT->DisableLazyCompilation(true); |
| 598 | LoadAssembly("@JITTest_AvailableExternallyGlobal = " |
| 599 | " available_externally global i32 7 " |
| 600 | " " |
| 601 | "define i32 @loader() { " |
| 602 | " %result = load i32* @JITTest_AvailableExternallyGlobal " |
| 603 | " ret i32 %result " |
| 604 | "} "); |
| 605 | Function *loaderIR = M->getFunction("loader"); |
| 606 | |
| 607 | int32_t (*loader)() = reinterpret_cast<int32_t(*)()>( |
| 608 | (intptr_t)TheJIT->getPointerToFunction(loaderIR)); |
| 609 | EXPECT_EQ(42, loader()) << "func should return 42 from the external global," |
| 610 | << " not 7 from the IR version."; |
| 611 | } |
| 612 | |
Jeffrey Yasskin | aad0d52 | 2009-12-17 21:35:29 +0000 | [diff] [blame] | 613 | } // anonymous namespace |
| 614 | // This function is intentionally defined differently in the statically-compiled |
| 615 | // program from the IR input to the JIT to assert that the JIT doesn't use its |
| 616 | // definition. |
| 617 | extern "C" int32_t JITTest_AvailableExternallyFunction() { |
| 618 | return 42; |
| 619 | } |
| 620 | namespace { |
| 621 | |
Andrew Trick | 0005cc7 | 2011-08-26 23:39:30 +0000 | [diff] [blame] | 622 | // ARM tests disabled pending fix for PR10783. |
| 623 | #if !defined(__arm__) |
Jeffrey Yasskin | aad0d52 | 2009-12-17 21:35:29 +0000 | [diff] [blame] | 624 | TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) { |
| 625 | TheJIT->DisableLazyCompilation(true); |
| 626 | LoadAssembly("define available_externally i32 " |
| 627 | " @JITTest_AvailableExternallyFunction() { " |
| 628 | " ret i32 7 " |
| 629 | "} " |
| 630 | " " |
| 631 | "define i32 @func() { " |
| 632 | " %result = tail call i32 " |
| 633 | " @JITTest_AvailableExternallyFunction() " |
| 634 | " ret i32 %result " |
| 635 | "} "); |
| 636 | Function *funcIR = M->getFunction("func"); |
| 637 | |
| 638 | int32_t (*func)() = reinterpret_cast<int32_t(*)()>( |
| 639 | (intptr_t)TheJIT->getPointerToFunction(funcIR)); |
| 640 | EXPECT_EQ(42, func()) << "func should return 42 from the static version," |
| 641 | << " not 7 from the IR version."; |
| 642 | } |
| 643 | |
Jeffrey Yasskin | 39c75f2 | 2010-03-04 19:45:09 +0000 | [diff] [blame] | 644 | TEST_F(JITTest, EscapedLazyStubStillCallable) { |
| 645 | TheJIT->DisableLazyCompilation(false); |
| 646 | LoadAssembly("define internal i32 @stubbed() { " |
| 647 | " ret i32 42 " |
| 648 | "} " |
| 649 | " " |
| 650 | "define i32()* @get_stub() { " |
| 651 | " ret i32()* @stubbed " |
| 652 | "} "); |
| 653 | typedef int32_t(*StubTy)(); |
| 654 | |
| 655 | // Call get_stub() to get the address of @stubbed without actually JITting it. |
| 656 | Function *get_stubIR = M->getFunction("get_stub"); |
| 657 | StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>( |
| 658 | (intptr_t)TheJIT->getPointerToFunction(get_stubIR)); |
| 659 | StubTy stubbed = get_stub(); |
| 660 | // Now get_stubIR is the only reference to stubbed's stub. |
| 661 | get_stubIR->eraseFromParent(); |
| 662 | // Now there are no references inside the JIT, but we've got a pointer outside |
| 663 | // it. The stub should be callable and return the right value. |
| 664 | EXPECT_EQ(42, stubbed()); |
| 665 | } |
| 666 | |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 667 | // Converts the LLVM assembly to bitcode and returns it in a std::string. An |
| 668 | // empty string indicates an error. |
| 669 | std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) { |
| 670 | Module TempModule("TempModule", Context); |
| 671 | if (!LoadAssemblyInto(&TempModule, Assembly)) { |
| 672 | return ""; |
| 673 | } |
| 674 | |
| 675 | std::string Result; |
| 676 | raw_string_ostream OS(Result); |
| 677 | WriteBitcodeToFile(&TempModule, OS); |
| 678 | OS.flush(); |
| 679 | return Result; |
| 680 | } |
| 681 | |
| 682 | // Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode' |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 683 | // lazily. The associated Module (owned by the ExecutionEngine) is returned in |
| 684 | // M. Both will be NULL on an error. Bitcode must live at least as long as the |
| 685 | // ExecutionEngine. |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 686 | ExecutionEngine *getJITFromBitcode( |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 687 | LLVMContext &Context, const std::string &Bitcode, Module *&M) { |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 688 | // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires. |
| 689 | MemoryBuffer *BitcodeBuffer = |
Chris Lattner | 796e64b | 2010-04-05 22:49:48 +0000 | [diff] [blame] | 690 | MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"); |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 691 | std::string errMsg; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 692 | M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg); |
| 693 | if (M == NULL) { |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 694 | ADD_FAILURE() << errMsg; |
| 695 | delete BitcodeBuffer; |
| 696 | return NULL; |
| 697 | } |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 698 | ExecutionEngine *TheJIT = EngineBuilder(M) |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 699 | .setEngineKind(EngineKind::JIT) |
| 700 | .setErrorStr(&errMsg) |
| 701 | .create(); |
| 702 | if (TheJIT == NULL) { |
| 703 | ADD_FAILURE() << errMsg; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 704 | delete M; |
| 705 | M = NULL; |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 706 | return NULL; |
| 707 | } |
| 708 | return TheJIT; |
| 709 | } |
| 710 | |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 711 | TEST(LazyLoadedJITTest, MaterializableAvailableExternallyFunctionIsntCompiled) { |
| 712 | LLVMContext Context; |
| 713 | const std::string Bitcode = |
| 714 | AssembleToBitcode(Context, |
| 715 | "define available_externally i32 " |
| 716 | " @JITTest_AvailableExternallyFunction() { " |
| 717 | " ret i32 7 " |
| 718 | "} " |
| 719 | " " |
| 720 | "define i32 @func() { " |
| 721 | " %result = tail call i32 " |
| 722 | " @JITTest_AvailableExternallyFunction() " |
| 723 | " ret i32 %result " |
| 724 | "} "); |
| 725 | ASSERT_FALSE(Bitcode.empty()) << "Assembling failed"; |
| 726 | Module *M; |
| 727 | OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M)); |
| 728 | ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT."; |
| 729 | TheJIT->DisableLazyCompilation(true); |
| 730 | |
| 731 | Function *funcIR = M->getFunction("func"); |
| 732 | Function *availableFunctionIR = |
| 733 | M->getFunction("JITTest_AvailableExternallyFunction"); |
| 734 | |
| 735 | // Double-check that the available_externally function is still unmaterialized |
| 736 | // when getPointerToFunction needs to find out if it's available_externally. |
| 737 | EXPECT_TRUE(availableFunctionIR->isMaterializable()); |
| 738 | |
| 739 | int32_t (*func)() = reinterpret_cast<int32_t(*)()>( |
| 740 | (intptr_t)TheJIT->getPointerToFunction(funcIR)); |
| 741 | EXPECT_EQ(42, func()) << "func should return 42 from the static version," |
| 742 | << " not 7 from the IR version."; |
| 743 | } |
| 744 | |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 745 | TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) { |
| 746 | LLVMContext Context; |
| 747 | const std::string Bitcode = |
| 748 | AssembleToBitcode(Context, |
| 749 | "define i32 @recur1(i32 %a) { " |
| 750 | " %zero = icmp eq i32 %a, 0 " |
| 751 | " br i1 %zero, label %done, label %notdone " |
| 752 | "done: " |
| 753 | " ret i32 3 " |
| 754 | "notdone: " |
| 755 | " %am1 = sub i32 %a, 1 " |
| 756 | " %result = call i32 @recur2(i32 %am1) " |
| 757 | " ret i32 %result " |
| 758 | "} " |
| 759 | " " |
| 760 | "define i32 @recur2(i32 %b) { " |
| 761 | " %result = call i32 @recur1(i32 %b) " |
| 762 | " ret i32 %result " |
| 763 | "} "); |
| 764 | ASSERT_FALSE(Bitcode.empty()) << "Assembling failed"; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 765 | Module *M; |
| 766 | OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M)); |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 767 | ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT."; |
| 768 | TheJIT->DisableLazyCompilation(true); |
| 769 | |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 770 | Function *recur1IR = M->getFunction("recur1"); |
| 771 | Function *recur2IR = M->getFunction("recur2"); |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 772 | EXPECT_TRUE(recur1IR->isMaterializable()); |
| 773 | EXPECT_TRUE(recur2IR->isMaterializable()); |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 774 | |
| 775 | int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>( |
| 776 | (intptr_t)TheJIT->getPointerToFunction(recur1IR)); |
| 777 | EXPECT_EQ(3, recur1(4)); |
| 778 | } |
Andrew Trick | 0005cc7 | 2011-08-26 23:39:30 +0000 | [diff] [blame] | 779 | #endif // !defined(__arm__) |
Jeffrey Yasskin | c5818fb | 2009-12-22 23:47:23 +0000 | [diff] [blame] | 780 | |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 781 | // This code is copied from JITEventListenerTest, but it only runs once for all |
| 782 | // the tests in this directory. Everything seems fine, but that's strange |
| 783 | // behavior. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 784 | class JITEnvironment : public testing::Environment { |
| 785 | virtual void SetUp() { |
Reid Kleckner | 4b1511b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 786 | // Required to create a JIT. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 787 | InitializeNativeTarget(); |
| 788 | } |
| 789 | }; |
| 790 | testing::Environment* const jit_env = |
| 791 | testing::AddGlobalTestEnvironment(new JITEnvironment); |
| 792 | |
| 793 | } |