xref: /llvm-project/llvm/lib/Support/OptimizedStructLayout.cpp (revision 541ef3d61e9341cd38420c0dbca9250c4d0ea04c)
18423a6f3SJohn McCall //===--- OptimizedStructLayout.cpp - Optimal data layout algorithm ----------------===//
28423a6f3SJohn McCall //
38423a6f3SJohn McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48423a6f3SJohn McCall // See https://llvm.org/LICENSE.txt for license information.
58423a6f3SJohn McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68423a6f3SJohn McCall //
78423a6f3SJohn McCall //===----------------------------------------------------------------------===//
88423a6f3SJohn McCall //
98423a6f3SJohn McCall // This file implements the performOptimizedStructLayout interface.
108423a6f3SJohn McCall //
118423a6f3SJohn McCall //===----------------------------------------------------------------------===//
128423a6f3SJohn McCall 
138423a6f3SJohn McCall #include "llvm/Support/OptimizedStructLayout.h"
14*541ef3d6SKazu Hirata #include <optional>
158423a6f3SJohn McCall 
168423a6f3SJohn McCall using namespace llvm;
178423a6f3SJohn McCall 
188423a6f3SJohn McCall using Field = OptimizedStructLayoutField;
198423a6f3SJohn McCall 
208423a6f3SJohn McCall #ifndef NDEBUG
checkValidLayout(ArrayRef<Field> Fields,uint64_t Size,Align MaxAlign)218423a6f3SJohn McCall static void checkValidLayout(ArrayRef<Field> Fields, uint64_t Size,
228423a6f3SJohn McCall                              Align MaxAlign) {
238423a6f3SJohn McCall   uint64_t LastEnd = 0;
248423a6f3SJohn McCall   Align ComputedMaxAlign;
258423a6f3SJohn McCall   for (auto &Field : Fields) {
268423a6f3SJohn McCall     assert(Field.hasFixedOffset() &&
278423a6f3SJohn McCall            "didn't assign a fixed offset to field");
288423a6f3SJohn McCall     assert(isAligned(Field.Alignment, Field.Offset) &&
298423a6f3SJohn McCall            "didn't assign a correctly-aligned offset to field");
308423a6f3SJohn McCall     assert(Field.Offset >= LastEnd &&
318423a6f3SJohn McCall            "didn't assign offsets in ascending order");
328423a6f3SJohn McCall     LastEnd = Field.getEndOffset();
338423a6f3SJohn McCall     assert(Field.Alignment <= MaxAlign &&
348423a6f3SJohn McCall            "didn't compute MaxAlign correctly");
358423a6f3SJohn McCall     ComputedMaxAlign = std::max(Field.Alignment, MaxAlign);
368423a6f3SJohn McCall   }
378423a6f3SJohn McCall   assert(LastEnd == Size && "didn't compute LastEnd correctly");
388423a6f3SJohn McCall   assert(ComputedMaxAlign == MaxAlign && "didn't compute MaxAlign correctly");
398423a6f3SJohn McCall }
408423a6f3SJohn McCall #endif
418423a6f3SJohn McCall 
428423a6f3SJohn McCall std::pair<uint64_t, Align>
performOptimizedStructLayout(MutableArrayRef<Field> Fields)438423a6f3SJohn McCall llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
448423a6f3SJohn McCall #ifndef NDEBUG
458423a6f3SJohn McCall   // Do some simple precondition checks.
468423a6f3SJohn McCall   {
478423a6f3SJohn McCall     bool InFixedPrefix = true;
488423a6f3SJohn McCall     size_t LastEnd = 0;
498423a6f3SJohn McCall     for (auto &Field : Fields) {
508423a6f3SJohn McCall       assert(Field.Size > 0 && "field of zero size");
518423a6f3SJohn McCall       if (Field.hasFixedOffset()) {
528423a6f3SJohn McCall         assert(InFixedPrefix &&
538423a6f3SJohn McCall                "fixed-offset fields are not a strict prefix of array");
548423a6f3SJohn McCall         assert(LastEnd <= Field.Offset &&
558423a6f3SJohn McCall                "fixed-offset fields overlap or are not in order");
568423a6f3SJohn McCall         LastEnd = Field.getEndOffset();
578423a6f3SJohn McCall         assert(LastEnd > Field.Offset &&
588423a6f3SJohn McCall                "overflow in fixed-offset end offset");
598423a6f3SJohn McCall       } else {
608423a6f3SJohn McCall         InFixedPrefix = false;
618423a6f3SJohn McCall       }
628423a6f3SJohn McCall     }
638423a6f3SJohn McCall   }
648423a6f3SJohn McCall #endif
658423a6f3SJohn McCall 
668423a6f3SJohn McCall   // Do an initial pass over the fields.
678423a6f3SJohn McCall   Align MaxAlign;
688423a6f3SJohn McCall 
698423a6f3SJohn McCall   // Find the first flexible-offset field, tracking MaxAlign.
708423a6f3SJohn McCall   auto FirstFlexible = Fields.begin(), E = Fields.end();
718423a6f3SJohn McCall   while (FirstFlexible != E && FirstFlexible->hasFixedOffset()) {
728423a6f3SJohn McCall     MaxAlign = std::max(MaxAlign, FirstFlexible->Alignment);
738423a6f3SJohn McCall     ++FirstFlexible;
748423a6f3SJohn McCall   }
758423a6f3SJohn McCall 
768423a6f3SJohn McCall   // If there are no flexible fields, we're done.
778423a6f3SJohn McCall   if (FirstFlexible == E) {
788423a6f3SJohn McCall     uint64_t Size = 0;
798423a6f3SJohn McCall     if (!Fields.empty())
808423a6f3SJohn McCall       Size = Fields.back().getEndOffset();
818423a6f3SJohn McCall 
828423a6f3SJohn McCall #ifndef NDEBUG
838423a6f3SJohn McCall     checkValidLayout(Fields, Size, MaxAlign);
848423a6f3SJohn McCall #endif
858423a6f3SJohn McCall     return std::make_pair(Size, MaxAlign);
868423a6f3SJohn McCall   }
878423a6f3SJohn McCall 
888423a6f3SJohn McCall   // Walk over the flexible-offset fields, tracking MaxAlign and
898423a6f3SJohn McCall   // assigning them a unique number in order of their appearance.
908423a6f3SJohn McCall   // We'll use this unique number in the comparison below so that
918423a6f3SJohn McCall   // we can use array_pod_sort, which isn't stable.  We won't use it
928423a6f3SJohn McCall   // past that point.
938423a6f3SJohn McCall   {
948423a6f3SJohn McCall     uintptr_t UniqueNumber = 0;
958423a6f3SJohn McCall     for (auto I = FirstFlexible; I != E; ++I) {
968423a6f3SJohn McCall       I->Scratch = reinterpret_cast<void*>(UniqueNumber++);
978423a6f3SJohn McCall       MaxAlign = std::max(MaxAlign, I->Alignment);
988423a6f3SJohn McCall     }
998423a6f3SJohn McCall   }
1008423a6f3SJohn McCall 
1018423a6f3SJohn McCall   // Sort the flexible elements in order of decreasing alignment,
1028423a6f3SJohn McCall   // then decreasing size, and then the original order as recorded
1038423a6f3SJohn McCall   // in Scratch.  The decreasing-size aspect of this is only really
1048423a6f3SJohn McCall   // important if we get into the gap-filling stage below, but it
1058423a6f3SJohn McCall   // doesn't hurt here.
1068423a6f3SJohn McCall   array_pod_sort(FirstFlexible, E,
1078423a6f3SJohn McCall                  [](const Field *lhs, const Field *rhs) -> int {
1088423a6f3SJohn McCall     // Decreasing alignment.
1098423a6f3SJohn McCall     if (lhs->Alignment != rhs->Alignment)
1108423a6f3SJohn McCall       return (lhs->Alignment < rhs->Alignment ? 1 : -1);
1118423a6f3SJohn McCall 
1128423a6f3SJohn McCall     // Decreasing size.
1138423a6f3SJohn McCall     if (lhs->Size != rhs->Size)
1148423a6f3SJohn McCall       return (lhs->Size < rhs->Size ? 1 : -1);
1158423a6f3SJohn McCall 
1168423a6f3SJohn McCall     // Original order.
1178423a6f3SJohn McCall     auto lhsNumber = reinterpret_cast<uintptr_t>(lhs->Scratch);
1188423a6f3SJohn McCall     auto rhsNumber = reinterpret_cast<uintptr_t>(rhs->Scratch);
1198423a6f3SJohn McCall     if (lhsNumber != rhsNumber)
1208423a6f3SJohn McCall       return (lhsNumber < rhsNumber ? -1 : 1);
1218423a6f3SJohn McCall 
1228423a6f3SJohn McCall     return 0;
1238423a6f3SJohn McCall   });
1248423a6f3SJohn McCall 
1258423a6f3SJohn McCall   // Do a quick check for whether that sort alone has given us a perfect
1268423a6f3SJohn McCall   // layout with no interior padding.  This is very common: if the
1278423a6f3SJohn McCall   // fixed-layout fields have no interior padding, and they end at a
1288423a6f3SJohn McCall   // sufficiently-aligned offset for all the flexible-layout fields,
1298423a6f3SJohn McCall   // and the flexible-layout fields all have sizes that are multiples
1308423a6f3SJohn McCall   // of their alignment, then this will reliably trigger.
1318423a6f3SJohn McCall   {
1328423a6f3SJohn McCall     bool HasPadding = false;
1338423a6f3SJohn McCall     uint64_t LastEnd = 0;
1348423a6f3SJohn McCall 
1358423a6f3SJohn McCall     // Walk the fixed-offset fields.
1368423a6f3SJohn McCall     for (auto I = Fields.begin(); I != FirstFlexible; ++I) {
1378423a6f3SJohn McCall       assert(I->hasFixedOffset());
1388423a6f3SJohn McCall       if (LastEnd != I->Offset) {
1398423a6f3SJohn McCall         HasPadding = true;
1408423a6f3SJohn McCall         break;
1418423a6f3SJohn McCall       }
1428423a6f3SJohn McCall       LastEnd = I->getEndOffset();
1438423a6f3SJohn McCall     }
1448423a6f3SJohn McCall 
1458423a6f3SJohn McCall     // Walk the flexible-offset fields, optimistically assigning fixed
1468423a6f3SJohn McCall     // offsets.  Note that we maintain a strict division between the
1478423a6f3SJohn McCall     // fixed-offset and flexible-offset fields, so if we end up
1488423a6f3SJohn McCall     // discovering padding later in this loop, we can just abandon this
1498423a6f3SJohn McCall     // work and we'll ignore the offsets we already assigned.
1508423a6f3SJohn McCall     if (!HasPadding) {
1518423a6f3SJohn McCall       for (auto I = FirstFlexible; I != E; ++I) {
1528423a6f3SJohn McCall         auto Offset = alignTo(LastEnd, I->Alignment);
1538423a6f3SJohn McCall         if (LastEnd != Offset) {
1548423a6f3SJohn McCall           HasPadding = true;
1558423a6f3SJohn McCall           break;
1568423a6f3SJohn McCall         }
1578423a6f3SJohn McCall         I->Offset = Offset;
1588423a6f3SJohn McCall         LastEnd = I->getEndOffset();
1598423a6f3SJohn McCall       }
1608423a6f3SJohn McCall     }
1618423a6f3SJohn McCall 
1628423a6f3SJohn McCall     // If we already have a perfect layout, we're done.
1638423a6f3SJohn McCall     if (!HasPadding) {
1648423a6f3SJohn McCall #ifndef NDEBUG
1658423a6f3SJohn McCall       checkValidLayout(Fields, LastEnd, MaxAlign);
1668423a6f3SJohn McCall #endif
1678423a6f3SJohn McCall       return std::make_pair(LastEnd, MaxAlign);
1688423a6f3SJohn McCall     }
1698423a6f3SJohn McCall   }
1708423a6f3SJohn McCall 
1718423a6f3SJohn McCall   // The algorithm sketch at this point is as follows.
1728423a6f3SJohn McCall   //
1738423a6f3SJohn McCall   // Consider the padding gaps between fixed-offset fields in ascending
1748423a6f3SJohn McCall   // order.  Let LastEnd be the offset of the first byte following the
1758423a6f3SJohn McCall   // field before the gap, or 0 if the gap is at the beginning of the
1768423a6f3SJohn McCall   // structure.  Find the "best" flexible-offset field according to the
1778423a6f3SJohn McCall   // criteria below.  If no such field exists, proceed to the next gap.
1788423a6f3SJohn McCall   // Otherwise, add the field at the first properly-aligned offset for
1798423a6f3SJohn McCall   // that field that is >= LastEnd, then update LastEnd and repeat in
1808423a6f3SJohn McCall   // order to fill any remaining gap following that field.
1818423a6f3SJohn McCall   //
1828423a6f3SJohn McCall   // Next, let LastEnd to be the offset of the first byte following the
1838423a6f3SJohn McCall   // last fixed-offset field, or 0 if there are no fixed-offset fields.
1848423a6f3SJohn McCall   // While there are flexible-offset fields remaining, find the "best"
1858423a6f3SJohn McCall   // flexible-offset field according to the criteria below, add it at
1868423a6f3SJohn McCall   // the first properly-aligned offset for that field that is >= LastEnd,
1878423a6f3SJohn McCall   // and update LastEnd to the first byte following the field.
1888423a6f3SJohn McCall   //
1898423a6f3SJohn McCall   // The "best" field is chosen by the following criteria, considered
1908423a6f3SJohn McCall   // strictly in order:
1918423a6f3SJohn McCall   //
1928423a6f3SJohn McCall   // - When filling a gap betweeen fields, the field must fit.
1938423a6f3SJohn McCall   // - A field is preferred if it requires less padding following LastEnd.
1948423a6f3SJohn McCall   // - A field is preferred if it is more aligned.
1958423a6f3SJohn McCall   // - A field is preferred if it is larger.
1968423a6f3SJohn McCall   // - A field is preferred if it appeared earlier in the initial order.
1978423a6f3SJohn McCall   //
1988423a6f3SJohn McCall   // Minimizing leading padding is a greedy attempt to avoid padding
1998423a6f3SJohn McCall   // entirely.  Preferring more-aligned fields is an attempt to eliminate
2008423a6f3SJohn McCall   // stricter constraints earlier, with the idea that weaker alignment
2018423a6f3SJohn McCall   // constraints may be resolvable with less padding elsewhere.  These
2028423a6f3SJohn McCall   // These two rules are sufficient to ensure that we get the optimal
2038423a6f3SJohn McCall   // layout in the "C-style" case.  Preferring larger fields tends to take
2048423a6f3SJohn McCall   // better advantage of large gaps and may be more likely to have a size
2058423a6f3SJohn McCall   // that's a multiple of a useful alignment.  Preferring the initial
2068423a6f3SJohn McCall   // order may help somewhat with locality but is mostly just a way of
2078423a6f3SJohn McCall   // ensuring deterministic output.
2088423a6f3SJohn McCall   //
2098423a6f3SJohn McCall   // Note that this algorithm does not guarantee a minimal layout.  Picking
2108423a6f3SJohn McCall   // a larger object greedily may leave a gap that cannot be filled as
2118423a6f3SJohn McCall   // efficiently.  Unfortunately, solving this perfectly is an NP-complete
2128423a6f3SJohn McCall   // problem (by reduction from bin-packing: let B_i be the bin sizes and
2138423a6f3SJohn McCall   // O_j be the object sizes; add fixed-offset fields such that the gaps
2148423a6f3SJohn McCall   // between them have size B_i, and add flexible-offset fields with
2158423a6f3SJohn McCall   // alignment 1 and size O_j; if the layout size is equal to the end of
2168423a6f3SJohn McCall   // the last fixed-layout field, the objects fit in the bins; note that
2178423a6f3SJohn McCall   // this doesn't even require the complexity of alignment).
2188423a6f3SJohn McCall 
2198423a6f3SJohn McCall   // The implementation below is essentially just an optimized version of
2208423a6f3SJohn McCall   // scanning the list of remaining fields looking for the best, which
2218423a6f3SJohn McCall   // would be O(n^2).  In the worst case, it doesn't improve on that.
2228423a6f3SJohn McCall   // However, in practice it'll just scan the array of alignment bins
2238423a6f3SJohn McCall   // and consider the first few elements from one or two bins.  The
2248423a6f3SJohn McCall   // number of bins is bounded by a small constant: alignments are powers
2258423a6f3SJohn McCall   // of two that are vanishingly unlikely to be over 64 and fairly unlikely
2268423a6f3SJohn McCall   // to be over 8.  And multiple elements only need to be considered when
2278423a6f3SJohn McCall   // filling a gap between fixed-offset fields, which doesn't happen very
2288423a6f3SJohn McCall   // often.  We could use a data structure within bins that optimizes for
2298423a6f3SJohn McCall   // finding the best-sized match, but it would require allocating memory
2308423a6f3SJohn McCall   // and copying data, so it's unlikely to be worthwhile.
2318423a6f3SJohn McCall 
2328423a6f3SJohn McCall 
2338423a6f3SJohn McCall   // Start by organizing the flexible-offset fields into bins according to
2348423a6f3SJohn McCall   // their alignment.  We expect a small enough number of bins that we
2358423a6f3SJohn McCall   // don't care about the asymptotic costs of walking this.
2368423a6f3SJohn McCall   struct AlignmentQueue {
2378423a6f3SJohn McCall     /// The minimum size of anything currently in this queue.
2388423a6f3SJohn McCall     uint64_t MinSize;
2398423a6f3SJohn McCall 
2408423a6f3SJohn McCall     /// The head of the queue.  A singly-linked list.  The order here should
2418423a6f3SJohn McCall     /// be consistent with the earlier sort, i.e. the elements should be
2428423a6f3SJohn McCall     /// monotonically descending in size and otherwise in the original order.
2438423a6f3SJohn McCall     ///
2448423a6f3SJohn McCall     /// We remove the queue from the array as soon as this is empty.
2458423a6f3SJohn McCall     OptimizedStructLayoutField *Head;
2468423a6f3SJohn McCall 
2478423a6f3SJohn McCall     /// The alignment requirement of the queue.
2488423a6f3SJohn McCall     Align Alignment;
2498423a6f3SJohn McCall 
2508423a6f3SJohn McCall     static Field *getNext(Field *Cur) {
2518423a6f3SJohn McCall       return static_cast<Field *>(Cur->Scratch);
2528423a6f3SJohn McCall     }
2538423a6f3SJohn McCall   };
2548423a6f3SJohn McCall   SmallVector<AlignmentQueue, 8> FlexibleFieldsByAlignment;
2558423a6f3SJohn McCall   for (auto I = FirstFlexible; I != E; ) {
2568423a6f3SJohn McCall     auto Head = I;
2578423a6f3SJohn McCall     auto Alignment = I->Alignment;
2588423a6f3SJohn McCall 
2598423a6f3SJohn McCall     uint64_t MinSize = I->Size;
2608423a6f3SJohn McCall     auto LastInQueue = I;
2618423a6f3SJohn McCall     for (++I; I != E && I->Alignment == Alignment; ++I) {
2628423a6f3SJohn McCall       LastInQueue->Scratch = I;
2638423a6f3SJohn McCall       LastInQueue = I;
2648423a6f3SJohn McCall       MinSize = std::min(MinSize, I->Size);
2658423a6f3SJohn McCall     }
2668423a6f3SJohn McCall     LastInQueue->Scratch = nullptr;
2678423a6f3SJohn McCall 
2688423a6f3SJohn McCall     FlexibleFieldsByAlignment.push_back({MinSize, Head, Alignment});
2698423a6f3SJohn McCall   }
2708423a6f3SJohn McCall 
2718423a6f3SJohn McCall #ifndef NDEBUG
2728423a6f3SJohn McCall   // Verify that we set the queues up correctly.
2738423a6f3SJohn McCall   auto checkQueues = [&]{
2748423a6f3SJohn McCall     bool FirstQueue = true;
2758423a6f3SJohn McCall     Align LastQueueAlignment;
2768423a6f3SJohn McCall     for (auto &Queue : FlexibleFieldsByAlignment) {
2778423a6f3SJohn McCall       assert((FirstQueue || Queue.Alignment < LastQueueAlignment) &&
2788423a6f3SJohn McCall              "bins not in order of descending alignment");
2798423a6f3SJohn McCall       LastQueueAlignment = Queue.Alignment;
2808423a6f3SJohn McCall       FirstQueue = false;
2818423a6f3SJohn McCall 
2828423a6f3SJohn McCall       assert(Queue.Head && "queue was empty");
2838423a6f3SJohn McCall       uint64_t LastSize = ~(uint64_t)0;
2848423a6f3SJohn McCall       for (auto I = Queue.Head; I; I = Queue.getNext(I)) {
2858423a6f3SJohn McCall         assert(I->Alignment == Queue.Alignment && "bad field in queue");
2868423a6f3SJohn McCall         assert(I->Size <= LastSize && "queue not in descending size order");
2878423a6f3SJohn McCall         LastSize = I->Size;
2888423a6f3SJohn McCall       }
2898423a6f3SJohn McCall     }
2908423a6f3SJohn McCall   };
2918423a6f3SJohn McCall   checkQueues();
2928423a6f3SJohn McCall #endif
2938423a6f3SJohn McCall 
2948423a6f3SJohn McCall   /// Helper function to remove a field from a queue.
2958423a6f3SJohn McCall   auto spliceFromQueue = [&](AlignmentQueue *Queue, Field *Last, Field *Cur) {
2968423a6f3SJohn McCall     assert(Last ? Queue->getNext(Last) == Cur : Queue->Head == Cur);
2978423a6f3SJohn McCall 
2988423a6f3SJohn McCall     // If we're removing Cur from a non-initial position, splice it out
2998423a6f3SJohn McCall     // of the linked list.
3008423a6f3SJohn McCall     if (Last) {
3018423a6f3SJohn McCall       Last->Scratch = Cur->Scratch;
3028423a6f3SJohn McCall 
3038423a6f3SJohn McCall       // If Cur was the last field in the list, we need to update MinSize.
3048423a6f3SJohn McCall       // We can just use the last field's size because the list is in
3058423a6f3SJohn McCall       // descending order of size.
3068423a6f3SJohn McCall       if (!Cur->Scratch)
3078423a6f3SJohn McCall         Queue->MinSize = Last->Size;
3088423a6f3SJohn McCall 
3098423a6f3SJohn McCall     // Otherwise, replace the head.
3108423a6f3SJohn McCall     } else {
3118423a6f3SJohn McCall       if (auto NewHead = Queue->getNext(Cur))
3128423a6f3SJohn McCall         Queue->Head = NewHead;
3138423a6f3SJohn McCall 
3148423a6f3SJohn McCall       // If we just emptied the queue, destroy its bin.
3158423a6f3SJohn McCall       else
3168423a6f3SJohn McCall         FlexibleFieldsByAlignment.erase(Queue);
3178423a6f3SJohn McCall     }
3188423a6f3SJohn McCall   };
3198423a6f3SJohn McCall 
3208423a6f3SJohn McCall   // Do layout into a local array.  Doing this in-place on Fields is
3218423a6f3SJohn McCall   // not really feasible.
3228423a6f3SJohn McCall   SmallVector<Field, 16> Layout;
3238423a6f3SJohn McCall   Layout.reserve(Fields.size());
3248423a6f3SJohn McCall 
3258423a6f3SJohn McCall   // The offset that we're currently looking to insert at (or after).
3268423a6f3SJohn McCall   uint64_t LastEnd = 0;
3278423a6f3SJohn McCall 
3288423a6f3SJohn McCall   // Helper function to splice Cur out of the given queue and add it
3298423a6f3SJohn McCall   // to the layout at the given offset.
3308423a6f3SJohn McCall   auto addToLayout = [&](AlignmentQueue *Queue, Field *Last, Field *Cur,
3318423a6f3SJohn McCall                          uint64_t Offset) -> bool {
3328423a6f3SJohn McCall     assert(Offset == alignTo(LastEnd, Cur->Alignment));
3338423a6f3SJohn McCall 
3348423a6f3SJohn McCall     // Splice out.  This potentially invalidates Queue.
3358423a6f3SJohn McCall     spliceFromQueue(Queue, Last, Cur);
3368423a6f3SJohn McCall 
3378423a6f3SJohn McCall     // Add Cur to the layout.
3388423a6f3SJohn McCall     Layout.push_back(*Cur);
3398423a6f3SJohn McCall     Layout.back().Offset = Offset;
3408423a6f3SJohn McCall     LastEnd = Layout.back().getEndOffset();
3418423a6f3SJohn McCall 
3428423a6f3SJohn McCall     // Always return true so that we can be tail-called.
3438423a6f3SJohn McCall     return true;
3448423a6f3SJohn McCall   };
3458423a6f3SJohn McCall 
3468423a6f3SJohn McCall   // Helper function to try to find a field in the given queue that'll
3478423a6f3SJohn McCall   // fit starting at StartOffset but before EndOffset (if present).
3488423a6f3SJohn McCall   // Note that this never fails if EndOffset is not provided.
349b1df3a2cSFangrui Song   auto tryAddFillerFromQueue = [&](AlignmentQueue *Queue, uint64_t StartOffset,
350b1df3a2cSFangrui Song                                    std::optional<uint64_t> EndOffset) -> bool {
3518423a6f3SJohn McCall     assert(Queue->Head);
3528423a6f3SJohn McCall     assert(StartOffset == alignTo(LastEnd, Queue->Alignment));
353326a5a26SJohn McCall     assert(!EndOffset || StartOffset < *EndOffset);
3548423a6f3SJohn McCall 
3558423a6f3SJohn McCall     // Figure out the maximum size that a field can be, and ignore this
3568423a6f3SJohn McCall     // queue if there's nothing in it that small.
3578423a6f3SJohn McCall     auto MaxViableSize =
3588423a6f3SJohn McCall       (EndOffset ? *EndOffset - StartOffset : ~(uint64_t)0);
359b1df3a2cSFangrui Song     if (Queue->MinSize > MaxViableSize)
360b1df3a2cSFangrui Song       return false;
3618423a6f3SJohn McCall 
3628423a6f3SJohn McCall     // Find the matching field.  Note that this should always find
3638423a6f3SJohn McCall     // something because of the MinSize check above.
3648423a6f3SJohn McCall     for (Field *Cur = Queue->Head, *Last = nullptr; true;
3658423a6f3SJohn McCall            Last = Cur, Cur = Queue->getNext(Cur)) {
3668423a6f3SJohn McCall       assert(Cur && "didn't find a match in queue despite its MinSize");
3678423a6f3SJohn McCall       if (Cur->Size <= MaxViableSize)
3688423a6f3SJohn McCall         return addToLayout(Queue, Last, Cur, StartOffset);
3698423a6f3SJohn McCall     }
3708423a6f3SJohn McCall 
3718423a6f3SJohn McCall     llvm_unreachable("didn't find a match in queue despite its MinSize");
3728423a6f3SJohn McCall   };
3738423a6f3SJohn McCall 
3748423a6f3SJohn McCall   // Helper function to find the "best" flexible-offset field according
3758423a6f3SJohn McCall   // to the criteria described above.
376b1df3a2cSFangrui Song   auto tryAddBestField = [&](std::optional<uint64_t> BeforeOffset) -> bool {
377326a5a26SJohn McCall     assert(!BeforeOffset || LastEnd < *BeforeOffset);
3788423a6f3SJohn McCall     auto QueueB = FlexibleFieldsByAlignment.begin();
3798423a6f3SJohn McCall     auto QueueE = FlexibleFieldsByAlignment.end();
3808423a6f3SJohn McCall 
3818423a6f3SJohn McCall     // Start by looking for the most-aligned queue that doesn't need any
3828423a6f3SJohn McCall     // leading padding after LastEnd.
3838423a6f3SJohn McCall     auto FirstQueueToSearch = QueueB;
3848423a6f3SJohn McCall     for (; FirstQueueToSearch != QueueE; ++FirstQueueToSearch) {
3858423a6f3SJohn McCall       if (isAligned(FirstQueueToSearch->Alignment, LastEnd))
3868423a6f3SJohn McCall         break;
3878423a6f3SJohn McCall     }
3888423a6f3SJohn McCall 
3898423a6f3SJohn McCall     uint64_t Offset = LastEnd;
3908423a6f3SJohn McCall     while (true) {
3918423a6f3SJohn McCall       // Invariant: all of the queues in [FirstQueueToSearch, QueueE)
3928423a6f3SJohn McCall       // require the same initial padding offset.
3938423a6f3SJohn McCall 
3948423a6f3SJohn McCall       // Search those queues in descending order of alignment for a
3958423a6f3SJohn McCall       // satisfactory field.
3968423a6f3SJohn McCall       for (auto Queue = FirstQueueToSearch; Queue != QueueE; ++Queue) {
3978423a6f3SJohn McCall         if (tryAddFillerFromQueue(Queue, Offset, BeforeOffset))
3988423a6f3SJohn McCall           return true;
3998423a6f3SJohn McCall       }
4008423a6f3SJohn McCall 
4018423a6f3SJohn McCall       // Okay, we don't need to scan those again.
4028423a6f3SJohn McCall       QueueE = FirstQueueToSearch;
4038423a6f3SJohn McCall 
4048423a6f3SJohn McCall       // If we started from the first queue, we're done.
4058423a6f3SJohn McCall       if (FirstQueueToSearch == QueueB)
4068423a6f3SJohn McCall         return false;
4078423a6f3SJohn McCall 
4088423a6f3SJohn McCall       // Otherwise, scan backwards to find the most-aligned queue that
409326a5a26SJohn McCall       // still has minimal leading padding after LastEnd.  If that
410326a5a26SJohn McCall       // minimal padding is already at or past the end point, we're done.
4118423a6f3SJohn McCall       --FirstQueueToSearch;
4128423a6f3SJohn McCall       Offset = alignTo(LastEnd, FirstQueueToSearch->Alignment);
413326a5a26SJohn McCall       if (BeforeOffset && Offset >= *BeforeOffset)
414326a5a26SJohn McCall         return false;
4158423a6f3SJohn McCall       while (FirstQueueToSearch != QueueB &&
4168423a6f3SJohn McCall              Offset == alignTo(LastEnd, FirstQueueToSearch[-1].Alignment))
4178423a6f3SJohn McCall         --FirstQueueToSearch;
4188423a6f3SJohn McCall     }
4198423a6f3SJohn McCall   };
4208423a6f3SJohn McCall 
4218423a6f3SJohn McCall   // Phase 1: fill the gaps between fixed-offset fields with the best
4228423a6f3SJohn McCall   // flexible-offset field that fits.
4238423a6f3SJohn McCall   for (auto I = Fields.begin(); I != FirstFlexible; ++I) {
424326a5a26SJohn McCall     assert(LastEnd <= I->Offset);
4258423a6f3SJohn McCall     while (LastEnd != I->Offset) {
4268423a6f3SJohn McCall       if (!tryAddBestField(I->Offset))
4278423a6f3SJohn McCall         break;
4288423a6f3SJohn McCall     }
4298423a6f3SJohn McCall     Layout.push_back(*I);
4308423a6f3SJohn McCall     LastEnd = I->getEndOffset();
4318423a6f3SJohn McCall   }
4328423a6f3SJohn McCall 
4338423a6f3SJohn McCall #ifndef NDEBUG
4348423a6f3SJohn McCall   checkQueues();
4358423a6f3SJohn McCall #endif
4368423a6f3SJohn McCall 
4378423a6f3SJohn McCall   // Phase 2: repeatedly add the best flexible-offset field until
4388423a6f3SJohn McCall   // they're all gone.
4398423a6f3SJohn McCall   while (!FlexibleFieldsByAlignment.empty()) {
440aadaafacSKazu Hirata     bool Success = tryAddBestField(std::nullopt);
4418423a6f3SJohn McCall     assert(Success && "didn't find a field with no fixed limit?");
4428423a6f3SJohn McCall     (void) Success;
4438423a6f3SJohn McCall   }
4448423a6f3SJohn McCall 
4458423a6f3SJohn McCall   // Copy the layout back into place.
4468423a6f3SJohn McCall   assert(Layout.size() == Fields.size());
4478423a6f3SJohn McCall   memcpy(Fields.data(), Layout.data(),
4488423a6f3SJohn McCall          Fields.size() * sizeof(OptimizedStructLayoutField));
4498423a6f3SJohn McCall 
4508423a6f3SJohn McCall #ifndef NDEBUG
4518423a6f3SJohn McCall   // Make a final check that the layout is valid.
4528423a6f3SJohn McCall   checkValidLayout(Fields, LastEnd, MaxAlign);
4538423a6f3SJohn McCall #endif
4548423a6f3SJohn McCall 
4558423a6f3SJohn McCall   return std::make_pair(LastEnd, MaxAlign);
4568423a6f3SJohn McCall }
457