the backends (including C and x86) do not respect alignment on malloc. consider test/Features/alignment.ll. The final malloc (%T) should be codegened to a memalign to preserve the alignment. Once this is done, all memalign and posix_memalign calls can be safely transformed into llvm malloc instructions. Plus it is just broken right now.
Created attachment 809 [details] align test
This test (align.ll) should output all 0s if the align is being respected. It is derived from: #include <stdlib.h> #include <stdio.h> #include <malloc.h> int main(int argc, char** argv) { int x; char* p; for (x = 0; x < 100; ++x) { p = memalign(256, 1); printf("%d", (int)p & 0x00FF); } } changing the memalign call into a malloc 1, align 256, which should have the same semantics according to the language reference.
true. The problem is that not all targets support memalign, e.g. darwin.
*** Bug 1531 has been marked as a duplicate of this bug. ***
There are a few ways to fix the issue: - write a memory allocator, eventually on top of malloc, that can provide memory for any alignment, and use it instead of posix_memalign when it is not available (or always use it if it's better). - just allocate more than necessary, increment the pointer before returning it, and write implementation-specific info in that padding memory so that the incremented pointer can still work with free. An alternative would be to somehow write how much it needs to be decremented before calling free. No such trick would be necessary if the free instruction took an align argument which value was the same as the corresponding malloc, but it may be non-trivial or impossible to find out with C code.
> - write a memory allocator, eventually on top of malloc > - just allocate more than necessary. Neither of these are abi compatible with GCC. this would prevent you from compiling one .c file with llvm and one .c file with GCC and linking them together. Why not just call posix_memalign if that is what you want? -Chris
Because posix_memalign is not available on all platforms.
The real fix for this is to just remove the mallocinst instruction.
*** This bug has been marked as a duplicate of bug 715 ***