[llvm-bugs] [Bug 46189] New: wrong vectorized code generated at -O2 on x86_64-linux-gnu

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jun 4 00:16:26 PDT 2020


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

            Bug ID: 46189
           Summary: wrong vectorized code generated at -O2 on
                    x86_64-linux-gnu
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: kazu at google.com
                CC: llvm-bugs at lists.llvm.org

A recent patch:

https://github.com/llvm/llvm-project/commit/9fa58d1bf2f83a556c109f701aacfb92e2184c23

causes the following testcase to produce an incorrect result:

// $ clang++ -O2 bug.cc  (on x86_64-linux-gnu)
#include <utility>
#include <stdio.h>

__attribute__((noinline))
std::pair<std::pair<int, int>, std::pair<int, int>>
foo(const double x, const double y, const double r, const double xr, const
double yr) {
  std::pair<int, int> start = std::make_pair(static_cast<int>((x - r) / xr),
                                             static_cast<int>((y - r) / yr));
  std::pair<int, int> end = std::make_pair(static_cast<int>((x + r) / xr),
                                           static_cast<int>((y + r) / yr));
  return std::make_pair(start, end);
}

int main(void) {
  double x = 5.0;
  double y = 7.0;
  double r = 1.0;
  double xr = 2.0;
  double yr = 2.0;

  // Expect:
  // first.first:   (5.0-1.0)/2.0 = 2.0
  // first.second:  (7.0-1.0)/2.0 = 3.0
  // second.first:  (5.0+1.0)/2.0 = 3.0
  // second.second: (7.0+1.0)/2.0 = 4.0
  auto a = foo(x, y, r, xr, yr);
  printf("foo: %d %d %d %d\n",
         a.first.first, a.first.second, a.second.first, a.second.second);
  return 0;
}

Without the patch, I get:

$ clang++ -O2 bug.cc && ./a.out
foo: 2 3 3 4

With the patch, I get:

$ clang++ -O2 bug.cc && ./a.out
foo: 2 3 3 1

FWIW, without the patch, `foo` looks like:

        movapd  %xmm0, %xmm5
        subsd   %xmm2, %xmm5
        addsd   %xmm2, %xmm0
        unpcklpd        %xmm0, %xmm5    # xmm5 = xmm5[0],xmm0[0]
        unpcklpd        %xmm3, %xmm3    # xmm3 = xmm3[0,0]
        divpd   %xmm3, %xmm5
        cvttpd2dq       %xmm5, %xmm0
        movapd  %xmm1, %xmm3
        subsd   %xmm2, %xmm3
        addsd   %xmm2, %xmm1
        unpcklpd        %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0]
        unpcklpd        %xmm4, %xmm4    # xmm4 = xmm4[0,0]
        divpd   %xmm4, %xmm3
        cvttpd2dq       %xmm3, %xmm1
        unpcklps        %xmm1, %xmm0    # xmm0 =
xmm0[0],xmm1[0],xmm0[1],xmm1[1]
        movq    %xmm0, %rax
        pshufd  $78, %xmm0, %xmm0       # xmm0 = xmm0[2,3,0,1]
        movq    %xmm0, %rdx
        retq

With the patch, `foo` looks like:

        movapd  %xmm0, %xmm5
        subsd   %xmm2, %xmm5
        addsd   %xmm2, %xmm0
        unpcklpd        %xmm0, %xmm5    # xmm5 = xmm5[0],xmm0[0]
        unpcklpd        %xmm3, %xmm3    # xmm3 = xmm3[0,0]
        divpd   %xmm3, %xmm5
        cvttpd2dq       %xmm5, %xmm0
        subsd   %xmm2, %xmm1
        addsd   %xmm2, %xmm2
        unpcklpd        %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0]
        unpcklpd        %xmm4, %xmm4    # xmm4 = xmm4[0,0]
        divpd   %xmm4, %xmm1
        cvttpd2dq       %xmm1, %xmm1
        unpcklps        %xmm1, %xmm0    # xmm0 =
xmm0[0],xmm1[0],xmm0[1],xmm1[1]
        movq    %xmm0, %rax
        pshufd  $78, %xmm0, %xmm0       # xmm0 = xmm0[2,3,0,1]
        movq    %xmm0, %rdx
        retq

-- 
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/20200604/c93a0e1d/attachment-0001.html>


More information about the llvm-bugs mailing list