<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 - [LoopVectorization] Invalid code after interleaving add reduction"
   href="https://bugs.llvm.org/show_bug.cgi?id=48546">48546</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LoopVectorization] Invalid code after interleaving add reduction
          </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>Linux
          </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>congzhecao@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following C code generate an issue as follows:

int printf(const char *, ...);
int a, b, e;
char c, d;
int main() {
  d = 19;
  for (; d < 45; d++) {
    char *f = &c;
    *f &= 1;
    e = b== 0 ?:a%b;
    (*f)--;
  }
  printf("%d\n", c);
}

This code compiled with the following flag causes a bug:

clang test.c -O2 && ./a.out
Output: 254

The correct output is '0'.

Upon further investigation, the bug is caused by the interleaving of the Loop
Vectorizer.

The follwing command produces correct code:

clang test.c -O2 -mllvm -force-vector-interleave=1 && ./a.out

The root cause is not clear yet, but it seems like there is an add reduction
that the loop vectorization legalily determines that it is safe to vectorize,
but in this case it generates an incorrect result.

The IVDescriptor.cpp:629, the function call to AddReductionVar for Add
reduction finds this to be a signed i1 add reduction, which to me seems like an
invalid type.

The test file reduction-small-size.ll is important for this because it has the
same type if reduction, but the interleaving is forced to 1, if we force it to
2 we have an invalid code.</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>