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

            Bug ID: 128790
           Summary: reassoc splits each multiply-add chain of a vectorised
                    loop in two without regard to how many chains are live
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: freddie at witherden dot org
  Target Milestone: ---

Consider the following (I can't believe it's not APL) C snippet:

#define F(M, k) M(k, 0) M(k, 1) M(k, 2) M(k, 3) M(k, 4)
#define L(k, i) double a##k##i = s[r][k*5 + i][j];
#define S(k, i) s[r][k*5 + i][j] = a##k##i;
#define W(k, c) double w##k = e*wg[(k*8 + c)*8 + j];
#define T(k, c, i) a##k##i += w##k*u[(c*5 + i)*8 + j];
#define R(k, c) T(k, c, 0) T(k, c, 1) T(k, c, 2) T(k, c, 3) T(k, c, 4)
#define C(c) { double e = op[(c*8 + r)*8 + j]; \
                W(0, c) W(1, c) W(2, c) R(0, c) R(1, c) R(2, c) }

void tile(const double *restrict op, const double *restrict wg,
          const double *restrict u, double (*restrict s)[15][8])
{
    for (int r = 0; r < 8; r++)
        #pragma omp simd
        for (int j = 0; j < 8; j++)
        {
            F(L, 0) F(L, 1) F(L, 2)
            C(0) C(1) C(2) C(3) C(4) C(5) C(6) C(7)
            F(S, 0) F(S, 1) F(S, 2)
        }
}

Compiling this gcc 16.2.0 at -O3 -ffast-math -fopenmp-simd
-mcpu=apple-m1 gives an inner loop with ~400 instructions with a lot of them
being stack spills.  This is due to the reassoc path cracking the FMA chains in
two, which creates register pressure.  Passing -fno-tree-reassoc clears the
issue up and gives an inner loop with ~250 instructions, no stack spills, and
~2x better performance on my M4.

Thus, it seems as if the heuristics for reassoc need tweaking to ensure that it
does not result in too many accumulators.

Reply via email to