| 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: |
| 28 |
LayoutTest() {} |
| 29 |
LayoutTest(const LayoutTest &) = delete; |
| 30 |
LayoutTest &operator=(const LayoutTest &) = delete; |
| 31 |
~LayoutTest() { assert(Verified); } |
| 32 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |
| 133 |
|
| 134 |
// PR 51131 |
| 135 |
TEST(OptimizedStructLayoutTest, HighAlignment) { |
| 136 |
// Handle the case where a flexible field has such a high alignment |
| 137 |
// requirement that aligning LastEnd to it gives an offset past the |
| 138 |
// end of the gap before the next fixed-alignment field. |
| 139 |
LayoutTest() |
| 140 |
.fixed(8, 8, 0) |
| 141 |
.fixed(8, 8, 8) |
| 142 |
.fixed(64, 64, 64) |
| 143 |
.flexible(1, 1, 16) |
| 144 |
.flexible(1, 1, 17) |
| 145 |
.flexible(4, 128, 128) |
| 146 |
.flexible(1, 1, 18) |
| 147 |
.flexible(1, 1, 19) |
| 148 |
.verify(132, 128); |
| 149 |
} |