Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alignment on malloc instruction is not codegened correctly #1734

Closed
llvmbot opened this issue Apr 26, 2007 · 9 comments
Closed

alignment on malloc instruction is not codegened correctly #1734

llvmbot opened this issue Apr 26, 2007 · 9 comments
Labels
bugzilla Issues migrated from bugzilla duplicate Resolved as duplicate llvm:codegen

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 26, 2007

Bugzilla Link 1362
Resolution DUPLICATE
Resolved on Mar 17, 2009 13:41
Version trunk
OS Linux
Attachments align test
Reporter LLVM Bugzilla Contributor

Extended Description

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.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Apr 26, 2007

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.

@lattner
Copy link
Collaborator

lattner commented Apr 26, 2007

true. The problem is that not all targets support memalign, e.g. darwin.

@lattner
Copy link
Collaborator

lattner commented Jul 2, 2007

*** Bug llvm/llvm-bugzilla-archive#1531 has been marked as a duplicate of this bug. ***

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jul 3, 2007

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.

@lattner
Copy link
Collaborator

lattner commented Jul 3, 2007

  • 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

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jul 3, 2007

Because posix_memalign is not available on all platforms.

@lattner
Copy link
Collaborator

lattner commented Mar 17, 2009

The real fix for this is to just remove the mallocinst instruction.

@lattner
Copy link
Collaborator

lattner commented Mar 17, 2009

*** This bug has been marked as a duplicate of bug #1087 ***

@lattner
Copy link
Collaborator

lattner commented Nov 27, 2021

mentioned in issue #1087

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla duplicate Resolved as duplicate llvm:codegen
Projects
None yet
Development

No branches or pull requests

2 participants