Skip to content

Commit

Permalink
AggrTypeBuilder: mark aggregate as packed when field offset is not na…
Browse files Browse the repository at this point in the history
…turally aligned, to fix #4719 (#4722)
  • Loading branch information
JohanEngelen authored Aug 5, 2024
1 parent 48c1dff commit 7486117
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ir/irtypeaggr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ AggrTypeBuilder::AggrTypeBuilder(unsigned offset) : m_offset(offset) {
void AggrTypeBuilder::addType(llvm::Type *type, unsigned size) {
const unsigned fieldAlignment = getABITypeAlign(type);
assert(fieldAlignment);
assert((m_offset & (fieldAlignment - 1)) == 0 && "Field is misaligned");
// If the field offset does not have natural alignment, mark the aggregate as
// packed for IR.
if ((m_offset & (fieldAlignment - 1)) != 0) {
m_packed = true;
}
m_defaultTypes.push_back(type);
m_offset += size;
m_fieldIndex++;
Expand Down
33 changes: 33 additions & 0 deletions tests/compilable/gh4719.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %ldc -c %s
// RUN: %ldc -c %s -O

struct TraceBuf {
align(1) uint args;
}

// Test correct compilation (no error) of the context pointer type for the delegate of `foo`.
void foo() {
byte[2] fixDescs;
TraceBuf fixLog;

auto dlg = delegate() {
fixDescs[0] = 1;
fixLog.args = 1;
};
}

class TraceClass {
align(1)
uint args;
}

// Test correct compilation (no error) of the context pointer type for the delegate of `foo2`.
void foo2() {
byte[2] fixDescs;
scope TraceClass fixLog;

auto dlg = delegate() {
fixDescs[0] = 1;
fixLog.args = 1;
};
}

0 comments on commit 7486117

Please sign in to comment.