blob: de30ddaa15fc15ed28bb4fb412050d3fd1db9e3b [file] [log] [blame]
Reid Kleckner4b1511b2009-07-18 00:42:18 +00001//===- JITTest.cpp - Unit tests for the JIT -------------------------------===//
Jeffrey Yasskin489393d2009-07-08 21:59:57 +00002//
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
10#include "gtest/gtest.h"
11#include "llvm/ADT/OwningPtr.h"
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000012#include "llvm/ADT/SmallPtrSet.h"
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000013#include "llvm/Assembly/Parser.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000014#include "llvm/BasicBlock.h"
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +000015#include "llvm/Bitcode/ReaderWriter.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000016#include "llvm/Constant.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/ExecutionEngine/JIT.h"
20#include "llvm/ExecutionEngine/JITMemoryManager.h"
21#include "llvm/Function.h"
22#include "llvm/GlobalValue.h"
23#include "llvm/GlobalVariable.h"
Reid Kleckner4b1511b2009-07-18 00:42:18 +000024#include "llvm/LLVMContext.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000025#include "llvm/Module.h"
26#include "llvm/ModuleProvider.h"
27#include "llvm/Support/IRBuilder.h"
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +000028#include "llvm/Support/MemoryBuffer.h"
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000029#include "llvm/Support/SourceMgr.h"
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +000030#include "llvm/Support/TypeBuilder.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000031#include "llvm/Target/TargetSelect.h"
32#include "llvm/Type.h"
33
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000034#include <vector>
35
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000036using namespace llvm;
37
38namespace {
39
40Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
41 std::vector<const Type*> params;
Owen Andersondebcb012009-07-29 22:17:13 +000042 const FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
Dan Gohmanc6f40b62009-07-11 13:56:14 +000043 params, false);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000044 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
Owen Anderson1d0be152009-08-13 21:58:54 +000045 BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000046 IRBuilder<> builder(Entry);
47 Value *Load = builder.CreateLoad(G);
48 const Type *GTy = G->getType()->getElementType();
Owen Andersoneed707b2009-07-24 23:12:02 +000049 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000050 builder.CreateStore(Add, G);
51 builder.CreateRet(Add);
52 return F;
53}
54
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000055std::string DumpFunction(const Function *F) {
56 std::string Result;
57 raw_string_ostream(Result) << "" << *F;
58 return Result;
59}
60
61class RecordingJITMemoryManager : public JITMemoryManager {
62 const OwningPtr<JITMemoryManager> Base;
63public:
64 RecordingJITMemoryManager()
65 : Base(JITMemoryManager::CreateDefaultMemManager()) {
Eric Christopher116664a2009-11-12 03:12:18 +000066 stubsAllocated = 0;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000067 }
68
69 virtual void setMemoryWritable() { Base->setMemoryWritable(); }
70 virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
71 virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
72 virtual void AllocateGOT() { Base->AllocateGOT(); }
73 virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000074 struct StartFunctionBodyCall {
75 StartFunctionBodyCall(uint8_t *Result, const Function *F,
76 uintptr_t ActualSize, uintptr_t ActualSizeResult)
77 : Result(Result), F(F), F_dump(DumpFunction(F)),
78 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
79 uint8_t *Result;
80 const Function *F;
81 std::string F_dump;
82 uintptr_t ActualSize;
83 uintptr_t ActualSizeResult;
84 };
85 std::vector<StartFunctionBodyCall> startFunctionBodyCalls;
86 virtual uint8_t *startFunctionBody(const Function *F,
87 uintptr_t &ActualSize) {
88 uintptr_t InitialActualSize = ActualSize;
89 uint8_t *Result = Base->startFunctionBody(F, ActualSize);
90 startFunctionBodyCalls.push_back(
91 StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize));
92 return Result;
93 }
Eric Christopher116664a2009-11-12 03:12:18 +000094 int stubsAllocated;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000095 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
96 unsigned Alignment) {
Eric Christopher116664a2009-11-12 03:12:18 +000097 stubsAllocated++;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000098 return Base->allocateStub(F, StubSize, Alignment);
99 }
100 struct EndFunctionBodyCall {
101 EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart,
102 uint8_t *FunctionEnd)
103 : F(F), F_dump(DumpFunction(F)),
104 FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {}
105 const Function *F;
106 std::string F_dump;
107 uint8_t *FunctionStart;
108 uint8_t *FunctionEnd;
109 };
110 std::vector<EndFunctionBodyCall> endFunctionBodyCalls;
111 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
112 uint8_t *FunctionEnd) {
113 endFunctionBodyCalls.push_back(
114 EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
115 Base->endFunctionBody(F, FunctionStart, FunctionEnd);
116 }
117 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
118 return Base->allocateSpace(Size, Alignment);
119 }
120 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
121 return Base->allocateGlobal(Size, Alignment);
122 }
123 struct DeallocateFunctionBodyCall {
124 DeallocateFunctionBodyCall(const void *Body) : Body(Body) {}
125 const void *Body;
126 };
127 std::vector<DeallocateFunctionBodyCall> deallocateFunctionBodyCalls;
128 virtual void deallocateFunctionBody(void *Body) {
129 deallocateFunctionBodyCalls.push_back(DeallocateFunctionBodyCall(Body));
130 Base->deallocateFunctionBody(Body);
131 }
132 struct DeallocateExceptionTableCall {
133 DeallocateExceptionTableCall(const void *ET) : ET(ET) {}
134 const void *ET;
135 };
136 std::vector<DeallocateExceptionTableCall> deallocateExceptionTableCalls;
137 virtual void deallocateExceptionTable(void *ET) {
138 deallocateExceptionTableCalls.push_back(DeallocateExceptionTableCall(ET));
139 Base->deallocateExceptionTable(ET);
140 }
141 struct StartExceptionTableCall {
142 StartExceptionTableCall(uint8_t *Result, const Function *F,
143 uintptr_t ActualSize, uintptr_t ActualSizeResult)
144 : Result(Result), F(F), F_dump(DumpFunction(F)),
145 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
146 uint8_t *Result;
147 const Function *F;
148 std::string F_dump;
149 uintptr_t ActualSize;
150 uintptr_t ActualSizeResult;
151 };
152 std::vector<StartExceptionTableCall> startExceptionTableCalls;
153 virtual uint8_t* startExceptionTable(const Function* F,
154 uintptr_t &ActualSize) {
155 uintptr_t InitialActualSize = ActualSize;
156 uint8_t *Result = Base->startExceptionTable(F, ActualSize);
157 startExceptionTableCalls.push_back(
158 StartExceptionTableCall(Result, F, InitialActualSize, ActualSize));
159 return Result;
160 }
161 struct EndExceptionTableCall {
162 EndExceptionTableCall(const Function *F, uint8_t *TableStart,
163 uint8_t *TableEnd, uint8_t* FrameRegister)
164 : F(F), F_dump(DumpFunction(F)),
165 TableStart(TableStart), TableEnd(TableEnd),
166 FrameRegister(FrameRegister) {}
167 const Function *F;
168 std::string F_dump;
169 uint8_t *TableStart;
170 uint8_t *TableEnd;
171 uint8_t *FrameRegister;
172 };
173 std::vector<EndExceptionTableCall> endExceptionTableCalls;
174 virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
175 uint8_t *TableEnd, uint8_t* FrameRegister) {
176 endExceptionTableCalls.push_back(
177 EndExceptionTableCall(F, TableStart, TableEnd, FrameRegister));
178 return Base->endExceptionTable(F, TableStart, TableEnd, FrameRegister);
179 }
180};
181
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000182bool LoadAssemblyInto(Module *M, const char *assembly) {
183 SMDiagnostic Error;
184 bool success =
185 NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
186 std::string errMsg;
187 raw_string_ostream os(errMsg);
188 Error.Print("", os);
189 EXPECT_TRUE(success) << os.str();
190 return success;
191}
192
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000193class JITTest : public testing::Test {
194 protected:
195 virtual void SetUp() {
196 M = new Module("<main>", Context);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000197 MP = new ExistingModuleProvider(M);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000198 RJMM = new RecordingJITMemoryManager;
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000199 RJMM->setPoisonMemory(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000200 std::string Error;
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000201 TheJIT.reset(EngineBuilder(MP).setEngineKind(EngineKind::JIT)
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000202 .setJITMemoryManager(RJMM)
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000203 .setErrorStr(&Error).create());
204 ASSERT_TRUE(TheJIT.get() != NULL) << Error;
205 }
206
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000207 void LoadAssembly(const char *assembly) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000208 LoadAssemblyInto(M, assembly);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000209 }
210
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000211 LLVMContext Context;
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000212 Module *M; // Owned by MP.
213 ModuleProvider *MP; // Owned by ExecutionEngine.
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000214 RecordingJITMemoryManager *RJMM;
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000215 OwningPtr<ExecutionEngine> TheJIT;
216};
217
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000218// Regression test for a bug. The JIT used to allocate globals inside the same
219// memory block used for the function, and when the function code was freed,
220// the global was left in the same place. This test allocates a function
221// that uses and global, deallocates it, and then makes sure that the global
222// stays alive after that.
223TEST(JIT, GlobalInFunction) {
224 LLVMContext context;
225 Module *M = new Module("<main>", context);
226 ExistingModuleProvider *MP = new ExistingModuleProvider(M);
227
228 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
229 // Tell the memory manager to poison freed memory so that accessing freed
230 // memory is more easily tested.
231 MemMgr->setPoisonMemory(true);
232 std::string Error;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000233 OwningPtr<ExecutionEngine> JIT(EngineBuilder(MP)
Daniel Dunbard370d772009-07-18 06:08:49 +0000234 .setEngineKind(EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000235 .setErrorStr(&Error)
236 .setJITMemoryManager(MemMgr)
237 // The next line enables the fix:
238 .setAllocateGVsWithCode(false)
239 .create());
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000240 ASSERT_EQ(Error, "");
241
242 // Create a global variable.
Owen Anderson1d0be152009-08-13 21:58:54 +0000243 const Type *GTy = Type::getInt32Ty(context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000244 GlobalVariable *G = new GlobalVariable(
245 *M,
246 GTy,
247 false, // Not constant.
248 GlobalValue::InternalLinkage,
Benjamin Kramerfeba7562009-07-31 20:56:31 +0000249 Constant::getNullValue(GTy),
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000250 "myglobal");
251
252 // Make a function that points to a global.
253 Function *F1 = makeReturnGlobal("F1", G, M);
254
255 // Get the pointer to the native code to force it to JIT the function and
256 // allocate space for the global.
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000257 void (*F1Ptr)() =
258 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000259
260 // Since F1 was codegen'd, a pointer to G should be available.
261 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
262 ASSERT_NE((int32_t*)NULL, GPtr);
263 EXPECT_EQ(0, *GPtr);
264
265 // F1() should increment G.
266 F1Ptr();
267 EXPECT_EQ(1, *GPtr);
268
269 // Make a second function identical to the first, referring to the same
270 // global.
271 Function *F2 = makeReturnGlobal("F2", G, M);
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000272 void (*F2Ptr)() =
273 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000274
275 // F2() should increment G.
276 F2Ptr();
277 EXPECT_EQ(2, *GPtr);
278
279 // Deallocate F1.
280 JIT->freeMachineCodeForFunction(F1);
281
282 // F2() should *still* increment G.
283 F2Ptr();
284 EXPECT_EQ(3, *GPtr);
285}
286
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000287int PlusOne(int arg) {
288 return arg + 1;
289}
290
291TEST_F(JITTest, FarCallToKnownFunction) {
292 // x86-64 can only make direct calls to functions within 32 bits of
293 // the current PC. To call anything farther away, we have to load
294 // the address into a register and call through the register. The
295 // current JIT does this by allocating a stub for any far call.
296 // There was a bug in which the JIT tried to emit a direct call when
297 // the target was already in the JIT's global mappings and lazy
298 // compilation was disabled.
299
300 Function *KnownFunction = Function::Create(
301 TypeBuilder<int(int), false>::get(Context),
302 GlobalValue::ExternalLinkage, "known", M);
303 TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
304
305 // int test() { return known(7); }
306 Function *TestFunction = Function::Create(
307 TypeBuilder<int(), false>::get(Context),
308 GlobalValue::ExternalLinkage, "test", M);
309 BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
310 IRBuilder<> Builder(Entry);
311 Value *result = Builder.CreateCall(
312 KnownFunction,
313 ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
314 Builder.CreateRet(result);
315
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000316 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000317 int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
318 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
319 // This used to crash in trying to call PlusOne().
320 EXPECT_EQ(8, TestFunctionPtr());
321}
322
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000323// Test a function C which calls A and B which call each other.
324TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000325 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000326
327 const FunctionType *Func1Ty =
328 cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
329 std::vector<const Type*> arg_types;
330 arg_types.push_back(Type::getInt1Ty(Context));
331 const FunctionType *FuncTy = FunctionType::get(
332 Type::getVoidTy(Context), arg_types, false);
333 Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
334 "func1", M);
335 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
336 "func2", M);
337 Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
338 "func3", M);
339 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
340 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
341 BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
342 BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
343 BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
344 BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
345 BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
346
347 // Make Func1 call Func2(0) and Func3(0).
348 IRBuilder<> Builder(Block1);
349 Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
350 Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
351 Builder.CreateRetVoid();
352
353 // void Func2(bool b) { if (b) { Func3(false); return; } return; }
354 Builder.SetInsertPoint(Block2);
355 Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
356 Builder.SetInsertPoint(True2);
357 Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
358 Builder.CreateRetVoid();
359 Builder.SetInsertPoint(False2);
360 Builder.CreateRetVoid();
361
362 // void Func3(bool b) { if (b) { Func2(false); return; } return; }
363 Builder.SetInsertPoint(Block3);
364 Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
365 Builder.SetInsertPoint(True3);
366 Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
367 Builder.CreateRetVoid();
368 Builder.SetInsertPoint(False3);
369 Builder.CreateRetVoid();
370
371 // Compile the function to native code
372 void (*F1Ptr)() =
373 reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
374
375 F1Ptr();
376}
377
378// Regression test for PR5162. This used to trigger an AssertingVH inside the
379// JIT's Function to stub mapping.
380TEST_F(JITTest, NonLazyLeaksNoStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000381 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000382
383 // Create two functions with a single basic block each.
384 const FunctionType *FuncTy =
385 cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
386 Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
387 "func1", M);
388 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
389 "func2", M);
390 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
391 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
392
393 // The first function calls the second and returns the result
394 IRBuilder<> Builder(Block1);
395 Value *Result = Builder.CreateCall(Func2);
396 Builder.CreateRet(Result);
397
398 // The second function just returns a constant
399 Builder.SetInsertPoint(Block2);
400 Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
401
402 // Compile the function to native code
403 (void)TheJIT->getPointerToFunction(Func1);
404
405 // Free the JIT state for the functions
406 TheJIT->freeMachineCodeForFunction(Func1);
407 TheJIT->freeMachineCodeForFunction(Func2);
408
409 // Delete the first function (and show that is has no users)
410 EXPECT_EQ(Func1->getNumUses(), 0u);
411 Func1->eraseFromParent();
412
413 // Delete the second function (and show that it has no users - it had one,
414 // func1 but that's gone now)
415 EXPECT_EQ(Func2->getNumUses(), 0u);
416 Func2->eraseFromParent();
417}
418
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000419TEST_F(JITTest, ModuleDeletion) {
Jeffrey Yasskinb2352242009-10-28 00:28:31 +0000420 TheJIT->DisableLazyCompilation(false);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000421 LoadAssembly("define void @main() { "
422 " call i32 @computeVal() "
423 " ret void "
424 "} "
425 " "
426 "define internal i32 @computeVal() { "
427 " ret i32 0 "
428 "} ");
429 Function *func = M->getFunction("main");
430 TheJIT->getPointerToFunction(func);
431 TheJIT->deleteModuleProvider(MP);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000432
433 SmallPtrSet<const void*, 2> FunctionsDeallocated;
434 for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
435 i != e; ++i) {
436 FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
437 }
438 for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
439 EXPECT_TRUE(FunctionsDeallocated.count(
440 RJMM->startFunctionBodyCalls[i].Result))
441 << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
442 }
443 EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
444 RJMM->deallocateFunctionBodyCalls.size());
445
446 SmallPtrSet<const void*, 2> ExceptionTablesDeallocated;
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000447 unsigned NumTablesDeallocated = 0;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000448 for (unsigned i = 0, e = RJMM->deallocateExceptionTableCalls.size();
449 i != e; ++i) {
450 ExceptionTablesDeallocated.insert(
451 RJMM->deallocateExceptionTableCalls[i].ET);
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000452 if (RJMM->deallocateExceptionTableCalls[i].ET != NULL) {
453 // If JITEmitDebugInfo is off, we'll "deallocate" NULL, which doesn't
454 // appear in startExceptionTableCalls.
455 NumTablesDeallocated++;
456 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000457 }
458 for (unsigned i = 0, e = RJMM->startExceptionTableCalls.size(); i != e; ++i) {
459 EXPECT_TRUE(ExceptionTablesDeallocated.count(
460 RJMM->startExceptionTableCalls[i].Result))
461 << "Function's exception table leaked: \n"
462 << RJMM->startExceptionTableCalls[i].F_dump;
463 }
464 EXPECT_EQ(RJMM->startExceptionTableCalls.size(),
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000465 NumTablesDeallocated);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000466}
467
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000468// ARM and PPC still emit stubs for calls since the target may be too far away
469// to call directly. This #if can probably be removed when
470// http://llvm.org/PR5201 is fixed.
471#if !defined(__arm__) && !defined(__powerpc__) && !defined(__ppc__)
Eric Christopher116664a2009-11-12 03:12:18 +0000472typedef int (*FooPtr) ();
473
474TEST_F(JITTest, NoStubs) {
475 LoadAssembly("define void @bar() {"
476 "entry: "
477 "ret void"
478 "}"
479 " "
480 "define i32 @foo() {"
481 "entry:"
482 "call void @bar()"
483 "ret i32 undef"
484 "}"
485 " "
486 "define i32 @main() {"
487 "entry:"
488 "%0 = call i32 @foo()"
489 "call void @bar()"
490 "ret i32 undef"
491 "}");
492 Function *foo = M->getFunction("foo");
493 uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
494 FooPtr ptr = (FooPtr)(tmp);
495
496 (ptr)();
497
498 // We should now allocate no more stubs, we have the code to foo
499 // and the existing stub for bar.
500 int stubsBefore = RJMM->stubsAllocated;
501 Function *func = M->getFunction("main");
502 TheJIT->getPointerToFunction(func);
503
504 Function *bar = M->getFunction("bar");
505 TheJIT->getPointerToFunction(bar);
506
507 ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
508}
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000509#endif // !ARM && !PPC
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000510
511TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
512 TheJIT->DisableLazyCompilation(true);
513 LoadAssembly("define i8()* @get_foo_addr() { "
514 " ret i8()* @foo "
515 "} "
516 " "
517 "define i8 @foo() { "
518 " ret i8 42 "
519 "} ");
520 Function *F_get_foo_addr = M->getFunction("get_foo_addr");
521
522 typedef char(*fooT)();
523 fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>(
524 (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr));
525 fooT foo_addr = get_foo_addr();
526
527 // Now free get_foo_addr. This should not free the machine code for foo or
528 // any call stub returned as foo's canonical address.
529 TheJIT->freeMachineCodeForFunction(F_get_foo_addr);
530
531 // Check by calling the reported address of foo.
532 EXPECT_EQ(42, foo_addr());
533
534 // The reported address should also be the same as the result of a subsequent
535 // getPointerToFunction(foo).
536#if 0
537 // Fails until PR5126 is fixed:
538 Function *F_foo = M->getFunction("foo");
539 fooT foo = reinterpret_cast<fooT>(
540 (intptr_t)TheJIT->getPointerToFunction(F_foo));
541 EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr);
Bill Wendling0c2749f2009-11-13 21:58:54 +0000542#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000543}
Eric Christopher116664a2009-11-12 03:12:18 +0000544
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000545TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
546 Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
547 GlobalValue::ExternalLinkage, "test", M);
548 BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
549 IRBuilder<> Builder(Entry);
550 Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1);
551 Builder.CreateRet(Val);
552
553 TheJIT->DisableLazyCompilation(true);
554 // Compile the function once, and make sure it works.
555 int (*OrigFPtr)() = reinterpret_cast<int(*)()>(
556 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
557 EXPECT_EQ(1, OrigFPtr());
558
559 // Now change the function to return a different value.
560 Entry->eraseFromParent();
561 BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F);
562 Builder.SetInsertPoint(NewEntry);
563 Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2);
564 Builder.CreateRet(Val);
565 // Recompile it, which should produce a new function pointer _and_ update the
566 // old one.
567 int (*NewFPtr)() = reinterpret_cast<int(*)()>(
568 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
569
570 EXPECT_EQ(2, NewFPtr())
571 << "The new pointer should call the new version of the function";
572 EXPECT_EQ(2, OrigFPtr())
573 << "The old pointer's target should now jump to the new version";
574}
575
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000576} // anonymous namespace
577// This variable is intentionally defined differently in the statically-compiled
578// program from the IR input to the JIT to assert that the JIT doesn't use its
579// definition.
580extern "C" int32_t JITTest_AvailableExternallyGlobal;
581int32_t JITTest_AvailableExternallyGlobal = 42;
582namespace {
583
584TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
585 TheJIT->DisableLazyCompilation(true);
586 LoadAssembly("@JITTest_AvailableExternallyGlobal = "
587 " available_externally global i32 7 "
588 " "
589 "define i32 @loader() { "
590 " %result = load i32* @JITTest_AvailableExternallyGlobal "
591 " ret i32 %result "
592 "} ");
593 Function *loaderIR = M->getFunction("loader");
594
595 int32_t (*loader)() = reinterpret_cast<int32_t(*)()>(
596 (intptr_t)TheJIT->getPointerToFunction(loaderIR));
597 EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
598 << " not 7 from the IR version.";
599}
600
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000601} // anonymous namespace
602// This function is intentionally defined differently in the statically-compiled
603// program from the IR input to the JIT to assert that the JIT doesn't use its
604// definition.
605extern "C" int32_t JITTest_AvailableExternallyFunction() {
606 return 42;
607}
608namespace {
609
610TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
611 TheJIT->DisableLazyCompilation(true);
612 LoadAssembly("define available_externally i32 "
613 " @JITTest_AvailableExternallyFunction() { "
614 " ret i32 7 "
615 "} "
616 " "
617 "define i32 @func() { "
618 " %result = tail call i32 "
619 " @JITTest_AvailableExternallyFunction() "
620 " ret i32 %result "
621 "} ");
622 Function *funcIR = M->getFunction("func");
623
624 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
625 (intptr_t)TheJIT->getPointerToFunction(funcIR));
626 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
627 << " not 7 from the IR version.";
628}
629
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000630// Converts the LLVM assembly to bitcode and returns it in a std::string. An
631// empty string indicates an error.
632std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
633 Module TempModule("TempModule", Context);
634 if (!LoadAssemblyInto(&TempModule, Assembly)) {
635 return "";
636 }
637
638 std::string Result;
639 raw_string_ostream OS(Result);
640 WriteBitcodeToFile(&TempModule, OS);
641 OS.flush();
642 return Result;
643}
644
645// Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode'
646// lazily. The associated ModuleProvider (owned by the ExecutionEngine) is
647// returned in MP. Both will be NULL on an error. Bitcode must live at least
648// as long as the ExecutionEngine.
649ExecutionEngine *getJITFromBitcode(
650 LLVMContext &Context, const std::string &Bitcode, ModuleProvider *&MP) {
651 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
652 MemoryBuffer *BitcodeBuffer =
653 MemoryBuffer::getMemBuffer(Bitcode.c_str(),
654 Bitcode.c_str() + Bitcode.size(),
655 "Bitcode for test");
656 std::string errMsg;
657 MP = getBitcodeModuleProvider(BitcodeBuffer, Context, &errMsg);
658 if (MP == NULL) {
659 ADD_FAILURE() << errMsg;
660 delete BitcodeBuffer;
661 return NULL;
662 }
663 ExecutionEngine *TheJIT = EngineBuilder(MP)
664 .setEngineKind(EngineKind::JIT)
665 .setErrorStr(&errMsg)
666 .create();
667 if (TheJIT == NULL) {
668 ADD_FAILURE() << errMsg;
669 delete MP;
670 MP = NULL;
671 return NULL;
672 }
673 return TheJIT;
674}
675
676TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
677 LLVMContext Context;
678 const std::string Bitcode =
679 AssembleToBitcode(Context,
680 "define i32 @recur1(i32 %a) { "
681 " %zero = icmp eq i32 %a, 0 "
682 " br i1 %zero, label %done, label %notdone "
683 "done: "
684 " ret i32 3 "
685 "notdone: "
686 " %am1 = sub i32 %a, 1 "
687 " %result = call i32 @recur2(i32 %am1) "
688 " ret i32 %result "
689 "} "
690 " "
691 "define i32 @recur2(i32 %b) { "
692 " %result = call i32 @recur1(i32 %b) "
693 " ret i32 %result "
694 "} ");
695 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
696 ModuleProvider *MP;
697 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, MP));
698 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
699 TheJIT->DisableLazyCompilation(true);
700
701 Module *M = MP->getModule();
702 Function *recur1IR = M->getFunction("recur1");
703 Function *recur2IR = M->getFunction("recur2");
704 EXPECT_TRUE(recur1IR->hasNotBeenReadFromBitcode());
705 EXPECT_TRUE(recur2IR->hasNotBeenReadFromBitcode());
706
707 int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>(
708 (intptr_t)TheJIT->getPointerToFunction(recur1IR));
709 EXPECT_EQ(3, recur1(4));
710}
711
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000712// This code is copied from JITEventListenerTest, but it only runs once for all
713// the tests in this directory. Everything seems fine, but that's strange
714// behavior.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000715class JITEnvironment : public testing::Environment {
716 virtual void SetUp() {
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000717 // Required to create a JIT.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000718 InitializeNativeTarget();
719 }
720};
721testing::Environment* const jit_env =
722 testing::AddGlobalTestEnvironment(new JITEnvironment);
723
724}