blob: cf995aac7a1a06e8e012400c23443e0dfc64c2c2 [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
Ulrich Weigand72072852013-05-06 16:21:50 +000038// Tests on ARM, PowerPC and SystemZ disabled as we're running the old jit
39#if !defined(__arm__) && !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 }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000130 virtual bool applyPermissions(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
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000188// Regression test for a bug. The JIT used to allocate globals inside the same
189// memory block used for the function, and when the function code was freed,
190// the global was left in the same place. This test allocates a function
191// that uses and global, deallocates it, and then makes sure that the global
192// stays alive after that.
193TEST(JIT, GlobalInFunction) {
194 LLVMContext context;
195 Module *M = new Module("<main>", context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000196
197 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
198 // Tell the memory manager to poison freed memory so that accessing freed
199 // memory is more easily tested.
200 MemMgr->setPoisonMemory(true);
201 std::string Error;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000202 OwningPtr<ExecutionEngine> JIT(EngineBuilder(M)
Daniel Dunbard370d772009-07-18 06:08:49 +0000203 .setEngineKind(EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000204 .setErrorStr(&Error)
205 .setJITMemoryManager(MemMgr)
206 // The next line enables the fix:
207 .setAllocateGVsWithCode(false)
208 .create());
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000209 ASSERT_EQ(Error, "");
210
211 // Create a global variable.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000212 Type *GTy = Type::getInt32Ty(context);
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000213 GlobalVariable *G = new GlobalVariable(
214 *M,
215 GTy,
216 false, // Not constant.
217 GlobalValue::InternalLinkage,
Benjamin Kramerfeba7562009-07-31 20:56:31 +0000218 Constant::getNullValue(GTy),
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000219 "myglobal");
220
221 // Make a function that points to a global.
222 Function *F1 = makeReturnGlobal("F1", G, M);
223
224 // Get the pointer to the native code to force it to JIT the function and
225 // allocate space for the global.
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000226 void (*F1Ptr)() =
227 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F1));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000228
229 // Since F1 was codegen'd, a pointer to G should be available.
230 int32_t *GPtr = (int32_t*)JIT->getPointerToGlobalIfAvailable(G);
231 ASSERT_NE((int32_t*)NULL, GPtr);
232 EXPECT_EQ(0, *GPtr);
233
234 // F1() should increment G.
235 F1Ptr();
236 EXPECT_EQ(1, *GPtr);
237
238 // Make a second function identical to the first, referring to the same
239 // global.
240 Function *F2 = makeReturnGlobal("F2", G, M);
Jeffrey Yasskin0f2ba782009-10-06 19:06:16 +0000241 void (*F2Ptr)() =
242 reinterpret_cast<void(*)()>((intptr_t)JIT->getPointerToFunction(F2));
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000243
244 // F2() should increment G.
245 F2Ptr();
246 EXPECT_EQ(2, *GPtr);
247
248 // Deallocate F1.
249 JIT->freeMachineCodeForFunction(F1);
250
251 // F2() should *still* increment G.
252 F2Ptr();
253 EXPECT_EQ(3, *GPtr);
254}
255
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000256int PlusOne(int arg) {
257 return arg + 1;
258}
259
260TEST_F(JITTest, FarCallToKnownFunction) {
261 // x86-64 can only make direct calls to functions within 32 bits of
262 // the current PC. To call anything farther away, we have to load
263 // the address into a register and call through the register. The
264 // current JIT does this by allocating a stub for any far call.
265 // There was a bug in which the JIT tried to emit a direct call when
266 // the target was already in the JIT's global mappings and lazy
267 // compilation was disabled.
268
269 Function *KnownFunction = Function::Create(
270 TypeBuilder<int(int), false>::get(Context),
271 GlobalValue::ExternalLinkage, "known", M);
272 TheJIT->addGlobalMapping(KnownFunction, (void*)(intptr_t)PlusOne);
273
274 // int test() { return known(7); }
275 Function *TestFunction = Function::Create(
276 TypeBuilder<int(), false>::get(Context),
277 GlobalValue::ExternalLinkage, "test", M);
278 BasicBlock *Entry = BasicBlock::Create(Context, "entry", TestFunction);
279 IRBuilder<> Builder(Entry);
280 Value *result = Builder.CreateCall(
281 KnownFunction,
282 ConstantInt::get(TypeBuilder<int, false>::get(Context), 7));
283 Builder.CreateRet(result);
284
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000285 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskinea5ed002009-10-06 00:35:55 +0000286 int (*TestFunctionPtr)() = reinterpret_cast<int(*)()>(
287 (intptr_t)TheJIT->getPointerToFunction(TestFunction));
288 // This used to crash in trying to call PlusOne().
289 EXPECT_EQ(8, TestFunctionPtr());
290}
291
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000292// Test a function C which calls A and B which call each other.
293TEST_F(JITTest, NonLazyCompilationStillNeedsStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000294 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000295
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000296 FunctionType *Func1Ty =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000297 cast<FunctionType>(TypeBuilder<void(void), false>::get(Context));
Jay Foad5fdd6c82011-07-12 14:06:48 +0000298 std::vector<Type*> arg_types;
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000299 arg_types.push_back(Type::getInt1Ty(Context));
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000300 FunctionType *FuncTy = FunctionType::get(
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000301 Type::getVoidTy(Context), arg_types, false);
302 Function *Func1 = Function::Create(Func1Ty, Function::ExternalLinkage,
303 "func1", M);
304 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
305 "func2", M);
306 Function *Func3 = Function::Create(FuncTy, Function::InternalLinkage,
307 "func3", M);
308 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
309 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
310 BasicBlock *True2 = BasicBlock::Create(Context, "cond_true", Func2);
311 BasicBlock *False2 = BasicBlock::Create(Context, "cond_false", Func2);
312 BasicBlock *Block3 = BasicBlock::Create(Context, "block3", Func3);
313 BasicBlock *True3 = BasicBlock::Create(Context, "cond_true", Func3);
314 BasicBlock *False3 = BasicBlock::Create(Context, "cond_false", Func3);
315
316 // Make Func1 call Func2(0) and Func3(0).
317 IRBuilder<> Builder(Block1);
318 Builder.CreateCall(Func2, ConstantInt::getTrue(Context));
319 Builder.CreateCall(Func3, ConstantInt::getTrue(Context));
320 Builder.CreateRetVoid();
321
322 // void Func2(bool b) { if (b) { Func3(false); return; } return; }
323 Builder.SetInsertPoint(Block2);
324 Builder.CreateCondBr(Func2->arg_begin(), True2, False2);
325 Builder.SetInsertPoint(True2);
326 Builder.CreateCall(Func3, ConstantInt::getFalse(Context));
327 Builder.CreateRetVoid();
328 Builder.SetInsertPoint(False2);
329 Builder.CreateRetVoid();
330
331 // void Func3(bool b) { if (b) { Func2(false); return; } return; }
332 Builder.SetInsertPoint(Block3);
333 Builder.CreateCondBr(Func3->arg_begin(), True3, False3);
334 Builder.SetInsertPoint(True3);
335 Builder.CreateCall(Func2, ConstantInt::getFalse(Context));
336 Builder.CreateRetVoid();
337 Builder.SetInsertPoint(False3);
338 Builder.CreateRetVoid();
339
340 // Compile the function to native code
341 void (*F1Ptr)() =
342 reinterpret_cast<void(*)()>((intptr_t)TheJIT->getPointerToFunction(Func1));
343
344 F1Ptr();
345}
346
347// Regression test for PR5162. This used to trigger an AssertingVH inside the
348// JIT's Function to stub mapping.
349TEST_F(JITTest, NonLazyLeaksNoStubs) {
Jeffrey Yasskin18fec732009-10-27 22:39:42 +0000350 TheJIT->DisableLazyCompilation(true);
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000351
352 // Create two functions with a single basic block each.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000353 FunctionType *FuncTy =
Jeffrey Yasskine5f87982009-10-13 21:32:57 +0000354 cast<FunctionType>(TypeBuilder<int(), false>::get(Context));
355 Function *Func1 = Function::Create(FuncTy, Function::ExternalLinkage,
356 "func1", M);
357 Function *Func2 = Function::Create(FuncTy, Function::InternalLinkage,
358 "func2", M);
359 BasicBlock *Block1 = BasicBlock::Create(Context, "block1", Func1);
360 BasicBlock *Block2 = BasicBlock::Create(Context, "block2", Func2);
361
362 // The first function calls the second and returns the result
363 IRBuilder<> Builder(Block1);
364 Value *Result = Builder.CreateCall(Func2);
365 Builder.CreateRet(Result);
366
367 // The second function just returns a constant
368 Builder.SetInsertPoint(Block2);
369 Builder.CreateRet(ConstantInt::get(TypeBuilder<int, false>::get(Context),42));
370
371 // Compile the function to native code
372 (void)TheJIT->getPointerToFunction(Func1);
373
374 // Free the JIT state for the functions
375 TheJIT->freeMachineCodeForFunction(Func1);
376 TheJIT->freeMachineCodeForFunction(Func2);
377
378 // Delete the first function (and show that is has no users)
379 EXPECT_EQ(Func1->getNumUses(), 0u);
380 Func1->eraseFromParent();
381
382 // Delete the second function (and show that it has no users - it had one,
383 // func1 but that's gone now)
384 EXPECT_EQ(Func2->getNumUses(), 0u);
385 Func2->eraseFromParent();
386}
387
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000388TEST_F(JITTest, ModuleDeletion) {
Jeffrey Yasskinb2352242009-10-28 00:28:31 +0000389 TheJIT->DisableLazyCompilation(false);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000390 LoadAssembly("define void @main() { "
391 " call i32 @computeVal() "
392 " ret void "
393 "} "
394 " "
395 "define internal i32 @computeVal() { "
396 " ret i32 0 "
397 "} ");
398 Function *func = M->getFunction("main");
399 TheJIT->getPointerToFunction(func);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000400 TheJIT->removeModule(M);
401 delete M;
Jeffrey Yasskin7a9034c2009-10-27 00:03:05 +0000402
403 SmallPtrSet<const void*, 2> FunctionsDeallocated;
404 for (unsigned i = 0, e = RJMM->deallocateFunctionBodyCalls.size();
405 i != e; ++i) {
406 FunctionsDeallocated.insert(RJMM->deallocateFunctionBodyCalls[i].Body);
407 }
408 for (unsigned i = 0, e = RJMM->startFunctionBodyCalls.size(); i != e; ++i) {
409 EXPECT_TRUE(FunctionsDeallocated.count(
410 RJMM->startFunctionBodyCalls[i].Result))
411 << "Function leaked: \n" << RJMM->startFunctionBodyCalls[i].F_dump;
412 }
413 EXPECT_EQ(RJMM->startFunctionBodyCalls.size(),
414 RJMM->deallocateFunctionBodyCalls.size());
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000415}
416
Simon Atanasyand9389352012-05-16 19:07:55 +0000417// ARM, MIPS and PPC still emit stubs for calls since the target may be
418// too far away to call directly. This #if can probably be removed when
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000419// http://llvm.org/PR5201 is fixed.
Simon Atanasyand9389352012-05-16 19:07:55 +0000420#if !defined(__arm__) && !defined(__mips__) && \
421 !defined(__powerpc__) && !defined(__ppc__)
Eric Christopher116664a2009-11-12 03:12:18 +0000422typedef int (*FooPtr) ();
423
424TEST_F(JITTest, NoStubs) {
425 LoadAssembly("define void @bar() {"
426 "entry: "
427 "ret void"
428 "}"
429 " "
430 "define i32 @foo() {"
431 "entry:"
432 "call void @bar()"
433 "ret i32 undef"
434 "}"
435 " "
436 "define i32 @main() {"
437 "entry:"
438 "%0 = call i32 @foo()"
439 "call void @bar()"
440 "ret i32 undef"
441 "}");
442 Function *foo = M->getFunction("foo");
443 uintptr_t tmp = (uintptr_t)(TheJIT->getPointerToFunction(foo));
444 FooPtr ptr = (FooPtr)(tmp);
445
446 (ptr)();
447
448 // We should now allocate no more stubs, we have the code to foo
449 // and the existing stub for bar.
450 int stubsBefore = RJMM->stubsAllocated;
451 Function *func = M->getFunction("main");
452 TheJIT->getPointerToFunction(func);
453
454 Function *bar = M->getFunction("bar");
455 TheJIT->getPointerToFunction(bar);
456
457 ASSERT_EQ(stubsBefore, RJMM->stubsAllocated);
458}
Jeffrey Yasskin630382a2009-11-24 02:11:14 +0000459#endif // !ARM && !PPC
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000460
461TEST_F(JITTest, FunctionPointersOutliveTheirCreator) {
462 TheJIT->DisableLazyCompilation(true);
463 LoadAssembly("define i8()* @get_foo_addr() { "
464 " ret i8()* @foo "
465 "} "
466 " "
467 "define i8 @foo() { "
468 " ret i8 42 "
469 "} ");
470 Function *F_get_foo_addr = M->getFunction("get_foo_addr");
471
472 typedef char(*fooT)();
473 fooT (*get_foo_addr)() = reinterpret_cast<fooT(*)()>(
474 (intptr_t)TheJIT->getPointerToFunction(F_get_foo_addr));
475 fooT foo_addr = get_foo_addr();
476
477 // Now free get_foo_addr. This should not free the machine code for foo or
478 // any call stub returned as foo's canonical address.
479 TheJIT->freeMachineCodeForFunction(F_get_foo_addr);
480
481 // Check by calling the reported address of foo.
482 EXPECT_EQ(42, foo_addr());
483
484 // The reported address should also be the same as the result of a subsequent
485 // getPointerToFunction(foo).
486#if 0
487 // Fails until PR5126 is fixed:
488 Function *F_foo = M->getFunction("foo");
489 fooT foo = reinterpret_cast<fooT>(
490 (intptr_t)TheJIT->getPointerToFunction(F_foo));
491 EXPECT_EQ((intptr_t)foo, (intptr_t)foo_addr);
Bill Wendling0c2749f2009-11-13 21:58:54 +0000492#endif
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000493}
Eric Christopher116664a2009-11-12 03:12:18 +0000494
Ulrich Weigand1218bc42013-05-06 16:10:35 +0000495// ARM does not have an implementation of replaceMachineCodeForFunction(),
496// so recompileAndRelinkFunction doesn't work.
497#if !defined(__arm__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000498TEST_F(JITTest, FunctionIsRecompiledAndRelinked) {
499 Function *F = Function::Create(TypeBuilder<int(void), false>::get(Context),
500 GlobalValue::ExternalLinkage, "test", M);
501 BasicBlock *Entry = BasicBlock::Create(Context, "entry", F);
502 IRBuilder<> Builder(Entry);
503 Value *Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 1);
504 Builder.CreateRet(Val);
505
506 TheJIT->DisableLazyCompilation(true);
507 // Compile the function once, and make sure it works.
508 int (*OrigFPtr)() = reinterpret_cast<int(*)()>(
509 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
510 EXPECT_EQ(1, OrigFPtr());
511
512 // Now change the function to return a different value.
513 Entry->eraseFromParent();
514 BasicBlock *NewEntry = BasicBlock::Create(Context, "new_entry", F);
515 Builder.SetInsertPoint(NewEntry);
516 Val = ConstantInt::get(TypeBuilder<int, false>::get(Context), 2);
517 Builder.CreateRet(Val);
518 // Recompile it, which should produce a new function pointer _and_ update the
519 // old one.
520 int (*NewFPtr)() = reinterpret_cast<int(*)()>(
521 (intptr_t)TheJIT->recompileAndRelinkFunction(F));
522
523 EXPECT_EQ(2, NewFPtr())
524 << "The new pointer should call the new version of the function";
525 EXPECT_EQ(2, OrigFPtr())
526 << "The old pointer's target should now jump to the new version";
527}
Ulrich Weigand1218bc42013-05-06 16:10:35 +0000528#endif // !defined(__arm__)
Jeffrey Yasskin92fdf452009-12-22 23:18:18 +0000529
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000530} // anonymous namespace
531// This variable is intentionally defined differently in the statically-compiled
532// program from the IR input to the JIT to assert that the JIT doesn't use its
533// definition.
534extern "C" int32_t JITTest_AvailableExternallyGlobal;
Bill Wendling80668fb2012-10-17 08:08:06 +0000535int32_t JITTest_AvailableExternallyGlobal LLVM_ATTRIBUTE_USED = 42;
Jeffrey Yasskin898e9df2009-12-13 20:30:32 +0000536namespace {
537
538TEST_F(JITTest, AvailableExternallyGlobalIsntEmitted) {
539 TheJIT->DisableLazyCompilation(true);
540 LoadAssembly("@JITTest_AvailableExternallyGlobal = "
541 " available_externally global i32 7 "
542 " "
543 "define i32 @loader() { "
544 " %result = load i32* @JITTest_AvailableExternallyGlobal "
545 " ret i32 %result "
546 "} ");
547 Function *loaderIR = M->getFunction("loader");
548
549 int32_t (*loader)() = reinterpret_cast<int32_t(*)()>(
550 (intptr_t)TheJIT->getPointerToFunction(loaderIR));
551 EXPECT_EQ(42, loader()) << "func should return 42 from the external global,"
552 << " not 7 from the IR version.";
553}
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000554} // anonymous namespace
555// This function is intentionally defined differently in the statically-compiled
556// program from the IR input to the JIT to assert that the JIT doesn't use its
557// definition.
NAKAMURA Takumi8ab27a32012-10-12 08:09:29 +0000558extern "C" int32_t JITTest_AvailableExternallyFunction() LLVM_ATTRIBUTE_USED;
NAKAMURA Takumi97eb05b2012-10-12 01:34:07 +0000559extern "C" int32_t JITTest_AvailableExternallyFunction() {
Jeffrey Yasskinaad0d522009-12-17 21:35:29 +0000560 return 42;
561}
562namespace {
563
564TEST_F(JITTest, AvailableExternallyFunctionIsntCompiled) {
565 TheJIT->DisableLazyCompilation(true);
566 LoadAssembly("define available_externally i32 "
567 " @JITTest_AvailableExternallyFunction() { "
568 " ret i32 7 "
569 "} "
570 " "
571 "define i32 @func() { "
572 " %result = tail call i32 "
573 " @JITTest_AvailableExternallyFunction() "
574 " ret i32 %result "
575 "} ");
576 Function *funcIR = M->getFunction("func");
577
578 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
579 (intptr_t)TheJIT->getPointerToFunction(funcIR));
580 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
581 << " not 7 from the IR version.";
582}
583
Jeffrey Yasskin39c75f22010-03-04 19:45:09 +0000584TEST_F(JITTest, EscapedLazyStubStillCallable) {
585 TheJIT->DisableLazyCompilation(false);
586 LoadAssembly("define internal i32 @stubbed() { "
587 " ret i32 42 "
588 "} "
589 " "
590 "define i32()* @get_stub() { "
591 " ret i32()* @stubbed "
592 "} ");
593 typedef int32_t(*StubTy)();
594
595 // Call get_stub() to get the address of @stubbed without actually JITting it.
596 Function *get_stubIR = M->getFunction("get_stub");
597 StubTy (*get_stub)() = reinterpret_cast<StubTy(*)()>(
598 (intptr_t)TheJIT->getPointerToFunction(get_stubIR));
599 StubTy stubbed = get_stub();
600 // Now get_stubIR is the only reference to stubbed's stub.
601 get_stubIR->eraseFromParent();
602 // Now there are no references inside the JIT, but we've got a pointer outside
603 // it. The stub should be callable and return the right value.
604 EXPECT_EQ(42, stubbed());
605}
606
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000607// Converts the LLVM assembly to bitcode and returns it in a std::string. An
608// empty string indicates an error.
609std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
610 Module TempModule("TempModule", Context);
611 if (!LoadAssemblyInto(&TempModule, Assembly)) {
612 return "";
613 }
614
615 std::string Result;
616 raw_string_ostream OS(Result);
617 WriteBitcodeToFile(&TempModule, OS);
618 OS.flush();
619 return Result;
620}
621
622// Returns a newly-created ExecutionEngine that reads the bitcode in 'Bitcode'
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000623// lazily. The associated Module (owned by the ExecutionEngine) is returned in
624// M. Both will be NULL on an error. Bitcode must live at least as long as the
625// ExecutionEngine.
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000626ExecutionEngine *getJITFromBitcode(
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000627 LLVMContext &Context, const std::string &Bitcode, Module *&M) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000628 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
629 MemoryBuffer *BitcodeBuffer =
Chris Lattner796e64b2010-04-05 22:49:48 +0000630 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000631 std::string errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000632 M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
633 if (M == NULL) {
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000634 ADD_FAILURE() << errMsg;
635 delete BitcodeBuffer;
636 return NULL;
637 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000638 ExecutionEngine *TheJIT = EngineBuilder(M)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000639 .setEngineKind(EngineKind::JIT)
640 .setErrorStr(&errMsg)
641 .create();
642 if (TheJIT == NULL) {
643 ADD_FAILURE() << errMsg;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000644 delete M;
645 M = NULL;
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000646 return NULL;
647 }
648 return TheJIT;
649}
650
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000651TEST(LazyLoadedJITTest, MaterializableAvailableExternallyFunctionIsntCompiled) {
652 LLVMContext Context;
653 const std::string Bitcode =
654 AssembleToBitcode(Context,
655 "define available_externally i32 "
656 " @JITTest_AvailableExternallyFunction() { "
657 " ret i32 7 "
658 "} "
659 " "
660 "define i32 @func() { "
661 " %result = tail call i32 "
662 " @JITTest_AvailableExternallyFunction() "
663 " ret i32 %result "
664 "} ");
665 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
666 Module *M;
667 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
668 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
669 TheJIT->DisableLazyCompilation(true);
670
671 Function *funcIR = M->getFunction("func");
672 Function *availableFunctionIR =
673 M->getFunction("JITTest_AvailableExternallyFunction");
674
675 // Double-check that the available_externally function is still unmaterialized
676 // when getPointerToFunction needs to find out if it's available_externally.
677 EXPECT_TRUE(availableFunctionIR->isMaterializable());
678
679 int32_t (*func)() = reinterpret_cast<int32_t(*)()>(
680 (intptr_t)TheJIT->getPointerToFunction(funcIR));
681 EXPECT_EQ(42, func()) << "func should return 42 from the static version,"
682 << " not 7 from the IR version.";
683}
684
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000685TEST(LazyLoadedJITTest, EagerCompiledRecursionThroughGhost) {
686 LLVMContext Context;
687 const std::string Bitcode =
688 AssembleToBitcode(Context,
689 "define i32 @recur1(i32 %a) { "
690 " %zero = icmp eq i32 %a, 0 "
691 " br i1 %zero, label %done, label %notdone "
692 "done: "
693 " ret i32 3 "
694 "notdone: "
695 " %am1 = sub i32 %a, 1 "
696 " %result = call i32 @recur2(i32 %am1) "
697 " ret i32 %result "
698 "} "
699 " "
700 "define i32 @recur2(i32 %b) { "
701 " %result = call i32 @recur1(i32 %b) "
702 " ret i32 %result "
703 "} ");
704 ASSERT_FALSE(Bitcode.empty()) << "Assembling failed";
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000705 Module *M;
706 OwningPtr<ExecutionEngine> TheJIT(getJITFromBitcode(Context, Bitcode, M));
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000707 ASSERT_TRUE(TheJIT.get()) << "Failed to create JIT.";
708 TheJIT->DisableLazyCompilation(true);
709
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000710 Function *recur1IR = M->getFunction("recur1");
711 Function *recur2IR = M->getFunction("recur2");
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000712 EXPECT_TRUE(recur1IR->isMaterializable());
713 EXPECT_TRUE(recur2IR->isMaterializable());
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000714
715 int32_t (*recur1)(int32_t) = reinterpret_cast<int32_t(*)(int32_t)>(
716 (intptr_t)TheJIT->getPointerToFunction(recur1IR));
717 EXPECT_EQ(3, recur1(4));
718}
Ulrich Weigand72072852013-05-06 16:21:50 +0000719#endif // !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__)
Jeffrey Yasskinc5818fb2009-12-22 23:47:23 +0000720
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000721// This code is copied from JITEventListenerTest, but it only runs once for all
722// the tests in this directory. Everything seems fine, but that's strange
723// behavior.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000724class JITEnvironment : public testing::Environment {
725 virtual void SetUp() {
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000726 // Required to create a JIT.
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000727 InitializeNativeTarget();
728 }
729};
730testing::Environment* const jit_env =
731 testing::AddGlobalTestEnvironment(new JITEnvironment);
732
733}