From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23874 invoked by alias); 16 Jul 2013 14:35:06 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 20375 invoked by uid 48); 16 Jul 2013 14:33:04 -0000 From: "yann at droneaud dot fr" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/57908] New: alignment of arrays allocated stack on amd64/x86_64: 16 bytes ? Date: Tue, 16 Jul 2013 14:35:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: yann at droneaud dot fr X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2013-07/txt/msg00782.txt.bz2 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