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

            Bug ID: 128768
           Summary: Vectorised loop keeps one base register per data
                    reference when their common base is computed in an
                    outer loop
           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 C snippet:

#define S(c, i) s[r][i][j] += e*b[((k + c)*5 + i)*8 + j];
#define C(c) { double e = op[(r*8 + c)*8 + j]; \
                S(c, 0) S(c, 1) S(c, 2) S(c, 3) S(c, 4) }

void tile(long n, long k, const double *restrict op,
            const double *restrict b, double (*restrict s)[5][8])
{
    for (long r = 0; r < n; r++)
        #pragma omp simd
        for (long j = 0; j < 8; j++)
        {
            C(0) C(1) C(2) C(3) C(4) C(5) C(6) C(7)
        }
}

which compiled as gcc -O3 -fopenmp-simd -mcpu=apple-m1 results in inefficient
code being generated for the inner loop of the form:

    L4: ldr   x8, [sp, 304]         ; base reloaded
        ldr   q31, [x2, x0]
        ldr   q3, [x6, x0]
        ldr   q4, [x8, x0]
        ldr   x8, [sp, 240]         ; base reloaded
        ldr   q26, [x26, x0]
        ldr   q1, [x5, x0]
        fmla  v3.2d, v4.2d, v31.2d
        ldr   q28, [x8, x0]
        ldr   x8, [sp, 192]         ; base reloaded
        ...                         ; 23 more reloads
        add   x0, x0, 16
        cmp   x0, 64
        bne   L4

where I count (hopefully correctly) 40 fmla, 53 ldr q, 5 str q, 26 ldr from sp.
 The sp loads are the suspect ones: the inner loop of tile does 53 vector loads
per pass (8 of op, 40 of b, 5 of s) and their addresses have three bases, op +
512*r, b + 320*k and s + 320*r.  Yet, one of the optimization passes seems to
expand this into 53 base pointers which leads to a bunch of them being spilled
to the stack.

This hurts performance quite a bit on my M4 (hacking the ASM to use 3 base
pointers gives me a ~1.5x speedup).  No reason to think this is an ARM specific
issue, however, seems to lie upstream.

Reply via email to