llvm / llvm/llvm-project

[X86] Failure to keep BMI/BMI2/TBM style bit manipulations patterns on avx512 mask predicates

Open
#158,649 3 comments 0 reactions 0 assignees View on GitHub
backend:X86 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Noticed while triaging #158646

Many of the basic BMI bit operations could easily be performed purely on the predicate registers, but instead they do a round trip to the gprs:
```
inline
__mmask8 kblsmsk(__mmask8 x) {
return x ^ (x - 1);
}
inline
__mmask8 kblsr(__mmask8 x) {
return x & (x - 1);
}
inline
__mmask8 kblsi(__mmask8 x) {
return x & -x;
}
```
(NOTE: The above are hacky implementations making use of the mmask types just being integers in the itrinsics headers)

`(x - 1)` can be performed using kadd + allones
`-x` might be trickier but it should be doable as `not(x) + 1` (the 1 can be done as allones+kshift)

The BMI2 BZHI op and many of the TBM patterns could be easy to implement as well.

https://clang.godbolt.org/z/f1v636frj

Something like the blsi case doesn't even manage to keep the and on the predicate masks:
```asm
test_blsi(long long vector[8], long long vector[8], long long vector[8], long long vector[8]):
vpcmpeqq %zmm1, %zmm0, %k0
kmovd %k0, %eax
movl %eax, %ecx
negb %cl
andb %al, %cl
kmovd %ecx, %k1
vpandq %zmm1, %zmm0, %zmm0 {%k1}
retq
```
-->
```asm
test_blsi(long long vector[8], long long vector[8], long long vector[8], long long vector[8]):
vpcmpeqq %zmm1, %zmm0, %k0
kmovd %k0, %eax
negb %al
kmovd %eax, %k1
kandb %k0, %k1
vpandq %zmm1, %zmm0, %zmm0 {%k1}
retq
```

Contributor guide

Open the contributing guide

Research direction

Start with the C++ reproducer and assembly comparison in the issue, then review the related context from #158646. Verify the BMI, BMI2, and TBM mask patterns on Godbolt; done means operations such as BLSI remain in predicate registers instead of round-tripping through general-purpose registers.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.