https://gcc.gnu.org/g:07b484c2683b2e95c38fc28aabe239d5601d462c

commit r17-5798-g07b484c2683b2e95c38fc28aabe239d5601d462c
Author: Hu, Lin1 <[email protected]>
Date:   Wed Oct 28 09:56:52 2026 +0800

    i386: Don't emit AVX512VL/AVX512DQ vextract forms without those ISAs
    
    *vec_extract<mode><ssescalarmodelower>_valign requires only AVX512F,
    but can select forms that require AVX512VL or AVX512DQ.  Use the 512-bit
    source form without AVX512VL and the 32x4 forms without AVX512DQ.  An
    unmasked extract returns the same 128-bit lane.
    
    gcc/ChangeLog:
    
            * config/i386/sse.md 
(*vec_extract<mode><ssescalarmodelower>_valign):
            Use the 256-bit source form only with TARGET_AVX512VL and the 64x2
            forms only with TARGET_AVX512DQ.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/avx512f-vec-extract-novl-1.c: New test.
            * gcc.target/i386/avx512f-vec-extract-novl-2.c: Ditto.

Diff:
---
 gcc/config/i386/sse.md                                  | 14 ++++++++++----
 .../gcc.target/i386/avx512f-vec-extract-novl-1.c        | 17 +++++++++++++++++
 .../gcc.target/i386/avx512f-vec-extract-novl-2.c        | 16 ++++++++++++++++
 3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/gcc/config/i386/sse.md b/gcc/config/i386/sse.md
index 4e6761a71f62..6f0b2c5f7160 100644
--- a/gcc/config/i386/sse.md
+++ b/gcc/config/i386/sse.md
@@ -15117,10 +15117,16 @@
   if (byte_offset % 16 == 0)
     {
       operands[2] = GEN_INT (byte_offset / 16);
-      if (byte_offset / 16 == 1)
-       return "vextract<shuffletype><extract_suf>\t{%2, %t1, %x0|%x0, %t1, 
%2}";
-      else
-       return "vextract<shuffletype><extract_suf>\t{%2, %1, %x0|%x0, %1, %2}";
+      /* The 256-bit source form needs AVX512VL and the 64x2 forms need
+        AVX512DQ.  An unmasked 32x4 extract gives the same result.  */
+      bool ymm_p = (byte_offset / 16 == 1) && TARGET_AVX512VL;
+      if (GET_MODE_UNIT_SIZE (<MODE>mode) == 8 && !TARGET_AVX512DQ)
+       return (ymm_p
+               ? "vextract<shuffletype>32x4\t{%2, %t1, %x0|%x0, %t1, %2}"
+               : "vextract<shuffletype>32x4\t{%2, %1, %x0|%x0, %1, %2}");
+      return (ymm_p
+             ? "vextract<shuffletype><extract_suf>\t{%2, %t1, %x0|%x0, %t1, 
%2}"
+             : "vextract<shuffletype><extract_suf>\t{%2, %1, %x0|%x0, %1, 
%2}");
     }
   else
     return "valign<ternlogsuffix>\t{%2, %1, %1, %<xtg_mode>0|%<xtg_mode>0, %1, 
%1, %2}";
diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-1.c 
b/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-1.c
new file mode 100644
index 000000000000..1fd3d1022099
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-1.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512f -mno-avx512vl -mno-avx512dq" } */
+
+typedef int v16si __attribute__ ((vector_size (64)));
+typedef float v16sf __attribute__ ((vector_size (64)));
+typedef long long v8di __attribute__ ((vector_size (64)));
+typedef double v8df __attribute__ ((vector_size (64)));
+
+int f1 (v16si v) { return v[4]; }
+float f2 (v16sf v) { return v[4]; }
+long long f3 (v8di v) { return v[2]; }
+double f4 (v8df v) { return v[2]; }
+
+/* EVEX vextract needs AVX512VL for YMM sources and AVX512DQ for 64x2.  */
+/* { dg-final { scan-assembler-not "vextract\[^\n\]*%ymm" } } */
+/* { dg-final { scan-assembler-not "vextract\[if\]64x2" } } */
+/* { dg-final { scan-assembler-times "vextract\[if\]32x4\[\t \]+\\\$1, %zmm" 4 
} } */
diff --git a/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-2.c 
b/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-2.c
new file mode 100644
index 000000000000..2573830bdcba
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/avx512f-vec-extract-novl-2.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512f -mavx512dq -mno-avx512vl" } */
+
+typedef int v16si __attribute__ ((vector_size (64)));
+typedef float v16sf __attribute__ ((vector_size (64)));
+typedef long long v8di __attribute__ ((vector_size (64)));
+typedef double v8df __attribute__ ((vector_size (64)));
+
+int f1 (v16si v) { return v[4]; }
+float f2 (v16sf v) { return v[4]; }
+long long f3 (v8di v) { return v[2]; }
+double f4 (v8df v) { return v[2]; }
+
+/* { dg-final { scan-assembler-not "vextract\[^\n\]*%ymm" } } */
+/* { dg-final { scan-assembler-times "vextract\[if\]32x4\[\t \]+\\\$1, %zmm" 2 
} } */
+/* { dg-final { scan-assembler-times "vextract\[if\]64x2\[\t \]+\\\$1, %zmm" 2 
} } */

Reply via email to