| Issue |
242237
|
| Summary |
[X86][SCEV] AVX, FMA AutoVec of manual loops produces suboptimal code
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
kiui6
|
# Description
Clang version: 23.1.4 (https://github.com/llvm/llvm-project 7a941dfb2877a0b8cf30323fefa85ad019e0b0f5)
Target: x86_64-pc-windows-msvc
AVX autovectorization in 2 manully written loops with intrinsics that also handle tail produce suboptimal machine code.
## Minimal Reproducible Example
```c++
#include <cstdint>
#include <immintrin.h>
void MixAccumulateAVX(float* out, const float* src, float gain, uint32_t frames) noexcept
{
const __m256 gainVec8 = _mm256_set1_ps(gain);
const uint32_t avxEnd = frames & ~7;
uint32_t i = 0;
for(; i < avxEnd; i += 8) {
__m256 s0 = _mm256_loadu_ps(src + i);
__m256 o0 = _mm256_loadu_ps(out + i);
_mm256_storeu_ps(out + i, _mm256_fmadd_ps(s0, gainVec8, o0));
}
for(; i < frames; i++) {
out[i] += src[i] * gain;
}
}
void (*gMixAccumulate)(float* out, const float* src, float gain, uint32_t frames) noexcept;
int main() {
volatile bool doesSupportAVX = true;
if(doesSupportAVX) {
gMixAccumulate = &MixAccumulateAVX;
} else {
gMixAccumulate = nullptr;
}
float* out = new float[1024];
float* src = "" float[1024];
gMixAccumulate(out, src, 0.5f, 1024);
return 0;
}
```
Build command
```bat
clang++ -O3 -mavx -mavx2 -mfma -o main.exe main.cpp
```
This code produces suboptimal assembly:
[assemblyAutovec.txt](https://github.com/user-attachments/files/34797522/assemblyAutovec.txt)
I was able to produce more optimal version manually by disabling auto vectorization and unrolls. What's interesting I couldn't make it produce better results with [[assume]] or if(!cond) __builtin_unreachable();.
`__restrict`
`if(avxEnd % 32 != 0) __builtin_unreachable();`
`if(frames - avxEnd >= 8) __builtin_unreachable();`
And other assumes have no effect on auto vectorization.
```c++
void MixAccumulateAVX(float* out, const float* src, float gain, uint32_t frames) noexcept
{
const __m256 gainVec8 = _mm256_set1_ps(gain);
uint32_t i = 0;
const uint32_t avxEnd = frames & ~31u;
_Pragma("clang loop unroll(disable)")
for(; i < avxEnd; i += 32) {
__m256 s0 = _mm256_loadu_ps(src + i);
__m256 s1 = _mm256_loadu_ps(src + i + 8);
__m256 s2 = _mm256_loadu_ps(src + i + 16);
__m256 s3 = _mm256_loadu_ps(src + i + 24);
__m256 o0 = _mm256_loadu_ps(out + i);
__m256 o1 = _mm256_loadu_ps(out + i + 8);
__m256 o2 = _mm256_loadu_ps(out + i + 16);
__m256 o3 = _mm256_loadu_ps(out + i + 24);
_mm256_storeu_ps(out + i, _mm256_fmadd_ps(s0, gainVec8, o0));
_mm256_storeu_ps(out + i + 8, _mm256_fmadd_ps(s1, gainVec8, o1));
_mm256_storeu_ps(out + i + 16, _mm256_fmadd_ps(s2, gainVec8, o2));
_mm256_storeu_ps(out + i + 24, _mm256_fmadd_ps(s3, gainVec8, o3));
}
const __m128 gainVec4 = _mm_set1_ps(gain);
const uint32_t sseEnd = frames & ~3u;
_Pragma("clang loop unroll(disable)")
for (; i < sseEnd; i += 4) {
__m128 s = _mm_loadu_ps(src + i);
__m128 o = _mm_loadu_ps(out + i);
_mm_storeu_ps(out + i, _mm_fmadd_ps(s, gainVec4, o));
}
_Pragma("clang loop vectorize(disable) unroll(disable)")
for(; i < frames; i++) {
out[i] += src[i] * gain;
}
}
```
This produces following assembly output:
[assemblyManul.txt](https://github.com/user-attachments/files/34797738/assemblyManul.txt)
I also attached all build artifacts as is
[Artifacts.zip](https://github.com/user-attachments/files/34798039/Artifacts.zip)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs