vect: Verify that GET_MODE_NUNITS is power-of-2

Message ID 20230310201620.2097011-1-collison@rivosinc.com
State New
Headers
Series vect: Verify that GET_MODE_NUNITS is power-of-2 |

Commit Message

Michael Collison March 10, 2023, 8:16 p.m. UTC
  While working on autovectorizing for the RISCV port I encountered an issue
where can_duplicate_and_interleave_p assumes that GET_MODE_NUNITS is a
power of two. The RISC-V target has vector modes (e.g. VNx1DImode) that
are not a power of two.

Tested on RISCV and x86_64-linux-gnu. Okay?

2023-03-09  Michael Collison  <collison@rivosinc.com>

	* poly-int.h (exact_div_p): New function to
	verify that argument is a power of 2 poly_int.
	* tree-vect-slp.cc (can_duplicate_and_interleave_p):
	Check that GET_MODE_NUNITS is a power of 2.
---
 gcc/poly-int.h       | 17 +++++++++++++++++
 gcc/tree-vect-slp.cc |  3 ++-
 2 files changed, 19 insertions(+), 1 deletion(-)
  

Comments

Richard Biener March 13, 2023, 9:08 a.m. UTC | #1
On Fri, Mar 10, 2023 at 9:16 PM Michael Collison <collison@rivosinc.com> wrote:
>
> While working on autovectorizing for the RISCV port I encountered an issue
> where can_duplicate_and_interleave_p assumes that GET_MODE_NUNITS is a
> power of two. The RISC-V target has vector modes (e.g. VNx1DImode) that
> are not a power of two.

We do not support vector types that do not have a power-of-two
element count, see TYPE_VECTOR_SUBPARTS.

Also your test below verifies that nunits is divisible by two, not that it
is power-of-two?  So maybe what you want to know is whether
known_gt (nunits, 1)?

> Tested on RISCV and x86_64-linux-gnu. Okay?
>
> 2023-03-09  Michael Collison  <collison@rivosinc.com>
>
>         * poly-int.h (exact_div_p): New function to
>         verify that argument is a power of 2 poly_int.
>         * tree-vect-slp.cc (can_duplicate_and_interleave_p):
>         Check that GET_MODE_NUNITS is a power of 2.
> ---
>  gcc/poly-int.h       | 17 +++++++++++++++++
>  gcc/tree-vect-slp.cc |  3 ++-
>  2 files changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/poly-int.h b/gcc/poly-int.h
> index 12571455081..d09632f341f 100644
> --- a/gcc/poly-int.h
> +++ b/gcc/poly-int.h
> @@ -2219,6 +2219,23 @@ multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
>    return constant_multiple_p (a, b, multiple);
>  }
>
> +/* Return true, if A is known to be a multiple of B.  */
> +
> +template<unsigned int N, typename Ca, typename Cb>
> +inline bool
> +exact_div_p (const poly_int_pod<N, Ca> &a, Cb b)
> +{
> +  typedef POLY_CONST_COEFF (Ca, Cb) C;
> +  poly_int<N, C> r;
> +  for (unsigned int i = 0; i < N; i++)
> +    {
> +      if ((a.coeffs[i] % b) != 0)
> +       return false;
> +
> +    }
> +  return true;
> +}
> +
>  /* Return A / B, given that A is known to be a multiple of B.  */
>
>  template<unsigned int N, typename Ca, typename Cb>
> diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
> index 9a4e000925e..6be2036a13a 100644
> --- a/gcc/tree-vect-slp.cc
> +++ b/gcc/tree-vect-slp.cc
> @@ -426,7 +426,8 @@ can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
>           if (vector_type
>               && VECTOR_MODE_P (TYPE_MODE (vector_type))
>               && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
> -                          GET_MODE_SIZE (base_vector_mode)))
> +                          GET_MODE_SIZE (base_vector_mode))
> +             && exact_div_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)), 2))
>             {
>               /* Try fusing consecutive sequences of COUNT / NVECTORS elements
>                  together into elements of type INT_TYPE and using the result
> --
> 2.34.1
>
  

Patch

diff --git a/gcc/poly-int.h b/gcc/poly-int.h
index 12571455081..d09632f341f 100644
--- a/gcc/poly-int.h
+++ b/gcc/poly-int.h
@@ -2219,6 +2219,23 @@  multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
   return constant_multiple_p (a, b, multiple);
 }
 
+/* Return true, if A is known to be a multiple of B.  */
+
+template<unsigned int N, typename Ca, typename Cb>
+inline bool
+exact_div_p (const poly_int_pod<N, Ca> &a, Cb b)
+{
+  typedef POLY_CONST_COEFF (Ca, Cb) C;
+  poly_int<N, C> r;
+  for (unsigned int i = 0; i < N; i++)
+    {
+      if ((a.coeffs[i] % b) != 0)
+	return false;
+
+    }
+  return true;
+}
+
 /* Return A / B, given that A is known to be a multiple of B.  */
 
 template<unsigned int N, typename Ca, typename Cb>
diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index 9a4e000925e..6be2036a13a 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -426,7 +426,8 @@  can_duplicate_and_interleave_p (vec_info *vinfo, unsigned int count,
 	  if (vector_type
 	      && VECTOR_MODE_P (TYPE_MODE (vector_type))
 	      && known_eq (GET_MODE_SIZE (TYPE_MODE (vector_type)),
-			   GET_MODE_SIZE (base_vector_mode)))
+			   GET_MODE_SIZE (base_vector_mode))
+	      && exact_div_p (GET_MODE_NUNITS (TYPE_MODE (vector_type)), 2))
 	    {
 	      /* Try fusing consecutive sequences of COUNT / NVECTORS elements
 		 together into elements of type INT_TYPE and using the result