<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 --- - Miscompile explicitly vectorized code"
   href="http://llvm.org/bugs/show_bug.cgi?id=22753">22753</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompile explicitly vectorized code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.6
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>martin.kronbichler@it.uu.se
          </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-3.6 miscompiles the following code at -mavx.

$ clang -v
clang version 3.6.0 (tags/RELEASE_360/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation:
$HOME/sw/bin/../lib/gcc/x86_64-linux-gnu/4.9.1
Found candidate GCC installation:
$HOME/sw/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/3.4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.6.3
Selected GCC installation: $HOME/sw/bin/../lib/gcc/x86_64-linux-gnu/4.9.1
Candidate multilib: .;@m64
Selected multilib: .;@m64

-------------------------------------------------

#include <stdio.h>

typedef double __m256d __attribute__((__vector_size__(32)));

class VectorizedArray
{
public:
  VectorizedArray &
  operator += (const VectorizedArray &vec)
  {
    data += vec.data;
    return *this;
  }

  __m256d data;
};

inline
VectorizedArray
operator + (const VectorizedArray &u,
            const VectorizedArray &v)
{
  VectorizedArray tmp = u;
  return tmp+=v;
}


int main()
{
  VectorizedArray a, b, c;
  for (unsigned int i=0; i<4; ++i)
    {
      ((double*)(&a.data))[i] = 2.;
      ((double*)(&b.data))[i] = -1.;
    }
  c = a + b;
  for (unsigned int i=0; i<4; ++i)
    printf("%lf\n", ((const double*)(&c.data))[i]);
}

-------------------------------------------------


$ clang -mavx test.cc && ./a.out
1.000000
1.000000
0.000000
0.000000

(Should be 4 times 1.000000).


The wrong code is in operator+(VectorizedArray const&, VectorizedArray const&):
        vzeroupper
        callq   _ZN15VectorizedArraypLERKS_
        vmovaps (%rax), %ymm0
        vmovaps %ymm0, 64(%rsp)
        vmovupd 64(%rsp), %xmm0
        movq    %rbp, %rsp
        popq    %rbp
        vzeroupper
        retq

Note how the content of %ymm0 gets assigned into an %xmm0 and thus, the upper
128 bits are lost. The code generation is wrong both at -O0 (shown above) and
-O2 and happens for both __m256 and __m256d. clang 3.5 and previous do fine on
this example.</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>