<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - Miscompilation of program using 'restrict' due to overaggressive vectorization"
   href="https://llvm.org/bugs/show_bug.cgi?id=27955">27955</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompilation of program using 'restrict' due to overaggressive vectorization
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

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

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>zwarich@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Using Clang r271331 / LLVM r271325, the following program has different results
when compiled with -O0 and -O3. With -O0, the result is

0
1
3
6
10
15
21
28
36

With -O3, the result is

0
1
3
5
7
9
11
13
15

The problem is that the use of 'restrict' is converted into a 'noalias'
parameter that is then turned into 'alias.scope' metadata upon inlining. While
the original use of 'restrict' was just an assertion about the specific
arguments to 'add', the loop vectorizer assumes that the 'alias.scope' metadata
implies that there are additionally no interiteration dependencies, which is
false.

---
#include <stddef.h>
#include <stdio.h>

static void add(int* restrict p, int* q) {
  *p += *q;
}

__attribute__((noinline)) void f(int* a, int* b, size_t size) {
  for (size_t i = 0; i < size; i++) {
    add(&a[i], &b[i]);
  }
}

int main() {
  const size_t kArraySize = 9;
  int a[kArraySize];

  for (size_t i = 0; i < kArraySize; i++) {
    a[i] = i;
  }

  f(a + 1, a, kArraySize - 1);

  for (size_t i = 0; i < kArraySize; i++) {
    printf("%d\n", a[i]);
  }
}
---</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>