alignas and clang
Julian Waters
tanksherman27 at gmail.com
Mon Mar 6 15:22:17 UTC 2023
Hi Thomas,
>From what I know gcc is actually the one that's not correct here, a bigger
alignment is considered stricter in C++ (though the Standard really should
make this much clearer) since a lower alignment value results in more
possible addresses, hence making the type's alignment weaker, and alignas
is very explicitly forbidden from weakening alignment, especially the
regular (unmodified) minimum alignment of said type. I don't know if there
are places where we purposely weaken type alignment in HotSpot though
best regards,
Julian
On Mon, Mar 6, 2023 at 10:19 PM Thomas Stüfe <thomas.stuefe at gmail.com>
wrote:
> Hi,
>
> I'm not sure if this has been discussed before. With alignas, on clang, if
> the natural type of the data has a larger alignment than what I requested
> with alignas, I get a compile error if the specified alignment is smaller
> than what would be the natural alignment of the type.
>
> Example:
>
> struct alignas(2) XX {
> void* p;
> };
>
> gives: error: requested alignment is less than minimum alignment of 8 for
> type 'XX'
>
> Happens on both MacOS and Linux clang build. Does not happen with GCC.
> Does not happen with ATTRIBUTE_ALIGN(2).
>
> Is this a clang bug? The standard [1] says:
>
> "The object or the type declared by such a declaration will have its
> alignment requirement equal to the strictest (largest) non-zero expression
> of all alignas specifiers used in the declaration, unless it would weaken
> the natural alignment of the type. "
>
> That reads to me like gcc is correct.
>
> This is a bit of a drawback compared to ATTRIBUTE_ALIGN, especially if
> coding for different 32-bit and 64-bit platforms.
>
> [1] https://en.cppreference.com/w/cpp/language/alignas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/hotspot-dev/attachments/20230306/4d0b347d/attachment.htm>
More information about the hotspot-dev
mailing list