RFR: 8252584: HotSpot Style Guide should permit alignas
Julian Waters
jwaters at openjdk.org
Sat Nov 26 17:19:10 UTC 2022
On Sat, 26 Nov 2022 04:17:23 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:
> No. Any proposal to permit `alignas` needs to address questions around "extended alignment", and especially "over-aligned types". Note that support for extended alignment in any particular context is implementation defined. (C++14 3.11/3)
>
> It looks like nearly all uses of compiler-specific alignment decorations (such as `__attribute__((aligned))`) in HotSpot are on variables with static duration. I think those are probably fine. (Early gcc implementations of `alignas` were pretty limited (more so than `__attribute__((aligned))` from what I found on the web), but that seems to have been fixed.)
>
> The only exceptions I found are uses of ZCACHE_ALIGN on class data members, which doesn't actually ensure alignment of those members!
I'm assuming you're referring to this when talking about extended alignment? I'm not sure if you mean this warning has to be included together in the Style Guide with alignas
An extended alignment is represented by an alignment greater than alignof(std::max_align_t). It is
implementation-defined whether any extended alignments are supported and the contexts in which they
are supported (7.6.2). A type having an extended alignment requirement is an over-aligned type. [ Note:
every over-aligned type is or contains a class type to which extended alignment applies (possibly through a
non-static data member). — end note ]
I don't think there's anything we can actually do about ZCACHE_ALIGNED, unfortunately, and the dynamic memory alignment mentioned in 8252584 is still going to be an issue regardless of whichever syntax we use (Utilizing a quick test I cobbled together):
`g++ -std=c++14 -o ./align alignment.cpp`
#include <cassert>
#include <cstdint>
#include <iostream>
#include <malloc.h>
#include <new>
class __attribute__((aligned(32))) AlignedVec {
double x, y, z;
};
int main() {
std::cout << "sizeof(AlignedVec) is " << sizeof(AlignedVec) << '\n';
std::cout << "alignof(AlignedVec) is " << alignof(AlignedVec) << '\n';
auto Vec = AlignedVec{};
auto pVec = new AlignedVec[10];
if(reinterpret_cast<uintptr_t>(&Vec) % alignof(AlignedVec) == 0)
std::cout << "Vec is aligned to alignof(AlignedVec)!\n";
else
std::cout << "Vec is not aligned to alignof(AlignedVec)!\n";
if(reinterpret_cast<uintptr_t>(pVec) % alignof(AlignedVec) == 0)
std::cout << "pVec is aligned to alignof(AlignedVec)!\n";
else
std::cout << "pVec is not aligned to alignof(AlignedVec)!\n";
delete[] pVec;
}
`./align`
sizeof(AlignedVec) is 32
alignof(AlignedVec) is 32
Vec is aligned to alignof(AlignedVec)!
pVec is not aligned to alignof(AlignedVec)!
-------------
PR: https://git.openjdk.org/jdk/pull/11315
More information about the hotspot-dev
mailing list