<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94419>94419</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Math function vectorization failure with AVX-512
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
m13253
</td>
</tr>
</table>
<pre>
I am writing a machine learning software that needs to compute “Y = exp(a⋅X)”.
Sample code:
```c++
#include <cmath>
#include <cstddef>
void func(float a[]) {
for(std::size_t i = 0; i != 16; i++) {
a[i] = std::exp(a[i] * 2.0f);
}
}
```
Expected output:
```asm
push rbx
mov rbx, rdi
vmovups zmm0, ZMMWORD PTR [rdi]
vaddps zmm0, zmm0, zmm0
call _ZGVeN16v_expf@PLT
vmovups ZMMWORD PTR [rbx], zmm0
pop rbx
vzeroupper
ret
```
Actual output: Fails to use any vectorized math functions. (See <https://godbolt.org/z/975T6xbss>)
Clang version: 18.1.0
Compilation flags: `clang++ -Ofast -fopenmp -fveclib=libmvec -mprefer-vector-width=512 -march=skylake-avx512`
---
**Alternate 1:** without `* 2.0f`.
```c++
void func(float a[]) {
for(std::size_t i = 0; i != 16; i++) {
a[i] = std::exp(a[i]);
}
}
```
Output: Calls the AVX2 math function, instead of the AVX-512 one.
**Alternate 2:** separate `* 2.0f` and `std::exp`.
```c++
void func(float a[]) {
for(std::size_t i = 0; i != 16; i++) {
a[i] *= 2.0f;
}
for(std::size_t i = 0; i != 16; i++) {
a[i] = std::exp(a[i]);
}
}
```
Output: Fails to use any vectorized math functions.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVl9v2zYQ_zT0y0GCRFmy_aAHJ66HActStEHX9SU4iSebKyUSJKU4-fQDJduxs6BA3zqDoHTH4_358cez0Dm564hKlt-wfDPD3u-1Lds043k2q7R4Ln8HbOHJSi-7HSC0WO9lR6AIbRdUTjf-CS2B36OHjk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