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

            Bug ID: 128978
           Summary: amdgcn: cmla/cmls expanders give wrong results
                    (dpp_distribute_odd uses the EVEN unspec; cmls
                    computes op1*op2-op3)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          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,
                    julian at codesourcery dot com
  Target Milestone: ---
            Target: amdgcn-amdhsa

The amdgcn cml<addsub_as><mode>4 expander (cmla<mode>4 and cmls<mode>4 in
gcn-valu.md, added in r13-6812-g1bde3acee77) has two defects. With
-ffast-math the vectorizer uses it for .COMPLEX_FMA and .COMPLEX_FMS, so
loops like these get wrong results:

#define N 64

void
fma0 (_Complex double *restrict a, _Complex double *restrict b,
      _Complex double *restrict c)
{
  for (int i = 0; i < N; i++)
    c[i] += a[i] * b[i];
}

void
fms0 (_Complex double *restrict a, _Complex double *restrict b,
      _Complex double *restrict c)
{
  for (int i = 0; i < N; i++)
    c[i] -= a[i] * b[i];
}

amdgcn-amdhsa-gcc -O2 -ffast-math -S, trunk 20261121 (r17 at 12a68f71e25).

1. dpp_distribute_odd<mode> uses the wrong unspec:

(define_insn "@dpp_distribute_odd<mode>"
  [(set (match_operand:V_noHI 0 "register_operand"    "=v")
        (unspec:V_noHI
          [(match_operand:V_noHI 1 "register_operand" " v")]
          UNSPEC_MOV_DPP_DISTRIBUTE_EVEN))]

It should be UNSPEC_MOV_DPP_DISTRIBUTE_ODD. As written it is the same RTL
as dpp_distribute_even of the same operand, so the expand dump already has
only DISTRIBUTE_EVEN unspecs, and after CSE the "b b" copy of operand 1
is the "a a" one. For fms0 the loop body is:

        v_mov_b32  v6, v6 quad_perm:[0,0,2,2]        ; a a  (no [1,1,3,3]
anywhere)
        v_mov_b32  v7, v7 quad_perm:[0,0,2,2]
        flat_load_dwordx2  v[8:9], v[4:5] offset:0  ; c
        v_fma_f64  v[8:9], v[6:7], v[10:11], -v[8:9] ; t1 = a*op2 - c
        v_mul_f64  v[6:7], v[10:11], v[6:7]          ; t2 = a*op2, should be
b*op2
        v_mov_b32  v6, v6 quad_perm:[1,0,3,2]        ; swap pairs
        v_mov_b32  v7, v7 quad_perm:[1,0,3,2]
        s_mov_b32  exec_lo, 1431655765               ; even lanes
        s_mov_b32  exec_hi, 1431655765
        v_add_f64  v[10:11], v[8:9], -v[6:7]
        s_mov_b32  exec_lo, -1431655766              ; odd lanes
        s_mov_b32  exec_hi, -1431655766
        v_add_f64  v[10:11], v[6:7], v[8:9]

The only user of dpp_distribute_odd is this expander, so this affects both
cmla and cmls.

2. cmls computes the wrong operation. The expander starts from
fms<mode>4 (t1 = a*op2 - op3) and then subtracts b*d in the even lanes and
adds b*c in the odd lanes, i.e. it computes op1 * op2 - op3. But
IFN_COMPLEX_FMS, which maps to cmls, is formed by the vectorizer from
c - a * b (complex_fms_pattern), and the aarch64 and arm expanders
compute op3 - op1 * op2. So even with 1. fixed, every element of fms0's
result has the wrong sign. The md.texi description of cmls/cmls_conj
said op1 * op2 - op3, which is what the GCN expander follows; that is
PR122601, patch:
https://inbox.sourceware.org/gcc-patches/gcc-upstream.upstream-bugfix-pr122601.v1.20261123t2358...@gmail.com/

For the minus case the expander should start from fnma (op3 - a*op2) and
then add b*d in the even lanes and subtract b*c in the odd lanes.

The testsuite does not catch either: vect-complex-operations-run.c needs
vect_complex_add_double, which is only true for arm and aarch64. I only
have a cross compiler (no GCN hardware), so this is from the generated
code, not a run.

Reply via email to