llvm / llvm/llvm-project

[X86][LV] failed to fold scatter(stride store) to masked store

Open
#224,296 1 comment 0 reactions 0 assignees View on GitHub
backend:X86 missed-optimization optimization:AMDCPU vectorizers
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/baocbbv8n
```c++
struct s {
int x;
int y;
};

void test(int n, struct s * a)
{
__builtin_assume(n%16==0);
for (int i = 0; i < n; i++) {
a[i].x = 0;
}
}
```
icx:
```asm
test(int, s*):
test edi, edi
jle .LBB0_3
mov dx, 21845
mov eax, edi
xor ecx, ecx
vpxor xmm0, xmm0, xmm0
kmovd k1, edx
.LBB0_2:
vmovdqu32 zmmword ptr [rsi + 8*rcx] {k1}, zmm0
add rcx, 8
cmp rcx, rax
jb .LBB0_2
.LBB0_3:
vzeroupper
ret
```
clang:
```asm
.LCPI0_0:
.quad 8
.quad 9
.quad 10
.quad 11
.quad 12
.quad 13
.quad 14
.quad 15
.LCPI0_1:
.quad 0
.quad 1
.quad 2
.quad 3
.quad 4
.quad 5
.quad 6
.quad 7
.LCPI0_2:
.quad 16
test(int, s*):
test edi, edi
jle .LBB0_3
vmovdqa64 zmm0, zmmword ptr [rip + .LCPI0_0]
vmovdqa64 zmm1, zmmword ptr [rip + .LCPI0_1]
vpbroadcastq zmm3, qword ptr [rip + .LCPI0_2]
mov eax, edi
vpxor xmm2, xmm2, xmm2
.LBB0_2:
kxnorb k1, k0, k0
add rax, -16
vpscatterqd dword ptr [rsi + 8*zmm1] {k1}, ymm2
kxnorb k1, k0, k0
vpaddq zmm1, zmm1, zmm3
vpscatterqd dword ptr [rsi + 8*zmm0] {k1}, ymm2
vpaddq zmm0, zmm0, zmm3
jne .LBB0_2
.LBB0_3:
vzeroupper
ret
```

Contributor guide

Open the contributing guide

Research direction

Start with the Godbolt reproducer and the `test` function, comparing the icx and clang assembly shown in the issue. Trace the LLVM x86 loop-vectorization or scatter-store lowering path responsible for this output, then add or update a regression test demonstrating that the stride store is folded to a masked store and verify the generated assembly.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.