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