]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/113896 - reduction of permuted external vector
authorRichard Biener <rguenther@suse.de>
Tue, 13 Feb 2024 12:43:44 +0000 (13:43 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 14 Feb 2024 11:43:14 +0000 (12:43 +0100)
The following fixes eliding of the permutation of a BB reduction
of an existing vector which breaks materialization of live lanes
as we fail to permute the SLP_TREE_SCALAR_STMTS vector.

PR tree-optimization/113896
* tree-vect-slp.cc (vect_optimize_slp): Permute
SLP_TREE_SCALAR_STMTS when eliding a permuation in a
VEC_PERM node we need to preserve because it wraps an
extern vector.

* g++.dg/torture/pr113896.C: New testcase.

gcc/testsuite/g++.dg/torture/pr113896.C [new file with mode: 0644]
gcc/tree-vect-slp.cc

diff --git a/gcc/testsuite/g++.dg/torture/pr113896.C b/gcc/testsuite/g++.dg/torture/pr113896.C
new file mode 100644 (file)
index 0000000..534c1c2
--- /dev/null
@@ -0,0 +1,35 @@
+// { dg-do run }
+// { dg-additional-options "-ffast-math" }
+
+double a1 = 1.0;
+double a2 = 1.0;
+
+void __attribute__((noipa))
+f(double K[2], bool b)
+{
+    double A[] = {
+        b ? a1 : a2,
+        0,
+        0,
+        0
+    };
+
+    double sum{};
+    for(double  a : A) sum += a;
+    for(double& a : A) a /= sum;
+
+    if (b) {
+        K[0] = A[0]; // 1.0
+        K[1] = A[1]; // 0.0
+    } else {
+        K[0] = A[0] + A[1];
+    }
+}
+
+int main()
+{
+  double K[2]{};
+  f(K, true);
+  if (K[0] != 1. || K[1] != 0.)
+    __builtin_abort ();
+}
index af477c31aa3d9740daad801d1c9b8d3289f8be02..b3e3d9e700944613406e442ac6e75e471b6916ac 100644 (file)
@@ -4058,6 +4058,15 @@ vect_optimize_slp (vec_info *vinfo)
                {
                  /* Preserve the special VEC_PERM we use to shield existing
                     vector defs from the rest.  But make it a no-op.  */
+                 auto_vec<stmt_vec_info, 64> saved;
+                 saved.create (SLP_TREE_SCALAR_STMTS (old).length ());
+                 for (unsigned i = 0;
+                      i < SLP_TREE_SCALAR_STMTS (old).length (); ++i)
+                   saved.quick_push (SLP_TREE_SCALAR_STMTS (old)[i]);
+                 for (unsigned i = 0;
+                      i < SLP_TREE_SCALAR_STMTS (old).length (); ++i)
+                   SLP_TREE_SCALAR_STMTS (old)[i]
+                     = saved[SLP_TREE_LANE_PERMUTATION (old)[i].second];
                  unsigned i = 0;
                  for (std::pair<unsigned, unsigned> &p
                       : SLP_TREE_LANE_PERMUTATION (old))