[v1,2/2] LoongArch: Optimize vector constant extract-{even/odd} permutation.

Message ID 20231128073900.2452086-1-liwei@loongson.cn
State Committed
Commit 6b226c2611f0ec01f2cc1319bc41af37518f2dfd
Headers
Series [v1,1/2] LoongArch: Accelerate optimization of scalar signed/unsigned popcount. |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Testing passed

Commit Message

Li Wei Nov. 28, 2023, 7:39 a.m. UTC
  For vector constant extract-{even/odd} permutation replace the default
[x]vshuf instruction combination with [x]vilv{l/h} instruction, which
can reduce instructions and improves performance.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_is_odd_extraction):
	  Supplementary function prototype.
	(loongarch_is_even_extraction): Adjust.
	(loongarch_try_expand_lsx_vshuf_const): Adjust.
	(loongarch_is_extraction_permutation): Adjust.
	(loongarch_expand_vec_perm_const_2): Adjust.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/lasx-extract-even_odd-opt.c: New test.
---
 gcc/config/loongarch/loongarch.cc             | 33 +++++++++++-
 .../loongarch/lasx-extract-even_odd-opt.c     | 54 +++++++++++++++++++
 2 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/lasx-extract-even_odd-opt.c
  

Comments

Xi Ruoyao Nov. 29, 2023, 9:44 a.m. UTC | #1
On Tue, 2023-11-28 at 15:39 +0800, Li Wei wrote:
> For vector constant extract-{even/odd} permutation replace the default
> [x]vshuf instruction combination with [x]vilv{l/h} instruction, which
> can reduce instructions and improves performance.
> 
> gcc/ChangeLog:
> 
> 	* config/loongarch/loongarch.cc
> (loongarch_is_odd_extraction):
> 	  Supplementary function prototype.
        ^^
These two white spaces should be removed.  And I'd suggest "New forward
declaration".

Otherwise LGTM.

> 	(loongarch_is_even_extraction): Adjust.
> 	(loongarch_try_expand_lsx_vshuf_const): Adjust.
> 	(loongarch_is_extraction_permutation): Adjust.
> 	(loongarch_expand_vec_perm_const_2): Adjust.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/loongarch/lasx-extract-even_odd-opt.c: New test.
  
Lulu Cheng Dec. 2, 2023, 8:52 a.m. UTC | #2
在 2023/11/29 下午5:44, Xi Ruoyao 写道:
> On Tue, 2023-11-28 at 15:39 +0800, Li Wei wrote:
>> For vector constant extract-{even/odd} permutation replace the default
>> [x]vshuf instruction combination with [x]vilv{l/h} instruction, which
>> can reduce instructions and improves performance.
>>
>> gcc/ChangeLog:
>>
>> 	* config/loongarch/loongarch.cc
>> (loongarch_is_odd_extraction):
>> 	  Supplementary function prototype.
>          ^^
> These two white spaces should be removed.  And I'd suggest "New forward
> declaration".
>
> Otherwise LGTM.

pushed to r14-6073.  At the same time, the indentation problem has been 
modified.

Thanks.

>
>> 	(loongarch_is_even_extraction): Adjust.
>> 	(loongarch_try_expand_lsx_vshuf_const): Adjust.
>> 	(loongarch_is_extraction_permutation): Adjust.
>> 	(loongarch_expand_vec_perm_const_2): Adjust.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* gcc.target/loongarch/lasx-extract-even_odd-opt.c: New test.
  

Patch

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index d3896d72bc2..f89c346815d 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -8672,6 +8672,12 @@  loongarch_expand_vec_perm (rtx target, rtx op0, rtx op1, rtx sel)
     }
 }
 
+static bool
+loongarch_is_odd_extraction (struct expand_vec_perm_d *);
+
+static bool
+loongarch_is_even_extraction (struct expand_vec_perm_d *);
+
 static bool
 loongarch_try_expand_lsx_vshuf_const (struct expand_vec_perm_d *d)
 {
@@ -8694,6 +8700,24 @@  loongarch_try_expand_lsx_vshuf_const (struct expand_vec_perm_d *d)
       if (d->testing_p)
 	return true;
 
+      /* If match extract-even and extract-odd permutations pattern, use
+       * vselect much better than vshuf.  */
+      if (loongarch_is_odd_extraction (d)
+	  || loongarch_is_even_extraction (d))
+	{
+	  if (loongarch_expand_vselect_vconcat (d->target, d->op0, d->op1,
+						d->perm, d->nelt))
+	    return true;
+
+	  unsigned char perm2[MAX_VECT_LEN];
+	  for (i = 0; i < d->nelt; ++i)
+	    perm2[i] = (d->perm[i] + d->nelt) & (2 * d->nelt - 1);
+
+	  if (loongarch_expand_vselect_vconcat (d->target, d->op1, d->op0,
+						perm2, d->nelt))
+	    return true;
+	}
+
       for (i = 0; i < d->nelt; i += 1)
 	{
 	  rperm[i] = GEN_INT (d->perm[i]);
@@ -8878,7 +8902,7 @@  loongarch_is_even_extraction (struct expand_vec_perm_d *d)
 	  result = false;
 	  break;
 	}
-      buf += 1;
+      buf += 2;
     }
 
   return result;
@@ -8900,7 +8924,7 @@  loongarch_is_extraction_permutation (struct expand_vec_perm_d *d)
 	  result = false;
 	  break;
 	}
-      buf += 2;
+      buf += 1;
     }
 
   return result;
@@ -9377,6 +9401,11 @@  loongarch_expand_vec_perm_const_2 (struct expand_vec_perm_d *d)
 	 Selector after: { 1, 3, 1, 3 }.
 	 Even extraction selector sample: E_V4DImode, { 0, 2, 4, 6 }
 	 Selector after: { 0, 2, 0, 2 }.  */
+
+      /* Better implement of extract-even and extract-odd permutations.  */
+      if (loongarch_expand_vec_perm_even_odd (d))
+	return true;
+
       for (i = 0; i < d->nelt / 2; i += 1)
 	{
 	  idx = d->perm[i];
diff --git a/gcc/testsuite/gcc.target/loongarch/lasx-extract-even_odd-opt.c b/gcc/testsuite/gcc.target/loongarch/lasx-extract-even_odd-opt.c
new file mode 100644
index 00000000000..515f0c8621a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/lasx-extract-even_odd-opt.c
@@ -0,0 +1,54 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O3 -mlasx" } */
+/* { dg-final { scan-assembler "xvilvl.d" } } */
+/* { dg-final { scan-assembler "xvilvh.d" } } */
+
+#define CMUL(a, b, c)                                                         \
+  {                                                                           \
+    (c).ai = (a).ai * (b).ai - (a).bi * (b).bi;                               \
+    (c).bi = (a).ai * (b).bi + (a).bi * (b).ai;                               \
+    (c).ci = (a).ci * (b).ci - (a).di * (b).di;                               \
+    (c).di = (a).ci * (b).di + (a).di * (b).ci;                               \
+  }
+#define CSUM(a, b)                                                            \
+  {                                                                           \
+    (a).ai += (b).ai;                                                         \
+    (a).bi += (b).bi;                                                         \
+    (a).ci += (b).ci;                                                         \
+    (a).di += (b).di;                                                         \
+  }
+
+typedef struct
+{
+  double ai;
+  double bi;
+  double ci;
+  double di;
+} complex;
+
+typedef struct
+{
+  complex e[6][6];
+} matrix;
+
+typedef struct
+{
+  complex c[6];
+} vector;
+
+void
+mult_adj_mat_vec (matrix *a, vector *b, vector *c)
+{
+  register int i, j;
+  register complex x, y;
+  for (i = 0; i < 6; i++)
+    {
+      x.ai = x.bi = x.ci = x.di = 0.0;
+      for (j = 0; j < 6; j++)
+        {
+          CMUL (a->e[j][i], b->c[j], y);
+          CSUM (x, y);
+        }
+      c->c[i] = x;
+    }
+}