[AVX-512] select + add can produce better sequence
| Bugzilla Link | 33674 |
| Version | trunk |
| OS | All |
| CC | @topperc,@chriselrod,@hfinkel,@RKSimon,@ZviRackover |
Extended Description
This is the simplified C-code:
if (B[i] > 1)
Sum += A[i];
%8 = load <16 x i32>, <16 x i32>* %7, align 4, !dbg !27, !tbaa !30
%9 = icmp sgt <16 x i32> %8, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
%10 = getelementptr inbounds i32, i32* %0, i64 %4
%11 = bitcast i32* %10 to <16 x i32>*
%12 = call <16 x i32> @llvm.masked.load.v16i32.p0v16i32(<16 x i32>* %11, i32 4, <16 x i1> %9, <16 x i32> undef)
%13 = select <16 x i1> %9, <16 x i32> %12, <16 x i32> zeroinitializer
%14 = add nsw <16 x i32> %5, %13
This code generates the following sequence:
vmovdqu32 zmm2, zmmword ptr [rsi + rax]
vpcmpgtd k1, zmm2, zmm0
vmovdqu32 zmm2 {k1} {z}, zmmword ptr [rdi + rax]
vmovdqa32 zmm2 {k1} {z}, zmm2
vpaddd zmm1, zmm1, zmm2
The better sequence:
vpcmpd k1, zmm3, ZMMWORD PTR [rsi+rcx*4], 1
vpaddd zmm4{k1}, zmm4, ZMMWORD PTR [rdi+rcx*4]
mentioned in issue llvm/llvm-bugzilla-archive#50358
*** Bug llvm/llvm-bugzilla-archive#50358 has been marked as a duplicate of this bug. ***
A more interesting case is multiply, where instcombine will create a vector of 1s to push the select up. So in codgen we have to use a constant pool load. But if we pulled the select back down we could use a masked op or a blendv.
I think that might make sense. At least for the target independent binary operators. It looks like moving the select up is the InstCombine canonical IR for this
__m512i test_mm512_mask_add_epi32 (__mmask16 __k,__m512i __A, __m512i __B, __m512i __src) { return _mm512_mask_add_epi32(__A,__k,__A,__B); }
Where the pass through and one of the add inputs are the same.
Does SelectionDAG have the same canonicalization?
Current Codegen: https://c.godbolt.org/z/mXPKFh
define <16 x i32> @selectadd(<16 x i32>* %p0, <16 x i32>* %p1, <16 x i32> %sum) {
%ld0 = load <16 x i32>, <16 x i32>* %p0, align 4
%cmp = icmp sgt <16 x i32> %ld0, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
%ld1 = call <16 x i32> @llvm.masked.load.v16i32.p0v16i32(<16 x i32>* %p1, i32 4, <16 x i1> %cmp, <16 x i32> undef)
%sel = select <16 x i1> %cmp, <16 x i32> %ld1, <16 x i32> zeroinitializer
%add = add nsw <16 x i32> %sum, %sel
ret <16 x i32> %add
}
declare <16 x i32> @llvm.masked.load.v16i32.p0v16i32(<16 x i32>*, i32, <16 x i1>, <16 x i32>)
llc -mcpu=skx
.LCPI0_0:
.long 1 # 0x1
selectadd:
vpbroadcastd .LCPI0_0(%rip), %zmm1
vpcmpltd (%rdi), %zmm1, %k1
vmovdqu32 (%rsi), %zmm1 {%k1} {z}
vmovdqa32 %zmm1, %zmm1 {%k1} {z}
vpaddd %zmm1, %zmm0, %zmm0
retq
If we can swap %sel and %add then we get the optimal code:
define <16 x i32> @selectadd_opt(<16 x i32>* %p0, <16 x i32>* %p1, <16 x i32> %sum) {
%ld0 = load <16 x i32>, <16 x i32>* %p0, align 4
%cmp = icmp sgt <16 x i32> %ld0, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
%ld1 = call <16 x i32> @llvm.masked.load.v16i32.p0v16i32(<16 x i32>* %p1, i32 4, <16 x i1> %cmp, <16 x i32> undef)
%add = add nsw <16 x i32> %sum, %ld1
%sel = select <16 x i1> %cmp, <16 x i32> %add, <16 x i32> %sum
ret <16 x i32> %sel
}
.LCPI1_0:
.long 1 # 0x1
selectadd_opt:
vpbroadcastd .LCPI1_0(%rip), %zmm1
vpcmpltd (%rdi), %zmm1, %k1
vmovdqu32 (%rsi), %zmm1 {%k1} {z}
vpaddd %zmm1, %zmm0, %zmm0 {%k1}
retq
@Craig - do you think in general trying to moves selects afterward makes sense on AVX512?
A more interesting case is multiply, where instcombine will create a vector of 1s to push the select up. So in codgen we have to use a constant pool load. But if we pulled the select back down we could use a masked op or a blendv.
Candidate Patch: https://reviews.llvm.org/D122071
Resolving this as the original issues is now fixed by 10bb62319281c76827d6eda1ee70d4bac7e35935:
The mul(x,select(m,y,1)) -> select(m,mul(x,y),x) fold will be addressed by https://reviews.llvm.org/D122071 once the additional test changes have been addressed.
.LCPI0_0:
.long 1 # 0x1
selectadd:
vpbroadcastd .LCPI0_0(%rip), %zmm1 # zmm1 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
vpcmpltd (%rdi), %zmm1, %k1
vmovdqu32 (%rsi), %zmm1 {%k1} {z}
vpaddd %zmm1, %zmm0, %zmm0 {%k1}
retq
.LCPI1_0:
.long 1 # 0x1
selectadd_opt:
vpbroadcastd .LCPI1_0(%rip), %zmm1 # zmm1 = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
vpcmpltd (%rdi), %zmm1, %k1
vmovdqu32 (%rsi), %zmm1 {%k1} {z}
vpaddd %zmm1, %zmm0, %zmm0 {%k1}
retq