Issue 10826 - GC doesn't respect type alignments > 16 bytes
Summary: GC doesn't respect type alignments > 16 bytes
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 critical
Assignee: Steven Schveighoffer
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-08-15 11:33 UTC by bearophile_hugs
Modified: 2024-12-07 13:32 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-08-15 11:33:49 UTC
Currently the D GC allocates arrays aligned to 16 bytes fit to be used in XMM registers:

auto a1 = new double2[128];


But I think the D GC should also return this a2 aligned to 32 bytes, as needed for efficiency for code that uses YMM registers, that are 256 bits long:

auto a2 = new double4[64];


Eventually the D GC should return this a3 aligned to 64 bytes for efficiency of code that uses ZMM registers (Intel Xeon Phi), that are 512 bits long:

auto a3 = new double8[32];
Comment 1 Manu 2013-08-15 20:48:42 UTC
Yes, double4 should intrinsically be align(32), just like float4/double2 is intrinsically align(16). Likewise, align(64) for ZMM regs.

The GC should respect the explicit alignment of any type. If it doesn't, then that is another bug.
Comment 2 Manu 2013-08-15 20:51:55 UTC
For clarity, as a simple compiler rule, all __vector() types should be intrinsically aligned to their .sizeof.
This is correct on all architectures I know of.
There is the occasional architecture that might not mind a smaller alignment, but I think it's still valuable to enforce the alignment on those (rare) platforms for portability (structure consistency across platforms), especially since those platforms are often tested less thoroughly.
Comment 3 Steven Schveighoffer 2020-08-09 12:45:34 UTC
This came up again: https://forum.dlang.org/post/rgionugyekzpxuetyslh@forum.dlang.org

I have an idea that might work: when allocating an array of items with alignment greater than 16 bytes, just offset the first element when calculating the size.
Comment 4 Dlang Bot 2020-08-10 02:43:33 UTC
@schveiguy created dlang/druntime pull request #3192 "fix issue 10826 -- make sure large arrays obey 32-byte or greater alignment" fixing this issue:

- fix issue 10826 -- make sure 32-byte aligned types (such as
  __vector(ubyte[32]) ) are aligned to 32-bytes when put into large
  arrays.

https://github.com/dlang/druntime/pull/3192
Comment 5 kinke 2022-10-28 19:04:44 UTC
Raising the importance to critical. Greater-than-natural alignments are respected by LDC pretty much everywhere AFAIK (stack and globals) - except for druntime's GC. And are used for optimizations. It's pretty embarrassing that people cannot safely GC-allocate arrays of vectors > 128 bit without potentially hitting segfaults (incl. @safe code obviously).

And it's obviously not limited to vectors or arrays, but applies to all GC allocations of types with alignment > 16.
Comment 6 dlangBugzillaToGithub 2024-12-07 13:32:53 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17259

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB