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