Skip to content
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

Closed
llvmbot opened this issue Feb 9, 2016 · 13 comments
Closed

alignof(double) should yield 4 on x86 #26921

llvmbot opened this issue Feb 9, 2016 · 13 comments
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 9, 2016

Bugzilla Link 26547
Resolution FIXED
Resolved on Dec 10, 2018 11:29
Version 3.7
OS Linux
Attachments C program illustrating the bug
Reporter LLVM Bugzilla Contributor
CC @zygoloid,@rnk
Fixed by commit(s) 345419

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.

@rnk
Copy link
Collaborator

rnk commented Feb 9, 2016

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
Author: Chad Rosier mcrosier@apple.com
Date: Tue Jul 26 07:03:04 2011 +0000

After further discussion it has been determined that alignof should report
the preferred alignment.  Thus, revert r135934, r135935, and r135940.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136062 91177308-0d34-0410-b5e6-96231b3b80d8

I wouldn't touch it with a ten foot pole.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 9, 2016

this behavior has been changed before and that was reverted:

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.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Feb 10, 2016

Chad, do you remember where this discussion took place and what the rationale was? This certainly looks like a bug.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 10, 2016

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

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 10, 2016

Seems to be part of rdar://9802874

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 10, 2016

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.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 11, 2016

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.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Feb 11, 2016

So, in recent versions of GCC, for x86_32:

alignof(double) == 8 for C and C++
alignof(double) == 8 for C++
_Alignof(double) == 4 for 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.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Feb 11, 2016

GCC bug filed as gcc.gnu.org/#69763 .

@llvmbot
Copy link
Collaborator Author

llvmbot commented Oct 10, 2018

Is there any possibility of fixing this? GCC current returns the correct value for alignof(double); it'd be good to have the same behavior as the other major compiler. I'd be willing to fix it, if my change would be merged.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Oct 10, 2018

Is there any possibility of fixing this? GCC current returns the correct
value for alignof(double); it'd be good to have the same behavior as the
other major compiler. I'd be willing to fix it, if my change would be merged.

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

@llvmbot
Copy link
Collaborator Author

llvmbot commented Oct 12, 2018

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

@llvmbot
Copy link
Collaborator Author

llvmbot commented Oct 12, 2018

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang Clang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

2 participants