You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
classInstanceSize is not a multiple of its classInstanceAlignment #21065
Why is this a bug? There aren't any contiguous arrays of instances, so no need for any tail padding.
Edit: Well, unless you are worried about D-allocated extern(C++) class refs being passed to C++, and C++ going ahead and doing a memcpy for the whole (C++-padded) instance size, or something along these lines (which noone has come across yet?). Edit2: Which seems unlikely, given that one uses D classes to represent polymorphic C++ classes only.
No, not in D; in your example, you have a void[] only. So if an allocator has an assumption that allocated block sizes need to be a multiple of the desired alignment, it's IMO clearly that allocator's responsibility/problem. - Closures are another example for blocks where the size might not be a multiple of its alignment.
If you're doing custom bounds checks in GDC, maybe try abs(p - init.ptr) >= init.length to hopefully prevent the earlier const-folding? [Which is invalid for the current D semantics; I guess such a layout+alignment combo is not representable in C++, so maybe not handled in gcc.]
Tricking the compiler is a futile experience. Just generate data structures that are compliant with architectures and their strict alignment requirements.
Don't think so. When dealing with statics, it's not unreasonable for the optimizer to do CCP, nor to convert ptr + size into a memory reference of type typeof(class).
Alternatively, all D classes - both extern(C++) and extern(D) - should be defined as packed then, meaning their underlying data alignment (classInstanceAlignment) is 1. This does not affect alignment of individual fields.
Again, this isn't a problem for DMD nor LDC, just for GDC. I'm not really sure the tail-padding 'elimination' is actually worth it, but just because the gcc optimizer can't cope with such 'exotic' aggregates IMO doesn't justify changing the D class ABI in general.
Alternatively, all D classes - both extern(C++) and extern(D) - should be defined as packed then, meaning their underlying data alignment (classInstanceAlignment) is 1.
This is similar to what LDC does and what I mentioned above - we emit the IR struct as packed, meaning that the IR alignment is 1. But the frontend classInstanceAlignment matches the actual allocation alignments (e.g., for a scope stack allocation, we use an explicit align(8) IR attribute for a CXX instance). [And the IR align(1) (for the aggregate itself) doesn't affect/pessimize any field access, they are all appropriately aligned unless explicitly overridden with a D align(N).]
It gets complicated when your mixing alignments. I immediately expect bus errors to occur on SPARC. But that will need to be regression tested first before anything else.
[I meant align(N) for the fields. E.g., class C { align(64) int a; } for an explicit over-alignment, or class C { int a; align(4) long b; } with an under-aligned b.]
Activity
ibuclaw commentedon Mar 23, 2025
It's not as simple as adjusting the
structsizeof classes in finalizeSize, as derived classes are packed together.https://godbolt.org/z/vWf7vWj8z
std.allocator: Comment out broken class instance size test
std.allocator: Comment out broken class instance size test
Fix dlang#21065 - classInstanceSize is not a multiple of its classIns…
std.allocator: Comment out broken class instance size test (#10717)
Fix dlang#21065 - classInstanceSize is not a multiple of its classIns…
kinke commentedon Mar 27, 2025
Why is this a bug? There aren't any contiguous arrays of instances, so no need for any tail padding.
Edit: Well, unless you are worried about D-allocated
extern(C++)class refs being passed to C++, and C++ going ahead and doing a memcpy for the whole (C++-padded) instance size, or something along these lines (which noone has come across yet?).Edit2: Which seems unlikely, given that one uses D classes to represent polymorphic C++ classes only.
TurkeyMan commentedon Mar 27, 2025
I actually do have arrays of class instances :P ... allocation pools cause this scenario.
ibuclaw commentedon Mar 27, 2025
@kinke you can have arrays of class instances, or an array of T[1] to be precise.
kinke commentedon Mar 27, 2025
No, not in D; in your example, you have a
void[]only. So if an allocator has an assumption that allocated block sizes need to be a multiple of the desired alignment, it's IMO clearly that allocator's responsibility/problem. - Closures are another example for blocks where the size might not be a multiple of its alignment.2 remaining items
ibuclaw commentedon Mar 28, 2025
My understanding
is interpreted as
At which point, the compiler notices this class type is in violation, as the pointer comparison has one address that is unaligned.
kinke commentedon Mar 28, 2025
Interpreted where, in GDC only? There's a reason we use
void[]for the init-symbol payload. :) And the helper function usesvoid[]exclusively too:dmd/druntime/src/rt/arraycat.d
Lines 22 to 30 in 21cfefc
kinke commentedon Mar 28, 2025
If you're doing custom bounds checks in GDC, maybe try
abs(p - init.ptr) >= init.lengthto hopefully prevent the earlier const-folding? [Which is invalid for the current D semantics; I guess such a layout+alignment combo is not representable in C++, so maybe not handled in gcc.]ibuclaw commentedon Mar 28, 2025
Tricking the compiler is a futile experience. Just generate data structures that are compliant with architectures and their strict alignment requirements.
ibuclaw commentedon Mar 28, 2025
Try compiling any example with
-O2 -Warray-bounds. The error is what happens when you try tonewany bad class via dnewclassT.kinke commentedon Mar 28, 2025
Well to me that's clearly a gcc/gdc bug, and that tail padding not needed to match any alignment requirements.
ibuclaw commentedon Mar 28, 2025
Don't think so. When dealing with statics, it's not unreasonable for the optimizer to do CCP, nor to convert
ptr + sizeinto a memory reference of typetypeof(class).https://d.godbolt.org/z/6jEaEWh1a
ibuclaw commentedon Mar 28, 2025
Alternatively, all D classes - both
extern(C++)andextern(D)- should be defined as packed then, meaning their underlying data alignment (classInstanceAlignment) is1. This does not affect alignment of individual fields.kinke commentedon Mar 28, 2025
Again, this isn't a problem for DMD nor LDC, just for GDC. I'm not really sure the tail-padding 'elimination' is actually worth it, but just because the gcc optimizer can't cope with such 'exotic' aggregates IMO doesn't justify changing the D class ABI in general.
kinke commentedon Mar 28, 2025
This is similar to what LDC does and what I mentioned above - we emit the IR struct as packed, meaning that the IR alignment is 1. But the frontend
classInstanceAlignmentmatches the actual allocation alignments (e.g., for ascopestack allocation, we use an explicitalign(8)IR attribute for aCXXinstance). [And the IRalign(1)(for the aggregate itself) doesn't affect/pessimize any field access, they are all appropriately aligned unless explicitly overridden with a Dalign(N).]ibuclaw commentedon Mar 28, 2025
align(N) class C{}is ignored by the front-end.It gets complicated when your mixing alignments. I immediately expect bus errors to occur on SPARC. But that will need to be regression tested first before anything else.
kinke commentedon Mar 28, 2025
[I meant
align(N)for the fields. E.g.,class C { align(64) int a; }for an explicit over-alignment, orclass C { int a; align(4) long b; }with an under-alignedb.]ibuclaw commentedon May 11, 2025
@kinke thanks for the clarification.
Fixed in downstream. https://gcc.gnu.org/cgi-bin/gcc-gitref.cgi?r=r15-9070
std.allocator: Comment out broken class instance size test (dlang#10717)