Martin J. O'Riordan via llvm-dev
2016-Oct-03 11:55 UTC
[llvm-dev] Default alignment for 'malloc'
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? Related too, is it possible to specify that the stack pointer always points to memory which is aligned to a particular boundary? Thanks, MartinO -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161003/a3d0313b/attachment.html>
Michael Kruse via llvm-dev
2016-Oct-03 12:43 UTC
[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
Martin J. O'Riordan via llvm-dev
2016-Oct-03 13:45 UTC
[llvm-dev] Default alignment for 'malloc'
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
Joerg Sonnenberger via llvm-dev
2016-Oct-03 18:01 UTC
[llvm-dev] Default alignment for 'malloc'
On Mon, Oct 03, 2016 at 02:43:03PM +0200, Michael Kruse via llvm-dev wrote:> 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.Note that this only applies to base types. Vector types certainly can require larger alignment in practice and that's why posix_memalign exists. Joerg