AVX-512: Horizontal add pattern match does not work on <16 x i32> vectors
| Bugzilla Link | 33758 |
| Resolution | FIXED |
| Resolved on | Jul 25, 2019 01:08 |
| Version | trunk |
| OS | Windows NT |
| Attachments | Hadd example for <8 x i32> and <16 x i32> cases |
| CC | @topperc,@RKSimon,@rotateright,@ZviRackover |
| Fixed by commit(s) | r366933 |
Extended Description
The phadd instruction is not chosen from <16 x i32> shuffles+add combination. This affects reduction tail code.
The same pattern generates better sequence on <8 x i32> code and on AVX2 target.
vpshufd $238, %zmm0, %zmm1
vpaddd %zmm1, %zmm0, %zmm0
vpshufd $229, %zmm0, %zmm1
vpaddd %zmm1, %zmm0, %zmm0
vmovd %xmm0, %eax
better sequence:
vpshufd $238, %zmm0, %zmm1
vpaddd %zmm1, %zmm0, %zmm0
vphadd %zmm0, %zmm0, %zmm0
vmovd %xmm0, %eax
run llc -skx < hadd.ll to reproduce
There's a tiny bit more we can do to improve optsize builds (which is the same as fast-horiz but more likely to be requested) code:
https://godbolt.org/z/yP5CgS
https://reviews.llvm.org/D65047 should handle the partial reduction cases
Committed rL366933
There's a tiny bit more we can do to improve optsize builds (which is the same as fast-horiz but more likely to be requested) code:
https://godbolt.org/z/yP5CgS
https://reviews.llvm.org/D65047 should handle the partial reduction cases
There's a tiny bit more we can do to improve optsize builds (which is the same as fast-horiz but more likely to be requested) code:
https://godbolt.org/z/yP5CgS
We still don't match the 2nd example to a hadd, but there is no actual AVX512 capable target with -fast-hops that would benefit from that optimization AFAIK.
I think we should resolve this bug as "FIXED ENOUGH". :)
I think this is the optimal output for -mcpu=skx
_hadd: vpshufd $78, %xmm0, %xmm1 ## xmm1 = xmm0[2,3,0,1] vpaddd %xmm1, %xmm0, %xmm0 vpshufd $229, %xmm0, %xmm1 ## xmm1 = xmm0[1,1,2,3] vpaddd %xmm1, %xmm0, %xmm0 vmovd %xmm0, %eax vzeroupper retq _hadd_16: vpshufd $78, %xmm0, %xmm1 ## xmm1 = xmm0[2,3,0,1] vpaddd %xmm1, %xmm0, %xmm0 vpshufd $229, %xmm0, %xmm1 ## xmm1 = xmm0[1,1,2,3] vpaddd %xmm1, %xmm0, %xmm0 vmovd %xmm0, %eax vzeroupper retq
There's an additional improvement for the first test on a theoretical target with -mattr=fast-hops after: https://reviews.llvm.org/rL353641
_hadd: vpshufd $78, %xmm0, %xmm1 ## xmm1 = xmm0[2,3,0,1] vpaddd %xmm1, %xmm0, %xmm0 vphaddd %xmm0, %xmm0, %xmm0 vmovd %xmm0, %eax vzeroupper retq
We have this down to all 128-bit ops
(for the 2nd example)
We have insert-of-insert and extract-of-extract folds plus shuffle narrowing, so after: https://reviews.llvm.org/D57336
We have this down to all 128-bit ops and if you have -mattr=fast-hops, the 1st case gets a hadd:
hadd: # @hadd vpshufd $78, %xmm0, %xmm1 # xmm1 = xmm0[2,3,0,1] vpaddd %ymm1, %ymm0, %ymm0 vphaddd %ymm0, %ymm0, %ymm0 vmovd %xmm0, %eax vzeroupper retq hadd_16: # @hadd_16 vpshufd $78, %xmm0, %xmm1 # xmm1 = xmm0[2,3,0,1] vpaddd %xmm1, %xmm0, %xmm0 vpshufd $229, %xmm0, %xmm1 # xmm1 = xmm0[1,1,2,3] vpaddd %xmm1, %xmm0, %xmm0 vmovd %xmm0, %eax vzeroupper retq
Current codegen: https://godbolt.org/z/OMVYFe
Update on this:
-
We can now distinguish between CPUs that have slow/fast horizontal op implementations, and that determines the codegen for the 256-bit example.
-
I removed a limit on the build vector lowering of horizontal ops here: https://reviews.llvm.org/rL350928
That should allow more h-ops for AVX512 generally, but that doesn't affect the case shown here. The first problem to overcome here is a missing generic DAG combine: t33: v8i32 = insert_subvector undef:v8i32, t35, Constant:i64<0> t21: v16i32 = insert_subvector undef:v16i32, t33, Constant:i64<0>
We should be able to squash the 2 inserts into 1.
IMO, the main problem here isn't producing horizontal ops. We should be reducing the width of the adds because they're full of undefs. Getting that to happen is a multi-step process if we're going to do it generally (which I think we should).
I've listed some necessary improvements here: https://reviews.llvm.org/D52912
Please review following fix.
https://reviews.llvm.org/D36454
Thanks
Wouldn't the extracts and inserts plus the fact that phadd is multiple uops be worse than the current code?
We should use ymm instruction for reduction.
There isn't a 512-bit horizontal add instruction in avx-512.