Skip to content
Open
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
31 changes: 21 additions & 10 deletions clang/lib/AST/RecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,13 +1280,6 @@ ItaniumRecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
(void)Allowed;
assert(Allowed && "Base subobject externally placed at overlapping offset");

if (InferAlignment && Offset < getDataSize().alignTo(AlignTo)) {
// The externally-supplied base offset is before the base offset we
// computed. Assume that the structure is packed.
Alignment = CharUnits::One();
InferAlignment = false;
}
}

if (!Base->Class->isEmpty()) {
Expand Down Expand Up @@ -2243,9 +2236,27 @@ ItaniumRecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
uint64_t ComputedOffset) {
uint64_t ExternalFieldOffset = External.getExternalFieldOffset(Field);

if (InferAlignment && ExternalFieldOffset < ComputedOffset) {
// The externally-supplied field offset is before the field offset we
// computed. Assume that the structure is packed.
// DWARF doesn't tell us whether a structure was declared as packed.
// So we try to figure out if the supplied Field is at a packed offset
// (i.e., the externally-supplied offset is less than the layout builder
// expected).
//
// There are cases where fields are placed at overlapping offsets (e.g.,
// as a result of [[no_unique_address]]). In those cases we don't want
// to incorrectly deduce that they are placed at packed offsets. Hence,
// ignore empty fields (which are the only fields that can overlap).
//
// FIXME: emit enough information in DWARF to get rid of InferAlignment.
//
CXXRecordDecl *CXX = nullptr;
if (auto *RT = dyn_cast<RecordType>(Field->getType()))
CXX = RT->getAsCXXRecordDecl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all just auto CXX = Field->getType()->getAsCXXRecordDecl();


const bool assume_packed = ExternalFieldOffset > 0 &&
Copy link
Contributor

@rjmccall rjmccall Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally use snake_case in LLVM projects; this should be named AssumePacked.

Why the > 0 condition?

I'm not sure I understand why we need to update anything here. When we have an external layout, do we not eventually just take the alignment from the external layout anyway? If so, what's the point of the extra bookkeeping?

ExternalFieldOffset < ComputedOffset &&
!(CXX && CXX->isEmpty());

if (InferAlignment && assume_packed) {
Alignment = CharUnits::One();
PreferredAlignment = CharUnits::One();
InferAlignment = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clangxx_host -gdwarf -o %t %s
// RUN: %lldb %t \
// RUN: -o "expr alignof(PackedNUA)" \
// RUN: -o "expr sizeof(PackedNUA)" \
// RUN: -o exit | FileCheck %s

struct Empty {};
struct __attribute((packed)) PackedNUA {
[[no_unique_address]] Empty a, b, c, d;
char x;
int y;
};
static_assert(alignof(PackedNUA) == 1);
static_assert(sizeof(PackedNUA) == 5);

PackedNUA packed;

// CHECK: (lldb) expr alignof(PackedNUA)
// CHECK-NEXT: ${{.*}} = 1
// CHECK: (lldb) expr sizeof(PackedNUA)
// CHECK-NEXT: ${{.*}} = 5

int main() { PackedNUA d; }
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// XFAIL: *

// RUN: %clang --target=x86_64-apple-macosx -c -gdwarf -o %t %s
// RUN: %lldb %t \
// RUN: -o "expr alignof(OverlappingFields)" \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// XFAIL: *

// RUN: %clang --target=x86_64-apple-macosx -c -gdwarf -o %t %s
// RUN: %lldb %t \
// RUN: -o "expr alignof(OverlappingDerived)" \
Expand Down