Skip to content

Fix a bug in OptimizedStructLayout when filling gaps before #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion llvm/lib/Support/OptimizedStructLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
Optional<uint64_t> EndOffset) -> bool {
assert(Queue->Head);
assert(StartOffset == alignTo(LastEnd, Queue->Alignment));
assert(!EndOffset || StartOffset < *EndOffset);

// Figure out the maximum size that a field can be, and ignore this
// queue if there's nothing in it that small.
Expand All @@ -372,6 +373,7 @@ llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
// Helper function to find the "best" flexible-offset field according
// to the criteria described above.
auto tryAddBestField = [&](Optional<uint64_t> BeforeOffset) -> bool {
assert(!BeforeOffset || LastEnd < *BeforeOffset);
auto QueueB = FlexibleFieldsByAlignment.begin();
auto QueueE = FlexibleFieldsByAlignment.end();

Expand Down Expand Up @@ -403,9 +405,12 @@ llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
return false;

// Otherwise, scan backwards to find the most-aligned queue that
// still has minimal leading padding after LastEnd.
// still has minimal leading padding after LastEnd. If that
// minimal padding is already at or past the end point, we're done.
--FirstQueueToSearch;
Offset = alignTo(LastEnd, FirstQueueToSearch->Alignment);
if (BeforeOffset && Offset >= *BeforeOffset)
return false;
while (FirstQueueToSearch != QueueB &&
Offset == alignTo(LastEnd, FirstQueueToSearch[-1].Alignment))
--FirstQueueToSearch;
Expand All @@ -415,6 +420,7 @@ llvm::performOptimizedStructLayout(MutableArrayRef<Field> Fields) {
// Phase 1: fill the gaps between fixed-offset fields with the best
// flexible-offset field that fits.
for (auto I = Fields.begin(); I != FirstFlexible; ++I) {
assert(LastEnd <= I->Offset);
while (LastEnd != I->Offset) {
if (!tryAddBestField(I->Offset))
break;
Expand Down
19 changes: 18 additions & 1 deletion llvm/unittests/Support/OptimizedStructLayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,21 @@ TEST(OptimizedStructLayoutTest, GardenPath) {
.flexible(2, 2, 42)
.flexible(2, 2, 48)
.verify(50, 4);
}
}

// PR 51131
TEST(OptimizedStructLayoutTest, HighAlignment) {
// Handle the case where a flexible field has such a high alignment
// requirement that aligning LastEnd to it gives an offset past the
// end of the gap before the next fixed-alignment field.
LayoutTest()
.fixed(8, 8, 0)
.fixed(8, 8, 8)
.fixed(64, 64, 64)
.flexible(1, 1, 16)
.flexible(1, 1, 17)
.flexible(4, 128, 128)
.flexible(1, 1, 18)
.flexible(1, 1, 19)
.verify(132, 128);
}