Equivalent alignment of __attribute__((__aligned__))

Is there a way using llvm::TargetData to determine the alignment of what would be equivalent
to __attribute__((__aligned__))? I'm looking for something like a max alignment number for the
target platform so that I know what the max alignment of an address returned by malloc(...) will

Thanks in advance


Not currently. The alignment implied by attribute(aligned) is probably not something that TargetData should have, but I think it *does* make sense to have the minimal guaranteed alignment of malloc.


malloc() doesn't have anything to do with attribute(aligned).
You can request objects allocated on the stack (with attrib aligned)
with alignment greater than what malloc could give you.

AFAIK the alignment of malloc() is guaranteed be to at least the
alignment of a long double (since that is the
C type with the largest alignment requirement).
I don't know what guarantees malloc() has regarding vector types, I
guess none, it just happens to work because malloc() returns
data aligned to 16 bytes.

IIRC if you run the program under valgrind for example it would only
give you sizeof(void*) alignment.

You can find out the minimum ABI alignment for a particular type using
TD->getABITypeAlignment(Ty) though.

Best regards,

Sorry as you interpreted I meant minimum alignment.

My issue is in translating from a C struct which has a member aligned with
__attribute__((__aligned__)) to a generated packed StructType where I use padding
to force the alignment of said member. Instances of this struct are placed on the heap
via malloc. Although I'm currently using static C code to determine the padding size, I'm trying
to avoid using such code since I'm not sure which llvm platforms don't support __attribute__((__aligned__)).
Knowing that malloc will give me an address aligned to any platform supported datatype, and knowing
this minimum alignment (which I believe is equivalent to __attribute__((__aligned__))), I could
calculate the above value. I'm currently using the alignment of long double instead.

Any recommended way of determining this value or doing the equivalent?


There is no good way to do this at the LLVM IR level, and you need a lot more information than just attribute(aligned). Take a look at the code in clang that handles record layout, for example.


Of course, clang has to do this. Thanks!