<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/121859>121859</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang++ is unable to optimize what clang-cl and clang can
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SEt-t
</td>
</tr>
</table>
<pre>
On Windows.
Compiling with
`clang++ -O3 -mavx2 -fopenmp -flto -fuse-ld=lld main.cpp hit.cpp`
results in loop not vectorized. LTO seems to work as `hit_test` is inlined.
Loop is properly vectorized when compiled with
`clang-cl /clang:-O3 /clang:-mavx2 /clang:-fopenmp -flto -fuse-ld=lld main.cpp hit.cpp`
or (rename files to *.c)
`clang -O3 -mavx2 -fopenmp -flto -fuse-ld=lld main.c hit.c`
or removing `-fopenmp` or removing marked function call in main.cpp.
main.cpp
```cpp
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
bool hit_test(float x, float y);
volatile uint64_t k = 1000000000;
int main(int argc, char **argv)
{
uint64_t n = 0;
int ts = clock();
#pragma omp parallel
{
uint64_t nx = 0;
clock(); // <- this frightens optimizer, any external function call
uint64_t r = (uint64_t)__builtin_frame_address(0);
#pragma omp for nowait schedule(dynamic, 16384)
for (uint64_t i = k; i > 0; i--)
{
r += 0x9e3779b97f4a7c15;
float x = (int)(r & 0xFFFFFF)*(2.f/0xFFFFFF) - 1;
float y = (int)(r >> 40)*(2.f/0xFFFFFF) - 1;
if (hit_test(x, y))
nx++;
}
#pragma omp atomic
n += nx;
}
int te = clock();
printf("%g\n", 4.*n/k);
printf("Time: %i\n", te-ts);
return 0;
}
```
hit.cpp
```cpp
#include <stdbool.h>
bool hit_test(float x, float y)
{
return x*x + y*y <= 1;
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVsuuozgQ_RpnUyIyJrwWLLiPrFrqxbQ0y8iBA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