<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 - [LV] Loop vectorizer wrongly propagate 'nuw' flag"
   href="https://bugs.llvm.org/show_bug.cgi?id=52111">52111</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LV] Loop vectorizer wrongly propagate 'nuw' flag
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </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>diegocaballero@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25337" name="attach_25337" title="Reproducer">attachment 25337</a> <a href="attachment.cgi?id=25337&action=edit" title="Reproducer">[details]</a></span>
Reproducer

Hello,

I think we found a correctness issue in the loop vectorizer related to the
incorrect propagation of the ‘nuw’ flag for instructions guarded by a
condition. The following pseudo-code illustrates the problem:

for (i = 0; i<4; ++i) {
  if(i > 0) {
    … = a[i-1]
  } 
}

In this example, the computation of `i-1` could be flagged with ‘nuw’ because
the guarding condition `i > 0` guarantees that the unsigned result will always
be within the range [0 - 2] and, therefore, will never wrap around. However,
when the loop is vectorized and predication flattens the control flow, the
previous property is no longer true:

for (i = 0; i<4; i+=4) {
  mask = {1, 1, 1, 0};
  … = masked_load(&a[i-1], mask); 
}

In this case, the computation of `i-1` is no longer guarded by the original
condition and the unsigned result will wrap-around for i=0. The vectorizer
currently preserves the original ‘nuw’ value for `i-1`, which leads to a poison
value that is later optimized away, resulting in an incorrect vector code.

Please, find attached a test case. The problematic code before vectorization
is:

concat_index_from_operand_id1:                    ; preds =
%concatenate.pivot.1.4
  %20 = add nsw i64 %fusion.indvar.dim.32, -1
  %21 = mul nuw nsw i64 %fusion.indvar.dim.24, 3
  %22 = add nuw nsw i64 %20, %21
  %23 = getelementptr inbounds [1 x [6 x float]], [1 x [6 x float]]* %11, i64
0, i64 0, i64 %22

After vectorization (opt before_lv.ll -scoped-noalias-aa -loop-vectorize
-ipsccp -mcpu=skx -S -o after_lv.ll):

vector.body:                                      ; preds = %vector.ph
  %14 = mul nuw nsw i64 %fusion.indvar.dim.24, 3
  %15 = add nuw nsw i64 -1, %14  // nuw & -1 -> Poison!
  %16 = getelementptr inbounds [1 x [6 x float]], [1 x [6 x float]]* %11, i64
0, i64 0, i64 %15

I haven’t thought too much about the solution. Maybe we could drop the
‘nuw’/’nsw’ flags of uniform operations that are not immediately nested in the
loop body. Perhaps, during predication?

Thanks,
Diego</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>