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