OptimizedStructLayout.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //===--- OptimizedStructLayout.cpp - Optimal data layout algorithm ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the performOptimizedStructLayout interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/OptimizedStructLayout.h"
  13. #include <optional>
  14. using namespace llvm;
  15. using Field = OptimizedStructLayoutField;
  16. #ifndef NDEBUG
  17. static void checkValidLayout(ArrayRef<Field> Fields, uint64_t Size,
  18. Align MaxAlign) {
  19. uint64_t LastEnd = 0;
  20. Align ComputedMaxAlign;
  21. for (auto &Field : Fields) {
  22. assert(Field.hasFixedOffset() &&
  23. "didn't assign a fixed offset to field");
  24. assert(isAligned(Field.Alignment, Field.Offset) &&
  25. "didn't assign a correctly-aligned offset to field");
  26. assert(Field.Offset >= LastEnd &&
  27. "didn't assign offsets in ascending order");
  28. LastEnd = Field.getEndOffset();
  29. assert(Field.Alignment <= MaxAlign &&
  30. "didn't compute MaxAlign correctly");
  31. ComputedMaxAlign = std::max(Field.Alignment, MaxAlign);
  32. }
  33. assert(LastEnd == Size && "didn't compute LastEnd correctly");
  34. assert(ComputedMaxAlign == MaxAlign && "didn't compute MaxAlign correctly");
  35. }
  36. #endif
  37. std::pair<uint64_t, Align>
  38. llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
  39. #ifndef NDEBUG
  40. // Do some simple precondition checks.
  41. {
  42. bool InFixedPrefix = true;
  43. size_t LastEnd = 0;
  44. for (auto &Field : Fields) {
  45. assert(Field.Size > 0 && "field of zero size");
  46. if (Field.hasFixedOffset()) {
  47. assert(InFixedPrefix &&
  48. "fixed-offset fields are not a strict prefix of array");
  49. assert(LastEnd <= Field.Offset &&
  50. "fixed-offset fields overlap or are not in order");
  51. LastEnd = Field.getEndOffset();
  52. assert(LastEnd > Field.Offset &&
  53. "overflow in fixed-offset end offset");
  54. } else {
  55. InFixedPrefix = false;
  56. }
  57. }
  58. }
  59. #endif
  60. // Do an initial pass over the fields.
  61. Align MaxAlign;
  62. // Find the first flexible-offset field, tracking MaxAlign.
  63. auto FirstFlexible = Fields.begin(), E = Fields.end();
  64. while (FirstFlexible != E && FirstFlexible->hasFixedOffset()) {
  65. MaxAlign = std::max(MaxAlign, FirstFlexible->Alignment);
  66. ++FirstFlexible;
  67. }
  68. // If there are no flexible fields, we're done.
  69. if (FirstFlexible == E) {
  70. uint64_t Size = 0;
  71. if (!Fields.empty())
  72. Size = Fields.back().getEndOffset();
  73. #ifndef NDEBUG
  74. checkValidLayout(Fields, Size, MaxAlign);
  75. #endif
  76. return std::make_pair(Size, MaxAlign);
  77. }
  78. // Walk over the flexible-offset fields, tracking MaxAlign and
  79. // assigning them a unique number in order of their appearance.
  80. // We'll use this unique number in the comparison below so that
  81. // we can use array_pod_sort, which isn't stable. We won't use it
  82. // past that point.
  83. {
  84. uintptr_t UniqueNumber = 0;
  85. for (auto I = FirstFlexible; I != E; ++I) {
  86. I->Scratch = reinterpret_cast<void*>(UniqueNumber++);
  87. MaxAlign = std::max(MaxAlign, I->Alignment);
  88. }
  89. }
  90. // Sort the flexible elements in order of decreasing alignment,
  91. // then decreasing size, and then the original order as recorded
  92. // in Scratch. The decreasing-size aspect of this is only really
  93. // important if we get into the gap-filling stage below, but it
  94. // doesn't hurt here.
  95. array_pod_sort(FirstFlexible, E,
  96. [](const Field *lhs, const Field *rhs) -> int {
  97. // Decreasing alignment.
  98. if (lhs->Alignment != rhs->Alignment)
  99. return (lhs->Alignment < rhs->Alignment ? 1 : -1);
  100. // Decreasing size.
  101. if (lhs->Size != rhs->Size)
  102. return (lhs->Size < rhs->Size ? 1 : -1);
  103. // Original order.
  104. auto lhsNumber = reinterpret_cast<uintptr_t>(lhs->Scratch);
  105. auto rhsNumber = reinterpret_cast<uintptr_t>(rhs->Scratch);
  106. if (lhsNumber != rhsNumber)
  107. return (lhsNumber < rhsNumber ? -1 : 1);
  108. return 0;
  109. });
  110. // Do a quick check for whether that sort alone has given us a perfect
  111. // layout with no interior padding. This is very common: if the
  112. // fixed-layout fields have no interior padding, and they end at a
  113. // sufficiently-aligned offset for all the flexible-layout fields,
  114. // and the flexible-layout fields all have sizes that are multiples
  115. // of their alignment, then this will reliably trigger.
  116. {
  117. bool HasPadding = false;
  118. uint64_t LastEnd = 0;
  119. // Walk the fixed-offset fields.
  120. for (auto I = Fields.begin(); I != FirstFlexible; ++I) {
  121. assert(I->hasFixedOffset());
  122. if (LastEnd != I->Offset) {
  123. HasPadding = true;
  124. break;
  125. }
  126. LastEnd = I->getEndOffset();
  127. }
  128. // Walk the flexible-offset fields, optimistically assigning fixed
  129. // offsets. Note that we maintain a strict division between the
  130. // fixed-offset and flexible-offset fields, so if we end up
  131. // discovering padding later in this loop, we can just abandon this
  132. // work and we'll ignore the offsets we already assigned.
  133. if (!HasPadding) {
  134. for (auto I = FirstFlexible; I != E; ++I) {
  135. auto Offset = alignTo(LastEnd, I->Alignment);
  136. if (LastEnd != Offset) {
  137. HasPadding = true;
  138. break;
  139. }
  140. I->Offset = Offset;
  141. LastEnd = I->getEndOffset();
  142. }
  143. }
  144. // If we already have a perfect layout, we're done.
  145. if (!HasPadding) {
  146. #ifndef NDEBUG
  147. checkValidLayout(Fields, LastEnd, MaxAlign);
  148. #endif
  149. return std::make_pair(LastEnd, MaxAlign);
  150. }
  151. }
  152. // The algorithm sketch at this point is as follows.
  153. //
  154. // Consider the padding gaps between fixed-offset fields in ascending
  155. // order. Let LastEnd be the offset of the first byte following the
  156. // field before the gap, or 0 if the gap is at the beginning of the
  157. // structure. Find the "best" flexible-offset field according to the
  158. // criteria below. If no such field exists, proceed to the next gap.
  159. // Otherwise, add the field at the first properly-aligned offset for
  160. // that field that is >= LastEnd, then update LastEnd and repeat in
  161. // order to fill any remaining gap following that field.
  162. //
  163. // Next, let LastEnd to be the offset of the first byte following the
  164. // last fixed-offset field, or 0 if there are no fixed-offset fields.
  165. // While there are flexible-offset fields remaining, find the "best"
  166. // flexible-offset field according to the criteria below, add it at
  167. // the first properly-aligned offset for that field that is >= LastEnd,
  168. // and update LastEnd to the first byte following the field.
  169. //
  170. // The "best" field is chosen by the following criteria, considered
  171. // strictly in order:
  172. //
  173. // - When filling a gap betweeen fields, the field must fit.
  174. // - A field is preferred if it requires less padding following LastEnd.
  175. // - A field is preferred if it is more aligned.
  176. // - A field is preferred if it is larger.
  177. // - A field is preferred if it appeared earlier in the initial order.
  178. //
  179. // Minimizing leading padding is a greedy attempt to avoid padding
  180. // entirely. Preferring more-aligned fields is an attempt to eliminate
  181. // stricter constraints earlier, with the idea that weaker alignment
  182. // constraints may be resolvable with less padding elsewhere. These
  183. // These two rules are sufficient to ensure that we get the optimal
  184. // layout in the "C-style" case. Preferring larger fields tends to take
  185. // better advantage of large gaps and may be more likely to have a size
  186. // that's a multiple of a useful alignment. Preferring the initial
  187. // order may help somewhat with locality but is mostly just a way of
  188. // ensuring deterministic output.
  189. //
  190. // Note that this algorithm does not guarantee a minimal layout. Picking
  191. // a larger object greedily may leave a gap that cannot be filled as
  192. // efficiently. Unfortunately, solving this perfectly is an NP-complete
  193. // problem (by reduction from bin-packing: let B_i be the bin sizes and
  194. // O_j be the object sizes; add fixed-offset fields such that the gaps
  195. // between them have size B_i, and add flexible-offset fields with
  196. // alignment 1 and size O_j; if the layout size is equal to the end of
  197. // the last fixed-layout field, the objects fit in the bins; note that
  198. // this doesn't even require the complexity of alignment).
  199. // The implementation below is essentially just an optimized version of
  200. // scanning the list of remaining fields looking for the best, which
  201. // would be O(n^2). In the worst case, it doesn't improve on that.
  202. // However, in practice it'll just scan the array of alignment bins
  203. // and consider the first few elements from one or two bins. The
  204. // number of bins is bounded by a small constant: alignments are powers
  205. // of two that are vanishingly unlikely to be over 64 and fairly unlikely
  206. // to be over 8. And multiple elements only need to be considered when
  207. // filling a gap between fixed-offset fields, which doesn't happen very
  208. // often. We could use a data structure within bins that optimizes for
  209. // finding the best-sized match, but it would require allocating memory
  210. // and copying data, so it's unlikely to be worthwhile.
  211. // Start by organizing the flexible-offset fields into bins according to
  212. // their alignment. We expect a small enough number of bins that we
  213. // don't care about the asymptotic costs of walking this.
  214. struct AlignmentQueue {
  215. /// The minimum size of anything currently in this queue.
  216. uint64_t MinSize;
  217. /// The head of the queue. A singly-linked list. The order here should
  218. /// be consistent with the earlier sort, i.e. the elements should be
  219. /// monotonically descending in size and otherwise in the original order.
  220. ///
  221. /// We remove the queue from the array as soon as this is empty.
  222. OptimizedStructLayoutField *Head;
  223. /// The alignment requirement of the queue.
  224. Align Alignment;
  225. static Field *getNext(Field *Cur) {
  226. return static_cast<Field *>(Cur->Scratch);
  227. }
  228. };
  229. SmallVector<AlignmentQueue, 8> FlexibleFieldsByAlignment;
  230. for (auto I = FirstFlexible; I != E; ) {
  231. auto Head = I;
  232. auto Alignment = I->Alignment;
  233. uint64_t MinSize = I->Size;
  234. auto LastInQueue = I;
  235. for (++I; I != E && I->Alignment == Alignment; ++I) {
  236. LastInQueue->Scratch = I;
  237. LastInQueue = I;
  238. MinSize = std::min(MinSize, I->Size);
  239. }
  240. LastInQueue->Scratch = nullptr;
  241. FlexibleFieldsByAlignment.push_back({MinSize, Head, Alignment});
  242. }
  243. #ifndef NDEBUG
  244. // Verify that we set the queues up correctly.
  245. auto checkQueues = [&]{
  246. bool FirstQueue = true;
  247. Align LastQueueAlignment;
  248. for (auto &Queue : FlexibleFieldsByAlignment) {
  249. assert((FirstQueue || Queue.Alignment < LastQueueAlignment) &&
  250. "bins not in order of descending alignment");
  251. LastQueueAlignment = Queue.Alignment;
  252. FirstQueue = false;
  253. assert(Queue.Head && "queue was empty");
  254. uint64_t LastSize = ~(uint64_t)0;
  255. for (auto I = Queue.Head; I; I = Queue.getNext(I)) {
  256. assert(I->Alignment == Queue.Alignment && "bad field in queue");
  257. assert(I->Size <= LastSize && "queue not in descending size order");
  258. LastSize = I->Size;
  259. }
  260. }
  261. };
  262. checkQueues();
  263. #endif
  264. /// Helper function to remove a field from a queue.
  265. auto spliceFromQueue = [&](AlignmentQueue *Queue, Field *Last, Field *Cur) {
  266. assert(Last ? Queue->getNext(Last) == Cur : Queue->Head == Cur);
  267. // If we're removing Cur from a non-initial position, splice it out
  268. // of the linked list.
  269. if (Last) {
  270. Last->Scratch = Cur->Scratch;
  271. // If Cur was the last field in the list, we need to update MinSize.
  272. // We can just use the last field's size because the list is in
  273. // descending order of size.
  274. if (!Cur->Scratch)
  275. Queue->MinSize = Last->Size;
  276. // Otherwise, replace the head.
  277. } else {
  278. if (auto NewHead = Queue->getNext(Cur))
  279. Queue->Head = NewHead;
  280. // If we just emptied the queue, destroy its bin.
  281. else
  282. FlexibleFieldsByAlignment.erase(Queue);
  283. }
  284. };
  285. // Do layout into a local array. Doing this in-place on Fields is
  286. // not really feasible.
  287. SmallVector<Field, 16> Layout;
  288. Layout.reserve(Fields.size());
  289. // The offset that we're currently looking to insert at (or after).
  290. uint64_t LastEnd = 0;
  291. // Helper function to splice Cur out of the given queue and add it
  292. // to the layout at the given offset.
  293. auto addToLayout = [&](AlignmentQueue *Queue, Field *Last, Field *Cur,
  294. uint64_t Offset) -> bool {
  295. assert(Offset == alignTo(LastEnd, Cur->Alignment));
  296. // Splice out. This potentially invalidates Queue.
  297. spliceFromQueue(Queue, Last, Cur);
  298. // Add Cur to the layout.
  299. Layout.push_back(*Cur);
  300. Layout.back().Offset = Offset;
  301. LastEnd = Layout.back().getEndOffset();
  302. // Always return true so that we can be tail-called.
  303. return true;
  304. };
  305. // Helper function to try to find a field in the given queue that'll
  306. // fit starting at StartOffset but before EndOffset (if present).
  307. // Note that this never fails if EndOffset is not provided.
  308. auto tryAddFillerFromQueue = [&](AlignmentQueue *Queue, uint64_t StartOffset,
  309. std::optional<uint64_t> EndOffset) -> bool {
  310. assert(Queue->Head);
  311. assert(StartOffset == alignTo(LastEnd, Queue->Alignment));
  312. assert(!EndOffset || StartOffset < *EndOffset);
  313. // Figure out the maximum size that a field can be, and ignore this
  314. // queue if there's nothing in it that small.
  315. auto MaxViableSize =
  316. (EndOffset ? *EndOffset - StartOffset : ~(uint64_t)0);
  317. if (Queue->MinSize > MaxViableSize)
  318. return false;
  319. // Find the matching field. Note that this should always find
  320. // something because of the MinSize check above.
  321. for (Field *Cur = Queue->Head, *Last = nullptr; true;
  322. Last = Cur, Cur = Queue->getNext(Cur)) {
  323. assert(Cur && "didn't find a match in queue despite its MinSize");
  324. if (Cur->Size <= MaxViableSize)
  325. return addToLayout(Queue, Last, Cur, StartOffset);
  326. }
  327. llvm_unreachable("didn't find a match in queue despite its MinSize");
  328. };
  329. // Helper function to find the "best" flexible-offset field according
  330. // to the criteria described above.
  331. auto tryAddBestField = [&](std::optional<uint64_t> BeforeOffset) -> bool {
  332. assert(!BeforeOffset || LastEnd < *BeforeOffset);
  333. auto QueueB = FlexibleFieldsByAlignment.begin();
  334. auto QueueE = FlexibleFieldsByAlignment.end();
  335. // Start by looking for the most-aligned queue that doesn't need any
  336. // leading padding after LastEnd.
  337. auto FirstQueueToSearch = QueueB;
  338. for (; FirstQueueToSearch != QueueE; ++FirstQueueToSearch) {
  339. if (isAligned(FirstQueueToSearch->Alignment, LastEnd))
  340. break;
  341. }
  342. uint64_t Offset = LastEnd;
  343. while (true) {
  344. // Invariant: all of the queues in [FirstQueueToSearch, QueueE)
  345. // require the same initial padding offset.
  346. // Search those queues in descending order of alignment for a
  347. // satisfactory field.
  348. for (auto Queue = FirstQueueToSearch; Queue != QueueE; ++Queue) {
  349. if (tryAddFillerFromQueue(Queue, Offset, BeforeOffset))
  350. return true;
  351. }
  352. // Okay, we don't need to scan those again.
  353. QueueE = FirstQueueToSearch;
  354. // If we started from the first queue, we're done.
  355. if (FirstQueueToSearch == QueueB)
  356. return false;
  357. // Otherwise, scan backwards to find the most-aligned queue that
  358. // still has minimal leading padding after LastEnd. If that
  359. // minimal padding is already at or past the end point, we're done.
  360. --FirstQueueToSearch;
  361. Offset = alignTo(LastEnd, FirstQueueToSearch->Alignment);
  362. if (BeforeOffset && Offset >= *BeforeOffset)
  363. return false;
  364. while (FirstQueueToSearch != QueueB &&
  365. Offset == alignTo(LastEnd, FirstQueueToSearch[-1].Alignment))
  366. --FirstQueueToSearch;
  367. }
  368. };
  369. // Phase 1: fill the gaps between fixed-offset fields with the best
  370. // flexible-offset field that fits.
  371. for (auto I = Fields.begin(); I != FirstFlexible; ++I) {
  372. assert(LastEnd <= I->Offset);
  373. while (LastEnd != I->Offset) {
  374. if (!tryAddBestField(I->Offset))
  375. break;
  376. }
  377. Layout.push_back(*I);
  378. LastEnd = I->getEndOffset();
  379. }
  380. #ifndef NDEBUG
  381. checkQueues();
  382. #endif
  383. // Phase 2: repeatedly add the best flexible-offset field until
  384. // they're all gone.
  385. while (!FlexibleFieldsByAlignment.empty()) {
  386. bool Success = tryAddBestField(std::nullopt);
  387. assert(Success && "didn't find a field with no fixed limit?");
  388. (void) Success;
  389. }
  390. // Copy the layout back into place.
  391. assert(Layout.size() == Fields.size());
  392. memcpy(Fields.data(), Layout.data(),
  393. Fields.size() * sizeof(OptimizedStructLayoutField));
  394. #ifndef NDEBUG
  395. // Make a final check that the layout is valid.
  396. checkValidLayout(Fields, LastEnd, MaxAlign);
  397. #endif
  398. return std::make_pair(LastEnd, MaxAlign);
  399. }