https://gcc.gnu.org/g:7543d3c4c4a4630709a002a3cf012c0512dc8eb6

commit r17-6670-g7543d3c4c4a4630709a002a3cf012c0512dc8eb6
Author: Igor Shevlyakov <[email protected]>
Date:   Tue Nov 24 13:07:43 2026 -0800

    vect: Fix the operands of the complex FMS pattern [PR129007]
    
    r16-4663-gc5fa3d4c88fc4f made vect_validate_multiplication return its
    operands in a flat vector 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 reads them the other
    way round.  For
    
      for (int i = 0; i < n; i++)
        c[i * s] -= x[i] * y[i];
    
    with -O2 -ffast-math -march=armv8.3-a the loaded x[i] and y[i] then go
    into FCMLA with the imaginary part broadcast to both lanes, and the
    result is wrong.  Use the same indices as the PLUS_MINUS form of the
    pattern earlier in the function, which makes the same call.
    
    Assisted-by: Claude Opus 5.5
    
            PR tree-optimization/129007
    
    gcc/ChangeLog:
    
            * tree-vect-slp-patterns.cc (complex_fms_pattern::matches): Take
            the operands of the multiplication from the right elements of
            res_ops.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/vect/pr129007.c: New test.
            * gcc.target/aarch64/pr129007-1.c: New test.
            * gcc.target/aarch64/pr129007-2.c: New test.
    
    Signed-off-by: Igor Shevlyakov <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/vect/pr129007.c          | 36 +++++++++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/pr129007-1.c | 33 ++++++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/pr129007-2.c | 18 ++++++++++++++
 gcc/tree-vect-slp-patterns.cc                 | 10 ++++----
 4 files changed, 92 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/pr129007.c 
b/gcc/testsuite/gcc.dg/vect/pr129007.c
new file mode 100644
index 000000000000..b7c18d012b2e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr129007.c
@@ -0,0 +1,36 @@
+/* PR tree-optimization/129007 */
+/* { dg-additional-options "-ffast-math" } */
+
+#include "tree-vect.h"
+
+#define N 16
+
+__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[N], y[N], c[N];
+
+  check_vect ();
+
+  for (int i = 0; i < N; 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, N, 1);
+
+  for (int i = 0; i < N; i++)
+    if (c[i] != 100 + 50 * 1.0i - x[i] * y[i])
+      __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/pr129007-1.c 
b/gcc/testsuite/gcc.target/aarch64/pr129007-1.c
new file mode 100644
index 000000000000..ffc2e0671802
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr129007-1.c
@@ -0,0 +1,33 @@
+/* PR tree-optimization/129007 */
+/* { dg-do run { target arm_v8_3a_complex_neon_hw } } */
+/* { dg-options "-O2 -ffast-math -march=armv8.3-a" } */
+
+#define N 16
+
+__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[N], y[N], c[N];
+
+  for (int i = 0; i < N; 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, N, 1);
+
+  for (int i = 0; i < N; i++)
+    if (c[i] != 100 + 50 * 1.0i - x[i] * y[i])
+      __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/pr129007-2.c 
b/gcc/testsuite/gcc.target/aarch64/pr129007-2.c
new file mode 100644
index 000000000000..b5b86fbc1041
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/pr129007-2.c
@@ -0,0 +1,18 @@
+/* PR tree-optimization/129007 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -march=armv8.3-a" } */
+
+/* c - a * b with the accumulator not loaded with LD2: the loaded x[i]
+   and y[i] go straight into FCMLA, without permuting their halves.  */
+
+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];
+}
+
+/* { dg-final { scan-assembler-times {\tfcmla\t} 2 } } */
+/* { dg-final { scan-assembler-not {\tdup\t} } } */
+/* { dg-final { scan-assembler-not {\text\t} } } */
diff --git a/gcc/tree-vect-slp-patterns.cc b/gcc/tree-vect-slp-patterns.cc
index c53325fecdc7..d99685c2daa4 100644
--- 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;

Reply via email to