<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 - LoopVectorizer miscompiles (when doing fold by tail masking?)"
   href="https://bugs.llvm.org/show_bug.cgi?id=43166">43166</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LoopVectorizer miscompiles (when doing fold by tail masking?)
          </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>Windows NT
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>bjorn.a.pettersson@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>ayal.zaks@intel.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=22447" name="attach_22447" title="A somewhat reduced ll test case">attachment 22447</a> <a href="attachment.cgi?id=22447&action=edit" title="A somewhat reduced ll test case">[details]</a></span>
A somewhat reduced ll test case

A test case (lv-bug.c) like this seems to miscompile when using -fvectorize:

  #include <stdint.h>
  #include <stdio.h>

  int y = 0;
  int b = 1;
  int d = 1;

  int main() {
    #pragma clang loop vectorize_width(4)
    for (int i = 0; i < 3; ++i) {
      b = (y == 0) ? d : (d / y);
    }
    if (b == 1)
      printf("GOOD!\n");
    else
      printf("BAD!\n");
  }


When compiled+executed (for x86_64 GNU/Linux) using

    build-all/bin/clang -O1 lv-bug.c && ./a.out

the result is "GOOD!"

When compiled+executed using

    build-all/bin/clang -O1 lv-bug.c -fvectorize && ./a.out

the result is "BAD!"


Also see comments in the attached lv-fold-tail-by-masking-bug.ll lit test case.

Afaict the problem is related to the "fold tail by masking" that takes place
(since we have a small trip count that even is smaller than the VF, but the
problem is also seen if we for example increase the trip count to 9).

Somehow the presence of the conditional sdiv (not executed for y==0) inside the
loop fools LV to vectorize the loop???
(if changing the sdiv to for example xor I see remarks such as "loop not
vectorized: Cannot fold tail by masking in the presence of live outs.")

I've tried lots of old (nightly) downstream builds of clang, and it started to
fail around October 2018. So I do not think that it is a new bug.
The ll-reproducer indicates that the problem exist in opt 8.0.0 (when testing
on <a href="https://godbolt.org/z/uaz-P9">https://godbolt.org/z/uaz-P9</a>).</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>