[llvm-dev] Default alignment for 'malloc'

Martin J. O'Riordan via llvm-dev llvm-dev at lists.llvm.org
Mon Oct 3 06:45:21 PDT 2016


Thanks Michael,

When vectorising loads and stores, it is very useful to know the actual alignment, especially when the memory architecture is a bit exotic.  Since we can issue two simultaneous load/store instructions, I have a stronger requirement for this information than would usually be the case, and while our 'malloc' does ensure optimal alignment for all data types, that information is not attached to the IR so the compiler seems no difference between 'void *malloc(size_t)' and 'void *foo(size_t)'.  I have started experimenting with '__attribute__((alloc_align(N)))', but this requires altering the Standard headers.  And of course I can edit the target independent source code for the compiler to explicitly add this information to 'malloc' et all.  But I was hoping that there was an existing mechanism such as a 'TargetTransformInfo' callback.

Regarding the stack pointer - I am referring to state of the SP as it is maintained by the compiler.  While I can actually ensure that it is always aligned (part of the ABI) in the prologue/epilogue, the IR doesn't possess this information, so the alignment optimisations have to specially check if the pointer being processed is the SP or not.

All the best,

	MartinO

-----Original Message-----
From: meinersbur at googlemail.com [mailto:meinersbur at googlemail.com] On Behalf Of Michael Kruse
Sent: 03 October 2016 13:43
To: Martin J. O'Riordan <martin.oriordan at movidius.com>
Cc: LLVM Developers <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] Default alignment for 'malloc'

2016-10-03 13:55 GMT+02:00 Martin J. O'Riordan via llvm-dev
<llvm-dev at lists.llvm.org>:
> I am trying to implement some new alignment based optimisations in our 
> target backend, and I am wondering if there a way a target can specify 
> that ‘malloc’, ‘realloc’ and ‘calloc’ always return a pointer to 
> memory that is aligned to a particular boundary?

malloc is guaranteed to be properly aligned for any C type. This would be 8 bytes on most systems for double. However, I think in practice most modern implementations return 16-byte aligned pointers. I don't think there is a way to annotate calls malloc to have some specific alignment from the backend, that has effect on passes before the backend.


> Related too, is it possible to specify that the stack pointer always 
> points to memory which is aligned to a particular boundary?

For eg. 16-byte alignment, declare with:
__attribute__((aligned(16)))


Michael



More information about the llvm-dev mailing list