[PATCH] D93476: [LV][ARM] Inloop reduction cost modelling

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 10:55:15 PST 2020


dmgreen created this revision.
dmgreen added reviewers: spatel, RKSimon, fhahn, SjoerdMeijer, efriedma.
Herald added subscribers: danielkiss, kerbowa, pengfei, rogfer01, bollu, hiraditya, kristof.beyls, nhaehnle, jvesely, arsenm.
dmgreen requested review of this revision.
Herald added a subscriber: vkmr.
Herald added a project: LLVM.

This adds cost modelling for the inloop vectorization added in 745bf6cf4471 <https://reviews.llvm.org/rG745bf6cf447110ae025e1c77338020f0c5d5ca80>. Up until now they have been modelled as the original underlying instruction, usually an add. This happens to works OK for MVE with instructions that are reducing into the same type as they are working on. But MVE's instructions can perform the equivalent of an extended MLA as a single instruction:

  %sa = sext <16 x i8> A to <16 x i32>
  %sb = sext <16 x i8> B to <16 x i32>
  %m = mul <16 x i32> %sa, %sb
  %r = vecreduce.add(%m)
  ->
  R = VMLADAV A, B

There are other instructions for performing add reductions of v4i32/v8i16/v16i8 into i32 (VADDV), for doing the same with v4i32->i64 (VADDLV) and for performing a v4i32/v8i16 MLA into an i64 (VMLALDAV). The i64 are particularly interesting as there are no native i64 add/mul instructions, leading to the i64 add and mull naturally getting very high costs.

Also worth mentioning, under NEON there is the concept of a sdot/udot instruction which performs a partial reduction from a v16i8 to a v4i32. They extend and mul/sum the first four elements from the inputs into the first element of the output, repeating for each of the four output lanes. They could possibly be represented in the same way as above in llvm, so long as a vecreduce.add could perform a partial reduction. The vectorizer would then produce a combination of in and outer loop reductions to efficiently use the sdot and udot instructions. Although this patch does not do that yet, it does suggest that separating the input reduction type from the produced result type is a useful concept to model. It also shows that a MLA reduction as a single instruction is fairly common.

This patch attempt to improve the costmodelling of in-loop reductions by:

- Adding some pattern matching in the loop vectorizer cost model to match extended reduction patterns that are optionally extended and/or MLA patterns. This marks the cost of the reduction instruction correctly and the sext/zext/mul leading up to it as free, which is otherwise difficult to tell and may get a very high cost. (In the long run this can hopefully be replaced by vplan producing a single node and costing it correctly, but that is not yet something that vplan can do).
- getArithmeticReductionCost is expanded to include a new result type for the reduction and a flag for specifying whether the reduction is a MLA pattern.
- Expanded the ARM costs to account for these expanded sizes, which is a fairly simple change in itself.
- Some minor alterations to allow inloop reduction larger than the highest vector width and i64 MVE reductions.
- An extra InLoopReductionImmediateChains map was added to the vectorizer for it to efficiently detect which instructions are reductions in the cost model.
- The tests have some updates to show what I believe is optimal vectorization and where we are now.

Put together this can greatly improve performance for reduction loop under MVE.


https://reviews.llvm.org/D93476

Files:
  llvm/include/llvm/Analysis/TargetTransformInfo.h
  llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
  llvm/include/llvm/CodeGen/BasicTTIImpl.h
  llvm/lib/Analysis/TargetTransformInfo.cpp
  llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
  llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h
  llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
  llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h
  llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp
  llvm/lib/Target/ARM/ARMTargetTransformInfo.h
  llvm/lib/Target/X86/X86TargetTransformInfo.cpp
  llvm/lib/Target/X86/X86TargetTransformInfo.h
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
  llvm/test/Transforms/LoopVectorize/ARM/mve-reduction-types.ll
  llvm/test/Transforms/LoopVectorize/ARM/mve-reductions.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93476.312542.patch
Type: text/x-patch
Size: 86749 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201217/f3f9b56e/attachment.bin>


More information about the llvm-commits mailing list