New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
alignof(double) should yield 4 on x86 #26921
Comments
Apparently there is some disagreement about whether alignof is supposed to return the preferred alignment or the ABI alignment. Without reading the standard, my intuition is to agree with you: alignof should tell you how things are aligned in structs, nothing else. However, this behavior has been changed before and that was reverted: commit 9f1210c3280104417a4ad30f0a00825ac8fa718a
I wouldn't touch it with a ten foot pole. |
That was OK back in 2011, for compatibility with how GCC behaved at the time. However, in GCC 4.9 (2014), GCC changed to conform to C11, and Clang should agree with GCC and should conform to the standard. See GCC Bug#52023 here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023 So, like it or not, this one's a no-brainer. |
Chad, do you remember where this discussion took place and what the rationale was? This certainly looks like a bug. |
Unfortunately, that was a lifetime ago and I don't recall the details. Bob, do you have any additional details (perhaps, in a radar)? |
Seems to be part of rdar://9802874 |
Yes, we had many discussions about this. Ultimately it came down to matching GCC. When I last tried it in 2012, GCC was still using the preferred alignment for alignof. If that has changed, it would make sense to reevaluate this for clang as well. |
Just to clarify: this bug report is not about alignof, which is the preferred alignment. In x86 GCC, alignof(double) has not changed and is still 8. This bug report is about alignof, which is the guaranteed alignment in conforming applications. alignof(double) was 8 in x86 GCC before GCC 4.9, but C11 says alignof(double) must be 4 if a standard-conforming program can generate a double with alignment of 4, as in the C program illustrating the bug here. That is why alignof(double) is 4 in x86 GCC 4.9 and later. |
So, in recent versions of GCC, for x86_32: alignof(double) == 8 for C and C++ So there's a GCC bug in there somewhere. The C and C++ versions of the standard alignof facility should not be producing different results. |
GCC bug filed as gcc.gnu.org/#69763 . |
Is there any possibility of fixing this? GCC current returns the correct value for |
Yes, we should change alignof and _Alignof (and not alignof) to return the minimum alignment rather than preferred alignment (matching the new GCC behavior and CWG 1879). Please make -fclang-abi-compat=7 (and earlier) use the old rules, and send the patch to the cfe-commits mailing list (it'll likely not be seen if you post it here). |
I've written a patch which fixes this bug, and have sent it to cfe-commits. I would appreciate if people check it out and give feedback :) |
Extended Description
A user ran into a problem when porting bleeding-edge GNU Emacs to a clang-based system; see http://lists.gnu.org/archive/html/emacs-devel/2016-02/msg00476.html. I tracked the issue down to a compiler bug with clang. Although Emacs 'configure' detects and works around the compiler bug, the compiler bug ought to be fixed.
On x86 (32-bit), clang aligns some doubles to just a multiple of 4, but says alignof(double) is 8; this violates the C standard.
Compile and run the attached program with "clang -m32" on an x86-64 platform; I used Fedora 23, which has clang version 3.7.0 (tags/RELEASE_370/final)
Target: x86_64-redhat-linux-gnu. The program exits with status 1, but should exit with status 0. GCC operates correctly.
If I compile with -DPRINT_DETAILS on clang, it outputs:
sizeof (struct { char a; double b; }) = 12
offsetof (struct { char a; double b; }, b) = 4
alignof (double) = 8
&s[0].b = 0x804a024, alignment_error = 4
If I compile the same with GCC it outputs:
sizeof (struct { char a; double b; }) = 12
offsetof (struct { char a; double b; }, b) = 4
alignof (double) = 4
&s[0].b = 0x804a024, alignment_error = 0
&s[1].b = 0x804a030, alignment_error = 0
Apparently clang is confusing alignof (most-conservative alignment) with alignof (best alignment). This bug was in GCC a while ago too, but it's been fixed there.
The text was updated successfully, but these errors were encountered: