Page MenuHomePhabricator

MS ABI: Consider alignment attributes on typedefs for layout
ClosedPublic

Authored by majnemer on Jul 29 2014, 3:45 PM.

Details

Summary

The MS ABI has a notion of 'required alignment' for fields; this
alignment supercedes pragma pack directives.

MSVC takes into account alignment attributes on typedefs when
determining whether or not a field has a certain required alignment.

Do the same in clang by tracking whether or not we saw such an attribute
when calculating the type's bitwidth and alignment.

This fixes PR20418.

Diff Detail

Repository
rL LLVM

Event Timeline

majnemer updated this revision to Diff 11999.Jul 29 2014, 3:45 PM
majnemer retitled this revision from to MS ABI: Consider alignment attributes on typedefs for layout.
majnemer updated this object.
majnemer added a reviewer: rnk.
majnemer added a subscriber: Unknown Object (MLST).
rnk edited edge metadata.Jul 29 2014, 4:00 PM

I'm skeptical that only one bit is enough information here...

include/clang/AST/ASTContext.h
147 ↗(On Diff #11999)

I'd rather just throw this in clang::. We can use it in record layout.

1658–1663 ↗(On Diff #11999)

Grumble grumble these helpers are just asking you to do multiple hash table lookups.... Nothing to do here, though.

test/Layout/ms-x86-pack-and-align.cpp
675 ↗(On Diff #11999)

Can you also add this test, just to satisfy my curiosity?

typedef int __declspec(align(2)) my_int_t;
#pragma pack(push, 1)
struct A {
  char a;
  my_int_t b;
};
#pragma pack(pop)
static_assert(sizeof(A) == 6, "");

I suspect because the required alignment is actually *lower* than the natural alignment of int, we will take the max alignment between the alignment attribute and the natural alignment get sizeof(A) == 8.

majnemer updated this revision to Diff 12004.Jul 29 2014, 6:04 PM
majnemer edited edge metadata.
  • Address review feedback
rnk accepted this revision.Jul 29 2014, 6:17 PM
rnk edited edge metadata.

lgtm

lib/AST/RecordLayoutBuilder.cpp
2264–2265 ↗(On Diff #12004)

Add a comment that this ignores alignment attributes, i.e. it gets the natural alignment.

This revision is now accepted and ready to land.Jul 29 2014, 6:17 PM
majnemer closed this revision.Jul 29 2014, 6:39 PM
majnemer updated this revision to Diff 12006.

Closed by commit rL214274 (authored by @majnemer).

emaste added a subscriber: emaste.Jul 30 2014, 12:16 PM
emaste added inline comments.
cfe/trunk/include/clang/AST/ASTContext.h
1625

This will need a corresponding change to LLDB