<div dir="ltr">Ahhh, I see what you mean. Seems to me that this may actually be compiler dependent, since C++14 simply states such situations are ill-formed and makes no promises of instead choosing the larger alignment when this happens. Does seem to be pretty annoying in that case :(<div><br></div><div>Love that pun though, on another note :P</div><div><br></div><div>best regards,</div><div>Julian</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 6, 2023 at 11:52 PM Thomas Stüfe <<a href="mailto:thomas.stuefe@gmail.com" target="_blank">thomas.stuefe@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>Hi Julian,</div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 6, 2023 at 4:22 PM Julian Waters <<a href="mailto:tanksherman27@gmail.com" target="_blank">tanksherman27@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Thomas,<div><br></div><div>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.</div></div></blockquote><div><br></div><div><div dir="ltr"><div>gcc does not weaken the alignment, though. It just chooses whatever is larger: natural or explicitly given alignment. So it behaves to spec, provided I understand the spec.<br></div></div></div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div> I don't know if there are places where we purposely weaken type alignment in HotSpot though</div></div></blockquote><div><br></div><div>Not weaken, and not purposefully. But the specified alignment may be smaller than the natural one, and the natural (hah) thing to do would be to choose whatever's larger.<br></div><div><br></div><div>For example, from <a href="https://github.com/openjdk/jdk/pull/12879" target="_blank">https://github.com/openjdk/jdk/pull/12879</a>:</div><div><br></div><div>template <class T> struct MyContainer {<br>  union alignas(alignof(T)) DataHolder {<br>    char bytes[sizeof(T)];<br>    void* p;<br>  };<br>  DataHolder d;<br>};<br></div><div><br></div><div>So, I want the compiler to allocate storage aligned for T, but postpone the actual construction of T. I also want to keep the thing in a freelist, hence the next pointer (here its a union, but that does not matter for the problem).</div><div><br></div><div>For T=int, on 64-bit, I would have expected the compiler to generate DataHolder with an alignment requirement of 8 since T needs 4 and void* needs 8. But the compiler does not accept this. However, clang accepts ATTRIBUTE_ALIGNED and then does what gcc does.<br></div><div><br></div><div>Cheers, Thomas</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>best regards,</div><div>Julian</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 6, 2023 at 10:19 PM Thomas Stüfe <<a href="mailto:thomas.stuefe@gmail.com" target="_blank">thomas.stuefe@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi,</div><div><br></div><div>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.</div><div><br></div><div>Example:<br></div><br>struct alignas(2) XX {<br>  void* p;<br>};<br><br>gives: error: requested alignment is less than minimum alignment of 8 for type 'XX'<br><br>Happens on both MacOS and Linux clang build. Does not happen with GCC. Does not happen with ATTRIBUTE_ALIGN(2).<br><br>Is this a clang bug? The standard [1] says:<br><br>"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. "<br><div><br></div><div>That reads to me like gcc is correct.<br></div><div><br></div>This is a bit of a drawback compared to ATTRIBUTE_ALIGN, especially if coding for different 32-bit and 64-bit platforms.<br><br>[1] <a href="https://en.cppreference.com/w/cpp/language/alignas" target="_blank">https://en.cppreference.com/w/cpp/language/alignas</a></div>
</blockquote></div>
</blockquote></div></div>
</blockquote></div>