JITMemoryManagerTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //===- 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. #include "llvm/ExecutionEngine/JITMemoryManager.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/OwningPtr.h"
  12. #include "llvm/IR/DerivedTypes.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/GlobalValue.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. namespace {
  19. Function *makeFakeFunction() {
  20. std::vector<Type*> params;
  21. FunctionType *FTy =
  22. FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false);
  23. return Function::Create(FTy, GlobalValue::ExternalLinkage);
  24. }
  25. // Allocate three simple functions that fit in the initial slab. This exercises
  26. // the code in the case that we don't have to allocate more memory to store the
  27. // function bodies.
  28. TEST(JITMemoryManagerTest, NoAllocations) {
  29. OwningPtr<JITMemoryManager> MemMgr(
  30. JITMemoryManager::CreateDefaultMemManager());
  31. uintptr_t size;
  32. std::string Error;
  33. // Allocate the functions.
  34. OwningPtr<Function> F1(makeFakeFunction());
  35. size = 1024;
  36. uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
  37. memset(FunctionBody1, 0xFF, 1024);
  38. MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + 1024);
  39. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  40. OwningPtr<Function> F2(makeFakeFunction());
  41. size = 1024;
  42. uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
  43. memset(FunctionBody2, 0xFF, 1024);
  44. MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + 1024);
  45. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  46. OwningPtr<Function> F3(makeFakeFunction());
  47. size = 1024;
  48. uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
  49. memset(FunctionBody3, 0xFF, 1024);
  50. MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + 1024);
  51. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  52. // Deallocate them out of order, in case that matters.
  53. MemMgr->deallocateFunctionBody(FunctionBody2);
  54. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  55. MemMgr->deallocateFunctionBody(FunctionBody1);
  56. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  57. MemMgr->deallocateFunctionBody(FunctionBody3);
  58. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  59. }
  60. // Make three large functions that take up most of the space in the slab. Then
  61. // try allocating three smaller functions that don't require additional slabs.
  62. TEST(JITMemoryManagerTest, TestCodeAllocation) {
  63. OwningPtr<JITMemoryManager> MemMgr(
  64. JITMemoryManager::CreateDefaultMemManager());
  65. uintptr_t size;
  66. std::string Error;
  67. // Big functions are a little less than the largest block size.
  68. const uintptr_t smallFuncSize = 1024;
  69. const uintptr_t bigFuncSize = (MemMgr->GetDefaultCodeSlabSize() -
  70. smallFuncSize * 2);
  71. // Allocate big functions
  72. OwningPtr<Function> F1(makeFakeFunction());
  73. size = bigFuncSize;
  74. uint8_t *FunctionBody1 = MemMgr->startFunctionBody(F1.get(), size);
  75. ASSERT_LE(bigFuncSize, size);
  76. memset(FunctionBody1, 0xFF, bigFuncSize);
  77. MemMgr->endFunctionBody(F1.get(), FunctionBody1, FunctionBody1 + bigFuncSize);
  78. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  79. OwningPtr<Function> F2(makeFakeFunction());
  80. size = bigFuncSize;
  81. uint8_t *FunctionBody2 = MemMgr->startFunctionBody(F2.get(), size);
  82. ASSERT_LE(bigFuncSize, size);
  83. memset(FunctionBody2, 0xFF, bigFuncSize);
  84. MemMgr->endFunctionBody(F2.get(), FunctionBody2, FunctionBody2 + bigFuncSize);
  85. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  86. OwningPtr<Function> F3(makeFakeFunction());
  87. size = bigFuncSize;
  88. uint8_t *FunctionBody3 = MemMgr->startFunctionBody(F3.get(), size);
  89. ASSERT_LE(bigFuncSize, size);
  90. memset(FunctionBody3, 0xFF, bigFuncSize);
  91. MemMgr->endFunctionBody(F3.get(), FunctionBody3, FunctionBody3 + bigFuncSize);
  92. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  93. // Check that each large function took it's own slab.
  94. EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
  95. // Allocate small functions
  96. OwningPtr<Function> F4(makeFakeFunction());
  97. size = smallFuncSize;
  98. uint8_t *FunctionBody4 = MemMgr->startFunctionBody(F4.get(), size);
  99. ASSERT_LE(smallFuncSize, size);
  100. memset(FunctionBody4, 0xFF, smallFuncSize);
  101. MemMgr->endFunctionBody(F4.get(), FunctionBody4,
  102. FunctionBody4 + smallFuncSize);
  103. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  104. OwningPtr<Function> F5(makeFakeFunction());
  105. size = smallFuncSize;
  106. uint8_t *FunctionBody5 = MemMgr->startFunctionBody(F5.get(), size);
  107. ASSERT_LE(smallFuncSize, size);
  108. memset(FunctionBody5, 0xFF, smallFuncSize);
  109. MemMgr->endFunctionBody(F5.get(), FunctionBody5,
  110. FunctionBody5 + smallFuncSize);
  111. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  112. OwningPtr<Function> F6(makeFakeFunction());
  113. size = smallFuncSize;
  114. uint8_t *FunctionBody6 = MemMgr->startFunctionBody(F6.get(), size);
  115. ASSERT_LE(smallFuncSize, size);
  116. memset(FunctionBody6, 0xFF, smallFuncSize);
  117. MemMgr->endFunctionBody(F6.get(), FunctionBody6,
  118. FunctionBody6 + smallFuncSize);
  119. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  120. // Check that the small functions didn't allocate any new slabs.
  121. EXPECT_EQ(3U, MemMgr->GetNumCodeSlabs());
  122. // Deallocate them out of order, in case that matters.
  123. MemMgr->deallocateFunctionBody(FunctionBody2);
  124. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  125. MemMgr->deallocateFunctionBody(FunctionBody1);
  126. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  127. MemMgr->deallocateFunctionBody(FunctionBody4);
  128. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  129. MemMgr->deallocateFunctionBody(FunctionBody3);
  130. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  131. MemMgr->deallocateFunctionBody(FunctionBody5);
  132. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  133. MemMgr->deallocateFunctionBody(FunctionBody6);
  134. EXPECT_TRUE(MemMgr->CheckInvariants(Error)) << Error;
  135. }
  136. // Allocate five global ints of varying widths and alignment, and check their
  137. // alignment and overlap.
  138. TEST(JITMemoryManagerTest, TestSmallGlobalInts) {
  139. OwningPtr<JITMemoryManager> MemMgr(
  140. JITMemoryManager::CreateDefaultMemManager());
  141. uint8_t *a = (uint8_t *)MemMgr->allocateGlobal(8, 0);
  142. uint16_t *b = (uint16_t*)MemMgr->allocateGlobal(16, 2);
  143. uint32_t *c = (uint32_t*)MemMgr->allocateGlobal(32, 4);
  144. uint64_t *d = (uint64_t*)MemMgr->allocateGlobal(64, 8);
  145. // Check the alignment.
  146. EXPECT_EQ(0U, ((uintptr_t)b) & 0x1);
  147. EXPECT_EQ(0U, ((uintptr_t)c) & 0x3);
  148. EXPECT_EQ(0U, ((uintptr_t)d) & 0x7);
  149. // Initialize them each one at a time and make sure they don't overlap.
  150. *a = 0xff;
  151. *b = 0U;
  152. *c = 0U;
  153. *d = 0U;
  154. EXPECT_EQ(0xffU, *a);
  155. EXPECT_EQ(0U, *b);
  156. EXPECT_EQ(0U, *c);
  157. EXPECT_EQ(0U, *d);
  158. *a = 0U;
  159. *b = 0xffffU;
  160. EXPECT_EQ(0U, *a);
  161. EXPECT_EQ(0xffffU, *b);
  162. EXPECT_EQ(0U, *c);
  163. EXPECT_EQ(0U, *d);
  164. *b = 0U;
  165. *c = 0xffffffffU;
  166. EXPECT_EQ(0U, *a);
  167. EXPECT_EQ(0U, *b);
  168. EXPECT_EQ(0xffffffffU, *c);
  169. EXPECT_EQ(0U, *d);
  170. *c = 0U;
  171. *d = 0xffffffffffffffffULL;
  172. EXPECT_EQ(0U, *a);
  173. EXPECT_EQ(0U, *b);
  174. EXPECT_EQ(0U, *c);
  175. EXPECT_EQ(0xffffffffffffffffULL, *d);
  176. // Make sure we didn't allocate any extra slabs for this tiny amount of data.
  177. EXPECT_EQ(1U, MemMgr->GetNumDataSlabs());
  178. }
  179. // Allocate a small global, a big global, and a third global, and make sure we
  180. // only use two slabs for that.
  181. TEST(JITMemoryManagerTest, TestLargeGlobalArray) {
  182. OwningPtr<JITMemoryManager> MemMgr(
  183. JITMemoryManager::CreateDefaultMemManager());
  184. size_t Size = 4 * MemMgr->GetDefaultDataSlabSize();
  185. uint64_t *a = (uint64_t*)MemMgr->allocateGlobal(64, 8);
  186. uint8_t *g = MemMgr->allocateGlobal(Size, 8);
  187. uint64_t *b = (uint64_t*)MemMgr->allocateGlobal(64, 8);
  188. // Check the alignment.
  189. EXPECT_EQ(0U, ((uintptr_t)a) & 0x7);
  190. EXPECT_EQ(0U, ((uintptr_t)g) & 0x7);
  191. EXPECT_EQ(0U, ((uintptr_t)b) & 0x7);
  192. // Initialize them to make sure we don't segfault and make sure they don't
  193. // overlap.
  194. memset(a, 0x1, 8);
  195. memset(g, 0x2, Size);
  196. memset(b, 0x3, 8);
  197. EXPECT_EQ(0x0101010101010101ULL, *a);
  198. // Just check the edges.
  199. EXPECT_EQ(0x02U, g[0]);
  200. EXPECT_EQ(0x02U, g[Size - 1]);
  201. EXPECT_EQ(0x0303030303030303ULL, *b);
  202. // Check the number of slabs.
  203. EXPECT_EQ(2U, MemMgr->GetNumDataSlabs());
  204. }
  205. // Allocate lots of medium globals so that we can test moving the bump allocator
  206. // to a new slab.
  207. TEST(JITMemoryManagerTest, TestManyGlobals) {
  208. OwningPtr<JITMemoryManager> MemMgr(
  209. JITMemoryManager::CreateDefaultMemManager());
  210. size_t SlabSize = MemMgr->GetDefaultDataSlabSize();
  211. size_t Size = 128;
  212. int Iters = (SlabSize / Size) + 1;
  213. // We should start with no slabs.
  214. EXPECT_EQ(0U, MemMgr->GetNumDataSlabs());
  215. // After allocating a bunch of globals, we should have two.
  216. for (int I = 0; I < Iters; ++I)
  217. MemMgr->allocateGlobal(Size, 8);
  218. EXPECT_EQ(2U, MemMgr->GetNumDataSlabs());
  219. // And after much more, we should have three.
  220. for (int I = 0; I < Iters; ++I)
  221. MemMgr->allocateGlobal(Size, 8);
  222. EXPECT_EQ(3U, MemMgr->GetNumDataSlabs());
  223. }
  224. // Allocate lots of function stubs so that we can test moving the stub bump
  225. // allocator to a new slab.
  226. TEST(JITMemoryManagerTest, TestManyStubs) {
  227. OwningPtr<JITMemoryManager> MemMgr(
  228. JITMemoryManager::CreateDefaultMemManager());
  229. size_t SlabSize = MemMgr->GetDefaultStubSlabSize();
  230. size_t Size = 128;
  231. int Iters = (SlabSize / Size) + 1;
  232. // We should start with no slabs.
  233. EXPECT_EQ(0U, MemMgr->GetNumDataSlabs());
  234. // After allocating a bunch of stubs, we should have two.
  235. for (int I = 0; I < Iters; ++I)
  236. MemMgr->allocateStub(NULL, Size, 8);
  237. EXPECT_EQ(2U, MemMgr->GetNumStubSlabs());
  238. // And after much more, we should have three.
  239. for (int I = 0; I < Iters; ++I)
  240. MemMgr->allocateStub(NULL, Size, 8);
  241. EXPECT_EQ(3U, MemMgr->GetNumStubSlabs());
  242. }
  243. // Check section allocation and alignment
  244. TEST(JITMemoryManagerTest, AllocateSection) {
  245. OwningPtr<JITMemoryManager> MemMgr(
  246. JITMemoryManager::CreateDefaultMemManager());
  247. uint8_t *code1 = MemMgr->allocateCodeSection(256, 0, 1, StringRef());
  248. uint8_t *data1 = MemMgr->allocateDataSection(256, 16, 2, StringRef(), true);
  249. uint8_t *code2 = MemMgr->allocateCodeSection(257, 32, 3, StringRef());
  250. uint8_t *data2 = MemMgr->allocateDataSection(256, 64, 4, StringRef(), false);
  251. uint8_t *code3 = MemMgr->allocateCodeSection(258, 64, 5, StringRef());
  252. EXPECT_NE((uint8_t*)0, code1);
  253. EXPECT_NE((uint8_t*)0, code2);
  254. EXPECT_NE((uint8_t*)0, data1);
  255. EXPECT_NE((uint8_t*)0, data2);
  256. // Check alignment
  257. EXPECT_EQ((uint64_t)code1 & 0xf, 0u);
  258. EXPECT_EQ((uint64_t)code2 & 0x1f, 0u);
  259. EXPECT_EQ((uint64_t)code3 & 0x3f, 0u);
  260. EXPECT_EQ((uint64_t)data1 & 0xf, 0u);
  261. EXPECT_EQ((uint64_t)data2 & 0x3f, 0u);
  262. }
  263. }