https://gcc.gnu.org/g:4cb25d3a3d609a2a291716bf99503a64789bde86
commit r17-6580-g4cb25d3a3d609a2a291716bf99503a64789bde86 Author: Sun, Qin <[email protected]> Date: Fri Nov 20 16:42:17 2026 +0800 i386: Add sdot_prod expanders for BF16 and FP16 Support the sdot_prod optab for BF16 and FP16 inputs with a float accumulator through the existing AVX512BF16 vdpbf16ps and AVX10.2 vdpphps insns, so the vectorizer can use them for floating point dot products. vdpbf16ps treats denormal inputs as zero and flushes denormal results, so its expander requires flag_unsafe_math_optimizations. gcc/ChangeLog: * config/i386/sse.md (ssePSmode, ssePSmodelower): Add BF modes. (sdot_prod<ssePSmodelower><mode>): New expanders. gcc/testsuite/ChangeLog: * gcc.target/i386/avx10_2-fp16-dotprod-1.c: New test. * gcc.target/i386/avx10_2-fp16-dotprod-2.c: Ditto. * gcc.target/i386/avx10_2-fp16-dotprod-3.c: Ditto. * gcc.target/i386/avx10_2-fp16-dotprod-4.c: Ditto. * gcc.target/i386/avx512bf16-dotprod-1.c: Ditto. * gcc.target/i386/avx512bf16-dotprod-2.c: Ditto. * gcc.target/i386/avx512bf16-dotprod-3.c: Ditto. * gcc.target/i386/avx512bf16-dotprod-4.c: Ditto. * gcc.target/i386/avx512bf16-dotprod-5.c: Ditto. Diff: --- gcc/config/i386/sse.md | 32 +++++++++++++++- .../gcc.target/i386/avx10_2-fp16-dotprod-1.c | 19 ++++++++++ .../gcc.target/i386/avx10_2-fp16-dotprod-2.c | 19 ++++++++++ .../gcc.target/i386/avx10_2-fp16-dotprod-3.c | 16 ++++++++ .../gcc.target/i386/avx10_2-fp16-dotprod-4.c | 43 ++++++++++++++++++++++ .../gcc.target/i386/avx512bf16-dotprod-1.c | 17 +++++++++ .../gcc.target/i386/avx512bf16-dotprod-2.c | 17 +++++++++ .../gcc.target/i386/avx512bf16-dotprod-3.c | 16 ++++++++ .../gcc.target/i386/avx512bf16-dotprod-4.c | 43 ++++++++++++++++++++++ .../gcc.target/i386/avx512bf16-dotprod-5.c | 18 +++++++++ 10 files changed, 238 insertions(+), 2 deletions(-) diff --git a/gcc/config/i386/sse.md b/gcc/config/i386/sse.md index 853ecddb3593..f70fa99b96d5 100644 --- a/gcc/config/i386/sse.md +++ b/gcc/config/i386/sse.md @@ -1242,7 +1242,8 @@ (V4TI "V16SF") (V2TI "V8SF") (V1TI "V4SF") (V8SF "V8SF") (V4SF "V4SF") (V4DF "V8SF") (V2DF "V4SF") - (V32HF "V16SF") (V16HF "V8SF") (V8HF "V4SF")]) + (V32HF "V16SF") (V16HF "V8SF") (V8HF "V4SF") + (V32BF "V16SF") (V16BF "V8SF") (V8BF "V4SF")]) (define_mode_attr ssePSmodelower [(V16SI "v16sf") (V8DF "v16sf") @@ -1254,7 +1255,8 @@ (V4TI "v16sf") (V2TI "v8sf") (V1TI "v4sf") (V8SF "v8sf") (V4SF "v4sf") (V4DF "v8sf") (V2DF "v4sf") - (V32HF "v16sf") (V16HF "v8sf") (V8HF "v4sf")]) + (V32HF "v16sf") (V16HF "v8sf") (V8HF "v4sf") + (V32BF "v16sf") (V16BF "v8sf") (V8BF "v4sf")]) (define_mode_attr ssePSmode2 [(V8DI "V8SF") (V4DI "V4SF")]) @@ -32680,6 +32682,18 @@ gen = gen_insn_fix<fixunssuffix>_trunc<ssehalfvecmodelower><vunpckfixt_model>2; "TARGET_AVX512BF16" "vdpbf16ps\t{%3, %2, %0%{%4%}|%0%{%4%}, %2, %3}") +(define_expand "sdot_prod<ssePSmodelower><mode>" + [(match_operand:<ssePSmode> 0 "register_operand") + (match_operand:VF_AVX512BF16VL 1 "register_operand") + (match_operand:VF_AVX512BF16VL 2 "register_operand") + (match_operand:<ssePSmode> 3 "register_operand")] + "TARGET_AVX512BF16 && flag_unsafe_math_optimizations && !flag_rounding_math" +{ + emit_insn (gen_avx512f_dpbf16ps_<ssePSmodelower> (operands[0], operands[3], + operands[1], operands[2])); + DONE; +}) + ;; KEYLOCKER (define_insn "loadiwkey" [(unspec_volatile:V2DI [(match_operand:V2DI 0 "register_operand" "x") @@ -32982,6 +32996,20 @@ gen = gen_insn_fix<fixunssuffix>_trunc<ssehalfvecmodelower><vunpckfixt_model>2; DONE; }) +(define_expand "sdot_prod<ssePSmodelower><mode>" + [(match_operand:<ssePSmode> 0 "register_operand") + (match_operand:VHF_AVX512VL 1 "register_operand") + (match_operand:VHF_AVX512VL 2 "register_operand") + (match_operand:<ssePSmode> 3 "register_operand")] + "TARGET_AVX10_2 && !flag_rounding_math" +{ + operands[1] = lowpart_subreg (<ssePSmode>mode, operands[1], <MODE>mode); + operands[2] = lowpart_subreg (<ssePSmode>mode, operands[2], <MODE>mode); + emit_insn (gen_vdpphps_<ssePSmodelower> (operands[0], operands[3], + operands[1], operands[2])); + DONE; +}) + (define_expand "udot_prod<ssedvecmodelower><mode>" [(match_operand:<ssedvecmode> 0 "register_operand") (match_operand:VI1_AVX512 1 "register_operand") diff --git a/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-1.c b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-1.c new file mode 100644 index 000000000000..7b0ca7dd21b3 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx10.2 -mprefer-vector-width=512 -fdump-tree-vect-details" } */ + +/* An fp16 dot product accumulated in float is a widening dot product; + with AVX10.2 the vectorizer should use vdpphps (sdot_prod optab). The + vectorized epilogue uses the next narrower form. */ + +float +dot_fp16 (const _Float16 *restrict a, const _Float16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-tree-dump "dot_prod pattern recognized" "vect" } } */ +/* { dg-final { scan-assembler {\tvdpphps\t[^\n]*%zmm[0-9]+, %zmm[0-9]+\n} } } */ +/* { dg-final { scan-assembler {\tvdpphps\t[^\n]*%ymm[0-9]+, %ymm[0-9]+\n} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-2.c b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-2.c new file mode 100644 index 000000000000..d162329d3753 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-2.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx10.2 -mprefer-vector-width=256 -fdump-tree-vect-details" } */ + +/* An fp16 dot product accumulated in float is a widening dot product; + with AVX10.2 the vectorizer should use vdpphps (sdot_prod optab). The + vectorized epilogue uses the next narrower form. */ + +float +dot_fp16 (const _Float16 *restrict a, const _Float16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-tree-dump "dot_prod pattern recognized" "vect" } } */ +/* { dg-final { scan-assembler {\tvdpphps\t[^\n]*%ymm[0-9]+, %ymm[0-9]+\n} } } */ +/* { dg-final { scan-assembler {\tvdpphps\t[^\n]*%xmm[0-9]+, %xmm[0-9]+\n} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-3.c b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-3.c new file mode 100644 index 000000000000..e3646374bd9d --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-3.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx512fp16 -mavx512vl -mno-avx10.2" } */ + +/* The pattern is recognized, but without AVX10.2 there is no optab, so + no vdpphps may be emitted. */ + +float +dot_fp16 (const _Float16 *restrict a, const _Float16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-assembler-not {\tvdpphps\t} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-4.c b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-4.c new file mode 100644 index 000000000000..3047f6ba1e1e --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx10_2-fp16-dotprod-4.c @@ -0,0 +1,43 @@ +/* { dg-do run } */ +/* { dg-options "-O3 -ffast-math -mavx10.2 -save-temps" } */ +/* { dg-require-effective-target avx10_2 } */ + +/* Check the value computed by the vectorized fp16 dot product. The inputs + are small integers scaled by a power of two, so every product and every + partial sum is exact in float and the result does not depend on the + summation order. */ + +__attribute__ ((noipa)) float +dot_fp16 (const _Float16 *restrict a, const _Float16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +int +main (void) +{ + if (!__builtin_cpu_supports ("avx10.2")) + return 0; + + _Float16 a[257], b[257]; + for (int i = 0; i < 257; ++i) + { + a[i] = (i % 31 - 15) * 0.0625f; + b[i] = (i % 13 - 6) * 0.0625f; + } + + float expected = 0.0f; + for (int n = 0; n <= 257; ++n) + { + if (dot_fp16 (a, b, n) != expected) + __builtin_abort (); + if (n < 257) + expected += (float) a[n] * (float) b[n]; + } + return 0; +} + +/* { dg-final { scan-assembler {\tvdpphps\t} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-1.c b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-1.c new file mode 100644 index 000000000000..5af4a55647c8 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-1.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx512bf16 -mavx512vl -mprefer-vector-width=512 -fdump-tree-vect-details" } */ + +/* A bf16 dot product accumulated in float is a widening dot product; + with AVX512BF16 the vectorizer should use vdpbf16ps (sdot_prod optab). */ + +float +dot_bf16 (const __bf16 *restrict a, const __bf16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-tree-dump "dot_prod pattern recognized" "vect" } } */ +/* { dg-final { scan-assembler {\tvdpbf16ps\t[^\n]*%zmm[0-9]+, %zmm[0-9]+\n} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-2.c b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-2.c new file mode 100644 index 000000000000..fdb535060132 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-2.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx512bf16 -mavx512vl -mprefer-vector-width=256 -fdump-tree-vect-details" } */ + +/* A bf16 dot product accumulated in float is a widening dot product; + with AVX512BF16 the vectorizer should use vdpbf16ps (sdot_prod optab). */ + +float +dot_bf16 (const __bf16 *restrict a, const __bf16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-tree-dump "dot_prod pattern recognized" "vect" } } */ +/* { dg-final { scan-assembler {\tvdpbf16ps\t[^\n]*%ymm[0-9]+, %ymm[0-9]+\n} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-3.c b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-3.c new file mode 100644 index 000000000000..f97a3c773c21 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-3.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -ffast-math -mavx512vl -mno-avx512bf16" } */ + +/* The pattern is recognized, but without AVX512BF16 there is no optab, so + no vdpbf16ps may be emitted. */ + +float +dot_bf16 (const __bf16 *restrict a, const __bf16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-assembler-not {\tvdpbf16ps\t} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-4.c b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-4.c new file mode 100644 index 000000000000..3cc67291a5df --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-4.c @@ -0,0 +1,43 @@ +/* { dg-do run } */ +/* { dg-options "-O3 -ffast-math -mavx512bf16 -mavx512vl -save-temps" } */ + +/* Check the value computed by the vectorized bf16 dot product. The inputs + are small integers scaled by a power of two, so every product and every + partial sum is exact in float and the result does not depend on the + summation order. */ + +__attribute__ ((noipa)) float +dot_bf16 (const __bf16 *restrict a, const __bf16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +int +main (void) +{ + if (!__builtin_cpu_supports ("avx512bf16") + || !__builtin_cpu_supports ("avx512vl")) + return 0; + + __bf16 a[257], b[257]; + for (int i = 0; i < 257; ++i) + { + a[i] = (i % 31 - 15) * 0.0625f; + b[i] = (i % 13 - 6) * 0.0625f; + } + + float expected = 0.0f; + for (int n = 0; n <= 257; ++n) + { + if (dot_bf16 (a, b, n) != expected) + __builtin_abort (); + if (n < 257) + expected += (float) a[n] * (float) b[n]; + } + return 0; +} + +/* { dg-final { scan-assembler {\tvdpbf16ps\t} } } */ diff --git a/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-5.c b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-5.c new file mode 100644 index 000000000000..83013d851f79 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512bf16-dotprod-5.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fassociative-math -fno-signed-zeros -fno-trapping-math -mavx512bf16 -mavx512vl" } */ + +/* vdpbf16ps ignores MXCSR: denormal inputs and results are flushed to + zero regardless of the DAZ/FTZ bits. Reassociation alone does not + permit that, so without -funsafe-math-optimizations the dot product + must not be expanded to vdpbf16ps. */ + +float +dot_bf16 (const __bf16 *restrict a, const __bf16 *restrict b, int n) +{ + float sum = 0.0f; + for (int i = 0; i < n; ++i) + sum += (float) a[i] * (float) b[i]; + return sum; +} + +/* { dg-final { scan-assembler-not {\tvdpbf16ps\t} } } */
