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