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