[llvm-bugs] [Bug 33967] New: TBAA leads to incorrect code generation (aliasing between scalar and SSE vector type)

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 27 12:48:20 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=33967

            Bug ID: 33967
           Summary: TBAA leads to incorrect code generation (aliasing
                    between scalar and SSE vector type)
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: wenzel.jakob at epfl.ch
                CC: llvm-bugs at lists.llvm.org

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 Bug 32056, 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 (https://reviews.llvm.org/D31885) 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;
}

-- 
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/20170727/55dca326/attachment.html>


More information about the llvm-bugs mailing list