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

AVX-512: Horizontal add pattern match does not work on <16 x i32> vectors

Open delena opened this issue 9 years ago • 14 comments

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

delena avatar Jul 12 '17 12:07 delena

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

RKSimon avatar Jul 25 '19 08:07 RKSimon

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

RKSimon avatar Jul 21 '19 10:07 RKSimon

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

RKSimon avatar Feb 26 '19 19:02 RKSimon

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". :)

rotateright avatar Feb 10 '19 15:02 rotateright

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

rotateright avatar Feb 10 '19 15:02 rotateright

We have this down to all 128-bit ops

(for the 2nd example)

rotateright avatar Feb 06 '19 00:02 rotateright

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

rotateright avatar Feb 06 '19 00:02 rotateright

Current codegen: https://godbolt.org/z/OMVYFe

RKSimon avatar Jan 11 '19 15:01 RKSimon

Update on this:

  1. We can now distinguish between CPUs that have slow/fast horizontal op implementations, and that determines the codegen for the 256-bit example.

  2. 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.

rotateright avatar Jan 11 '19 14:01 rotateright

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

rotateright avatar Oct 12 '18 14:10 rotateright

Please review following fix.

https://reviews.llvm.org/D36454

Thanks

llvmbot avatar Aug 08 '17 10:08 llvmbot

Wouldn't the extracts and inserts plus the fact that phadd is multiple uops be worse than the current code?

topperc avatar Jul 12 '17 17:07 topperc

We should use ymm instruction for reduction.

delena avatar Jul 12 '17 16:07 delena

There isn't a 512-bit horizontal add instruction in avx-512.

topperc avatar Jul 12 '17 15:07 topperc