]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
x86: Emit cvtne2ps2bf16 for odd increasing perm in __builtin_shufflevector
authorLevy Hsu <admin@levyhsu.com>
Thu, 13 Jun 2024 05:50:04 +0000 (15:20 +0930)
committerLevy Hsu <admin@levyhsu.com>
Mon, 17 Jun 2024 06:57:17 +0000 (06:57 +0000)
This patch updates the GCC x86 backend to efficiently handle
odd, incrementally increasing permutations of BF16 vectors
using the cvtne2ps2bf16 instruction.
It modifies ix86_vectorize_vec_perm_const to support these operations
and adds a specific predicate to ensure proper sequence handling.

gcc/ChangeLog:

* config/i386/i386-expand.cc
(ix86_vectorize_vec_perm_const): Convert BF to HI using subreg.
* config/i386/predicates.md
(vcvtne2ps2bf_parallel): New define_insn_and_split.
* config/i386/sse.md
(vpermt2_sepcial_bf16_shuffle_<mode>): New predicates matches odd increasing perm.

gcc/testsuite/ChangeLog:

* gcc.target/i386/vpermt2-special-bf16-shufflue.c: New test.

gcc/config/i386/i386-expand.cc
gcc/config/i386/predicates.md
gcc/config/i386/sse.md
gcc/testsuite/gcc.target/i386/vpermt2-special-bf16-shufflue.c [new file with mode: 0755]

index a4379b863170e4a8beeef5bad2f07190da7dd4fa..7c6a82ee6a2b8eb878e9d60fb44ab9ed22fc0d5b 100644 (file)
@@ -23657,8 +23657,8 @@ ix86_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
   if (GET_MODE_SIZE (vmode) == 64 && !TARGET_EVEX512)
     return false;
 
-  /* For HF mode vector, convert it to HI using subreg.  */
-  if (GET_MODE_INNER (vmode) == HFmode)
+  /* For HF and BF mode vector, convert it to HI using subreg.  */
+  if (GET_MODE_INNER (vmode) == HFmode || GET_MODE_INNER (vmode) == BFmode)
     {
       machine_mode orig_mode = vmode;
       vmode = mode_for_vector (HImode,
index 7afe3100cb7ec688b5f35ba2e43623c62711bda1..1676c50de711ce986398472e042268126235afc1 100644 (file)
 
   return true;
 })
+
+;; Check that each element is odd and incrementally increasing from 1
+(define_predicate "vcvtne2ps2bf_parallel"
+  (and (match_code "const_vector")
+       (match_code "const_int" "a"))
+{
+  for (int i = 0; i < XVECLEN (op, 0); ++i)
+    if (INTVAL (XVECEXP (op, 0, i)) != (2 * i + 1))
+      return false;
+  return true;
+})
index 680a46a0b08a541003466954b275625ae7fb1695..5ddd1c0a778c6487ca01021b5372251dffd7aadc 100644 (file)
   "TARGET_AVXVNNIINT16"
   "vpdp<vpdpwprodtype>\t{%3, %2, %0|%0, %2, %3}"
    [(set_attr "prefix" "vex")])
+
+(define_mode_attr hi_cvt_bf
+  [(V8HI "v8bf") (V16HI "v16bf") (V32HI "v32bf")])
+
+(define_mode_attr HI_CVT_BF
+  [(V8HI "V8BF") (V16HI "V16BF") (V32HI "V32BF")])
+
+(define_insn_and_split "vpermt2_sepcial_bf16_shuffle_<mode>"
+  [(set (match_operand:VI2_AVX512F 0 "register_operand")
+       (unspec:VI2_AVX512F
+         [(match_operand:VI2_AVX512F 1 "vcvtne2ps2bf_parallel")
+          (match_operand:VI2_AVX512F 2 "register_operand")
+          (match_operand:VI2_AVX512F 3 "nonimmediate_operand")]
+          UNSPEC_VPERMT2))]
+  "TARGET_AVX512VL && TARGET_AVX512BF16 && ix86_pre_reload_split ()"
+  "#"
+  "&& 1"
+  [(const_int 0)]
+{
+  rtx op0 = gen_reg_rtx (<HI_CVT_BF>mode);
+  operands[2] = lowpart_subreg (<ssePSmode>mode,
+                               force_reg (<MODE>mode, operands[2]),
+                               <MODE>mode);
+  operands[3] = lowpart_subreg (<ssePSmode>mode,
+                               force_reg (<MODE>mode, operands[3]),
+                               <MODE>mode);
+
+  emit_insn (gen_avx512f_cvtne2ps2bf16_<hi_cvt_bf>(op0,
+                                                  operands[3],
+                                                  operands[2]));
+  emit_move_insn (operands[0], lowpart_subreg (<MODE>mode, op0,
+                                              <HI_CVT_BF>mode));
+  DONE;
+}
+[(set_attr "mode" "<sseinsnmode>")])
diff --git a/gcc/testsuite/gcc.target/i386/vpermt2-special-bf16-shufflue.c b/gcc/testsuite/gcc.target/i386/vpermt2-special-bf16-shufflue.c
new file mode 100755 (executable)
index 0000000..5c65f2a
--- /dev/null
@@ -0,0 +1,27 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512bf16 -mavx512vl" } */
+/* { dg-final { scan-assembler-not "vpermi2b" } } */
+/* { dg-final { scan-assembler-times "vcvtne2ps2bf16" 3 } } */
+
+typedef __bf16 v8bf __attribute__((vector_size(16)));
+typedef __bf16 v16bf __attribute__((vector_size(32)));
+typedef __bf16 v32bf __attribute__((vector_size(64)));
+
+v8bf foo0(v8bf a, v8bf b)
+{
+  return __builtin_shufflevector(a, b, 1, 3, 5, 7, 9, 11, 13, 15);
+}
+
+v16bf foo1(v16bf a, v16bf b)
+{
+  return __builtin_shufflevector(a, b, 1, 3, 5, 7, 9, 11, 13, 15,
+                                 17, 19, 21, 23, 25, 27, 29, 31);
+}
+
+v32bf foo2(v32bf a, v32bf b)
+{
+  return __builtin_shufflevector(a, b, 1, 3, 5, 7, 9, 11, 13, 15, 
+                                 17, 19, 21, 23, 25, 27, 29, 31, 
+                                 33, 35, 37, 39, 41, 43, 45, 47, 
+                                 49, 51, 53, 55, 57, 59, 61, 63);
+}