blob: aa0c41d3ed2acf7502e9f9f456c7e5940cc9835b [file] [log] [blame]
Reid Kleckner10b4fc52009-07-23 21:46:56 +00001//===- JITMemoryManagerTest.cpp - Unit tests for the JIT memory manager ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/ADT/OwningPtr.h"
12#include "llvm/ExecutionEngine/JITMemoryManager.h"
13#include "llvm/DerivedTypes.h"
14#include "llvm/Function.h"
15#include "llvm/GlobalValue.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000016#include "llvm/LLVMContext.h"
Reid Kleckner10b4fc52009-07-23 21:46:56 +000017
18using namespace llvm;
19
20namespace {
21
22Function *makeFakeFunction() {
23 std::vector<const Type*> params;
Owen Anderson1d0be152009-08-13 21:58:54 +000024 const FunctionType *FTy =
25 FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000026 return Function::Create(FTy, GlobalValue::ExternalLinkage);
27}
28
29// Allocate three simple functions that fit in the initial slab. This exercises
30// the code in the case that we don't have to allocate more memory to store the
31// function bodies.
32TEST(JITMemoryManagerTest, NoAllocations) {
33 OwningPtr<JITMemoryManager> MemMgr(
34 JITMemoryManager::CreateDefaultMemManager());
35 uintptr_t size;
Reid Kleckner10b4fc52009-07-23 21:46:56 +000036 std::string Error;
37
38 // Allocate the functions.
39 OwningPtr<Function> F1(makeFakeFunction());
40 size = 1024;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000041 uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
42 memset(FunctionBody1, 0xFF, 1024);
43 MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + 1024);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000044 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
45
46 OwningPtr<Function> F2(makeFakeFunction());
47 size = 1024;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000048 uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
49 memset(FunctionBody2, 0xFF, 1024);
50 MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + 1024);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000051 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
52
53 OwningPtr<Function> F3(makeFakeFunction());
54 size = 1024;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000055 uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
56 memset(FunctionBody3, 0xFF, 1024);
57 MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + 1024);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000058 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
59
60 // Deallocate them out of order, in case that matters.
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000061 MemMgr->deallocateFunctionBody(FunctionBody2);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000062 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000063 MemMgr->deallocateFunctionBody(FunctionBody1);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000064 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000065 MemMgr->deallocateFunctionBody(FunctionBody3);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000066 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
67}
68
69// Make three large functions that take up most of the space in the slab. Then
70// try allocating three smaller functions that don't require additional slabs.
71TEST(JITMemoryManagerTest, TestCodeAllocation) {
72 OwningPtr<JITMemoryManager> MemMgr(
73 JITMemoryManager::CreateDefaultMemManager());
74 uintptr_t size;
Reid Kleckner10b4fc52009-07-23 21:46:56 +000075 std::string Error;
76
77 // Big functions are a little less than the largest block size.
78 const uintptr_t smallFuncSize = 1024;
79 const uintptr_t bigFuncSize = (MemMgr->GetDefaultCodeSlabSize() -
80 smallFuncSize * 2);
81
82 // Allocate big functions
83 OwningPtr<Function> F1(makeFakeFunction());
84 size = bigFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000085 uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000086 ASSERT_LE(bigFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000087 memset(FunctionBody1, 0xFF, bigFuncSize);
88 MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + bigFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000089 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
90
91 OwningPtr<Function> F2(makeFakeFunction());
92 size = bigFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000093 uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000094 ASSERT_LE(bigFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +000095 memset(FunctionBody2, 0xFF, bigFuncSize);
96 MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + bigFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +000097 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
98
99 OwningPtr<Function> F3(makeFakeFunction());
100 size = bigFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000101 uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000102 ASSERT_LE(bigFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000103 memset(FunctionBody3, 0xFF, bigFuncSize);
104 MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + bigFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000105 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
106
107 // Check that each large function took it's own slab.
108 EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
109
110 // Allocate small functions
111 OwningPtr<Function> F4(makeFakeFunction());
112 size = smallFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000113 uint8_t *FunctionBody4 = MemMgr->startFunctionBody(F4.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000114 ASSERT_LE(smallFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000115 memset(FunctionBody4, 0xFF, smallFuncSize);
116 MemMgr->endFunctionBody(F4.get(), FunctionBody4,
117 FunctionBody4 + smallFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000118 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
119
120 OwningPtr<Function> F5(makeFakeFunction());
121 size = smallFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000122 uint8_t *FunctionBody5 = MemMgr->startFunctionBody(F5.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000123 ASSERT_LE(smallFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000124 memset(FunctionBody5, 0xFF, smallFuncSize);
125 MemMgr->endFunctionBody(F5.get(), FunctionBody5,
126 FunctionBody5 + smallFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000127 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
128
129 OwningPtr<Function> F6(makeFakeFunction());
130 size = smallFuncSize;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000131 uint8_t *FunctionBody6 = MemMgr->startFunctionBody(F6.get(), size);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000132 ASSERT_LE(smallFuncSize, size);
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000133 memset(FunctionBody6, 0xFF, smallFuncSize);
134 MemMgr->endFunctionBody(F6.get(), FunctionBody6,
135 FunctionBody6 + smallFuncSize);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000136 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
137
138 // Check that the small functions didn't allocate any new slabs.
139 EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
140
141 // Deallocate them out of order, in case that matters.
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000142 MemMgr->deallocateFunctionBody(FunctionBody2);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000143 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000144 MemMgr->deallocateFunctionBody(FunctionBody1);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000145 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000146 MemMgr->deallocateFunctionBody(FunctionBody4);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000147 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000148 MemMgr->deallocateFunctionBody(FunctionBody3);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000149 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000150 MemMgr->deallocateFunctionBody(FunctionBody5);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000151 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
Jeffrey Yasskin1e861322009-10-20 18:13:21 +0000152 MemMgr->deallocateFunctionBody(FunctionBody6);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000153 EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
154}
155
156// Allocate five global ints of varying widths and alignment, and check their
157// alignment and overlap.
158TEST(JITMemoryManagerTest, TestSmallGlobalInts) {
159 OwningPtr<JITMemoryManager> MemMgr(
160 JITMemoryManager::CreateDefaultMemManager());
161 uint8_t *a = (uint8_t *)MemMgr->allocateGlobal(8, 0);
162 uint16_t *b = (uint16_t*)MemMgr->allocateGlobal(16, 2);
163 uint32_t *c = (uint32_t*)MemMgr->allocateGlobal(32, 4);
164 uint64_t *d = (uint64_t*)MemMgr->allocateGlobal(64, 8);
165
166 // Check the alignment.
167 EXPECT_EQ(0U, ((uintptr_t)b) & 0x1);
168 EXPECT_EQ(0U, ((uintptr_t)c) & 0x3);
169 EXPECT_EQ(0U, ((uintptr_t)d) & 0x7);
170
171 // Initialize them each one at a time and make sure they don't overlap.
172 *a = 0xff;
173 *b = 0U;
174 *c = 0U;
175 *d = 0U;
176 EXPECT_EQ(0xffU, *a);
177 EXPECT_EQ(0U, *b);
178 EXPECT_EQ(0U, *c);
179 EXPECT_EQ(0U, *d);
180 *a = 0U;
181 *b = 0xffffU;
182 EXPECT_EQ(0U, *a);
183 EXPECT_EQ(0xffffU, *b);
184 EXPECT_EQ(0U, *c);
185 EXPECT_EQ(0U, *d);
186 *b = 0U;
187 *c = 0xffffffffU;
188 EXPECT_EQ(0U, *a);
189 EXPECT_EQ(0U, *b);
190 EXPECT_EQ(0xffffffffU, *c);
191 EXPECT_EQ(0U, *d);
192 *c = 0U;
Reid Kleckner535c9c32009-07-23 22:27:18 +0000193 *d = 0xffffffffffffffffULL;
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000194 EXPECT_EQ(0U, *a);
195 EXPECT_EQ(0U, *b);
196 EXPECT_EQ(0U, *c);
Reid Kleckner535c9c32009-07-23 22:27:18 +0000197 EXPECT_EQ(0xffffffffffffffffULL, *d);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000198
199 // Make sure we didn't allocate any extra slabs for this tiny amount of data.
200 EXPECT_EQ(1U, MemMgr->GetNumDataSlabs());
201}
202
203// Allocate a small global, a big global, and a third global, and make sure we
204// only use two slabs for that.
205TEST(JITMemoryManagerTest, TestLargeGlobalArray) {
206 OwningPtr<JITMemoryManager> MemMgr(
207 JITMemoryManager::CreateDefaultMemManager());
208 size_t Size = 4 * MemMgr->GetDefaultDataSlabSize();
209 uint64_t *a = (uint64_t*)MemMgr->allocateGlobal(64, 8);
210 uint8_t *g = MemMgr->allocateGlobal(Size, 8);
211 uint64_t *b = (uint64_t*)MemMgr->allocateGlobal(64, 8);
212
213 // Check the alignment.
214 EXPECT_EQ(0U, ((uintptr_t)a) & 0x7);
215 EXPECT_EQ(0U, ((uintptr_t)g) & 0x7);
216 EXPECT_EQ(0U, ((uintptr_t)b) & 0x7);
217
218 // Initialize them to make sure we don't segfault and make sure they don't
219 // overlap.
220 memset(a, 0x1, 8);
221 memset(g, 0x2, Size);
222 memset(b, 0x3, 8);
Reid Kleckner535c9c32009-07-23 22:27:18 +0000223 EXPECT_EQ(0x0101010101010101ULL, *a);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000224 // Just check the edges.
225 EXPECT_EQ(0x02U, g[0]);
226 EXPECT_EQ(0x02U, g[Size - 1]);
Reid Kleckner535c9c32009-07-23 22:27:18 +0000227 EXPECT_EQ(0x0303030303030303ULL, *b);
Reid Kleckner10b4fc52009-07-23 21:46:56 +0000228
229 // Check the number of slabs.
230 EXPECT_EQ(2U, MemMgr->GetNumDataSlabs());
231}
232
233// Allocate lots of medium globals so that we can test moving the bump allocator
234// to a new slab.
235TEST(JITMemoryManagerTest, TestManyGlobals) {
236 OwningPtr<JITMemoryManager> MemMgr(
237 JITMemoryManager::CreateDefaultMemManager());
238 size_t SlabSize = MemMgr->GetDefaultDataSlabSize();
239 size_t Size = 128;
240 int Iters = (SlabSize / Size) + 1;
241
242 // We should start with one slab.
243 EXPECT_EQ(1U, MemMgr->GetNumDataSlabs());
244
245 // After allocating a bunch of globals, we should have two.
246 for (int I = 0; I < Iters; ++I)
247 MemMgr->allocateGlobal(Size, 8);
248 EXPECT_EQ(2U, MemMgr->GetNumDataSlabs());
249
250 // And after much more, we should have three.
251 for (int I = 0; I < Iters; ++I)
252 MemMgr->allocateGlobal(Size, 8);
253 EXPECT_EQ(3U, MemMgr->GetNumDataSlabs());
254}
255
256// Allocate lots of function stubs so that we can test moving the stub bump
257// allocator to a new slab.
258TEST(JITMemoryManagerTest, TestManyStubs) {
259 OwningPtr<JITMemoryManager> MemMgr(
260 JITMemoryManager::CreateDefaultMemManager());
261 size_t SlabSize = MemMgr->GetDefaultStubSlabSize();
262 size_t Size = 128;
263 int Iters = (SlabSize / Size) + 1;
264
265 // We should start with one slab.
266 EXPECT_EQ(1U, MemMgr->GetNumStubSlabs());
267
268 // After allocating a bunch of stubs, we should have two.
269 for (int I = 0; I < Iters; ++I)
270 MemMgr->allocateStub(NULL, Size, 8);
271 EXPECT_EQ(2U, MemMgr->GetNumStubSlabs());
272
273 // And after much more, we should have three.
274 for (int I = 0; I < Iters; ++I)
275 MemMgr->allocateStub(NULL, Size, 8);
276 EXPECT_EQ(3U, MemMgr->GetNumStubSlabs());
277}
278
279}