<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58840>58840</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LoopInterchange] Loops including reductions are incorrectly interchanged even when -ffast-math is disabled
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          utsumi-fj
      </td>
    </tr>
</table>

<pre>
    Loop interchange is applied to the loops in the function `test` in the following program even when `-ffast-math` is disabled.

test.c

```c
#include <stdio.h>
#define N 1000

__attribute__((noinline)) void test(double *x, double a[N][N]) {
  // incorrectly interchanged when -ffast-math is disabled
  for (int j = 0; j < N; j++)
    for (int i = 0; i < N; i++)
      *x += a[i][j];
}

int main(void) {
  double x = 0.0;
  double a[N][N];
  double tmp = 0.0;

  for (int j = 0; j < N; j++) {
    for (int i = 0; i < N; i++) {
      if (i % 2 == 0)
        tmp += 0.00000000000000001 * i;
      else
        tmp += 0.000000000000005 * i;

      a[i][j] = tmp;
      tmp *= -1.0;
    }
    tmp += 100;
  }

  test(&x, a);

  printf("%f\n", x);
}
```

```
$ clang --version
clang version 15.0.4 (https://github.com/llvm/llvm-project.git 5c68a1cb123161b54b72ce90e7975d95a8eaf2a4)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work03/lang/llvm/llvm/15.0.4/bin
$ clang -O2 -mllvm -enable-loopinterchange -Rpass=interchange -mcpu=a64fx test.c
test.c:6:5: remark: Loop interchanged with enclosing loop. [-Rpass=loop-interchange]
    for (int i = 0; i < N; i++)
    ^
$ ./a.out
0.000213
$ clang test.c
$ ./a.out
0.000001
```

I think that it is incorrect to apply loop interchange to the loops including reductions when `-ffast-math` is disabled.
As above, loop interchange is applied even when `-ffast-math` is disabled and its execution result is different from when loop interchange is not applied.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.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