Issue 242246
Summary [AArch64] Vector/scalar union forces args+retval to go through GPRs even in internal functions, unnecessary NEON<->GPR moves
Labels missed-optimization
Assignees
Reporter kubamracek
    Reproducer:

```c
typedef union {
  __float4 v;                         // float with __attribute__((ext_vector_type(4)))
  struct { float x, y, z, w; } parts; // or __uint128_t, not actually used
} float4;

__attribute__((noinline)) static float4 scale(float4 a, float s) {
  return (float4){ .v = a.v * s };
}

float4 use(float4 a, float s) {
  float4 b = scale(a, s);
  b.v += a.v;
  return scale(b, 0.5f);
}
```

The `scale` function ends up receiving the float4 vector via X0+X1 and also returning the result via X0+X1:

```asm
scale:
        fmov    d1, x0
        mov     v1.d[1], x1
        fmul    v0.4s, v1.4s, v0.s[0]
        mov     x1, v0.d[1]
 fmov    x0, d0
        ret
```

The caller does extra work to place the vectors into X0+X1 too (see godbolt below). For external functions, this is what AAPCS requires, but here `scale` is static/internal, so it doesn't have to follow the ABI, and changing its (module internal) argument and return type to a vector would remove all the NEON<->GPR transfers.

Godbolt: https://godbolt.org/z/jxWEjc93h

The pattern (union of vector + something else) comes from Blender/Cycles: <https://github.com/blender/cycles/blob/6abef8c62bf9d6896a81faf86bfd96121802a7be/src/util/types_float4.h#L19> Fixing it does improve the renderer's performance.

The pattern seems to be pretty common in heavilty optimized vector libraries/projects, both via "sse2neon"-like libraries or directly.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to