llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open RKSimon opened this issue 1 year ago • 3 comments

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:

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

-->

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

RKSimon avatar Sep 15 '25 14:09 RKSimon

@llvm/issue-subscribers-backend-x86

Author: Simon Pilgrim (RKSimon)

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

llvmbot avatar Sep 15 '25 14:09 llvmbot

For mmask32/64 its trickier to decide if we should use the BMI instructions or not: https://clang.godbolt.org/z/Yennz8M9b

RKSimon avatar Sep 15 '25 15:09 RKSimon

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

It should be cheaper to calculate it as ~(x - 1) (which is equal to -(x - 1) - 1), as the NOT can be combined with the AND:

kblsi:
    kxnorb 	k1, k0, k0
    kaddb	k1, k0, k1
    kandnb 	k0, k1, k0

WalterKruger avatar Feb 24 '26 05:02 WalterKruger