public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* attribute aligned(4) ignored
@ 2005-07-26  1:04 Chris Lattner
  2005-07-26  3:22 ` Ian Lance Taylor
  0 siblings, 1 reply; 9+ messages in thread
From: Chris Lattner @ 2005-07-26  1:04 UTC (permalink / raw)
  To: gcc-help


Hi All, I'm trying to reduce the alignment of the double in this structure 
to 4 bytes (from 8) on Darwin.  The goal of doing this is to reduce the 
structure to 12 bytes in size.  Here is my testcase:

struct Test {
   double D __attribute__((aligned(4)));    // only 4 bytes, not 8!
   int X;
};

int X() {
   return sizeof(struct Test);    // Should return 12, not 16.
}

Despite my attempt above, the structure is still 16 bytes in size, due to 
the tail padding required to pad the structure to ensure 8-byte alignment.

Does anyone know how to reduce the alignment of the double, eliminating 
the tail padding?

Thanks,

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  1:04 attribute aligned(4) ignored Chris Lattner
@ 2005-07-26  3:22 ` Ian Lance Taylor
  2005-07-26  4:58   ` Chris Lattner
  0 siblings, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2005-07-26  3:22 UTC (permalink / raw)
  To: Chris Lattner; +Cc: gcc-help

Chris Lattner <sabre@nondot.org> writes:

> Hi All, I'm trying to reduce the alignment of the double in this
> structure to 4 bytes (from 8) on Darwin.  The goal of doing this is to
> reduce the structure to 12 bytes in size.  Here is my testcase:
> 
> struct Test {
>    double D __attribute__((aligned(4)));    // only 4 bytes, not 8!
>    int X;
> };
> 
> int X() {
>    return sizeof(struct Test);    // Should return 12, not 16.
> }
> 
> Despite my attempt above, the structure is still 16 bytes in size, due
> to the tail padding required to pad the structure to ensure 8-byte
> alignment.
> 
> Does anyone know how to reduce the alignment of the double,
> eliminating the tail padding?

How about

   double D __attribute__((aligned(4), packed));

Ian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  3:22 ` Ian Lance Taylor
@ 2005-07-26  4:58   ` Chris Lattner
  2005-07-26  5:02     ` corey taylor
  2005-07-26  5:25     ` Ian Lance Taylor
  0 siblings, 2 replies; 9+ messages in thread
From: Chris Lattner @ 2005-07-26  4:58 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

On Mon, 25 Jul 2005, Ian Lance Taylor wrote:
> Chris Lattner <sabre@nondot.org> writes:
>> Hi All, I'm trying to reduce the alignment of the double in this
>> structure to 4 bytes (from 8) on Darwin.  The goal of doing this is to
>> reduce the structure to 12 bytes in size.  Here is my testcase:
>>
>> struct Test {
>>    double D __attribute__((aligned(4)));    // only 4 bytes, not 8!
>>    int X;
>> };
>>
>> int X() {
>>    return sizeof(struct Test);    // Should return 12, not 16.
>> }
>>
>> Despite my attempt above, the structure is still 16 bytes in size, due
>> to the tail padding required to pad the structure to ensure 8-byte
>> alignment.
>>
>> Does anyone know how to reduce the alignment of the double,
>> eliminating the tail padding?
>
> How about
>
>   double D __attribute__((aligned(4), packed));

This is exactly the sort of thing I want to do.  My structs can be 
arbitrarily complex, and can have a bunch of stuff in them that should not 
be packed.  This means that I don't want to use attribute packed on the 
structure itself, but using it on the member would be fine.

However, when I try this:

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

... the struct maintains its 8-byte alignment even though nothing inside 
of it requires 8-byte alignment any more.  In this particular case, for 
example, I want the struct to be 12-bytes, not 10: just reducing the 
alignment requirement of the double, without eliminating all intra-struct 
padding.  This is why I don't think I can use attribute packed on the 
struct itself.

Unfortunately, I'm not sure if this is possible, at least without using 
'packed' on the struct, then inserting a bunch of dummy members to 
explicitly insert the intra-field packing.  I hope there is a better way 
though, as this is really nasty and fragile.

Thanks,

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  4:58   ` Chris Lattner
@ 2005-07-26  5:02     ` corey taylor
  2005-07-26  5:04       ` Chris Lattner
  2005-07-26  5:25     ` Ian Lance Taylor
  1 sibling, 1 reply; 9+ messages in thread
From: corey taylor @ 2005-07-26  5:02 UTC (permalink / raw)
  To: Chris Lattner; +Cc: Ian Lance Taylor, gcc-help

Does the placement of the aligned and packed structure member affect the result?

What happens when the double D is at the end of the structure?

corey

On 7/25/05, Chris Lattner <sabre@nondot.org> wrote:
> On Mon, 25 Jul 2005, Ian Lance Taylor wrote:
> > Chris Lattner <sabre@nondot.org> writes:
> >> Hi All, I'm trying to reduce the alignment of the double in this
> >> structure to 4 bytes (from 8) on Darwin.  The goal of doing this is to
> >> reduce the structure to 12 bytes in size.  Here is my testcase:
> >>
> >> struct Test {
> >>    double D __attribute__((aligned(4)));    // only 4 bytes, not 8!
> >>    int X;
> >> };
> >>
> >> int X() {
> >>    return sizeof(struct Test);    // Should return 12, not 16.
> >> }
> >>
> >> Despite my attempt above, the structure is still 16 bytes in size, due
> >> to the tail padding required to pad the structure to ensure 8-byte
> >> alignment.
> >>
> >> Does anyone know how to reduce the alignment of the double,
> >> eliminating the tail padding?
> >
> > How about
> >
> >   double D __attribute__((aligned(4), packed));
> 
> This is exactly the sort of thing I want to do.  My structs can be
> arbitrarily complex, and can have a bunch of stuff in them that should not
> be packed.  This means that I don't want to use attribute packed on the
> structure itself, but using it on the member would be fine.
> 
> However, when I try this:
> 
> struct Test {
>    double D __attribute__((packed,aligned(4)));
>    short X;
> };
> 
> ... the struct maintains its 8-byte alignment even though nothing inside
> of it requires 8-byte alignment any more.  In this particular case, for
> example, I want the struct to be 12-bytes, not 10: just reducing the
> alignment requirement of the double, without eliminating all intra-struct
> padding.  This is why I don't think I can use attribute packed on the
> struct itself.
> 
> Unfortunately, I'm not sure if this is possible, at least without using
> 'packed' on the struct, then inserting a bunch of dummy members to
> explicitly insert the intra-field packing.  I hope there is a better way
> though, as this is really nasty and fragile.
> 
> Thanks,
> 
> -Chris
> 
> --
> http://nondot.org/sabre/
> http://llvm.org/
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  5:02     ` corey taylor
@ 2005-07-26  5:04       ` Chris Lattner
  0 siblings, 0 replies; 9+ messages in thread
From: Chris Lattner @ 2005-07-26  5:04 UTC (permalink / raw)
  To: corey taylor; +Cc: Ian Lance Taylor, gcc-help

On Tue, 26 Jul 2005, corey taylor wrote:

> Does the placement of the aligned and packed structure member affect the result?
>
> What happens when the double D is at the end of the structure?

The Darwin ABI specifies that doubles that are not the initial element 
already have 4-byte alignment.

-Chris

> On 7/25/05, Chris Lattner <sabre@nondot.org> wrote:
>> On Mon, 25 Jul 2005, Ian Lance Taylor wrote:
>>> Chris Lattner <sabre@nondot.org> writes:
>>>> Hi All, I'm trying to reduce the alignment of the double in this
>>>> structure to 4 bytes (from 8) on Darwin.  The goal of doing this is to
>>>> reduce the structure to 12 bytes in size.  Here is my testcase:
>>>>
>>>> struct Test {
>>>>    double D __attribute__((aligned(4)));    // only 4 bytes, not 8!
>>>>    int X;
>>>> };
>>>>
>>>> int X() {
>>>>    return sizeof(struct Test);    // Should return 12, not 16.
>>>> }
>>>>
>>>> Despite my attempt above, the structure is still 16 bytes in size, due
>>>> to the tail padding required to pad the structure to ensure 8-byte
>>>> alignment.
>>>>
>>>> Does anyone know how to reduce the alignment of the double,
>>>> eliminating the tail padding?
>>>
>>> How about
>>>
>>>   double D __attribute__((aligned(4), packed));
>>
>> This is exactly the sort of thing I want to do.  My structs can be
>> arbitrarily complex, and can have a bunch of stuff in them that should not
>> be packed.  This means that I don't want to use attribute packed on the
>> structure itself, but using it on the member would be fine.
>>
>> However, when I try this:
>>
>> struct Test {
>>    double D __attribute__((packed,aligned(4)));
>>    short X;
>> };
>>
>> ... the struct maintains its 8-byte alignment even though nothing inside
>> of it requires 8-byte alignment any more.  In this particular case, for
>> example, I want the struct to be 12-bytes, not 10: just reducing the
>> alignment requirement of the double, without eliminating all intra-struct
>> padding.  This is why I don't think I can use attribute packed on the
>> struct itself.
>>
>> Unfortunately, I'm not sure if this is possible, at least without using
>> 'packed' on the struct, then inserting a bunch of dummy members to
>> explicitly insert the intra-field packing.  I hope there is a better way
>> though, as this is really nasty and fragile.
>>
>> Thanks,
>>
>> -Chris
>>
>> --
>> http://nondot.org/sabre/
>> http://llvm.org/
>>
>

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  4:58   ` Chris Lattner
  2005-07-26  5:02     ` corey taylor
@ 2005-07-26  5:25     ` Ian Lance Taylor
  2005-07-26  5:57       ` Chris Lattner
  1 sibling, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2005-07-26  5:25 UTC (permalink / raw)
  To: Chris Lattner; +Cc: gcc-help

Chris Lattner <sabre@nondot.org> writes:

> > How about
> >
> >   double D __attribute__((aligned(4), packed));
> 
> This is exactly the sort of thing I want to do.  My structs can be
> arbitrarily complex, and can have a bunch of stuff in them that should
> not be packed.  This means that I don't want to use attribute packed
> on the structure itself, but using it on the member would be fine.
> 
> However, when I try this:
> 
> struct Test {
>    double D __attribute__((packed,aligned(4)));
>    short X;
> };
> 
> ... the struct maintains its 8-byte alignment even though nothing
> inside of it requires 8-byte alignment any more.

When I try that with a powerpc-eabi compiler, it appears to generates
a struct with a size of 12 bytes and an alignment of 4 bytes.  Do you
not see that with a Darwin compiler?  What target are you configuring
for?  What sources are you using?

Ian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  5:25     ` Ian Lance Taylor
@ 2005-07-26  5:57       ` Chris Lattner
  2005-07-26  6:27         ` Ian Lance Taylor
  0 siblings, 1 reply; 9+ messages in thread
From: Chris Lattner @ 2005-07-26  5:57 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

On Tue, 25 Jul 2005, Ian Lance Taylor wrote:
>> ... the struct maintains its 8-byte alignment even though nothing
>> inside of it requires 8-byte alignment any more.
>
> When I try that with a powerpc-eabi compiler, it appears to generates
> a struct with a size of 12 bytes and an alignment of 4 bytes.  Do you
> not see that with a Darwin compiler?  What target are you configuring
> for?  What sources are you using?

This is a powerpc-darwin specific issue I believe.  The Darwin ABI states 
(roughly) that doubles have 4-byte alignment, unless they are the first 
element of a structure (in which they have 8-byte alignment).  I think 
this is a holdover from AIX compatibility.

It is my guess that this ABI rule is overruling the user request, but I 
don't really know how to go about verifying this, or working around it :(

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  5:57       ` Chris Lattner
@ 2005-07-26  6:27         ` Ian Lance Taylor
  2005-07-26  6:44           ` Chris Lattner
  0 siblings, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2005-07-26  6:27 UTC (permalink / raw)
  To: Chris Lattner; +Cc: gcc-help

Chris Lattner <sabre@nondot.org> writes:

> On Tue, 25 Jul 2005, Ian Lance Taylor wrote:
> >> ... the struct maintains its 8-byte alignment even though nothing
> >> inside of it requires 8-byte alignment any more.
> >
> > When I try that with a powerpc-eabi compiler, it appears to generates
> > a struct with a size of 12 bytes and an alignment of 4 bytes.  Do you
> > not see that with a Darwin compiler?  What target are you configuring
> > for?  What sources are you using?
> 
> This is a powerpc-darwin specific issue I believe.  The Darwin ABI
> states (roughly) that doubles have 4-byte alignment, unless they are
> the first element of a structure (in which they have 8-byte
> alignment).  I think this is a holdover from AIX compatibility.
> 
> It is my guess that this ABI rule is overruling the user request, but
> I don't really know how to go about verifying this, or working around
> it :(

OK.  From the source code, that seems to be happening in
rs6000_special_round_type_align (config/rs6000/rs6000.c), which is
called by ROUND_TYPE_ALIGN (config/rs6000/darwin.h), which is called
by finalize_record_size (stor-layout.c).

rs6000_special_round_type_align is given the computed alignment and
the user specified alignment.  It appears to always force a maximum of
64.

Tracking this back through time and across files, it appears to have
been introduced here:

Mon Apr 22 12:00:46 1996  David Edelsohn  <edelsohn@mhpcc.edu>

	* rs6000.h (BIGGEST_FIELD_ALIGNMENT): Delete.
	(ADJUST_FIELD_ALIGN, ROUND_TYPE_ALIGN): Define.

when it looked like this:

/* AIX increases natural record alignment to doubleword if the first
   field is an FP double while the FP fields remain word aligned.  */
#define ROUND_TYPE_ALIGN(STRUCT, COMPUTED, SPECIFIED)  \
  ((TREE_CODE (STRUCT) == RECORD_TYPE                  \
    || TREE_CODE (STRUCT) == UNION_TYPE                        \
    || TREE_CODE (STRUCT) == QUAL_UNION_TYPE)          \
   && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode       \
   ? MAX (MAX ((COMPUTED), (SPECIFIED)), BIGGEST_ALIGNMENT) \
   : MAX ((COMPUTED), (SPECIFIED)))

The code now looks like this for a struct which begins with a double:

  return MAX (MAX (computed, specified), 64);

This ignores the attribute ((packed)), and I don't see why it is
correct to do so.

Ian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: attribute aligned(4) ignored
  2005-07-26  6:27         ` Ian Lance Taylor
@ 2005-07-26  6:44           ` Chris Lattner
  0 siblings, 0 replies; 9+ messages in thread
From: Chris Lattner @ 2005-07-26  6:44 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

On Tue, 25 Jul 2005, Ian Lance Taylor wrote:
> The code now looks like this for a struct which begins with a double:
>
>  return MAX (MAX (computed, specified), 64);
>
> This ignores the attribute ((packed)), and I don't see why it is
> correct to do so.

I totally agree.  Thanks a LOT for the help and the analysis.  I've filed 
this as GCC PR23071.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-07-26  6:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-26  1:04 attribute aligned(4) ignored Chris Lattner
2005-07-26  3:22 ` Ian Lance Taylor
2005-07-26  4:58   ` Chris Lattner
2005-07-26  5:02     ` corey taylor
2005-07-26  5:04       ` Chris Lattner
2005-07-26  5:25     ` Ian Lance Taylor
2005-07-26  5:57       ` Chris Lattner
2005-07-26  6:27         ` Ian Lance Taylor
2005-07-26  6:44           ` Chris Lattner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).