From: Haochen Jiang Date: Mon, 17 Jul 2023 02:46:07 +0000 (+0800) Subject: i386: Auto vectorize usdot_prod, udot_prod with AVXVNNIINT16 instruction. X-Git-Tag: basepoints/gcc-15~7587 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85c50b4a50af360026b807fca81ef65ba424bdb9;p=thirdparty%2Fgcc.git i386: Auto vectorize usdot_prod, udot_prod with AVXVNNIINT16 instruction. gcc/ChangeLog: * config/i386/sse.md (VI2_AVX2): Delete V32HI since we actually have the same iterator. Also renaming all the occurence to VI2_AVX2_AVX512BW. (usdot_prod): New define_expand. (udot_prod): Ditto. gcc/testsuite/ChangeLog: * gcc.target/i386/vnniint16-auto-vectorize-1.c: New test. * gcc.target/i386/vnniint16-auto-vectorize-2.c: Ditto. --- diff --git a/gcc/config/i386/sse.md b/gcc/config/i386/sse.md index 7471932b27e8..c3fa241af352 100644 --- a/gcc/config/i386/sse.md +++ b/gcc/config/i386/sse.md @@ -545,6 +545,9 @@ V32HI (V16HI "TARGET_AVX512VL")]) (define_mode_iterator VI2_AVX2 + [(V16HI "TARGET_AVX2") V8HI]) + +(define_mode_iterator VI2_AVX2_AVX512BW [(V32HI "TARGET_AVX512BW") (V16HI "TARGET_AVX2") V8HI]) (define_mode_iterator VI2_AVX512F @@ -637,9 +640,6 @@ (V16HI "TARGET_AVX2") V8HI (V8SI "TARGET_AVX2") V4SI]) -(define_mode_iterator VI2_AVX2_AVX512BW - [(V32HI "TARGET_AVX512BW") (V16HI "TARGET_AVX2") V8HI]) - (define_mode_iterator VI248_AVX512VL [V32HI V16SI V8DI (V16HI "TARGET_AVX512VL") (V8SI "TARGET_AVX512VL") @@ -15298,16 +15298,16 @@ }) (define_expand "mul3" - [(set (match_operand:VI2_AVX2 0 "register_operand") - (mult:VI2_AVX2 (match_operand:VI2_AVX2 1 "vector_operand") - (match_operand:VI2_AVX2 2 "vector_operand")))] + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand") + (mult:VI2_AVX2_AVX512BW (match_operand:VI2_AVX2_AVX512BW 1 "vector_operand") + (match_operand:VI2_AVX2_AVX512BW 2 "vector_operand")))] "TARGET_SSE2 && && " "ix86_fixup_binary_operands_no_copy (MULT, mode, operands);") (define_insn "*mul3" - [(set (match_operand:VI2_AVX2 0 "register_operand" "=x,") - (mult:VI2_AVX2 (match_operand:VI2_AVX2 1 "vector_operand" "%0,") - (match_operand:VI2_AVX2 2 "vector_operand" "xBm,m")))] + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand" "=x,") + (mult:VI2_AVX2_AVX512BW (match_operand:VI2_AVX2_AVX512BW 1 "vector_operand" "%0,") + (match_operand:VI2_AVX2_AVX512BW 2 "vector_operand" "xBm,m")))] "TARGET_SSE2 && !(MEM_P (operands[1]) && MEM_P (operands[2])) && && " "@ @@ -15320,28 +15320,28 @@ (set_attr "mode" "")]) (define_expand "mul3_highpart" - [(set (match_operand:VI2_AVX2 0 "register_operand") - (truncate:VI2_AVX2 + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand") + (truncate:VI2_AVX2_AVX512BW (lshiftrt: (mult: (any_extend: - (match_operand:VI2_AVX2 1 "vector_operand")) + (match_operand:VI2_AVX2_AVX512BW 1 "vector_operand")) (any_extend: - (match_operand:VI2_AVX2 2 "vector_operand"))) + (match_operand:VI2_AVX2_AVX512BW 2 "vector_operand"))) (const_int 16))))] "TARGET_SSE2 && && " "ix86_fixup_binary_operands_no_copy (MULT, mode, operands);") (define_insn "*mul3_highpart" - [(set (match_operand:VI2_AVX2 0 "register_operand" "=x,") - (truncate:VI2_AVX2 + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand" "=x,") + (truncate:VI2_AVX2_AVX512BW (lshiftrt: (mult: (any_extend: - (match_operand:VI2_AVX2 1 "vector_operand" "%0,")) + (match_operand:VI2_AVX2_AVX512BW 1 "vector_operand" "%0,")) (any_extend: - (match_operand:VI2_AVX2 2 "vector_operand" "xBm,m"))) + (match_operand:VI2_AVX2_AVX512BW 2 "vector_operand" "xBm,m"))) (const_int 16))))] "TARGET_SSE2 && !(MEM_P (operands[1]) && MEM_P (operands[2])) && && " @@ -15591,8 +15591,8 @@ (define_insn "avx512bw_pmaddwd512" [(set (match_operand: 0 "register_operand" "=v") (unspec: - [(match_operand:VI2_AVX2 1 "register_operand" "v") - (match_operand:VI2_AVX2 2 "nonimmediate_operand" "vm")] + [(match_operand:VI2_AVX2_AVX512BW 1 "register_operand" "v") + (match_operand:VI2_AVX2_AVX512BW 2 "nonimmediate_operand" "vm")] UNSPEC_PMADDWD512))] "TARGET_AVX512BW && " "vpmaddwd\t{%2, %1, %0|%0, %1, %2}"; @@ -21569,16 +21569,16 @@ }) (define_expand "smulhrs3" - [(set (match_operand:VI2_AVX2 0 "register_operand") - (truncate:VI2_AVX2 + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand") + (truncate:VI2_AVX2_AVX512BW (lshiftrt: (plus: (lshiftrt: (mult: (sign_extend: - (match_operand:VI2_AVX2 1 "nonimmediate_operand")) + (match_operand:VI2_AVX2_AVX512BW 1 "nonimmediate_operand")) (sign_extend: - (match_operand:VI2_AVX2 2 "nonimmediate_operand"))) + (match_operand:VI2_AVX2_AVX512BW 2 "nonimmediate_operand"))) (const_int 14)) (match_dup 3)) (const_int 1))))] @@ -21589,18 +21589,18 @@ }) (define_insn "*_pmulhrsw3" - [(set (match_operand:VI2_AVX2 0 "register_operand" "=x,") - (truncate:VI2_AVX2 + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand" "=x,") + (truncate:VI2_AVX2_AVX512BW (lshiftrt: (plus: (lshiftrt: (mult: (sign_extend: - (match_operand:VI2_AVX2 1 "vector_operand" "%0,")) + (match_operand:VI2_AVX2_AVX512BW 1 "vector_operand" "%0,")) (sign_extend: - (match_operand:VI2_AVX2 2 "vector_operand" "xBm,m"))) + (match_operand:VI2_AVX2_AVX512BW 2 "vector_operand" "xBm,m"))) (const_int 14)) - (match_operand:VI2_AVX2 3 "const1_operand")) + (match_operand:VI2_AVX2_AVX512BW 3 "const1_operand")) (const_int 1))))] "TARGET_SSSE3 && && && !(MEM_P (operands[1]) && MEM_P (operands[2]))" @@ -22327,8 +22327,8 @@ (set_attr "mode" "")]) (define_insn "_packusdw" - [(set (match_operand:VI2_AVX2 0 "register_operand" "=Yr,*x,") - (unspec:VI2_AVX2 + [(set (match_operand:VI2_AVX2_AVX512BW 0 "register_operand" "=Yr,*x,") + (unspec:VI2_AVX2_AVX512BW [(match_operand: 1 "register_operand" "0,0,") (match_operand: 2 "vector_operand" "YrBm,*xBm,m")] UNSPEC_US_TRUNCATE))] @@ -30340,6 +30340,42 @@ (UNSPEC_VPDPWSUD "wsud") (UNSPEC_VPDPWSUDS "wsuds") (UNSPEC_VPDPWUUD "wuud") (UNSPEC_VPDPWUUDS "wuuds")]) +(define_expand "usdot_prod" + [(match_operand: 0 "register_operand") + (match_operand:VI2_AVX2 1 "register_operand") + (match_operand:VI2_AVX2 2 "register_operand") + (match_operand: 3 "register_operand")] + "TARGET_AVXVNNIINT16" +{ + operands[1] = lowpart_subreg (mode, + force_reg (mode, operands[1]), + mode); + operands[2] = lowpart_subreg (mode, + force_reg (mode, operands[2]), + mode); + emit_insn (gen_vpdpwusd_ (operands[0], operands[3], + operands[1], operands[2])); + DONE; +}) + +(define_expand "udot_prod" + [(match_operand: 0 "register_operand") + (match_operand:VI2_AVX2 1 "register_operand") + (match_operand:VI2_AVX2 2 "register_operand") + (match_operand: 3 "register_operand")] + "TARGET_AVXVNNIINT16" +{ + operands[1] = lowpart_subreg (mode, + force_reg (mode, operands[1]), + mode); + operands[2] = lowpart_subreg (mode, + force_reg (mode, operands[2]), + mode); + emit_insn (gen_vpdpwuud_ (operands[0], operands[3], + operands[1], operands[2])); + DONE; +}) + (define_insn "vpdp_" [(set (match_operand:VI4_AVX 0 "register_operand" "=x") (unspec:VI4_AVX diff --git a/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-1.c b/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-1.c new file mode 100644 index 000000000000..73f0d3296aa4 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-1.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-mavxvnniint16 -O2" } */ +/* { dg-final { scan-assembler "vpdpwusd\t" } } */ +/* { dg-final { scan-assembler "vpdpwuud\t" } } */ + +int __attribute__((noinline, noclone, optimize("tree-vectorize"))) +usdot_prod_hi (unsigned short * restrict a, short * restrict b, + int c, int n) +{ + int i; + for (i = 0; i < n; i++) + { + c += ((int) a[i] * (int) b[i]); + } + return c; +} + +int __attribute__((noinline, noclone, optimize("tree-vectorize"))) +udot_prod_hi (unsigned short * restrict a, unsigned short *restrict b, + int c, int n) +{ + int i; + for (i = 0; i < n; i++) + { + c += ((int) a[i] * (int) b[i]); + } + return c; +} diff --git a/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-2.c b/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-2.c new file mode 100644 index 000000000000..90dc0eade7ef --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vnniint16-auto-vectorize-2.c @@ -0,0 +1,76 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -mavxvnniint16" } */ +/* { dg-require-effective-target avxvnniint16 } */ + +#define AVXVNNIINT16 +#ifndef CHECK +#define CHECK "avx-check.h" +#endif + +#ifndef TEST +#define TEST avx_test +#endif + +#include CHECK +#include "vnniint16-auto-vectorize-1.c" + +#define N 256 + +short a_i16[N]; +unsigned short b_u16[N], c_u16[N], d_u16[N]; +int i16_exp, i16_ref; + +int __attribute__((noinline, noclone, optimize("no-tree-vectorize"))) +udot_prod_hi_scalar (unsigned short * restrict a, unsigned short * restrict b, + int c, int n) +{ + int i; + for (i = 0; i < n; i++) + { + c += ((int) a[i] * (int) b[i]); + } + return c; +} + +int __attribute__((noinline, noclone, optimize("no-tree-vectorize"))) +usdot_prod_hi_scalar (unsigned short * restrict a, short *restrict b, + int c, int n) +{ + int i; + for (i = 0; i < n; i++) + { + c += ((int) a[i] * (int) b[i]); + } + return c; +} + +void init () +{ + int i; + + i16_exp = i16_ref = 65535; + + for (i = 0; i < N; i++) + { + a_i16[i] = -i + 2; + b_u16[i] = i * 2; + c_u16[i] = i * 3; + d_u16[i] = i * 4; + } +} + +void +TEST (void) +{ + init (); + i16_exp = usdot_prod_hi (a_i16, b_u16, i16_exp, N); + i16_ref = usdot_prod_hi_scalar (a_i16, b_u16, i16_ref, N); + if (i16_exp != i16_ref) + abort (); + + init (); + i16_exp = udot_prod_hi (c_u16, d_u16, i16_exp, N); + i16_ref = udot_prod_hi_scalar (c_u16, d_u16, i16_ref, N); + if (i16_exp != i16_ref) + abort (); +}