<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - TBAA leads to incorrect code generation (aliasing between scalar and SSE vector type)"
   href="https://bugs.llvm.org/show_bug.cgi?id=33967">33967</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>TBAA leads to incorrect code generation (aliasing between scalar and SSE vector type)
          </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>wenzel.jakob@epfl.ch
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the simple snippet of C++ code below, which performs type punning
between an SSE vector register type (__m128) and the underlying scalar type.

Although it may look contrived here, these types of constructions are essential
to implement efficient vector math libraries that use intrinsics internally
while providing standard C accessors to interface with the outside world.
When compiled on my machine (with "clang++ test.cpp -std=c++11 -O3 -msse4.2 -o
test"), I observe the following output 

0.000000 1.000000 2.000000 3.000000 4.000000 0.000000 6.000000 7.000000
                                             (^ note the zero value here, which
should be 5)

If compiled with -fno-strict-aliasing, the fifth entry is equal to 5.000000,
confirming that this is indeed an aliasing optimization-related issue.

In several of my projects developing, this leads to incorrect/inconsistent
results, and the only workaround is to disable aliasing -- obviously not a good
long-term solution as this is highly optimized / performance-sensitive code.
As vector instructions (AVX512, new ARM vector instructions, etc.) are becoming
an increasingly essential ingredient of efficient code, it would be great to
finally address this issue.

A related issue was discussed in <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - Invalid code generation (aliasing between scalar and vector type)"
   href="show_bug.cgi?id=32056">Bug 32056</a>, which focused on a case where the
relationship is made more explicit using a 'union' type annotation. The issue
here targets an orthogonal problem -- what the aliasing relationship of a
vector
type and its underlying scalar type should be.

At a technical level, it appears that this is not that hard to resolve -- Hal
Finkel (<a href="https://reviews.llvm.org/D31885">https://reviews.llvm.org/D31885</a>) wrote: "[this issue] if desired,
should just be fixed (the vector type should be a child of the scalar element
type in the current representation).""

Thank you,
Wenzel

======= C++ snippet =========

#include <immintrin.h>
#include <stdio.h>

struct A {
    A () {
        a = _mm_setr_ps(0.f, 1.f, 2.f, 3.f);
        b = _mm_setr_ps(4.f, 5.f, 6.f, 7.f);
    }

    const float *begin() { return (float *) &a; }
    const float *end() { return (float *) &a + 8; }

    __m128 a, b;
};

int main(int argc, char *argv[]) {
    A a;
    for (float value : a)
        printf("%f ", value);

    return 0;
}</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>