https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129007

            Bug ID: 129007
           Summary: [14/15/16/17 Regression] Wrong code for complex FMS
                    with a strided accumulator since r16-4663
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ishevlyakov at gcc dot gnu.org
                CC: ams at gcc dot gnu.org, ishevlyakov at gcc dot gnu.org,
                    rguenth at gcc dot gnu.org, tnfchris at gcc dot gnu.org
  Target Milestone: ---

Andrew Stubbs found this while fixing PR128978 (GCN cmla/cmls): with a
strided accumulator, the complex FMS pattern gets the wrong operands. It
reproduces on aarch64:

/* { dg-do run } */
/* { dg-additional-options "-ffast-math" } */

__attribute__((noipa)) void
f (_Complex double *restrict c, _Complex double *restrict x,
   _Complex double *restrict y, int n, int s)
{
  for (int i = 0; i < n; i++)
    c[i * s] -= x[i] * y[i];
}

int
main (void)
{
  _Complex double x[4], y[4], c[4];
  for (int i = 0; i < 4; i++)
    {
      x[i] = (i + 1) + (i + 2) * 1.0i;
      y[i] = (i + 3) + (i + 4) * 1.0i;
      c[i] = 100 + 50 * 1.0i;
    }

  f (c, x, y, 4, 1);

  for (int i = 0; i < 4; i++)
    if (c[i] != 100 + 50 * 1.0i - x[i] * y[i])
      __builtin_abort ();
  return 0;
}

With -O2 -ffast-math -march=armv8.3-a (run on Compiler Explorer):

  GCC 12.5.0, 14.3.0, 15.2.0          pass
  GCC 14.4.0, 15.3.0, 16.1.0, trunk   abort

15.2 feeds the loaded x[i] and y[i] straight into

        fcmla   v29.2d, v30.2d, v31.2d, #180
        fcmla   v29.2d, v30.2d, v31.2d, #270

15.3 and 16.1 first swap the halves of both (ext v.16b, v.16b, v.16b,
#8); 14.4 and trunk broadcast the imaginary part of both (dup v.2d,
v.d[1]).

A bisect with an aarch64 cross cc1 between the gcc-15 and gcc-16 branch
points gives r16-4663-gc5fa3d4c88fc4f (vect: Fix operand swapping on
complex multiplication detection [PR122408]). It was backported as
r15-10468-g5b7639b0de2f and r14-12166-g1d4b0555d6d2, which 15.3.0 and
14.4.0 contain.

That commit made vect_validate_multiplication return the operands in a
flat res_ops = { L0, L1, R0, R1 }. complex_fms_pattern::matches calls it
with the operands swapped, (right_op, left_op), so the caller's right_op
is res_ops[0..1] and its left_op res_ops[2..3], but the code after the
call was converted as if it were the other way round:

  kind from right_op[0]                ->  res_ops[2]   (should be [0])
  EVENODD: right_op[0], [1], left_op[1] ->  [2], [3], [1]  (should be [0], [1],
[3])
  else:    right_op[1], [0], left_op[0] ->  [3], [2], [0]  (should be [1], [0],
[2])

The release branches (14, 15 and 16) also have a second problem: the
commutative retry declares its own auto_vec<slp_tree> res_ops, shadowing
the outer one, so after a successful retry the code reads the operands
of the failed first attempt. Trunk no longer has that since the
vect_validate_multiplication_commutative rewrite, which is why its code
differs from 16's, but it keeps the wrong indices.

Fixing only the indices gives the 15.2 code again, both on trunk and at
the gcc-16 branch point (fixing only the shadowing at the branch point
gives exactly trunk's dup code). The change for trunk:

--- a/gcc/tree-vect-slp-patterns.cc
+++ b/gcc/tree-vect-slp-patterns.cc
@@ -1443,20 +1443,20 @@ complex_fms_pattern::matches (complex_operation_t op,
   ops->truncate (0);
   ops->create (4);

-  complex_perm_kinds_t kind = linear_loads_p (perm_cache, res_ops[2]);
+  complex_perm_kinds_t kind = linear_loads_p (perm_cache, res_ops[0]);
   if (kind == PERM_EVENODD)
     {
       ops->quick_push (l0node[0]);
-      ops->quick_push (res_ops[2]);
-      ops->quick_push (res_ops[3]);
+      ops->quick_push (res_ops[0]);
       ops->quick_push (res_ops[1]);
+      ops->quick_push (res_ops[3]);
     }
   else
     {
       ops->quick_push (l0node[0]);
-      ops->quick_push (res_ops[3]);
-      ops->quick_push (res_ops[2]);
+      ops->quick_push (res_ops[1]);
       ops->quick_push (res_ops[0]);
+      ops->quick_push (res_ops[2]);
     }

   return ifn;

I have only checked the generated code with a cross compiler, not run
it on aarch64, and I have not run the pr122408_*.f90 tests with it.

Reply via email to