Re: [PATCH] Fix runtime error for nonlinear iv vectorization(step_mult).

2024-03-21 Thread Richard Biener
On Thu, Mar 21, 2024 at 9:35 AM liuhongt  wrote:
>
> wi::from_mpz doesn't take a sign argument, we want it to be wrapped
> instead of saturation, so pass utype and true to it, and it fixes the
> bug.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> Ok for trunk and backport to gcc13?

OK for both.

Thanks,
Richard.

> gcc/ChangeLog:
>
> PR tree-optimization/114396
> * tree-vect-loop.cc (vect_peel_nonlinear_iv_init): Pass utype
> and true to wi::from_mpz.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.target/i386/pr114396.c: New test.
> ---
>  gcc/testsuite/gcc.target/i386/pr114396.c | 105 +++
>  gcc/tree-vect-loop.cc|   2 +-
>  2 files changed, 106 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr114396.c
>
> diff --git a/gcc/testsuite/gcc.target/i386/pr114396.c 
> b/gcc/testsuite/gcc.target/i386/pr114396.c
> new file mode 100644
> index 000..4c4015f871f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr114396.c
> @@ -0,0 +1,105 @@
> +/* { dg-do run } */
> +/* { dg-options "-O1 -fwrapv -fno-vect-cost-model" } */
> +
> +short a = 0xF;
> +short b[16];
> +unsigned short ua = 0xF;
> +unsigned short ub[16];
> +
> +short
> +__attribute__((noipa))
> +foo (short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +b[e] = a *= 5;
> +  return a;
> +}
> +
> +short
> +__attribute__((noipa))
> +foo1 (short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +b[e] = a *= -5;
> +  return a;
> +}
> +
> +unsigned short
> +__attribute__((noipa))
> +foou (unsigned short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +ub[e] = a *= -5;
> +  return a;
> +}
> +
> +unsigned short
> +__attribute__((noipa))
> +foou1 (unsigned short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +ub[e] = a *= 5;
> +  return a;
> +}
> +
> +short
> +__attribute__((noipa,optimize("O3")))
> +foo_o3 (short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +b[e] = a *= 5;
> +  return a;
> +}
> +
> +short
> +__attribute__((noipa,optimize("O3")))
> +foo1_o3 (short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +b[e] = a *= -5;
> +  return a;
> +}
> +
> +unsigned short
> +__attribute__((noipa,optimize("O3")))
> +foou_o3 (unsigned short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +ub[e] = a *= -5;
> +  return a;
> +}
> +
> +unsigned short
> +__attribute__((noipa,optimize("O3")))
> +foou1_o3 (unsigned short a)
> +{
> +  for (int e = 0; e < 9; e += 1)
> +ub[e] = a *= 5;
> +  return a;
> +}
> +
> +int main() {
> +  unsigned short uexp, ures;
> +  short exp, res;
> +  exp = foo (a);
> +  res = foo_o3 (a);
> +  if (exp != res)
> +__builtin_abort ();
> +
> +  exp = foo1 (a);
> +  res = foo1_o3 (a);
> +  if (uexp != ures)
> +__builtin_abort ();
> +
> +  uexp = foou (a);
> +  ures = foou_o3 (a);
> +  if (uexp != ures)
> +__builtin_abort ();
> +
> +  uexp = foou1 (a);
> +  ures = foou1_o3 (a);
> +  if (uexp != ures)
> +__builtin_abort ();
> +
> +  return 0;
> +}
> diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> index 4375ebdcb49..2921a9e6aa1 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -9454,7 +9454,7 @@ vect_peel_nonlinear_iv_init (gimple_seq* stmts, tree 
> init_expr,
> wi::to_mpz (skipn, exp, UNSIGNED);
> mpz_ui_pow_ui (mod, 2, TYPE_PRECISION (type));
> mpz_powm (res, base, exp, mod);
> -   begin = wi::from_mpz (type, res, TYPE_SIGN (type));
> +   begin = wi::from_mpz (utype, res, true);
> tree mult_expr = wide_int_to_tree (utype, begin);
> init_expr = gimple_build (stmts, MULT_EXPR, utype,
>   init_expr, mult_expr);
> --
> 2.31.1
>


[PATCH] Fix runtime error for nonlinear iv vectorization(step_mult).

2024-03-21 Thread liuhongt
wi::from_mpz doesn't take a sign argument, we want it to be wrapped
instead of saturation, so pass utype and true to it, and it fixes the
bug.

Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
Ok for trunk and backport to gcc13?

gcc/ChangeLog:

PR tree-optimization/114396
* tree-vect-loop.cc (vect_peel_nonlinear_iv_init): Pass utype
and true to wi::from_mpz.

gcc/testsuite/ChangeLog:

* gcc.target/i386/pr114396.c: New test.
---
 gcc/testsuite/gcc.target/i386/pr114396.c | 105 +++
 gcc/tree-vect-loop.cc|   2 +-
 2 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr114396.c

diff --git a/gcc/testsuite/gcc.target/i386/pr114396.c 
b/gcc/testsuite/gcc.target/i386/pr114396.c
new file mode 100644
index 000..4c4015f871f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr114396.c
@@ -0,0 +1,105 @@
+/* { dg-do run } */
+/* { dg-options "-O1 -fwrapv -fno-vect-cost-model" } */
+
+short a = 0xF;
+short b[16];
+unsigned short ua = 0xF;
+unsigned short ub[16];
+
+short
+__attribute__((noipa))
+foo (short a)
+{
+  for (int e = 0; e < 9; e += 1)
+b[e] = a *= 5;
+  return a;
+}
+
+short
+__attribute__((noipa))
+foo1 (short a)
+{
+  for (int e = 0; e < 9; e += 1)
+b[e] = a *= -5;
+  return a;
+}
+
+unsigned short
+__attribute__((noipa))
+foou (unsigned short a)
+{
+  for (int e = 0; e < 9; e += 1)
+ub[e] = a *= -5;
+  return a;
+}
+
+unsigned short
+__attribute__((noipa))
+foou1 (unsigned short a)
+{
+  for (int e = 0; e < 9; e += 1)
+ub[e] = a *= 5;
+  return a;
+}
+
+short
+__attribute__((noipa,optimize("O3")))
+foo_o3 (short a)
+{
+  for (int e = 0; e < 9; e += 1)
+b[e] = a *= 5;
+  return a;
+}
+
+short
+__attribute__((noipa,optimize("O3")))
+foo1_o3 (short a)
+{
+  for (int e = 0; e < 9; e += 1)
+b[e] = a *= -5;
+  return a;
+}
+
+unsigned short
+__attribute__((noipa,optimize("O3")))
+foou_o3 (unsigned short a)
+{
+  for (int e = 0; e < 9; e += 1)
+ub[e] = a *= -5;
+  return a;
+}
+
+unsigned short
+__attribute__((noipa,optimize("O3")))
+foou1_o3 (unsigned short a)
+{
+  for (int e = 0; e < 9; e += 1)
+ub[e] = a *= 5;
+  return a;
+}
+
+int main() {
+  unsigned short uexp, ures;
+  short exp, res;
+  exp = foo (a);
+  res = foo_o3 (a);
+  if (exp != res)
+__builtin_abort ();
+
+  exp = foo1 (a);
+  res = foo1_o3 (a);
+  if (uexp != ures)
+__builtin_abort ();
+
+  uexp = foou (a);
+  ures = foou_o3 (a);
+  if (uexp != ures)
+__builtin_abort ();
+
+  uexp = foou1 (a);
+  ures = foou1_o3 (a);
+  if (uexp != ures)
+__builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 4375ebdcb49..2921a9e6aa1 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -9454,7 +9454,7 @@ vect_peel_nonlinear_iv_init (gimple_seq* stmts, tree 
init_expr,
wi::to_mpz (skipn, exp, UNSIGNED);
mpz_ui_pow_ui (mod, 2, TYPE_PRECISION (type));
mpz_powm (res, base, exp, mod);
-   begin = wi::from_mpz (type, res, TYPE_SIGN (type));
+   begin = wi::from_mpz (utype, res, true);
tree mult_expr = wide_int_to_tree (utype, begin);
init_expr = gimple_build (stmts, MULT_EXPR, utype,
  init_expr, mult_expr);
-- 
2.31.1