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