blob: 8780aa556c43443193b6253dd109de577cb07b2b [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
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000010#include "llvm/BasicBlock.h"
11#include "llvm/Constant.h"
12#include "llvm/Constants.h"
13#include "llvm/DerivedTypes.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000014#include "llvm/Function.h"
15#include "llvm/GlobalValue.h"
16#include "llvm/GlobalVariable.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000017#include "llvm/IRBuilder.h"
Reid Kleckner4b1511b2009-07-18 00:42:18 +000018#include "llvm/LLVMContext.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000019#include "llvm/Module.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000020#include "llvm/Type.h"
21#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/Assembly/Parser.h"
24#include "llvm/Bitcode/ReaderWriter.h"
25#include "llvm/ExecutionEngine/JIT.h"
26#include "llvm/ExecutionEngine/JITMemoryManager.h"
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +000027#include "llvm/Support/MemoryBuffer.h"
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +000028#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/TargetSelect.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000030#include "llvm/Support/TypeBuilder.h"
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000031
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000032#include "gtest/gtest.h"
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000033#include <vector>
34
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000035using namespace llvm;
36
37namespace {
38
39Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
Jay Foad5fdd6c82011-07-12 14:06:48 +000040 std::vector<Type*> params;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000041 FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
Dan Gohmanc6f40b62009-07-11 13:56:14 +000042 params, false);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000043 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
Owen Anderson1d0be152009-08-13 21:58:54 +000044 BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000045 IRBuilder<> builder(Entry);
46 Value *Load = builder.CreateLoad(G);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000047 Type *GTy = G->getType()->getElementType();
Owen Andersoneed707b2009-07-24 23:12:02 +000048 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000049 builder.CreateStore(Add, G);
50 builder.CreateRet(Add);
51 return F;
52}
53
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000054std::string DumpFunction(const Function *F) {
55 std::string Result;
56 raw_string_ostream(Result) << "" << *F;
57 return Result;
58}
59
60class RecordingJITMemoryManager : public JITMemoryManager {
61 const OwningPtr<JITMemoryManager> Base;
62public:
63 RecordingJITMemoryManager()
64 : Base(JITMemoryManager::CreateDefaultMemManager()) {
Eric Christopher116664a2009-11-12 03:12:18 +000065 stubsAllocated = 0;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000066 }
Danil Malyshev30b9e322012-03-28 21:46:36 +000067 virtual void *getPointerToNamedFunction(const std::string &Name,
68 bool AbortOnFailure = true) {
69 return Base->getPointerToNamedFunction(Name, AbortOnFailure);
70 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000071
72 virtual void setMemoryWritable() { Base->setMemoryWritable(); }
73 virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
74 virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
75 virtual void AllocateGOT() { Base->AllocateGOT(); }
76 virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000077 struct StartFunctionBodyCall {
78 StartFunctionBodyCall(uint8_t *Result, const Function *F,
79 uintptr_t ActualSize, uintptr_t ActualSizeResult)
80 : Result(Result), F(F), F_dump(DumpFunction(F)),
81 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
82 uint8_t *Result;
83 const Function *F;
84 std::string F_dump;
85 uintptr_t ActualSize;
86 uintptr_t ActualSizeResult;
87 };
88 std::vector<StartFunctionBodyCall> startFunctionBodyCalls;
89 virtual uint8_t *startFunctionBody(const Function *F,
90 uintptr_t &ActualSize) {
91 uintptr_t InitialActualSize = ActualSize;
92 uint8_t *Result = Base->startFunctionBody(F, ActualSize);
93 startFunctionBodyCalls.push_back(
94 StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize));
95 return Result;
96 }
Eric Christopher116664a2009-11-12 03:12:18 +000097 int stubsAllocated;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000098 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
99 unsigned Alignment) {
Eric Christopher116664a2009-11-12 03:12:18 +0000100 stubsAllocated++;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000101 return Base->allocateStub(F, StubSize, Alignment);
102 }
103 struct EndFunctionBodyCall {
104 EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart,
105 uint8_t *FunctionEnd)
106 : F(F), F_dump(DumpFunction(F)),
107 FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {}
108 const Function *F;
109 std::string F_dump;
110 uint8_t *FunctionStart;
111 uint8_t *FunctionEnd;
112 };
113 std::vector<EndFunctionBodyCall> endFunctionBodyCalls;
114 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
115 uint8_t *FunctionEnd) {
116 endFunctionBodyCalls.push_back(
117 EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
118 Base->endFunctionBody(F, FunctionStart, FunctionEnd);
119 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000120 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
121 unsigned SectionID) {
122 return Base->allocateDataSection(Size, Alignment, SectionID);
123 }
124 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
125 unsigned SectionID) {
126 return Base->allocateCodeSection(Size, Alignment, SectionID);
127 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000128 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 Yasskinc5818fb2009-12-22 23:47:23 +0000193bool 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 Lattnerd8b7aa22011-10-16 04:47:35 +0000199 Error.print("", os);
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000200 EXPECT_TRUE(success) << os.str();
201 return success;
202}
203
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000204class JITTest : public testing::Test {
205 protected:
206 virtual void SetUp() {
207 M = new Module("<main>", Context);
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000208 RJMM = new RecordingJITMemoryManager;
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000209 RJMM->setPoisonMemory(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000210 std::string Error;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000211 TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000212 .setJITMemoryManager(RJMM)
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000213 .setErrorStr(&Error).create());
214 ASSERT_TRUE(TheJIT.get() != NULL) << Error;
215 }
216
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000217 void LoadAssembly(const char *assembly) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000218 LoadAssemblyInto(M, assembly);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000219 }
220
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000221 LLVMContext Context;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000222 Module *M; // Owned by ExecutionEngine.
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000223 RecordingJITMemoryManager *RJMM;
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000224 OwningPtr<ExecutionEngine> TheJIT;
225};
226
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000227// Regression test for a bug. The JIT used to allocate globals inside the same
228// memory block used for the function, and when the function code was freed,
229// the global was left in the same place. This test allocates a function
230// that uses and global, deallocates it, and then makes sure that the global
231// stays alive after that.
232TEST(JIT, GlobalInFunction) {
233 LLVMContext context;
234 Module *M = new Module("<main>", context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000235
236 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
237 // Tell the memory manager to poison freed memory so that accessing freed
238 // memory is more easily tested.
239 MemMgr->setPoisonMemory(true);
240 std::string Error;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000241 OwningPtr<ExecutionEngine> JIT(EngineBuilder(M)
Daniel Dunbard370d772009-07-18 06:08:49 +0000242 .setEngineKind(EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000243 .setErrorStr(&Error)
244 .setJITMemoryManager(MemMgr)
245 // The next line enables the fix:
246 .setAllocateGVsWithCode(false)
247 .create());
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000248 ASSERT_EQ(Error, "");
249
250 // Create a global variable.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000251 Type *GTy = Type::getInt32Ty(context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000252 GlobalVariable *G = new GlobalVariable(
253 *M,
254 GTy,
255 false, // Not constant.
256 GlobalValue::InternalLinkage,
Benjamin Kramerfeba7562009-07-31 20:56:31 +0000257 Constant::getNullValue(GTy),
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000258 "myglobal");
259
260 // Make a function that points to a global.
261 Function *F1 = makeReturnGlobal("F1", G, M);
262
263 // Get the pointer to the native code to force it to JIT the function and
264 // allocate space for the global.
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000265 void (*F1Ptr)() =
266 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000267
268 // Since F1 was codegen'd, a pointer to G should be available.
269 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
270 ASSERT_NE((int32_t*)NULL, GPtr);
271 EXPECT_EQ(0, *GPtr);
272
273 // F1() should increment G.
274 F1Ptr();
275 EXPECT_EQ(1, *GPtr);
276
277 // Make a second function identical to the first, referring to the same
278 // global.
279 Function *F2 = makeReturnGlobal("F2", G, M);
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000280 void (*F2Ptr)() =
281 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000282
283 // F2() should increment G.
284 F2Ptr();
285 EXPECT_EQ(2, *GPtr);
286
287 // Deallocate F1.
288 JIT->freeMachineCodeForFunction(F1);
289
290 // F2() should *still* increment G.
291 F2Ptr();
292 EXPECT_EQ(3, *GPtr);
293}
294
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000295int PlusOne(int arg) {
296 return arg + 1;
297}
298
Andrew Trick0005cc72011-08-26 23:39:30 +0000299// ARM tests disabled pending fix for PR10783.
300#if !defined(__arm__)
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000301TEST_F(JITTest, FarCallToKnownFunction) {
302 // x86-64 can only make direct calls to functions within 32 bits of
303 // the current PC. To call anything farther away, we have to load
304 // the address into a register and call through the register. The
305 // current JIT does this by allocating a stub for any far call.
306 // There was a bug in which the JIT tried to emit a direct call when
307 // the target was already in the JIT's global mappings and lazy
308 // compilation was disabled.
309
310 Function *KnownFunction = Function::Create(
311 TypeBuilder<int(int), false>::get(Context),
312 GlobalValue::ExternalLinkage, "known", M);
313 TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
314
315 // int test() { return known(7); }
316 Function *TestFunction = Function::Create(
317 TypeBuilder<int(), false>::get(Context),
318 GlobalValue::ExternalLinkage, "test", M);
319 BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
320 IRBuilder<> Builder(Entry);
321 Value *result = Builder.CreateCall(
322 KnownFunction,
323 ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
324 Builder.CreateRet(result);
325
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000326 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000327 int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
328 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
329 // This used to crash in trying to call PlusOne().
330 EXPECT_EQ(8, TestFunctionPtr());
331}
332
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000333// Test a function C which calls A and B which call each other.
334TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000335 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000336
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000337 FunctionType *Func1Ty =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000338 cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
Jay Foad5fdd6c82011-07-12 14:06:48 +0000339 std::vector<Type*> arg_types;
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000340 arg_types.push_back(Type::getInt1Ty(Context));
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000341 FunctionType *FuncTy = FunctionType::get(
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000342 Type::getVoidTy(Context), arg_types, false);
343 Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
344 "func1", M);
345 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
346 "func2", M);
347 Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
348 "func3", M);
349 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
350 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
351 BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
352 BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
353 BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
354 BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
355 BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
356
357 // Make Func1 call Func2(0) and Func3(0).
358 IRBuilder<> Builder(Block1);
359 Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
360 Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
361 Builder.CreateRetVoid();
362
363 // void Func2(bool b) { if (b) { Func3(false); return; } return; }
364 Builder.SetInsertPoint(Block2);
365 Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
366 Builder.SetInsertPoint(True2);
367 Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
368 Builder.CreateRetVoid();
369 Builder.SetInsertPoint(False2);
370 Builder.CreateRetVoid();
371
372 // void Func3(bool b) { if (b) { Func2(false); return; } return; }
373 Builder.SetInsertPoint(Block3);
374 Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
375 Builder.SetInsertPoint(True3);
376 Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
377 Builder.CreateRetVoid();
378 Builder.SetInsertPoint(False3);
379 Builder.CreateRetVoid();
380
381 // Compile the function to native code
382 void (*F1Ptr)() =
383 reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
384
385 F1Ptr();
386}
387
388// Regression test for PR5162. This used to trigger an AssertingVH inside the
389// JIT's Function to stub mapping.
390TEST_F(JITTest, NonLazyLeaksNoStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000391 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000392
393 // Create two functions with a single basic block each.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000394 FunctionType *FuncTy =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000395 cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
396 Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
397 "func1", M);
398 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
399 "func2", M);
400 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
401 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
402
403 // The first function calls the second and returns the result
404 IRBuilder<> Builder(Block1);
405 Value *Result = Builder.CreateCall(Func2);
406 Builder.CreateRet(Result);
407
408 // The second function just returns a constant
409 Builder.SetInsertPoint(Block2);
410 Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
411
412 // Compile the function to native code
413 (void)TheJIT->getPointerToFunction(Func1);
414
415 // Free the JIT state for the functions
416 TheJIT->freeMachineCodeForFunction(Func1);
417 TheJIT->freeMachineCodeForFunction(Func2);
418
419 // Delete the first function (and show that is has no users)
420 EXPECT_EQ(Func1->getNumUses(), 0u);
421 Func1->eraseFromParent();
422
423 // Delete the second function (and show that it has no users - it had one,
424 // func1 but that's gone now)
425 EXPECT_EQ(Func2->getNumUses(), 0u);
426 Func2->eraseFromParent();
427}
428
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000429TEST_F(JITTest, ModuleDeletion) {
Jeffrey Yasskinb2352242009-10-28 00:28:31 +0000430 TheJIT->DisableLazyCompilation(false);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000431 LoadAssembly("define void @main() { "
432 " call i32 @computeVal() "
433 " ret void "
434 "} "
435 " "
436 "define internal i32 @computeVal() { "
437 " ret i32 0 "
438 "} ");
439 Function *func = M->getFunction("main");
440 TheJIT->getPointerToFunction(func);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000441 TheJIT->removeModule(M);
442 delete M;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000443
444 SmallPtrSet<const void*, 2> FunctionsDeallocated;
445 for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
446 i != e; ++i) {
447 FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
448 }
449 for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
450 EXPECT_TRUE(FunctionsDeallocated.count(
451 RJMM->startFunctionBodyCalls[i].Result))
452 << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
453 }
454 EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
455 RJMM->deallocateFunctionBodyCalls.size());
456
457 SmallPtrSet<const void*, 2> ExceptionTablesDeallocated;
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000458 unsigned NumTablesDeallocated = 0;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000459 for (unsigned i = 0, e = RJMM->deallocateExceptionTableCalls.size();
460 i != e; ++i) {
461 ExceptionTablesDeallocated.insert(
462 RJMM->deallocateExceptionTableCalls[i].ET);
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000463 if (RJMM->deallocateExceptionTableCalls[i].ET != NULL) {
464 // If JITEmitDebugInfo is off, we'll "deallocate" NULL, which doesn't
465 // appear in startExceptionTableCalls.
466 NumTablesDeallocated++;
467 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000468 }
469 for (unsigned i = 0, e = RJMM->startExceptionTableCalls.size(); i != e; ++i) {
470 EXPECT_TRUE(ExceptionTablesDeallocated.count(
471 RJMM->startExceptionTableCalls[i].Result))
472 << "Function's exception table leaked: \n"
473 << RJMM->startExceptionTableCalls[i].F_dump;
474 }
475 EXPECT_EQ(RJMM->startExceptionTableCalls.size(),
Jeffrey Yasskinb069c912009-11-11 05:30:02 +0000476 NumTablesDeallocated);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000477}
Andrew Trick0005cc72011-08-26 23:39:30 +0000478#endif // !defined(__arm__)
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000479
Simon Atanasyand9389352012-05-16 19:07:55 +0000480// ARM, MIPS and PPC still emit stubs for calls since the target may be
481// too far away to call directly. This #if can probably be removed when
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000482// http://llvm.org/PR5201 is fixed.
Simon Atanasyand9389352012-05-16 19:07:55 +0000483#if !defined(__arm__) && !defined(__mips__) && \
484 !defined(__powerpc__) && !defined(__ppc__)
Eric Christopher116664a2009-11-12 03:12:18 +0000485typedef int (*FooPtr) ();
486
487TEST_F(JITTest, NoStubs) {
488 LoadAssembly("define void @bar() {"
489 "entry: "
490 "ret void"
491 "}"
492 " "
493 "define i32 @foo() {"
494 "entry:"
495 "call void @bar()"
496 "ret i32 undef"
497 "}"
498 " "
499 "define i32 @main() {"
500 "entry:"
501 "%0 = call i32 @foo()"
502 "call void @bar()"
503 "ret i32 undef"
504 "}");
505 Function *foo = M->getFunction("foo");
506 uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
507 FooPtr ptr = (FooPtr)(tmp);
508
509 (ptr)();
510
511 // We should now allocate no more stubs, we have the code to foo
512 // and the existing stub for bar.
513 int stubsBefore = RJMM->stubsAllocated;
514 Function *func = M->getFunction("main");
515 TheJIT->getPointerToFunction(func);
516
517 Function *bar = M->getFunction("bar");
518 TheJIT->getPointerToFunction(bar);
519
520 ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
521}
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000522#endif // !ARM && !PPC
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000523
524TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
525 TheJIT->DisableLazyCompilation(true);
526 LoadAssembly("define i8()* @get_foo_addr() { "
527 " ret i8()* @foo "
528 "} "
529 " "
530 "define i8 @foo() { "
531 " ret i8 42 "
532 "} ");
533 Function *F_get_foo_addr = M->getFunction("get_foo_addr");
534
535 typedef char(*fooT)();
536 fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>(
537 (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr));
538 fooT foo_addr = get_foo_addr();
539
540 // Now free get_foo_addr. This should not free the machine code for foo or
541 // any call stub returned as foo's canonical address.
542 TheJIT->freeMachineCodeForFunction(F_get_foo_addr);
543
544 // Check by calling the reported address of foo.
545 EXPECT_EQ(42, foo_addr());
546
547 // The reported address should also be the same as the result of a subsequent
548 // getPointerToFunction(foo).
549#if 0
550 // Fails until PR5126 is fixed:
551 Function *F_foo = M->getFunction("foo");
552 fooT foo = reinterpret_cast<fooT>(
553 (intptr_t)TheJIT->getPointerToFunction(F_foo));
554 EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr);
Bill Wendling0c2749f2009-11-13 21:58:54 +0000555#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000556}
Eric Christopher116664a2009-11-12 03:12:18 +0000557
Simon Atanasyand9389352012-05-16 19:07:55 +0000558// ARM and MIPS do not have an implementation
559// of replaceMachineCodeForFunction(), so recompileAndRelinkFunction
560// doesn't work.
561#if !defined(__arm__) && !defined(__mips__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000562TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
563 Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
564 GlobalValue::ExternalLinkage, "test", M);
565 BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
566 IRBuilder<> Builder(Entry);
567 Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1);
568 Builder.CreateRet(Val);
569
570 TheJIT->DisableLazyCompilation(true);
571 // Compile the function once, and make sure it works.
572 int (*OrigFPtr)() = reinterpret_cast<int(*)()>(
573 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
574 EXPECT_EQ(1, OrigFPtr());
575
576 // Now change the function to return a different value.
577 Entry->eraseFromParent();
578 BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F);
579 Builder.SetInsertPoint(NewEntry);
580 Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2);
581 Builder.CreateRet(Val);
582 // Recompile it, which should produce a new function pointer _and_ update the
583 // old one.
584 int (*NewFPtr)() = reinterpret_cast<int(*)()>(
585 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
586
587 EXPECT_EQ(2, NewFPtr())
588 << "The new pointer should call the new version of the function";
589 EXPECT_EQ(2, OrigFPtr())
590 << "The old pointer's target should now jump to the new version";
591}
Jeffrey Yasskinb9b88ea2009-12-23 00:58:02 +0000592#endif // !defined(__arm__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000593
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000594} // anonymous namespace
595// This variable is intentionally defined differently in the statically-compiled
596// program from the IR input to the JIT to assert that the JIT doesn't use its
597// definition.
598extern "C" int32_t JITTest_AvailableExternallyGlobal;
599int32_t JITTest_AvailableExternallyGlobal = 42;
600namespace {
601
602TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
603 TheJIT->DisableLazyCompilation(true);
604 LoadAssembly("@JITTest_AvailableExternallyGlobal = "
605 " available_externally global i32 7 "
606 " "
607 "define i32 @loader() { "
608 " %result = load i32* @JITTest_AvailableExternallyGlobal "
609 " ret i32 %result "
610 "} ");
611 Function *loaderIR = M->getFunction("loader");
612
613 int32_t (*loader)() = reinterpret_cast<int32_t(*)()>(
614 (intptr_t)TheJIT->getPointerToFunction(loaderIR));
615 EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
616 << " not 7 from the IR version.";
617}
618
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000619} // anonymous namespace
620// This function is intentionally defined differently in the statically-compiled
621// program from the IR input to the JIT to assert that the JIT doesn't use its
622// definition.
623extern "C" int32_t JITTest_AvailableExternallyFunction() {
624 return 42;
625}
626namespace {
627
Andrew Trick0005cc72011-08-26 23:39:30 +0000628// ARM tests disabled pending fix for PR10783.
629#if !defined(__arm__)
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000630TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
631 TheJIT->DisableLazyCompilation(true);
632 LoadAssembly("define available_externally i32 "
633 " @JITTest_AvailableExternallyFunction() { "
634 " ret i32 7 "
635 "} "
636 " "
637 "define i32 @func() { "
638 " %result = tail call i32 "
639 " @JITTest_AvailableExternallyFunction() "
640 " ret i32 %result "
641 "} ");
642 Function *funcIR = M->getFunction("func");
643
644 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
645 (intptr_t)TheJIT->getPointerToFunction(funcIR));
646 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
647 << " not 7 from the IR version.";
648}
649
Jeffrey Yasskin39c75f22010-03-04 19:45:09 +0000650TEST_F(JITTest, EscapedLazyStubStillCallable) {
651 TheJIT->DisableLazyCompilation(false);
652 LoadAssembly("define internal i32 @stubbed() { "
653 " ret i32 42 "
654 "} "
655 " "
656 "define i32()* @get_stub() { "
657 " ret i32()* @stubbed "
658 "} ");
659 typedef int32_t(*StubTy)();
660
661 // Call get_stub() to get the address of @stubbed without actually JITting it.
662 Function *get_stubIR = M->getFunction("get_stub");
663 StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>(
664 (intptr_t)TheJIT->getPointerToFunction(get_stubIR));
665 StubTy stubbed = get_stub();
666 // Now get_stubIR is the only reference to stubbed's stub.
667 get_stubIR->eraseFromParent();
668 // Now there are no references inside the JIT, but we've got a pointer outside
669 // it. The stub should be callable and return the right value.
670 EXPECT_EQ(42, stubbed());
671}
672
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000673// Converts the LLVM assembly to bitcode and returns it in a std::string. An
674// empty string indicates an error.
675std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
676 Module TempModule("TempModule", Context);
677 if (!LoadAssemblyInto(&TempModule, Assembly)) {
678 return "";
679 }
680
681 std::string Result;
682 raw_string_ostream OS(Result);
683 WriteBitcodeToFile(&TempModule, OS);
684 OS.flush();
685 return Result;
686}
687
688// Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode'
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000689// lazily. The associated Module (owned by the ExecutionEngine) is returned in
690// M. Both will be NULL on an error. Bitcode must live at least as long as the
691// ExecutionEngine.
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000692ExecutionEngine *getJITFromBitcode(
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000693 LLVMContext &Context, const std::string &Bitcode, Module *&M) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000694 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
695 MemoryBuffer *BitcodeBuffer =
Chris Lattner796e64b2010-04-05 22:49:48 +0000696 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000697 std::string errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000698 M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
699 if (M == NULL) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000700 ADD_FAILURE() << errMsg;
701 delete BitcodeBuffer;
702 return NULL;
703 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000704 ExecutionEngine *TheJIT = EngineBuilder(M)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000705 .setEngineKind(EngineKind::JIT)
706 .setErrorStr(&errMsg)
707 .create();
708 if (TheJIT == NULL) {
709 ADD_FAILURE() << errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000710 delete M;
711 M = NULL;
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000712 return NULL;
713 }
714 return TheJIT;
715}
716
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000717TEST(LazyLoadedJITTest, MaterializableAvailableExternallyFunctionIsntCompiled) {
718 LLVMContext Context;
719 const std::string Bitcode =
720 AssembleToBitcode(Context,
721 "define available_externally i32 "
722 " @JITTest_AvailableExternallyFunction() { "
723 " ret i32 7 "
724 "} "
725 " "
726 "define i32 @func() { "
727 " %result = tail call i32 "
728 " @JITTest_AvailableExternallyFunction() "
729 " ret i32 %result "
730 "} ");
731 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
732 Module *M;
733 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
734 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
735 TheJIT->DisableLazyCompilation(true);
736
737 Function *funcIR = M->getFunction("func");
738 Function *availableFunctionIR =
739 M->getFunction("JITTest_AvailableExternallyFunction");
740
741 // Double-check that the available_externally function is still unmaterialized
742 // when getPointerToFunction needs to find out if it's available_externally.
743 EXPECT_TRUE(availableFunctionIR->isMaterializable());
744
745 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
746 (intptr_t)TheJIT->getPointerToFunction(funcIR));
747 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
748 << " not 7 from the IR version.";
749}
750
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000751TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
752 LLVMContext Context;
753 const std::string Bitcode =
754 AssembleToBitcode(Context,
755 "define i32 @recur1(i32 %a) { "
756 " %zero = icmp eq i32 %a, 0 "
757 " br i1 %zero, label %done, label %notdone "
758 "done: "
759 " ret i32 3 "
760 "notdone: "
761 " %am1 = sub i32 %a, 1 "
762 " %result = call i32 @recur2(i32 %am1) "
763 " ret i32 %result "
764 "} "
765 " "
766 "define i32 @recur2(i32 %b) { "
767 " %result = call i32 @recur1(i32 %b) "
768 " ret i32 %result "
769 "} ");
770 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000771 Module *M;
772 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000773 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
774 TheJIT->DisableLazyCompilation(true);
775
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000776 Function *recur1IR = M->getFunction("recur1");
777 Function *recur2IR = M->getFunction("recur2");
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000778 EXPECT_TRUE(recur1IR->isMaterializable());
779 EXPECT_TRUE(recur2IR->isMaterializable());
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000780
781 int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>(
782 (intptr_t)TheJIT->getPointerToFunction(recur1IR));
783 EXPECT_EQ(3, recur1(4));
784}
Andrew Trick0005cc72011-08-26 23:39:30 +0000785#endif // !defined(__arm__)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000786
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000787// This code is copied from JITEventListenerTest, but it only runs once for all
788// the tests in this directory. Everything seems fine, but that's strange
789// behavior.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000790class JITEnvironment : public testing::Environment {
791 virtual void SetUp() {
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000792 // Required to create a JIT.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000793 InitializeNativeTarget();
794 }
795};
796testing::Environment* const jit_env =
797 testing::AddGlobalTestEnvironment(new JITEnvironment);
798
799}