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

Ensure RUBY_ALIGNOF == the "alignment requirement" defined in C11 #3570

Merged
merged 7 commits into from Sep 25, 2020

Conversation

shyouhei
Copy link
Member

Context: #3261 (comment)

GCC/clang have __alignof__ operator. This is very much like C11 _Alignof, but can be different on a i386 System V ABI situation. The difference is troublesome. We would like to stick to what C11 says.

This is how we achieve that.

Availability of `alignas` is checked in include/ruby/internal/stdalign.h
now.  That does not need this configure check.  Also as commented in the
header, we see `_Alignas` being inadequate for our purpose.
It is reported that on a system of i386 System V ABI, GCC returns 8 for
__alignof__(double).  OTOH the ABI defines alignments of double to be 4,
and ISO/IEC 9899:2011 reads that _Alignof(double) shall return 4 on such
machine.  What we want in ruby is 4 instead of 8 there.  We cannot use
__alignof__.

Additionally, both old GCC / old clang return 8 for _Alignof(double) on
such platforms.  They are their bugs, and already fixed in recent
versions.  But we have to support older compilers for a while.  Shall
check sanity of _Alignof.
Now that RUBY_ALIGNOF behaves like C11's _Alignof.  This is not
necessarily the best stack arrangement.  We can just give up using
__builtin_alloca_with_align(), and let alloca choose what is optimal.
Check if RUBY_ALIGNOF(double) is the alignment to store a double inside
of a struct.
@shyouhei shyouhei requested review from mrkn and akr September 24, 2020 02:40
@shyouhei
Copy link
Member Author

Am checking icc / sunc...

Was definitely a bad idea to use constexpr.  It is not ubiquitous.
C++ compilers are optional.  Skip C++ tests when they are absent.
@shyouhei
Copy link
Member Author

Both icc/sunc seems working _Alignof, no problem observed for me.

@akr
Copy link
Contributor

akr commented Sep 24, 2020

It seems fine.

I recommend some comment about a struct with flexible array member, though.

mrkn
mrkn previously approved these changes Sep 24, 2020
Copy link
Member

@mrkn mrkn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it enough to check only double?

@mrkn mrkn dismissed their stale review September 24, 2020 09:00

The previous approvement is by mistake.

@shyouhei
Copy link
Member Author

@mrkn I think so. There are other types that exhibit similar behaviour (e.g. unsigned long long), but that doesn't necessarily mean we should also check them. If double is NG everything else shall also be NG.

@mrkn mrkn self-requested a review September 25, 2020 00:35
Added description and URL about nested flexible array member.
@shyouhei shyouhei merged commit c6c241e into ruby:master Sep 25, 2020
# * querying the alignment of a type, you definitely should use that instead.
# * There are 2 known pitfalls for this fallback implementation:
# *
# * Fitst, it is either an undefined behaviour (C) or an explicit error (C++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fitst => First

matzbot pushed a commit that referenced this pull request Sep 29, 2020
Reported by Mau Magnaguagno See: #3570 (comment)
shyouhei added a commit to shyouhei/ruby that referenced this pull request Oct 6, 2020
This was a workaround for RUBY_ALIGNOF's glitch, which has already been
fixed.  See also ruby#3570
shyouhei added a commit that referenced this pull request Oct 6, 2020
This was a workaround for RUBY_ALIGNOF's glitch, which has already been
fixed.  See also #3570
@shyouhei shyouhei deleted the RBIMPL_ALIGNOF branch July 4, 2023 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants