public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/57908] New: alignment of arrays allocated stack on amd64/x86_64: 16 bytes ?
@ 2013-07-16 14:35 yann at droneaud dot fr
  2013-07-16 14:39 ` [Bug c/57908] " yann at droneaud dot fr
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: yann at droneaud dot fr @ 2013-07-16 14:35 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57908

            Bug ID: 57908
           Summary: alignment of arrays allocated stack on amd64/x86_64:
                    16 bytes ?
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yann at droneaud dot fr

According to "System V Application Binary Interface, AMD64 Architecture
Processor Supplement, Draft Version 0.90"

    Aggregates and Unions
    ---------------------

    An array uses the same alignment as its elements, except that a local or 
    global array variable that requires at least 16 bytes, or a C99 local or 
    global variable-length array variable, always has alignment of at least 16 
    bytes.[4]
    No other changes required.

    [4] The alignment requirement allows the use of SSE instructions when 
        operating on the array.
        The compiler cannot in general calculate the size of a variable-length 
        array (VLA), but it is expected that most VLAs will require at least 16 
        bytes, so it is logical to mandate that VLAs have at least a 16-byte 
        alignment.


As I understand the ABI specifications, arrays allocated on stack must be
aligned on 16 bytes boundaries, whatever its length is: eg. an array of 7 bytes
get aligned on 16 bytes boundaries.

A test program seems to verify that, with gcc version 4.8.1 20130603 (Red Hat
4.8.1-1) (GCC) :

     kind         name              address   size   alignment   required

   Arrays                                                                
   object |         u8 |     0x7fffefdd486f |    1 |         1 |        1
   object |       u8_0 |     0x7fffefdd4860 |    8 |        32 |        1
   object |       u8_1 |     0x7fffefdd4850 |    7 |        16 |        1
   object |       u8_2 |     0x7fffefdd4840 |    6 |        64 |        1
   object |       u8_3 |     0x7fffefdd4830 |    5 |        16 |        1
   object |       u8_4 |     0x7fffefdd4820 |    4 |        32 |        1
   object |       u8_5 |     0x7fffefdd4810 |    3 |        16 |        1
   object |       u8_6 |     0x7fffefdd4800 |    2 |      2048 |        1
   object |       u8_7 |     0x7fffefdd47ff |    1 |         1 |        1
   object |       u8_8 |     0x7fffefdd47fe |    1 |         2 |        1

IMHO it's a waste of memory and this behavor is inconsistent regarding
structure layout: eg. arrays in structure are not aligned on 16 bytes boundary.

But let's say the specification mandate such stack allocation, aligned on 16
bytes boundary.

Then enter LLVM/Clang clang version 3.3 (tags/RELEASE_33/rc3):

     kind         name              address   size   alignment   required

   Arrays                                                                
   object |         u8 |     0x7fff45f4154f |    1 |         1 |        1
   object |       u8_0 |     0x7fff45f41547 |    8 |         1 |        1
   object |       u8_1 |     0x7fff45f41540 |    7 |        64 |        1
   object |       u8_2 |     0x7fff45f4153a |    6 |         2 |        1
   object |       u8_3 |     0x7fff45f41535 |    5 |         1 |        1
   object |       u8_4 |     0x7fff45f41531 |    4 |         1 |        1
   object |       u8_5 |     0x7fff45f4152e |    3 |         2 |        1
   object |       u8_6 |     0x7fff45f4152c |    2 |         4 |        1
   object |       u8_7 |     0x7fff45f4152b |    1 |         1 |        1
   object |       u8_8 |     0x7fff45f4152a |    1 |         2 |        1

It seems that Clang is not aligning arrays on stack to 16 bytes boundary.

Note: for both compiler, tested on Fedora 19, the results were produced with a
test program compiled with default optimisation flag, with -O3, the results are
quite the same.

The source code of the test is available here:
https://gitorious.org/opteya/alignment/blobs/master/stack-alignment.c


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

end of thread, other threads:[~2013-07-16 15:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-16 14:35 [Bug c/57908] New: alignment of arrays allocated stack on amd64/x86_64: 16 bytes ? yann at droneaud dot fr
2013-07-16 14:39 ` [Bug c/57908] " yann at droneaud dot fr
2013-07-16 15:03 ` pinskia at gcc dot gnu.org
2013-07-16 15:06 ` [Bug target/57908] " pinskia at gcc dot gnu.org
2013-07-16 15:11 ` yann at droneaud dot fr
2013-07-16 15:18 ` yann at droneaud dot fr
2013-07-16 15:20 ` yann at droneaud dot fr
2013-07-16 15:28 ` pinskia at gcc dot gnu.org
2013-07-16 15:32 ` yann at droneaud dot fr
2013-07-16 15:36 ` pinskia at gcc dot gnu.org
2013-07-16 15:56 ` yann at droneaud dot fr

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).