This is an archive of the discontinued LLVM Phabricator instance.

PR25575: Make GCC 4.4+ comatible layout for packed bit-fileds of char type
ClosedPublic

Authored by DmitryPolukhin on Nov 20 2015, 6:15 AM.

Details

Summary

This CL is for discussion how to better fix bit-filed layout compatibility issue with GCC (see PR25575 for test case and more details). Current clang behavior is compatible with GCC 4.1-4.3 series but it was fixed in 4.4+. Ignoring packed attribute looks very odd and because it was also fixed in GCC 4.4+, it makes sense also fix it in clang.

Open questions:

  1. Should we add warning about changes in layout for packed bit-fileds of char type? GCC has it "note: offset of packed bit-field ‘b’ has changed in GCC 4.4" will add if needed.
  1. This CL completely removes warning "packed attribute ignored for field of type char". Should we keep it but apply only to normal fields (i.e. not bit-fields)?

This CL removes the warning because IMHO it makes no sense if we fix bit-field case to match GCC 4.4+. Packed attribute was actually ignored only for bit-field. For all other cases it is not ignored but reported only when alignment is already fulfilled (i.e. attribute just is not required and doesn't change anything). But there are tons of cases when packed attribute doesn't change anything but warning is not emitted, for example:
struct S1 {

char a;
char b __attribute((packed)); // warning: 'packed' attribute ignored for field of type 'char'
char c;

};

extern int a[sizeof(struct S1) == 3 ? 1 : -1];
extern int b[__alignof(struct S1) == 1 ? 1 : -1];

struct S2 {

short a;
short b __attribute((packed)); // no warning
short c;

};

extern int c[sizeof(struct S2) == 6 ? 1 : -1];
extern int d[__alignof(struct S2) == 2 ? 1 : -1];

In both cases you can remove __attribute((packed)) and program behavior will not change. But warning is generated only in the first case.

Diff Detail

Repository
rL LLVM

Event Timeline

DmitryPolukhin retitled this revision from to PR25575: Make GCC 4.4+ comatible layout for packed bit-fileds of char type.
DmitryPolukhin updated this object.
DmitryPolukhin added a reviewer: aaron.ballman.
DmitryPolukhin added a subscriber: cfe-commits.

It seems that check for type alignment <= 8 was there practically forever http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?r1=47197&r2=47196&pathrev=47197 and there is no good explanation why it was implemented. Subsequent changes only add more condition to exclude cases when it shouldn't be reported.

Please upload this patch with full context, see: http://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface

Should we add warning about changes in layout for packed bit-fileds of char type? GCC has it "note: offset of packed bit-field ‘b’ has changed in GCC 4.4" will add if needed.

Warning about this is a good idea. We did something similar when we (re-)introduced sync_fetch_and_nand, see:

def warn_sync_fetch_and_nand_semantics_change : Warning<
  "the semantics of this intrinsic changed with GCC "
  "version 4.4 - the newer semantics are provided here">,
  InGroup<DiagGroup<"sync-fetch-and-nand-semantics-changed">>;

Added warning about semantic change + uploaded context. PTAL

hfinkel added inline comments.Nov 25 2015, 1:53 PM
include/clang/Basic/DiagnosticSemaKinds.td
2783 ↗(On Diff #41131)

Calling this "a semantic" reads oddly to me. This sounds better to me:

def note_attribute_packed_for_bitfield_offset_changed : Warning<
  "the offset assigned to packed bit-field member %0 has changed with GCC version 4.4 - "
  "the newer offset is used here">,
  InGroup<DiagGroup<"attribute-packed-bitfield-offset-changed">>;
lib/Sema/SemaDeclAttr.cpp
1040 ↗(On Diff #41131)

This comment is now out of date?

DmitryPolukhin marked 2 inline comments as done.

Changed note text message + fixed outdated comment.

This is another GCC ABI compatibility issue. If there is no more comments, could someone please approve this CL?

aaron.ballman added inline comments.Dec 1 2015, 5:54 AM
lib/Sema/SemaDeclAttr.cpp
1046 ↗(On Diff #41222)

No need to call getDeclName(), the diagnostic can accept a NamedDecl directly (which properly quotes the name in the diagnostic).

DmitryPolukhin marked an inline comment as done.

Don't call getDeclName() that it is not required. PTAL

rjmccall added inline comments.Dec 1 2015, 11:49 AM
include/clang/Basic/DiagnosticSemaKinds.td
2790 ↗(On Diff #41510)

No, this diagnostic shouldn't make such an affirmative statement about the offset changing. I would instead suggest:

warning: 'packed' attribute was ignored on bit-fields with alignment 1 in older versions of GCC and Clang
DmitryPolukhin marked an inline comment as done.

Warning text updated, PTAL.

aaron.ballman added inline comments.Dec 2 2015, 7:56 AM
include/clang/Basic/DiagnosticSemaKinds.td
2788 ↗(On Diff #41597)

Would this read better as "on bit-fields with single-byte alignment" instead of "on bit-fields with alignment 1"?

test/Sema/struct-packed-align.c
155 ↗(On Diff #41597)

"ignores uses" -> "uses"

DmitryPolukhin marked 2 inline comments as done.

Fixed warning and comment.

rjmccall edited edge metadata.Dec 2 2015, 11:04 AM

LGTM, thanks!

aaron.ballman accepted this revision.Dec 2 2015, 11:15 AM
aaron.ballman edited edge metadata.

LGTM! Thank you!

This revision is now accepted and ready to land.Dec 2 2015, 11:15 AM
This revision was automatically updated to reflect the committed changes.