llvm-project
ad15ff81 - [LoongArch] Add PartialReduceMLA and getPartialReductionCost support (#203851)

Commit
20 hours ago
[LoongArch] Add PartialReduceMLA and getPartialReductionCost support (#203851) Use `vmulwev.h.b[u] + vmulwod.h.b[u] + 2 x vhaddw.w.h + 2 x vadd.w` in a row for `PARTIAL_REDUCE_MLA` opcodes. Also implement `getPartialReductionCost` so that we could guarantee the generated pattern is legal and efficient. For example, this snippet from 706.stockfish_r: ``` #include <cstdint> using IndexType = std::uint32_t; void transform(std::int32_t *__restrict__ output, const std::int8_t *weights, const std::int32_t *biases, const std::uint8_t *input) { for (IndexType i = 0; i < 0x1000; ++i) { const IndexType offset = i * 0x1000; std::int32_t sum = biases[i]; for (IndexType j = 0; j < 0x1000; ++j) { sum += weights[offset + j] * input[j]; } output[i] = sum; } } ``` We could generate this assembly code with `clang -O3 -mlasx -mllvm --vectorizer-maximize-bandwidth`. ``` transform: add.d $t2, $a1, $t1 xvldx $xr3, $t2, $a6 add.d $t3, $a3, $t1 xvldx $xr4, $t3, $a6 xvldx $xr5, $t2, $a7 xvldx $xr6, $t3, $a7 xvmulwod.h.bu.b $xr7, $xr4, $xr3 xvhaddw.w.h $xr7, $xr7, $xr7 xvmulwev.h.bu.b $xr3, $xr4, $xr3 xvhaddw.w.h $xr3, $xr3, $xr3 xvadd.w $xr3, $xr3, $xr7 xvadd.w $xr1, $xr3, $xr1 xvmulwod.h.bu.b $xr3, $xr6, $xr5 xvhaddw.w.h $xr3, $xr3, $xr3 xvmulwev.h.bu.b $xr4, $xr6, $xr5 xvhaddw.w.h $xr4, $xr4, $xr4 xvadd.w $xr3, $xr4, $xr3 addi.d $t1, $t1, 64 xvadd.w $xr2, $xr3, $xr2 bnez $t1, .LBB0_2 ``` Without this patch: ``` transform: add.d $t1, $a1, $t0 ldptr.d $t2, $t1, 4096 ldptr.d $t1, $t1, 4104 vinsgr2vr.d $vr3, $t2, 0 vinsgr2vr.d $vr4, $t1, 0 add.d $t1, $a3, $t0 ldptr.d $t2, $t1, 4096 ldptr.d $t1, $t1, 4104 vext2xv.w.b $xr3, $xr3 vext2xv.w.b $xr4, $xr4 vinsgr2vr.d $vr5, $t2, 0 vinsgr2vr.d $vr6, $t1, 0 vext2xv.wu.bu $xr5, $xr5 vext2xv.wu.bu $xr6, $xr6 xvmadd.w $xr1, $xr5, $xr3 addi.d $t0, $t0, 16 xvmadd.w $xr2, $xr6, $xr4 bnez $t0, .LBB0_2 ``` Here are the micro-benchmark results comparison, runs on Loongson 3C5000L. ``` === Validation === All implementations produce identical results. === Benchmark (ns per call) === Before (LASX) 4111440 ns/iter (50 iters) After (LASX) 1751189 ns/iter (50 iters) ``` It's worth noticing that we didn't enable `MaximizeBandwidth` by default, which AArch64 NEON did. There is an ongoing X86 [PR](https://github.com/llvm/llvm-project/pull/201666) and a [RFC discussion](https://discourse.llvm.org/t/rfc-enable-maximizebandwidth-by-default-for-x86-in-the-loop-vectorizer/90992) about the pros and cons about this change, we would better waiting for further investigations and SPEC benchmark results on LoongArch. Related: https://github.com/loongson-community/discussions/issues/119
Author
Parents
Loading