<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70687>70687</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect result in clang++ OpenMP for parallel loop increments > 1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nh2
</td>
</tr>
</table>
<pre>
I posted this initially to https://stackoverflow.com/questions/77385488/clang-openmp-loop-continues-counting-beyond-loop-condition-limit
It seems that clang (tried <= 17) miscompiles the following simple `-fopenmp` program:
```c++
#include <iostream>
#include <omp.h>
using namespace std;
int main(const int, const char **) {
const size_t n = 13; // triggers the issue
// const uint32_t n = 13; // fixes the issue
#pragma omp parallel for
for (uint32_t i = 0; i < n; i += 2) { // `+= 1` fixes the issue, as does `+= 2lu` or `+= 2llu`; `+= 2u` does not
cerr << "i = " << i << ", n = " << n << endl;
}
}
```
Despite my `i < n`, where `n = 13` this prints an infinite series of numbers on `clang++ -fopenmp`:
```
i = 0, n = 13
i = 2, n = 13
i = 4, n = 13
i = 6, n = 13
i = 8, n = 13
i = 10, n = 13
i = 12, n = 13 <- this should be impossible
i = 14, n = 13
...
```
This happens with clang++ 11, 14, and 16, but not with GCC. It's also fixed if `-fopenmp` is removed.
The issue dissappears if I do `+= 1` instead of `+= 2`.
According to https://stackoverflow.com/questions/7448812/openmp-loop-increment, a "loop increment greater than 1" is legal.
The issue also disappears if I change the type of `n` from `size_t` to `uint32_t`.
And it disappears when changing to `+= 2lu` or `+= 2llu`; but `+= 2u` does not fix it.
## Observation regarding warnings
With `-Wall` and `+= 2`, `clang++` prints these warnings:
```
clang-openmp-for-loop-test.cpp:49:3: warning: implicit conversion from 'unsigned long' to 'unsigned int' changes value from 9223372034707292167 to 2147483655 [-Wconstant-conversion]
for (uint32_t i = 0; i < n; i += 2) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clang-openmp-for-loop-test.cpp:49:3: warning: implicit conversion from 'unsigned long' to 'unsigned int' changes value from 9223372034707292167 to 2147483655 [-Wconstant-conversion]
for (uint32_t i = 0; i < n; i += 2) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clang-openmp-for-loop-test.cpp:49:3: warning: implicit conversion from 'unsigned long' to 'unsigned int' changes value from 9223372034707292167 to 2147483655 [-Wconstant-conversion]
for (uint32_t i = 0; i < n; i += 2) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 warnings generated.
```
There are multiple things I don't understand about these warnings:
1. Why is the same warning (at same code position) emitted 3 times?
2. Where is the value `9223372034707292167` coming from? It's a `-Wconstant-conversion` warning, surely it should warn about some constants I actually use. The value `2147483655` is the `maximum 32-bit-signed integer + 8`.
3. How comes this type-related warning goes away when I change `+= 2` to `+= 1`? Surely the _type_ of those two literals are the same?
I have the suspicion that these warnings accidentally reveal some internal transformations that clang does, and may give a hint on what's wrong.
## Environment
Ubuntu 22.04.
```sh
sudo apt install clang14 libomp-14-dev gcc+12 libstdc++-12-dev
```
(The `gcc+12 libstdc++-12-dev` are needed just for `#include <iostream>` to work, as clang uses GCC's stdlib headers.)
Compile:
```sh
clang++ myprogram.cpp -o myprogram -fopenmp -O2 -Wall -Wextra
```
Run:
```sh
OMP_NUM_THREADS=1 ./myprogram 2>&1 | head -n20
```
---
Our understanding on StackOverflow so far is:
> Still, this is also a clang compiler error: clang initially goes to the implicit cast route, and then messes up the loop (it should either produce a compilation error because of the illegal code, or handle it correctly as an extension like gcc does).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWEuT27gR_jWYSxdZJKjnQYcZjZX1wZmttbd8nALJFok1CDAAKFk55LenGqAkyh7NJpVDLjul0lB49Lv766ZwTjYaccPmT2z-_CAG3xq70S1_KE192nyE3jiPNfhWOpBaeimUOoE30HrfO1Y8Mr5jfOe8qL6ZA9q9Mse0Mh3ju38M6Lw02jG-Wy6L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