<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 - compiler crash when optimizing 3 or more HADD/HSUB intrinsics"
   href="https://bugs.llvm.org/show_bug.cgi?id=52040">52040</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>compiler crash when optimizing 3 or more HADD/HSUB intrinsics
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>benjsith@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, pengfei.wang@intel.com, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25315" name="attach_25315" title="Compiler output with error log/callstack">attachment 25315</a> <a href="attachment.cgi?id=25315&action=edit" title="Compiler output with error log/callstack">[details]</a></span>
Compiler output with error log/callstack

A week ago, I reported <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - compiler crash when optimizing multiple _mm_hsub_epi16 intrinsics in certain pattern"
   href="show_bug.cgi?id=51974">https://bugs.llvm.org/show_bug.cgi?id=51974</a> which was
fixed in commit 468ff703e114599ce8fb7457bd3c7ef0b219e952. However, I came
across a variant of it when at least 3 HSUB/HADD intrinsics are chained
together in a particular way. This causes a cycle in the DAG as well, even with
the new fix.

The following code is a minimal repro for it, causing an assert about cycles in
Debug and looping forever in Release:

#include <immintrin.h>
__m128i do_stuff(__m128i I0) {
    __m128i A = _mm_hadd_epi16(I0, I0);
    __m128i B = _mm_hadd_epi16(A, A);
    __m128i C = _mm_hadd_epi16(B, A);
    return C;
}

when compiled with 'clang -O1 -mavx2'. The same issue occurs if all the
_mm_hadd_epi16 calls are changed to one of _mm_hadd_epi32, _mm_hsub_epi16, or
_mm_hsub_epi32.

I looked into the code in combineVectorHADDSUB() in X86ISelLowering.cpp, and
noticed the following code being hit:

SDValue LHS0 = LHS.getOperand(0);
SDValue RHS0 = LHS.getOperand(1);
SDValue LHS1 = RHS.getOperand(0);
SDValue RHS1 = RHS.getOperand(1);

This appears to be a typo, and changing it to 

SDValue LHS0 = LHS.getOperand(0);
SDValue LHS1 = LHS.getOperand(1);
SDValue RHS0 = RHS.getOperand(0);
SDValue RHS1 = RHS.getOperand(1);

avoids the DAG cycle, and seems to optimize the function correctly. I don't
know what knock-on effects this would have.

I have also attached the debug output from the compiler I get from the test
case above which shows the cycle in the DAG and a callstack of the assert.

I tested on the latest trunk (commit e420164f40a907643db40e65fff51a6041d40090)
and it's still present. I have observed it on both Windows and Linux.</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>