https://gcc.gnu.org/g:199b021a38f30b681e0dbecd2d0296beabd50b13

commit r13-8475-g199b021a38f30b681e0dbecd2d0296beabd50b13
Author: liuhongt <hongtao....@intel.com>
Date:   Thu Mar 21 13:15:23 2024 +0800

    Fix runtime error for nonlinear iv vectorization(step_mult).
    
    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.
    
    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.
    
    (cherry picked from commit ac2f8c2a367151fc0410f904339c475a953cffc8)

Diff:
---
 gcc/testsuite/gcc.target/i386/pr114396.c | 105 +++++++++++++++++++++++++++++++
 gcc/tree-vect-loop.cc                    |   2 +-
 2 files changed, 106 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.target/i386/pr114396.c 
b/gcc/testsuite/gcc.target/i386/pr114396.c
new file mode 100644
index 00000000000..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 d08d4996771..9615161ad37 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -8730,7 +8730,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);

Reply via email to