LoongArch: Add reduc_widen_<su>sum optab support Add reduc_widen_ssum and reduc_widen_usum expansions for LSX and LASX. LoongArch has no accumulating form of vhaddw, so chain pairwise widening adds and merge the result into the accumulator with one vector add. Cover half-, quarter-, and eighth-width reductions. --- Changes in v2: - Replace `rtx (*gen) (rtx, rtx, rtx)` with `rtx_insn *(*gen) (rtx, rtx, rtx)` for r17-6440. gcc/ChangeLog: * config/loongarch/loongarch-protos.h (loongarch_expand_reduc_widen_sum): Declare. * config/loongarch/loongarch.cc (loongarch_emit_widen_pair_sum): New function. (loongarch_expand_reduc_widen_sum): New function. * config/loongarch/simd.md (IVEC_WS_2, IVEC_WS_4, IVEC_WS_8): New mode iterators. (WVEC_OCT, wvec_oct): New mode attributes. (reduc_widen_<su>sum<wvec_half><mode>3): New expand. (reduc_widen_<su>sum<wvec_quarter><mode>3): New expand. (reduc_widen_<su>sum<wvec_oct><mode>3): New expand. gcc/testsuite/ChangeLog: * lib/target-supports.exp (check_effective_target_vect_widen_sum_hi_to_si_pattern): Support LoongArch. * gcc.target/loongarch/vector/lasx/reduc-widen-sum.c: New test. * gcc.target/loongarch/vector/lsx/reduc-widen-sum.c: New test.
diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h index eb0ac09..63a671c 100644 --- a/gcc/config/loongarch/loongarch-protos.h +++ b/gcc/config/loongarch/loongarch-protos.h
@@ -179,6 +179,7 @@ extern void loongarch_expand_vector_group_init (rtx, rtx); extern void loongarch_expand_vector_init (rtx, rtx); extern void loongarch_expand_vec_unpack (rtx op[2], bool); +extern void loongarch_expand_reduc_widen_sum (rtx *, bool); extern void loongarch_expand_vec_perm_1 (rtx[]); extern void loongarch_expand_vector_extract (rtx, rtx, int); extern void loongarch_expand_vector_reduc (rtx (*)(rtx, rtx, rtx), rtx, rtx);
diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index c56d8c3..9bf6846 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc
@@ -10400,6 +10400,77 @@ } } +/* Emit a pairwise widening sum computing + + DST[i] = extend (SRC[2 * i]) + extend (SRC[2 * i + 1]), + + where DST has half as many elements as SRC, each element twice as wide. + UNSIGNED_P selects zero extension of the source elements. Return DST. */ + +static rtx +loongarch_emit_widen_pair_sum (rtx src, bool unsigned_p) +{ + machine_mode dst_mode; + rtx_insn *(*gen) (rtx, rtx, rtx); + + switch (GET_MODE (src)) + { + case E_V16QImode: + dst_mode = V8HImode; + gen = (unsigned_p ? gen_lsx_vhaddw_hu_bu : gen_lsx_vhaddw_h_b); + break; + case E_V8HImode: + dst_mode = V4SImode; + gen = (unsigned_p ? gen_lsx_vhaddw_wu_hu : gen_lsx_vhaddw_w_h); + break; + case E_V4SImode: + dst_mode = V2DImode; + gen = (unsigned_p ? gen_lsx_vhaddw_du_wu : gen_lsx_vhaddw_d_w); + break; + case E_V32QImode: + dst_mode = V16HImode; + gen = (unsigned_p ? gen_lasx_xvhaddw_hu_bu : gen_lasx_xvhaddw_h_b); + break; + case E_V16HImode: + dst_mode = V8SImode; + gen = (unsigned_p ? gen_lasx_xvhaddw_wu_hu : gen_lasx_xvhaddw_w_h); + break; + case E_V8SImode: + dst_mode = V4DImode; + gen = (unsigned_p ? gen_lasx_xvhaddw_du_wu : gen_lasx_xvhaddw_d_w); + break; + default: + gcc_unreachable (); + } + + rtx dst = gen_reg_rtx (dst_mode); + emit_insn (gen (dst, src, src)); + return dst; +} + +/* Expand a widening sum reduction, implementing the reduc_widen_ssum and + reduc_widen_usum optabs. OPERANDS[0] (the accumulator, whose elements are + wider than the source elements) is set to OPERANDS[2] plus the sum of the + elements of OPERANDS[1] widened to the accumulator's element width. The + source has two, four or eight times as many elements as the accumulator, + depending on the widths involved. + UNSIGNED_P selects zero extension of the source elements. */ + +void +loongarch_expand_reduc_widen_sum (rtx *operands, bool unsigned_p) +{ + machine_mode acc_mode = GET_MODE (operands[0]); + rtx src = operands[1]; + + do + src = loongarch_emit_widen_pair_sum (src, unsigned_p); + while (GET_MODE (src) != acc_mode); + + rtx_insn *add = gen_add3_insn (operands[0], operands[2], src); + gcc_assert (add != NULL); + emit_insn (add); +} + /* Expand an integral vector unpack operation. */ void
diff --git a/gcc/config/loongarch/simd.md b/gcc/config/loongarch/simd.md index 83c93ad..733d70f 100644 --- a/gcc/config/loongarch/simd.md +++ b/gcc/config/loongarch/simd.md
@@ -50,6 +50,16 @@ (define_mode_iterator IVEC_HB [(ILSX_HB "ISA_HAS_LSX") (ILASX_HB "ISA_HAS_LASX")]) +;; Sources of widening sum reductions, grouped by the number of source +;; elements that contribute to one accumulator element. The accumulator +;; element is two, four or eight times as wide as the source element. +(define_mode_iterator IVEC_WS_2 [(V16QI "ISA_HAS_LSX") (V32QI "ISA_HAS_LASX") + (V8HI "ISA_HAS_LSX") (V16HI "ISA_HAS_LASX") + (V4SI "ISA_HAS_LSX") (V8SI "ISA_HAS_LASX")]) +(define_mode_iterator IVEC_WS_4 [(V16QI "ISA_HAS_LSX") (V32QI "ISA_HAS_LASX") + (V8HI "ISA_HAS_LSX") (V16HI "ISA_HAS_LASX")]) +(define_mode_iterator IVEC_WS_8 [(V16QI "ISA_HAS_LSX") (V32QI "ISA_HAS_LASX")]) + ;; All modes longer than a byte (define_mode_iterator VEC_DWH [(LSX_DWH "ISA_HAS_LSX") (LASX_DWH "ISA_HAS_LASX")]) @@ -118,6 +128,9 @@ (define_mode_attr wvec_quarter [(V8HI "v2di") (V16HI "v4di") (V16QI "v4si") (V32QI "v8si")]) +(define_mode_attr WVEC_OCT [(V16QI "V2DI") (V32QI "V4DI")]) +(define_mode_attr wvec_oct [(V16QI "v2di") (V32QI "v4di")]) + ;; Integer vector modes with the same length and unit size as a mode. (define_mode_attr VIMODE [(V2DI "V2DI") (V4SI "V4SI") (V8HI "V8HI") (V16QI "V16QI") @@ -988,6 +1001,47 @@ DONE; }) +;; Widening sum reductions, implementing the reduc_widen_ssum and +;; reduc_widen_usum optabs. OPERANDS[0] and OPERANDS[2] hold the +;; accumulator, whose elements are twice, four times or eight times as wide +;; as the source elements in OPERANDS[1] and correspondingly fewer in +;; number. LoongArch has no accumulating form of vhaddw, so the pairwise +;; widening sum is accumulated into temporaries and merged with a single +;; vector add. + +(define_expand "reduc_widen_<su>sum<wvec_half><mode>3" + [(match_operand:<WVEC_HALF> 0 "register_operand" "=f") + (match_operand:IVEC_WS_2 1 "register_operand" "f") + (match_operand:<WVEC_HALF> 2 "register_operand" "f") + (any_extend (const_int 0))] + "" +{ + loongarch_expand_reduc_widen_sum (operands, <CODE> == ZERO_EXTEND); + DONE; +}) + +(define_expand "reduc_widen_<su>sum<wvec_quarter><mode>3" + [(match_operand:<WVEC_QUARTER> 0 "register_operand" "=f") + (match_operand:IVEC_WS_4 1 "register_operand" "f") + (match_operand:<WVEC_QUARTER> 2 "register_operand" "f") + (any_extend (const_int 0))] + "" +{ + loongarch_expand_reduc_widen_sum (operands, <CODE> == ZERO_EXTEND); + DONE; +}) + +(define_expand "reduc_widen_<su>sum<wvec_oct><mode>3" + [(match_operand:<WVEC_OCT> 0 "register_operand" "=f") + (match_operand:IVEC_WS_8 1 "register_operand" "f") + (match_operand:<WVEC_OCT> 2 "register_operand" "f") + (any_extend (const_int 0))] + "" +{ + loongarch_expand_reduc_widen_sum (operands, <CODE> == ZERO_EXTEND); + DONE; +}) + ; For "historical" reason we need a punned version of q_d variants. (define_mode_iterator DIVEC [(V2DI "ISA_HAS_LSX") (V4DI "ISA_HAS_LASX")])
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lasx/reduc-widen-sum.c b/gcc/testsuite/gcc.target/loongarch/vector/lasx/reduc-widen-sum.c new file mode 100644 index 0000000..4fd4cec --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/vector/lasx/reduc-widen-sum.c
@@ -0,0 +1,52 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -mlasx" } */ +/* { dg-final { scan-assembler "f_u8_u16:.*xvhaddw\\.hu\\.bu.*\\.size.*f_u8_u16" } } */ +/* { dg-final { scan-assembler "f_u16_u32:.*xvhaddw\\.wu\\.hu.*\\.size.*f_u16_u32" } } */ +/* { dg-final { scan-assembler "f_u32_u64:.*xvhaddw\\.du\\.wu.*\\.size.*f_u32_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s16:.*xvhaddw\\.h\\.b.*\\.size.*f_s8_s16" } } */ +/* { dg-final { scan-assembler "f_s16_s32:.*xvhaddw\\.w\\.h.*\\.size.*f_s16_s32" } } */ +/* { dg-final { scan-assembler "f_s32_s64:.*xvhaddw\\.d\\.w.*\\.size.*f_s32_s64" } } */ +/* { dg-final { scan-assembler "f_u8_u32:.*xvhaddw\\.hu\\.bu.*xvhaddw\\.wu\\.hu.*\\.size.*f_u8_u32" } } */ +/* { dg-final { scan-assembler "f_u16_u64:.*xvhaddw\\.wu\\.hu.*xvhaddw\\.du\\.wu.*\\.size.*f_u16_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s32:.*xvhaddw\\.h\\.b.*xvhaddw\\.w\\.h.*\\.size.*f_s8_s32" } } */ +/* { dg-final { scan-assembler "f_s16_s64:.*xvhaddw\\.w\\.h.*xvhaddw\\.d\\.w.*\\.size.*f_s16_s64" } } */ +/* { dg-final { scan-assembler "f_u8_u64:.*xvhaddw\\.hu\\.bu.*xvhaddw\\.wu\\.hu.*xvhaddw\\.du\\.wu.*\\.size.*f_u8_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s64:.*xvhaddw\\.h\\.b.*xvhaddw\\.w\\.h.*xvhaddw\\.d\\.w.*\\.size.*f_s8_s64" } } */ + +#define N 1024 + +unsigned char b_u8[N]; +unsigned short b_u16[N]; +unsigned int b_u32[N]; +signed char b_s8[N]; +short b_s16[N]; +int b_s32[N]; + +#define TEST(ACC, NAME, ARR) \ + ACC out_##NAME[N]; \ + void f_##NAME (void) \ + { \ + ACC s = 0; \ + int i; \ + for (i = 0; i < N; i++) \ + s += ARR[i]; \ + out_##NAME[0] = s; \ + } + +/* Half-width accumulator: one pairwise widening add. */ +TEST (unsigned short, u8_u16, b_u8) +TEST (unsigned int, u16_u32, b_u16) +TEST (unsigned long long, u32_u64, b_u32) +TEST (short, s8_s16, b_s8) +TEST (int, s16_s32, b_s16) +TEST (long long, s32_s64, b_s32) + +/* Quarter-width accumulator: two consecutive widening adds. */ +TEST (unsigned int, u8_u32, b_u8) +TEST (unsigned long long, u16_u64, b_u16) +TEST (int, s8_s32, b_s8) +TEST (long long, s16_s64, b_s16) + +/* Eighth-width accumulator: three consecutive widening adds. */ +TEST (unsigned long long, u8_u64, b_u8) +TEST (long long, s8_s64, b_s8)
diff --git a/gcc/testsuite/gcc.target/loongarch/vector/lsx/reduc-widen-sum.c b/gcc/testsuite/gcc.target/loongarch/vector/lsx/reduc-widen-sum.c new file mode 100644 index 0000000..66c2697 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/vector/lsx/reduc-widen-sum.c
@@ -0,0 +1,52 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -mlsx" } */ +/* { dg-final { scan-assembler "f_u8_u16:.*vhaddw\\.hu\\.bu.*\\.size.*f_u8_u16" } } */ +/* { dg-final { scan-assembler "f_u16_u32:.*vhaddw\\.wu\\.hu.*\\.size.*f_u16_u32" } } */ +/* { dg-final { scan-assembler "f_u32_u64:.*vhaddw\\.du\\.wu.*\\.size.*f_u32_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s16:.*vhaddw\\.h\\.b.*\\.size.*f_s8_s16" } } */ +/* { dg-final { scan-assembler "f_s16_s32:.*vhaddw\\.w\\.h.*\\.size.*f_s16_s32" } } */ +/* { dg-final { scan-assembler "f_s32_s64:.*vhaddw\\.d\\.w.*\\.size.*f_s32_s64" } } */ +/* { dg-final { scan-assembler "f_u8_u32:.*vhaddw\\.hu\\.bu.*vhaddw\\.wu\\.hu.*\\.size.*f_u8_u32" } } */ +/* { dg-final { scan-assembler "f_u16_u64:.*vhaddw\\.wu\\.hu.*vhaddw\\.du\\.wu.*\\.size.*f_u16_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s32:.*vhaddw\\.h\\.b.*vhaddw\\.w\\.h.*\\.size.*f_s8_s32" } } */ +/* { dg-final { scan-assembler "f_s16_s64:.*vhaddw\\.w\\.h.*vhaddw\\.d\\.w.*\\.size.*f_s16_s64" } } */ +/* { dg-final { scan-assembler "f_u8_u64:.*vhaddw\\.hu\\.bu.*vhaddw\\.wu\\.hu.*vhaddw\\.du\\.wu.*\\.size.*f_u8_u64" } } */ +/* { dg-final { scan-assembler "f_s8_s64:.*vhaddw\\.h\\.b.*vhaddw\\.w\\.h.*vhaddw\\.d\\.w.*\\.size.*f_s8_s64" } } */ + +#define N 1024 + +unsigned char b_u8[N]; +unsigned short b_u16[N]; +unsigned int b_u32[N]; +signed char b_s8[N]; +short b_s16[N]; +int b_s32[N]; + +#define TEST(ACC, NAME, ARR) \ + ACC out_##NAME[N]; \ + void f_##NAME (void) \ + { \ + ACC s = 0; \ + int i; \ + for (i = 0; i < N; i++) \ + s += ARR[i]; \ + out_##NAME[0] = s; \ + } + +/* Half-width accumulator: one pairwise widening add. */ +TEST (unsigned short, u8_u16, b_u8) +TEST (unsigned int, u16_u32, b_u16) +TEST (unsigned long long, u32_u64, b_u32) +TEST (short, s8_s16, b_s8) +TEST (int, s16_s32, b_s16) +TEST (long long, s32_s64, b_s32) + +/* Quarter-width accumulator: two consecutive widening adds. */ +TEST (unsigned int, u8_u32, b_u8) +TEST (unsigned long long, u16_u64, b_u16) +TEST (int, s8_s32, b_s8) +TEST (long long, s16_s64, b_s16) + +/* Eighth-width accumulator: three consecutive widening adds. */ +TEST (unsigned long long, u8_u64, b_u8) +TEST (long long, s8_s64, b_s8)
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp index ff0edfc..7753795 100644 --- a/gcc/testsuite/lib/target-supports.exp +++ b/gcc/testsuite/lib/target-supports.exp
@@ -9510,7 +9510,9 @@ || ([istarget aarch64*-*-*] && ![check_effective_target_aarch64_sve]) || [is-effective-target arm_neon] - || [istarget ia64-*-*] }}] + || [istarget ia64-*-*] + || ([istarget loongarch*-*-*] + && [check_effective_target_loongarch_sx]) }}] } # Return 1 if the target plus current options supports a vector