Skip to content
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

AggrTypeBuilder: mark aggregate as packed when field offset is not naturally aligned, to fix #4719 #4722

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
32 changes: 32 additions & 0 deletions tests/codegen/gh4719.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %ldc -c -output-ll -of=%t.ll %s
JohanEngelen marked this conversation as resolved.
Show resolved Hide resolved

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;
};
}
Loading