Bug 23071 - Darwin alignment ignores "attribute packed" for first 'double' element of a struct
Summary: Darwin alignment ignores "attribute packed" for first 'double' element of a s...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: unknown
: P2 normal
Target Milestone: 4.5.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-26 06:42 UTC by Chris Lattner
Modified: 2010-03-22 23:24 UTC (History)
4 users (show)

See Also:
Host:
Target: powerpc-darwin, powerpc-aix
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-01-15 21:14:22


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Lattner 2005-07-26 06:42:38 UTC
Darwin ignores the alignment/packed attributes on this structure, forcing the
structure to have 8-byte alignment and 16-byte size:

---
struct Test {
  double D __attribute__((packed,aligned(4)));
  short X;
};
---

Ian Lance Taylor did a great analysis of the history of this, tracking it back
to a patch by David Edelsohn in 1996.  The analysis is here:
http://gcc.gnu.org/ml/gcc-help/2005-07/msg00280.html

-Chris
Comment 1 Andrew Pinski 2005-07-26 13:21:11 UTC
I still don't believe this is a bug.
As the alignment of whole struct is still 8 as double is first, even if the alignment of that double is 4.
Comment 2 David Edelsohn 2005-07-26 14:08:23 UTC
The ABI specifies the alignment of the entire record is doubleword if the 
first field is an FP double, regardless of the alignment of the type itself.
Comment 3 Chris Lattner 2005-07-26 16:25:10 UTC
Okay, in that case, how does one change the alignment of the structure?  The only way I'm aware of to 
do this is with attribute packed, but that will also modify the inter-field padding of the structure.  For 
example, in this case:

struct Test {
  double D __attribute__((packed,aligned(4)));
  short X;
  int Y;
  short Z;
} __attribute__((packed,aligned(4)));

Not only is the alignment of the structure reduced to 4 bytes, the structure is also packed, resulting in 
the 'int' being unaligned and the size of the structure being 16 bytes (instead of 20).

"it sure would be nice if you could use 'attribute aligned' to reduce alignment without attribute packed"

-Chris
Comment 4 Ian Lance Taylor 2005-07-26 16:55:21 UTC
The whole point of attribute ((packed)) is to change the ABI.  If it can't
change the ABI, it is meaningless.  So I don't find the argument based on the
ABI to be particularly convincing.
Comment 5 David Edelsohn 2005-07-26 16:58:27 UTC
Subject: Re:  Darwin alignment ignores "attribute packed" for first 'double' element of a struct 

	If Chris and Apple want to change the behavior for Darwin, be my
guest. 

David
Comment 6 David Edelsohn 2005-07-26 17:02:17 UTC
reopen
Comment 7 mrs@gcc.gnu.org 2010-03-22 23:13:29 UTC
Subject: Bug 23071

Author: mrs
Date: Mon Mar 22 23:13:10 2010
New Revision: 157654

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=157654
Log:
	PR target/23071
	* config/rs6000/rs6000.c (darwin_rs6000_special_round_type_align):
	Don't overly align based upon packed packed fields.

Added:
    trunk/gcc/testsuite/gcc.target/powerpc/darwin-abi-12.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/rs6000/rs6000.c

Comment 8 Mike Stump 2010-03-22 23:18:32 UTC
The previous behavior was fairly broken, adding packed, _increased_ the alignment.  A user that adds packing, never wants more alignment:

struct Test {
  double D  __attribute__((packed,aligned(4)));
  short X;
} x;

struct {
  char x;
  struct Test t;
} b = { 1, { 2, 3}};

compared to:

struct Test {
  double D /*  __attribute__((packed,aligned(4))) */;
  short X;
} x;

struct {
  char x;
  struct Test t;
} b = { 1, { 2, 3}};
Comment 9 mrs@gcc.gnu.org 2010-03-22 23:21:42 UTC
This has now been fixed.