1 //=== - unittest/Support/OptimizedStructLayoutTest.cpp - Layout tests -----===//
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 #include "llvm/Support/OptimizedStructLayout.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
16 class LayoutTest {
17   struct Field {
18     uint64_t Size;
19     Align Alignment;
20     uint64_t ForcedOffset;
21     uint64_t ExpectedOffset;
22   };
23 
24   SmallVector<Field, 16> Fields;
25   bool Verified = false;
26 
27 public:
LayoutTest()28   LayoutTest() {}
29   LayoutTest(const LayoutTest &) = delete;
30   LayoutTest &operator=(const LayoutTest &) = delete;
~LayoutTest()31   ~LayoutTest() { assert(Verified); }
32 
flexible(uint64_t Size,uint64_t Alignment,uint64_t ExpectedOffset)33   LayoutTest &flexible(uint64_t Size, uint64_t Alignment,
34                        uint64_t ExpectedOffset) {
35     Fields.push_back({Size, Align(Alignment),
36                       OptimizedStructLayoutField::FlexibleOffset, ExpectedOffset});
37     return *this;
38   }
39 
fixed(uint64_t Size,uint64_t Alignment,uint64_t Offset)40   LayoutTest &fixed(uint64_t Size, uint64_t Alignment, uint64_t Offset) {
41     Fields.push_back({Size, Align(Alignment), Offset, Offset});
42     return *this;
43   }
44 
verify(uint64_t ExpectedSize,uint64_t ExpectedAlignment)45   void verify(uint64_t ExpectedSize, uint64_t ExpectedAlignment) {
46     SmallVector<OptimizedStructLayoutField, 8> LayoutFields;
47     LayoutFields.reserve(Fields.size());
48     for (auto &F : Fields)
49       LayoutFields.emplace_back(&F, F.Size, F.Alignment, F.ForcedOffset);
50 
51     auto SizeAndAlign = performOptimizedStructLayout(LayoutFields);
52 
53     EXPECT_EQ(SizeAndAlign.first, ExpectedSize);
54     EXPECT_EQ(SizeAndAlign.second, Align(ExpectedAlignment));
55 
56     for (auto &LF : LayoutFields) {
57       auto &F = *static_cast<const Field *>(LF.Id);
58       EXPECT_EQ(LF.Offset, F.ExpectedOffset);
59     }
60 
61     Verified = true;
62   }
63 };
64 
65 }
66 
TEST(OptimizedStructLayoutTest,Basic)67 TEST(OptimizedStructLayoutTest, Basic) {
68   LayoutTest()
69     .flexible(12, 4, 8)
70     .flexible(8,  8, 0)
71     .flexible(4,  4, 20)
72     .verify(24, 8);
73 }
74 
TEST(OptimizedStructLayoutTest,OddSize)75 TEST(OptimizedStructLayoutTest, OddSize) {
76   LayoutTest()
77     .flexible(8,  8, 16)
78     .flexible(4,  4, 12)
79     .flexible(1,  1, 10)
80     .flexible(10, 8, 0)
81     .verify(24, 8);
82 }
83 
TEST(OptimizedStructLayoutTest,Gaps)84 TEST(OptimizedStructLayoutTest, Gaps) {
85   LayoutTest()
86     .fixed(4, 4, 8)
87     .fixed(4, 4, 16)
88     .flexible(4, 4, 0)
89     .flexible(4, 4, 4)
90     .flexible(4, 4, 12)
91     .flexible(4, 4, 20)
92     .verify(24, 4);
93 }
94 
TEST(OptimizedStructLayoutTest,Greed)95 TEST(OptimizedStructLayoutTest, Greed) {
96   // The greedy algorithm doesn't find the optimal layout here, which
97   // would be to put the 5-byte field at the end.
98   LayoutTest()
99     .fixed(4, 4, 8)
100     .flexible(5, 4, 0)
101     .flexible(4, 4, 12)
102     .flexible(4, 4, 16)
103     .flexible(4, 4, 20)
104     .verify(24, 4);
105 }
106 
TEST(OptimizedStructLayoutTest,Jagged)107 TEST(OptimizedStructLayoutTest, Jagged) {
108   LayoutTest()
109     .flexible(1, 2, 18)
110     .flexible(13, 8, 0)
111     .flexible(3, 2, 14)
112     .verify(19, 8);
113 }
114 
TEST(OptimizedStructLayoutTest,GardenPath)115 TEST(OptimizedStructLayoutTest, GardenPath) {
116   // The 4-byte-aligned field is our highest priority, but the less-aligned
117   // fields keep leaving the end offset mis-aligned.
118   LayoutTest()
119     .fixed(7, 4, 0)
120     .flexible(4, 4, 44)
121     .flexible(6, 1, 7)
122     .flexible(5, 1, 13)
123     .flexible(7, 2, 18)
124     .flexible(4, 1, 25)
125     .flexible(4, 1, 29)
126     .flexible(1, 1, 33)
127     .flexible(4, 2, 34)
128     .flexible(4, 2, 38)
129     .flexible(2, 2, 42)
130     .flexible(2, 2, 48)
131     .verify(50, 4);
132 }