[LLVMbugs] [Bug 15424] New: Clang loses alignment of union members

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Mar 2 20:55:01 PST 2013


http://llvm.org/bugs/show_bug.cgi?id=15424

            Bug ID: 15424
           Summary: Clang loses alignment of union members
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jyasskin at google.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Clang seems to need an alignment to be repeated on fields of a union even
though they're guaranteed to have the highest alignment of any field.

$ cat test.c
#include <string.h>

typedef float vec __attribute__((vector_size(32)));
union U {
  vec v;
  char /*__attribute__((aligned(__alignof__(vec))))*/ c[sizeof(vec)];
};
void copy_vec(union U* u, const vec* v) { u->v = *v; }
void memcpy_char(union U* u, const vec* v) { memcpy(u->c, v, sizeof(vec)); }
void memcpy_union(union U* u, const vec* v) { memcpy(u, v, sizeof(vec)); }

Uncommenting the alignment above fixes the problem below.

Note the alignment of "16" in the memcpy call in memcpy_char below, as opposed
to the 32-byte alignment of the call in memcpy_union.

$ clang  -Xclang -target-feature -Xclang +avx -S test.c -O2  -Wall -o -
-emit-llvm
...
define void @memcpy_char(%union.U* nocapture %u, <8 x float>* nocapture %v) #0
{
  %1 = bitcast %union.U* %u to i8*
  %2 = bitcast <8 x float>* %v to i8*
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %1, i8* %2, i64 32, i32 16, i1
false)
  ret void
}
define void @memcpy_union(%union.U* nocapture %u, <8 x float>* nocapture %v) #0
{
  %1 = bitcast %union.U* %u to i8*
  %2 = bitcast <8 x float>* %v to i8*
  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %1, i8* %2, i64 32, i32 32, i1
false)
  ret void
}
...

This has the effect of pessimizing the generated code into not using AVX
instructions:

$ clang  -Xclang -target-feature -Xclang +avx -S test.c -O2  -Wall -o - 
copy_vec:                               # @copy_vec
        .cfi_startproc
# BB#0:
        vmovaps (%rsi), %ymm0
        vmovaps %ymm0, (%rdi)
        vzeroupper
        ret

memcpy_char:                            # @memcpy_char
        .cfi_startproc
# BB#0:
        vmovups 16(%rsi), %xmm0
        vmovups %xmm0, 16(%rdi)
        vmovaps (%rsi), %xmm0
        vmovaps %xmm0, (%rdi)
        ret

memcpy_union:                           # @memcpy_union
        .cfi_startproc
# BB#0:
        vmovaps (%rsi), %ymm0
        vmovaps %ymm0, (%rdi)
        vzeroupper
        ret


This is clang r175875.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130303/c622e341/attachment.html>


More information about the llvm-bugs mailing list