Skip to content

type alignment non forwarded to malloc instruction in C and C++ frontends #1903

@llvmbot

Description

@llvmbot
Bugzilla Link 1531
Resolution DUPLICATE
Resolved on Feb 22, 2010 12:55
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor

Extended Description

In the following C code (with GCC extensions):

struct test
{
int a;
int b attribute((aligned(16)));
};

void do_something(struct test*);

int main()
{
struct test* t = malloc(sizeof *t);
do_something(t);

return 0;

}

The type "struct test" has an associated alignment of 16.

However, LLVM will do a normal malloc instruction, without saying it wants an
alignment of 16.
While it is true that GCC won't be able to do that anyway because to it malloc
is a regular function that only allocates with an alignment of 8, the malloc
instruction can be smart enough here to allocate with the appropriate alignment.

Actual output:

; ModuleID = '/tmp/webcompile/_30695_0.bc'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64"
target triple = "i686-pc-linux-gnu"
%struct.test = type { i32, [12 x i8], i32, [12 x i8] }

define i32 @​main() {
entry:
%tmp16 = malloc %struct.test ; <%struct.test*> [#uses=1]
tail call void @​do_something( %struct.test* %tmp16 )
ret i32 0
}

declare void @​do_something(%struct.test*)

Expected output:

; ModuleID = '/tmp/webcompile/_30695_0.bc'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64"
target triple = "i686-pc-linux-gnu"
%struct.test = type { i32, [12 x i8], i32, [12 x i8] }

define i32 @​main() {
entry:
%tmp16 = malloc %struct.test, align 16 ; <%struct.test*> [#uses=1]
tail call void @​do_something( %struct.test* %tmp16 )
ret i32 0
}

declare void @​do_something(%struct.test*)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzilladuplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions