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