<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Clang loses alignment of union members"
   href="http://llvm.org/bugs/show_bug.cgi?id=15424">15424</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang loses alignment of union members
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jyasskin@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>