blob: 18063d12ab57b66d486d9f435933839711718690 [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
Chandler Carruth5a88dda2012-12-04 10:23:08 +000010#include "llvm/ExecutionEngine/JIT.h"
11#include "llvm/ADT/OwningPtr.h"
12#include "llvm/ADT/SmallPtrSet.h"
13#include "llvm/Assembly/Parser.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000014#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000015#include "llvm/ExecutionEngine/JITMemoryManager.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#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 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"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000030#include "llvm/Support/TargetSelect.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000031#include "gtest/gtest.h"
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000032#include <vector>
33
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000034using namespace llvm;
35
36namespace {
37
Bob Wilsona4de3f22013-05-20 06:13:09 +000038// Tests on PowerPC and SystemZ disabled as we're running the old jit
39#if !defined(__powerpc__) && !defined(__s390__)
Ulrich Weigand1218bc42013-05-06 16:10:35 +000040
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000041Function *makeReturnGlobal(std::string Name, GlobalVariable *G, Module *M) {
Jay Foad5fdd6c82011-07-12 14:06:48 +000042 std::vector<Type*> params;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000043 FunctionType *FTy = FunctionType::get(G->getType()->getElementType(),
Dan Gohmanc6f40b62009-07-11 13:56:14 +000044 params, false);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000045 Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, Name, M);
Owen Anderson1d0be152009-08-13 21:58:54 +000046 BasicBlock *Entry = BasicBlock::Create(M->getContext(), "entry", F);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000047 IRBuilder<> builder(Entry);
48 Value *Load = builder.CreateLoad(G);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000049 Type *GTy = G->getType()->getElementType();
Owen Andersoneed707b2009-07-24 23:12:02 +000050 Value *Add = builder.CreateAdd(Load, ConstantInt::get(GTy, 1LL));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +000051 builder.CreateStore(Add, G);
52 builder.CreateRet(Add);
53 return F;
54}
55
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000056std::string DumpFunction(const Function *F) {
57 std::string Result;
58 raw_string_ostream(Result) << "" << *F;
59 return Result;
60}
61
62class RecordingJITMemoryManager : public JITMemoryManager {
63 const OwningPtr<JITMemoryManager> Base;
64public:
65 RecordingJITMemoryManager()
66 : Base(JITMemoryManager::CreateDefaultMemManager()) {
Eric Christopher116664a2009-11-12 03:12:18 +000067 stubsAllocated = 0;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000068 }
Danil Malyshev30b9e322012-03-28 21:46:36 +000069 virtual void *getPointerToNamedFunction(const std::string &Name,
70 bool AbortOnFailure = true) {
71 return Base->getPointerToNamedFunction(Name, AbortOnFailure);
72 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000073
74 virtual void setMemoryWritable() { Base->setMemoryWritable(); }
75 virtual void setMemoryExecutable() { Base->setMemoryExecutable(); }
76 virtual void setPoisonMemory(bool poison) { Base->setPoisonMemory(poison); }
77 virtual void AllocateGOT() { Base->AllocateGOT(); }
78 virtual uint8_t *getGOTBase() const { return Base->getGOTBase(); }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +000079 struct StartFunctionBodyCall {
80 StartFunctionBodyCall(uint8_t *Result, const Function *F,
81 uintptr_t ActualSize, uintptr_t ActualSizeResult)
82 : Result(Result), F(F), F_dump(DumpFunction(F)),
83 ActualSize(ActualSize), ActualSizeResult(ActualSizeResult) {}
84 uint8_t *Result;
85 const Function *F;
86 std::string F_dump;
87 uintptr_t ActualSize;
88 uintptr_t ActualSizeResult;
89 };
90 std::vector<StartFunctionBodyCall> startFunctionBodyCalls;
91 virtual uint8_t *startFunctionBody(const Function *F,
92 uintptr_t &ActualSize) {
93 uintptr_t InitialActualSize = ActualSize;
94 uint8_t *Result = Base->startFunctionBody(F, ActualSize);
95 startFunctionBodyCalls.push_back(
96 StartFunctionBodyCall(Result, F, InitialActualSize, ActualSize));
97 return Result;
98 }
Eric Christopher116664a2009-11-12 03:12:18 +000099 int stubsAllocated;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000100 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
101 unsigned Alignment) {
Eric Christopher116664a2009-11-12 03:12:18 +0000102 stubsAllocated++;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000103 return Base->allocateStub(F, StubSize, Alignment);
104 }
105 struct EndFunctionBodyCall {
106 EndFunctionBodyCall(const Function *F, uint8_t *FunctionStart,
107 uint8_t *FunctionEnd)
108 : F(F), F_dump(DumpFunction(F)),
109 FunctionStart(FunctionStart), FunctionEnd(FunctionEnd) {}
110 const Function *F;
111 std::string F_dump;
112 uint8_t *FunctionStart;
113 uint8_t *FunctionEnd;
114 };
115 std::vector<EndFunctionBodyCall> endFunctionBodyCalls;
116 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
117 uint8_t *FunctionEnd) {
118 endFunctionBodyCalls.push_back(
119 EndFunctionBodyCall(F, FunctionStart, FunctionEnd));
120 Base->endFunctionBody(F, FunctionStart, FunctionEnd);
121 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000122 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Andrew Kaylor53608a32012-11-15 23:50:01 +0000123 unsigned SectionID, bool IsReadOnly) {
124 return Base->allocateDataSection(Size, Alignment, SectionID, IsReadOnly);
Jim Grosbach61425c02012-01-16 22:26:39 +0000125 }
126 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
127 unsigned SectionID) {
128 return Base->allocateCodeSection(Size, Alignment, SectionID);
129 }
David Tweedabb38fe2013-05-17 10:01:46 +0000130 virtual bool finalizeMemory(std::string *ErrMsg) { return false; }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000131 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
132 return Base->allocateSpace(Size, Alignment);
133 }
134 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
135 return Base->allocateGlobal(Size, Alignment);
136 }
137 struct DeallocateFunctionBodyCall {
138 DeallocateFunctionBodyCall(const void *Body) : Body(Body) {}
139 const void *Body;
140 };
141 std::vector<DeallocateFunctionBodyCall> deallocateFunctionBodyCalls;
142 virtual void deallocateFunctionBody(void *Body) {
143 deallocateFunctionBodyCalls.push_back(DeallocateFunctionBodyCall(Body));
144 Base->deallocateFunctionBody(Body);
145 }
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000146};
147
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000148bool LoadAssemblyInto(Module *M, const char *assembly) {
149 SMDiagnostic Error;
150 bool success =
151 NULL != ParseAssemblyString(assembly, M, Error, M->getContext());
152 std::string errMsg;
153 raw_string_ostream os(errMsg);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000154 Error.print("", os);
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000155 EXPECT_TRUE(success) << os.str();
156 return success;
157}
158
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000159class JITTest : public testing::Test {
160 protected:
Eli Benderskydced3cd2013-01-11 16:33:30 +0000161 virtual RecordingJITMemoryManager *createMemoryManager() {
162 return new RecordingJITMemoryManager;
163 }
164
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000165 virtual void SetUp() {
166 M = new Module("<main>", Context);
Eli Benderskydced3cd2013-01-11 16:33:30 +0000167 RJMM = createMemoryManager();
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000168 RJMM->setPoisonMemory(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000169 std::string Error;
Eli Benderskydced3cd2013-01-11 16:33:30 +0000170 TargetOptions Options;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000171 TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000172 .setJITMemoryManager(RJMM)
Eli Benderskydced3cd2013-01-11 16:33:30 +0000173 .setErrorStr(&Error)
174 .setTargetOptions(Options).create());
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000175 ASSERT_TRUE(TheJIT.get() != NULL) << Error;
176 }
177
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000178 void LoadAssembly(const char *assembly) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000179 LoadAssemblyInto(M, assembly);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000180 }
181
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000182 LLVMContext Context;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000183 Module *M; // Owned by ExecutionEngine.
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000184 RecordingJITMemoryManager *RJMM;
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000185 OwningPtr<ExecutionEngine> TheJIT;
186};
187
Bob Wilsona4de3f22013-05-20 06:13:09 +0000188// Tests on ARM disabled as we're running the old jit
189#if !defined(__arm__)
190
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000191// Regression test for a bug. The JIT used to allocate globals inside the same
192// memory block used for the function, and when the function code was freed,
193// the global was left in the same place. This test allocates a function
194// that uses and global, deallocates it, and then makes sure that the global
195// stays alive after that.
196TEST(JIT, GlobalInFunction) {
197 LLVMContext context;
198 Module *M = new Module("<main>", context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000199
200 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
201 // Tell the memory manager to poison freed memory so that accessing freed
202 // memory is more easily tested.
203 MemMgr->setPoisonMemory(true);
204 std::string Error;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000205 OwningPtr<ExecutionEngine> JIT(EngineBuilder(M)
Daniel Dunbard370d772009-07-18 06:08:49 +0000206 .setEngineKind(EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000207 .setErrorStr(&Error)
208 .setJITMemoryManager(MemMgr)
209 // The next line enables the fix:
210 .setAllocateGVsWithCode(false)
211 .create());
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000212 ASSERT_EQ(Error, "");
213
214 // Create a global variable.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000215 Type *GTy = Type::getInt32Ty(context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000216 GlobalVariable *G = new GlobalVariable(
217 *M,
218 GTy,
219 false, // Not constant.
220 GlobalValue::InternalLinkage,
Benjamin Kramerfeba7562009-07-31 20:56:31 +0000221 Constant::getNullValue(GTy),
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000222 "myglobal");
223
224 // Make a function that points to a global.
225 Function *F1 = makeReturnGlobal("F1", G, M);
226
227 // Get the pointer to the native code to force it to JIT the function and
228 // allocate space for the global.
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000229 void (*F1Ptr)() =
230 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000231
232 // Since F1 was codegen'd, a pointer to G should be available.
233 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
234 ASSERT_NE((int32_t*)NULL, GPtr);
235 EXPECT_EQ(0, *GPtr);
236
237 // F1() should increment G.
238 F1Ptr();
239 EXPECT_EQ(1, *GPtr);
240
241 // Make a second function identical to the first, referring to the same
242 // global.
243 Function *F2 = makeReturnGlobal("F2", G, M);
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000244 void (*F2Ptr)() =
245 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000246
247 // F2() should increment G.
248 F2Ptr();
249 EXPECT_EQ(2, *GPtr);
250
251 // Deallocate F1.
252 JIT->freeMachineCodeForFunction(F1);
253
254 // F2() should *still* increment G.
255 F2Ptr();
256 EXPECT_EQ(3, *GPtr);
257}
258
Bob Wilsona4de3f22013-05-20 06:13:09 +0000259#endif // !defined(__arm__)
260
261// ARM tests disabled pending fix for PR10783.
262#if !defined(__arm__)
263
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000264int PlusOne(int arg) {
265 return arg + 1;
266}
267
268TEST_F(JITTest, FarCallToKnownFunction) {
269 // x86-64 can only make direct calls to functions within 32 bits of
270 // the current PC. To call anything farther away, we have to load
271 // the address into a register and call through the register. The
272 // current JIT does this by allocating a stub for any far call.
273 // There was a bug in which the JIT tried to emit a direct call when
274 // the target was already in the JIT's global mappings and lazy
275 // compilation was disabled.
276
277 Function *KnownFunction = Function::Create(
278 TypeBuilder<int(int), false>::get(Context),
279 GlobalValue::ExternalLinkage, "known", M);
280 TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
281
282 // int test() { return known(7); }
283 Function *TestFunction = Function::Create(
284 TypeBuilder<int(), false>::get(Context),
285 GlobalValue::ExternalLinkage, "test", M);
286 BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
287 IRBuilder<> Builder(Entry);
288 Value *result = Builder.CreateCall(
289 KnownFunction,
290 ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
291 Builder.CreateRet(result);
292
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000293 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000294 int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
295 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
296 // This used to crash in trying to call PlusOne().
297 EXPECT_EQ(8, TestFunctionPtr());
298}
299
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000300// Test a function C which calls A and B which call each other.
301TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000302 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000303
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000304 FunctionType *Func1Ty =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000305 cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
Jay Foad5fdd6c82011-07-12 14:06:48 +0000306 std::vector<Type*> arg_types;
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000307 arg_types.push_back(Type::getInt1Ty(Context));
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000308 FunctionType *FuncTy = FunctionType::get(
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000309 Type::getVoidTy(Context), arg_types, false);
310 Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
311 "func1", M);
312 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
313 "func2", M);
314 Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
315 "func3", M);
316 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
317 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
318 BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
319 BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
320 BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
321 BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
322 BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
323
324 // Make Func1 call Func2(0) and Func3(0).
325 IRBuilder<> Builder(Block1);
326 Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
327 Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
328 Builder.CreateRetVoid();
329
330 // void Func2(bool b) { if (b) { Func3(false); return; } return; }
331 Builder.SetInsertPoint(Block2);
332 Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
333 Builder.SetInsertPoint(True2);
334 Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
335 Builder.CreateRetVoid();
336 Builder.SetInsertPoint(False2);
337 Builder.CreateRetVoid();
338
339 // void Func3(bool b) { if (b) { Func2(false); return; } return; }
340 Builder.SetInsertPoint(Block3);
341 Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
342 Builder.SetInsertPoint(True3);
343 Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
344 Builder.CreateRetVoid();
345 Builder.SetInsertPoint(False3);
346 Builder.CreateRetVoid();
347
348 // Compile the function to native code
349 void (*F1Ptr)() =
350 reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
351
352 F1Ptr();
353}
354
355// Regression test for PR5162. This used to trigger an AssertingVH inside the
356// JIT's Function to stub mapping.
357TEST_F(JITTest, NonLazyLeaksNoStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000358 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000359
360 // Create two functions with a single basic block each.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000361 FunctionType *FuncTy =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000362 cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
363 Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
364 "func1", M);
365 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
366 "func2", M);
367 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
368 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
369
370 // The first function calls the second and returns the result
371 IRBuilder<> Builder(Block1);
372 Value *Result = Builder.CreateCall(Func2);
373 Builder.CreateRet(Result);
374
375 // The second function just returns a constant
376 Builder.SetInsertPoint(Block2);
377 Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
378
379 // Compile the function to native code
380 (void)TheJIT->getPointerToFunction(Func1);
381
382 // Free the JIT state for the functions
383 TheJIT->freeMachineCodeForFunction(Func1);
384 TheJIT->freeMachineCodeForFunction(Func2);
385
386 // Delete the first function (and show that is has no users)
387 EXPECT_EQ(Func1->getNumUses(), 0u);
388 Func1->eraseFromParent();
389
390 // Delete the second function (and show that it has no users - it had one,
391 // func1 but that's gone now)
392 EXPECT_EQ(Func2->getNumUses(), 0u);
393 Func2->eraseFromParent();
394}
395
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000396TEST_F(JITTest, ModuleDeletion) {
Jeffrey Yasskinb2352242009-10-28 00:28:31 +0000397 TheJIT->DisableLazyCompilation(false);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000398 LoadAssembly("define void @main() { "
399 " call i32 @computeVal() "
400 " ret void "
401 "} "
402 " "
403 "define internal i32 @computeVal() { "
404 " ret i32 0 "
405 "} ");
406 Function *func = M->getFunction("main");
407 TheJIT->getPointerToFunction(func);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000408 TheJIT->removeModule(M);
409 delete M;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000410
411 SmallPtrSet<const void*, 2> FunctionsDeallocated;
412 for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
413 i != e; ++i) {
414 FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
415 }
416 for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
417 EXPECT_TRUE(FunctionsDeallocated.count(
418 RJMM->startFunctionBodyCalls[i].Result))
419 << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
420 }
421 EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
422 RJMM->deallocateFunctionBodyCalls.size());
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000423}
Bob Wilsona4de3f22013-05-20 06:13:09 +0000424#endif // !defined(__arm__)
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000425
Simon Atanasyand9389352012-05-16 19:07:55 +0000426// ARM, MIPS and PPC still emit stubs for calls since the target may be
427// too far away to call directly. This #if can probably be removed when
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000428// http://llvm.org/PR5201 is fixed.
Simon Atanasyand9389352012-05-16 19:07:55 +0000429#if !defined(__arm__) && !defined(__mips__) && \
430 !defined(__powerpc__) && !defined(__ppc__)
Eric Christopher116664a2009-11-12 03:12:18 +0000431typedef int (*FooPtr) ();
432
433TEST_F(JITTest, NoStubs) {
434 LoadAssembly("define void @bar() {"
435 "entry: "
436 "ret void"
437 "}"
438 " "
439 "define i32 @foo() {"
440 "entry:"
441 "call void @bar()"
442 "ret i32 undef"
443 "}"
444 " "
445 "define i32 @main() {"
446 "entry:"
447 "%0 = call i32 @foo()"
448 "call void @bar()"
449 "ret i32 undef"
450 "}");
451 Function *foo = M->getFunction("foo");
452 uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
453 FooPtr ptr = (FooPtr)(tmp);
454
455 (ptr)();
456
457 // We should now allocate no more stubs, we have the code to foo
458 // and the existing stub for bar.
459 int stubsBefore = RJMM->stubsAllocated;
460 Function *func = M->getFunction("main");
461 TheJIT->getPointerToFunction(func);
462
463 Function *bar = M->getFunction("bar");
464 TheJIT->getPointerToFunction(bar);
465
466 ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
467}
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000468#endif // !ARM && !PPC
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000469
Bob Wilsona4de3f22013-05-20 06:13:09 +0000470// Tests on ARM disabled as we're running the old jit
471#if !defined(__arm__)
472
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000473TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
474 TheJIT->DisableLazyCompilation(true);
475 LoadAssembly("define i8()* @get_foo_addr() { "
476 " ret i8()* @foo "
477 "} "
478 " "
479 "define i8 @foo() { "
480 " ret i8 42 "
481 "} ");
482 Function *F_get_foo_addr = M->getFunction("get_foo_addr");
483
484 typedef char(*fooT)();
485 fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>(
486 (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr));
487 fooT foo_addr = get_foo_addr();
488
489 // Now free get_foo_addr. This should not free the machine code for foo or
490 // any call stub returned as foo's canonical address.
491 TheJIT->freeMachineCodeForFunction(F_get_foo_addr);
492
493 // Check by calling the reported address of foo.
494 EXPECT_EQ(42, foo_addr());
495
496 // The reported address should also be the same as the result of a subsequent
497 // getPointerToFunction(foo).
498#if 0
499 // Fails until PR5126 is fixed:
500 Function *F_foo = M->getFunction("foo");
501 fooT foo = reinterpret_cast<fooT>(
502 (intptr_t)TheJIT->getPointerToFunction(F_foo));
503 EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr);
Bill Wendling0c2749f2009-11-13 21:58:54 +0000504#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000505}
Eric Christopher116664a2009-11-12 03:12:18 +0000506
Bob Wilsona4de3f22013-05-20 06:13:09 +0000507#endif //!defined(__arm__)
508
509// Tests on ARM disabled as we're running the old jit. In addition,
Ulrich Weigand1218bc42013-05-06 16:10:35 +0000510// ARM does not have an implementation of replaceMachineCodeForFunction(),
511// so recompileAndRelinkFunction doesn't work.
512#if !defined(__arm__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000513TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
514 Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
515 GlobalValue::ExternalLinkage, "test", M);
516 BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
517 IRBuilder<> Builder(Entry);
518 Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1);
519 Builder.CreateRet(Val);
520
521 TheJIT->DisableLazyCompilation(true);
522 // Compile the function once, and make sure it works.
523 int (*OrigFPtr)() = reinterpret_cast<int(*)()>(
524 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
525 EXPECT_EQ(1, OrigFPtr());
526
527 // Now change the function to return a different value.
528 Entry->eraseFromParent();
529 BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F);
530 Builder.SetInsertPoint(NewEntry);
531 Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2);
532 Builder.CreateRet(Val);
533 // Recompile it, which should produce a new function pointer _and_ update the
534 // old one.
535 int (*NewFPtr)() = reinterpret_cast<int(*)()>(
536 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
537
538 EXPECT_EQ(2, NewFPtr())
539 << "The new pointer should call the new version of the function";
540 EXPECT_EQ(2, OrigFPtr())
541 << "The old pointer's target should now jump to the new version";
542}
Ulrich Weigand1218bc42013-05-06 16:10:35 +0000543#endif // !defined(__arm__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000544
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000545} // anonymous namespace
546// This variable is intentionally defined differently in the statically-compiled
547// program from the IR input to the JIT to assert that the JIT doesn't use its
548// definition.
549extern "C" int32_t JITTest_AvailableExternallyGlobal;
Bill Wendling80668fb2012-10-17 08:08:06 +0000550int32_t JITTest_AvailableExternallyGlobal LLVM_ATTRIBUTE_USED = 42;
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000551namespace {
552
Bob Wilsona4de3f22013-05-20 06:13:09 +0000553// Tests on ARM disabled as we're running the old jit
554#if !defined(__arm__)
555
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000556TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
557 TheJIT->DisableLazyCompilation(true);
558 LoadAssembly("@JITTest_AvailableExternallyGlobal = "
559 " available_externally global i32 7 "
560 " "
561 "define i32 @loader() { "
562 " %result = load i32* @JITTest_AvailableExternallyGlobal "
563 " ret i32 %result "
564 "} ");
565 Function *loaderIR = M->getFunction("loader");
566
567 int32_t (*loader)() = reinterpret_cast<int32_t(*)()>(
568 (intptr_t)TheJIT->getPointerToFunction(loaderIR));
569 EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
570 << " not 7 from the IR version.";
571}
Bob Wilsona4de3f22013-05-20 06:13:09 +0000572#endif //!defined(__arm__)
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000573} // anonymous namespace
574// This function is intentionally defined differently in the statically-compiled
575// program from the IR input to the JIT to assert that the JIT doesn't use its
576// definition.
NAKAMURA Takumi8ab27a32012-10-12 08:09:29 +0000577extern "C" int32_t JITTest_AvailableExternallyFunction() LLVM_ATTRIBUTE_USED;
NAKAMURA Takumi97eb05b2012-10-12 01:34:07 +0000578extern "C" int32_t JITTest_AvailableExternallyFunction() {
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000579 return 42;
580}
581namespace {
582
Bob Wilsona4de3f22013-05-20 06:13:09 +0000583// ARM tests disabled pending fix for PR10783.
584#if !defined(__arm__)
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000585TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
586 TheJIT->DisableLazyCompilation(true);
587 LoadAssembly("define available_externally i32 "
588 " @JITTest_AvailableExternallyFunction() { "
589 " ret i32 7 "
590 "} "
591 " "
592 "define i32 @func() { "
593 " %result = tail call i32 "
594 " @JITTest_AvailableExternallyFunction() "
595 " ret i32 %result "
596 "} ");
597 Function *funcIR = M->getFunction("func");
598
599 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
600 (intptr_t)TheJIT->getPointerToFunction(funcIR));
601 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
602 << " not 7 from the IR version.";
603}
604
Jeffrey Yasskin39c75f22010-03-04 19:45:09 +0000605TEST_F(JITTest, EscapedLazyStubStillCallable) {
606 TheJIT->DisableLazyCompilation(false);
607 LoadAssembly("define internal i32 @stubbed() { "
608 " ret i32 42 "
609 "} "
610 " "
611 "define i32()* @get_stub() { "
612 " ret i32()* @stubbed "
613 "} ");
614 typedef int32_t(*StubTy)();
615
616 // Call get_stub() to get the address of @stubbed without actually JITting it.
617 Function *get_stubIR = M->getFunction("get_stub");
618 StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>(
619 (intptr_t)TheJIT->getPointerToFunction(get_stubIR));
620 StubTy stubbed = get_stub();
621 // Now get_stubIR is the only reference to stubbed's stub.
622 get_stubIR->eraseFromParent();
623 // Now there are no references inside the JIT, but we've got a pointer outside
624 // it. The stub should be callable and return the right value.
625 EXPECT_EQ(42, stubbed());
626}
627
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000628// Converts the LLVM assembly to bitcode and returns it in a std::string. An
629// empty string indicates an error.
630std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
631 Module TempModule("TempModule", Context);
632 if (!LoadAssemblyInto(&TempModule, Assembly)) {
633 return "";
634 }
635
636 std::string Result;
637 raw_string_ostream OS(Result);
638 WriteBitcodeToFile(&TempModule, OS);
639 OS.flush();
640 return Result;
641}
642
643// Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode'
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000644// lazily. The associated Module (owned by the ExecutionEngine) is returned in
645// M. Both will be NULL on an error. Bitcode must live at least as long as the
646// ExecutionEngine.
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000647ExecutionEngine *getJITFromBitcode(
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000648 LLVMContext &Context, const std::string &Bitcode, Module *&M) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000649 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
650 MemoryBuffer *BitcodeBuffer =
Chris Lattner796e64b2010-04-05 22:49:48 +0000651 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000652 std::string errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000653 M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
654 if (M == NULL) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000655 ADD_FAILURE() << errMsg;
656 delete BitcodeBuffer;
657 return NULL;
658 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000659 ExecutionEngine *TheJIT = EngineBuilder(M)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000660 .setEngineKind(EngineKind::JIT)
661 .setErrorStr(&errMsg)
662 .create();
663 if (TheJIT == NULL) {
664 ADD_FAILURE() << errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000665 delete M;
666 M = NULL;
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000667 return NULL;
668 }
669 return TheJIT;
670}
671
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000672TEST(LazyLoadedJITTest, MaterializableAvailableExternallyFunctionIsntCompiled) {
673 LLVMContext Context;
674 const std::string Bitcode =
675 AssembleToBitcode(Context,
676 "define available_externally i32 "
677 " @JITTest_AvailableExternallyFunction() { "
678 " ret i32 7 "
679 "} "
680 " "
681 "define i32 @func() { "
682 " %result = tail call i32 "
683 " @JITTest_AvailableExternallyFunction() "
684 " ret i32 %result "
685 "} ");
686 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
687 Module *M;
688 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
689 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
690 TheJIT->DisableLazyCompilation(true);
691
692 Function *funcIR = M->getFunction("func");
693 Function *availableFunctionIR =
694 M->getFunction("JITTest_AvailableExternallyFunction");
695
696 // Double-check that the available_externally function is still unmaterialized
697 // when getPointerToFunction needs to find out if it's available_externally.
698 EXPECT_TRUE(availableFunctionIR->isMaterializable());
699
700 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
701 (intptr_t)TheJIT->getPointerToFunction(funcIR));
702 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
703 << " not 7 from the IR version.";
704}
705
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000706TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
707 LLVMContext Context;
708 const std::string Bitcode =
709 AssembleToBitcode(Context,
710 "define i32 @recur1(i32 %a) { "
711 " %zero = icmp eq i32 %a, 0 "
712 " br i1 %zero, label %done, label %notdone "
713 "done: "
714 " ret i32 3 "
715 "notdone: "
716 " %am1 = sub i32 %a, 1 "
717 " %result = call i32 @recur2(i32 %am1) "
718 " ret i32 %result "
719 "} "
720 " "
721 "define i32 @recur2(i32 %b) { "
722 " %result = call i32 @recur1(i32 %b) "
723 " ret i32 %result "
724 "} ");
725 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000726 Module *M;
727 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000728 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
729 TheJIT->DisableLazyCompilation(true);
730
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000731 Function *recur1IR = M->getFunction("recur1");
732 Function *recur2IR = M->getFunction("recur2");
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000733 EXPECT_TRUE(recur1IR->isMaterializable());
734 EXPECT_TRUE(recur2IR->isMaterializable());
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000735
736 int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>(
737 (intptr_t)TheJIT->getPointerToFunction(recur1IR));
738 EXPECT_EQ(3, recur1(4));
739}
Bob Wilsona4de3f22013-05-20 06:13:09 +0000740#endif // !defined(__arm__)
741#endif // !defined(__powerpc__) && !defined(__s390__)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000742
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000743// This code is copied from JITEventListenerTest, but it only runs once for all
744// the tests in this directory. Everything seems fine, but that's strange
745// behavior.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000746class JITEnvironment : public testing::Environment {
747 virtual void SetUp() {
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000748 // Required to create a JIT.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000749 InitializeNativeTarget();
750 }
751};
752testing::Environment* const jit_env =
753 testing::AddGlobalTestEnvironment(new JITEnvironment);
754
755}