[LV] failed to vectorize std::bitset in SPEC17 541.leela_r
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
https://godbolt.org/z/evMv3qxfG
found from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121744
> 541.leela_r contains sth along the following:
```c++
#include
#include
class Foo
{
public:
void fun (std::bitset<21*21>& blacksq);
std::vector m_mcowner;
};
void Foo::fun(std::bitset<21*21>& blacksq)
{
for (unsigned int i = 0; i < blacksq.size(); i++)
if (blacksq[i])
m_mcowner[i]++;
}
```
gcc libstdc++:
```asm
"Foo::fun(std::bitset<441ul>&)":
mov r8, QWORD PTR [rdi]
vmovdqa xmm3, XMMWORD PTR .LC0[rip]
mov eax, 1
vpcmpeqd xmm9, xmm9, xmm9
vpbroadcastq xmm5, rax
mov eax, 3
vpsrld xmm8, xmm9, 26
kmovb k2, eax
mov eax, 4
vpxor xmm6, xmm6, xmm6
vpbroadcastd xmm7, eax
lea rdi, [r8+1760]
mov rdx, r8
.L3:
vpmovzxdq xmm2, xmm3
vpsrldq xmm1, xmm3, 8
vpmovzxdq xmm1, xmm1
vpandd xmm0, xmm3, xmm8
vpsrlq xmm2, xmm2, 6
vpextrq rax, xmm2, 1
vmovq rcx, xmm2
vpsrlq xmm1, xmm1, 6
vmovq xmm2, QWORD PTR [rsi+rcx*8]
vmovq rcx, xmm1
vpinsrq xmm2, xmm2, QWORD PTR [rsi+rax*8], 1
vpextrq rax, xmm1, 1
vmovq xmm1, QWORD PTR [rsi+rcx*8]
vpinsrq xmm1, xmm1, QWORD PTR [rsi+rax*8], 1
vpmovzxdq xmm4, xmm0
vpsrldq xmm0, xmm0, 8
vpmovzxdq xmm0, xmm0
vpsllvq xmm4, xmm5, xmm4
vpsllvq xmm0, xmm5, xmm0
vpandq xmm2, xmm2, xmm4
vpandq xmm0, xmm1, xmm0
vpcmpq k0, xmm2, xmm6, 4
vpcmpq k1, xmm0, xmm6, 4
kandb k0, k0, k2
kshiftlb k1, k1, 2
korb k3, k0, k1
kmovb eax, k3
and eax, 15
kmovb k1, eax
je .L2
vmovdqu32 xmm0{k1}{z}, XMMWORD PTR [rdx]
vpsubd xmm0, xmm0, xmm9
vmovdqu32 XMMWORD PTR [rdx]{k1}, xmm0
.L2:
add rdx, 16
vpaddd xmm3, xmm3, xmm7
cmp rdi, rdx
jne .L3
test BYTE PTR [rsi+55], 1
je .L10
inc DWORD PTR [r8+1760]
.L10:
ret
.LC0:
.long 0
.long 1
.long 2
.long 3
```
clang libstdc++ or libc++:
```asm
Foo::fun(std::bitset<441ul>&):
mov rax, qword ptr [rdi]
mov ecx, 6
jmp .LBB0_1
.LBB0_15:
add rcx, 7
cmp rcx, 447
je .LBB0_16
.LBB0_1:
lea rdx, [rcx - 6]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_3
inc dword ptr [rax + 4*rcx - 24]
.LBB0_3:
lea rdx, [rcx - 5]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_5
inc dword ptr [rax + 4*rcx - 20]
.LBB0_5:
lea rdx, [rcx - 4]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_7
inc dword ptr [rax + 4*rcx - 16]
.LBB0_7:
lea rdx, [rcx - 3]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_9
inc dword ptr [rax + 4*rcx - 12]
.LBB0_9:
lea rdx, [rcx - 2]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_11
inc dword ptr [rax + 4*rcx - 8]
.LBB0_11:
lea rdx, [rcx - 1]
mov rdi, rdx
shr rdi, 6
mov rdi, qword ptr [rsi + 8*rdi]
bt rdi, rdx
jae .LBB0_13
inc dword ptr [rax + 4*rcx - 4]
.LBB0_13:
mov rdx, rcx
shr rdx, 6
mov rdx, qword ptr [rsi + 8*rdx]
bt rdx, rcx
jae .LBB0_15
inc dword ptr [rax + 4*rcx]
jmp .LBB0_15
.LBB0_16:
ret
```
Contributor guide
Research direction
Start with the Godbolt reproducer and the linked GCC Bugzilla report, then compare the LLVM/Clang assembly with GCC's output for the std::bitset loop from SPEC17 541.leela_r. Trace the vectorization decisions for this loop and identify the relevant compiler entry point or diagnostics. Done means explaining or correcting why Clang fails to vectorize this case, with evidence from the reproducer.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100