Issue 238503
Summary [X86] Running maximum with llvm.maximum is 33% slower on AVX2 since #170069
Labels new issue
Assignees
Reporter gbaraldi
    Robots helped me gather the data

Since #170069, this loop takes one more cycle per element on x86 without AVX-512:

```c
void scanmax(double *restrict y, const double *restrict x, long n) {
  double acc = x[0];
  y[0] = acc;
  for (long i = 1; i < n; i++) {
    acc = __builtin_elementwise_maximum(acc, x[i]);   // llvm.maximum.f64
    y[i] = acc;
  }
}
```

Cycles per element with `-O2` (perf `cycles:u`, AMD EPYC 9354, 2^16 doubles):

| | Clang 22 | Clang 23 |
|---|---:|---:|
| `-march=x86-64-v3` | 3.01 | 4.02 |
| `-march=znver4` | 16.98 | 4.39 |

Clang 22 chose the `vmaxsd` operand order by branching on the sign of `acc`. In a running maximum that sign changes at most once, so the branch is always predicted, and only `vmaxsd` and `vblendvpd` wait for the previous `acc`:

```asm
        vmovq       rdx, xmm0
        test        rdx, rdx
        js          .LBB0_4
        vmovdqa     xmm2, xmm0
        jmp         .LBB0_14
.LBB0_4:
        vmovdqa     xmm2, xmm1
        vmovdqa     xmm1, xmm0
.LBB0_14:
        vmaxsd      xmm0, xmm1, xmm2
        vcmpunordsd xmm2, xmm1, xmm1
        vblendvpd   xmm1, xmm0, xmm1, xmm2
```

Clang 23 has no branch, but the `vandpd` that fixes the sign of a zero result sits between `vmaxsd` and the NaN blend, so three instructions wait for the previous `acc`:

```asm
        vmaxsd      xmm2, xmm0, qword ptr [rsi + 8*rcx + 8]
        vorpd       xmm3, xmm0, xmm1          ; xmm1 = 0x7FFFFFFFFFFFFFFF
        vandpd      xmm2, xmm3, xmm2
        vcmpunordsd xmm3, xmm0, xmm0
        vblendvpd   xmm0, xmm2, xmm0, xmm3
```

The loop isn't memory bound: the cycles per element are the same from 2^10 to 2^20 elements.

### AVX-512

With `vpternlogq`, two instructions on the `acc` chain are enough if the fixups are computed from the operand that isn't carried around the loop, here the loaded `x[i]`:

```asm
; xmm0 = acc, xmm1 = x[i], xmm7 = 0x7FFFFFFFFFFFFFFF
        vcmpunordsd xmm2, xmm1, xmm1          ; N = isnan(x) ? all ones : 0
        vorpd       xmm3, xmm1, xmm7
        vandnpd     xmm3, xmm2, xmm3          ; Z = isnan(x) ? 0 : x | 0x7FF...F
        vandpd      xmm4, xmm2, xmm1          ; W = isnan(x) ? x : 0
        vmaxsd      xmm0, xmm1, xmm0          ; acc if equal or either is NaN
        vpternlogq  xmm0, xmm3, xmm4, 0xEA    ; (max & Z) | W
```

`vmaxsd x, acc` returns `acc` when the two are equal or either is NaN. `& Z` turns a `-0.0` result into `+0.0` when `x` is `+0.0`, and leaves a NaN `acc` a NaN. `| W` gives `x` when `x` is NaN.

A hand-written loop with this runs at 3.04 cycles per element. llc 23's `-mcpu=znver4` loop takes 4.01 without unrolling, and Clang's unrolled loop takes 4.39. On about 645,000 outputs from inputs that include ±0, ±inf, NaN and denormals, it matched `llvm.maximum`, counting any NaN as a match for a NaN.
Separately, Clang 23's AVX-512 loop (4.39) is slower than its AVX2 loop (4.02). Without unrolling, both take 4.01.

### llc reproducer

`llc -O3 -mcpu=x86-64-v3` gives the branch on 22 and the `vorpd`/`vandpd` sequence on 23 and on main.

```llvm
define void @scanmax(ptr noalias %y, ptr noalias %x, i64 %n) {
entry:
  %x0 = load double, ptr %x, align 8
  store double %x0, ptr %y, align 8
  br label %loop

loop:
  %i = phi i64 [ 1, %entry ], [ %i.next, %loop ]
  %acc = phi double [ %x0, %entry ], [ %m, %loop ]
  %px = getelementptr inbounds double, ptr %x, i64 %i
  %v = load double, ptr %px, align 8
  %m = call double @llvm.maximum.f64(double %acc, double %v)
  %py = getelementptr inbounds double, ptr %y, i64 %i
  store double %m, ptr %py, align 8
  %i.next = add nuw nsw i64 %i, 1
  %c = icmp ult i64 %i.next, %n
  br i1 %c, label %loop, label %exit

exit:
  ret void
}

declare double @llvm.maximum.f64(double, double)
```

<details><summary>Benchmark</summary>

`clang -O2 -march=x86-64-v3 scanmax.c -o scanmax && perf stat -e cycles:u ./scanmax`. The table above divides the cycles by 3000 × 2^16.

```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

__attribute__((noinline)) void scanmax(double *restrict y,
                                       const double *restrict x, long n) {
  double acc = x[0];
  y[0] = acc;
  for (long i = 1; i < n; i++) {
    acc = __builtin_elementwise_maximum(acc, x[i]);
    y[i] = acc;
  }
}

static double now(void) {
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  return t.tv_sec + t.tv_nsec * 1e-9;
}

int main(void) {
  enum { N = 1 << 16, REPS = 3000 };
  double *x = malloc(N * sizeof *x), *y = malloc(N * sizeof *y);
  uint64_t s = 1;
  for (long i = 0; i < N; i++) {
    s = s * 6364136223846793005u + 1442695040888963407u;
    x[i] = (double)(s >> 11) * 0x1p-53 * 2 - 1; // uniform in [-1, 1)
  }
  double best = 1e9;
  for (int r = 0; r < REPS; r++) {
    double t = now();
    scanmax(y, x, N);
    t = now() - t;
    if (t < best)
      best = t;
  }
  printf("%.3f ns/element (y[N-1] = %g)\n", best / N * 1e9, y[N - 1]);
}
```

</details>

Tested with Clang/LLVM 22.1.8 and 23.1.4 as built by Julia, which carry a few patches, and with llc from main at ba80439dfe31.

Found while looking at https://github.com/JuliaLang/julia/issues/64505.


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

Reply via email to