Issue 241562
Summary [AArch64][SME] SVE instructions are sunk past SMSTOP in a locally streaming function with a stack protector
Labels new issue
Assignees
Reporter 0z5a
    With a stack protector, the vector add of this `__arm_locally_streaming` function is emitted after `smstop sm`, so SVE instructions run outside streaming mode. On a CPU that has SME but not SVE the program dies with SIGILL.

```c
#include <stdio.h>
#include <string.h>
typedef unsigned char v16 __attribute__((vector_size(16)));

__arm_locally_streaming __attribute__((noinline)) v16
f (v16 *p, v16 *q)
{
  return *p + *q;
}

int
main (void)
{
  v16 a, b, r;
  unsigned char o[16];
 memset (&a, 1, sizeof a);
  memset (&b, 2, sizeof b);
  r = f (&a, &b);
 memcpy (o, &r, sizeof o);
  printf ("%d\n", o[0]);
  return 0;
}
```

`clang -O2 -march=armv8-a+sme -fstack-protector-all`, trunk (8e8d8275f59d), `f` only:

```asm
        smstart sm
        ...
 ldr     q0, [x9]
        str     z0, [x8, #-1, mul vl]
        ldr     q0, [x1]
        str     z0, [x8, #-2, mul vl]
        smstop  sm
 ...
        cmp     x8, x9
        b.ne    .LBB0_2
        sub     x8, x29, #64
        ldr     z0, [x8, #-1, mul vl]
        ldr     z1, [x8, #-2, mul vl]
        add     z0.b, z1.b, z0.b
        ...
 ret
```

Run on an SME CPU without SVE (trunk assembly for `arm64-apple-macos`, assembled and linked natively):

| compiler and flags | result |
|---|---|
| trunk `-O2 -fstack-protector-all` (also `-O1`) | Illegal instruction |
| trunk `-O2 -fno-stack-protector` | `3` |
| trunk `-O2 -fstack-protector-all -mllvm -disable-machine-sink` | `3` |
| clang 21.1.0 `-O2 -fstack-protector-all` | Illegal instruction |
| GCC 16.2 `-O2 -fstack-protector-all` | `3` |

Expected output is `3`. Unlike #71093, no call is involved: the only mode switch is the function's own `smstop` before the stack-protector check. The assembly for `aarch64-linux-gnu` has the same three instructions after `smstop sm` in every release from 18.1.0 to 23.1.0 and in trunk.

On macOS the default `-fstack-protector` is enough once the function has a local `char` array, so no extra flag is needed to hit this there.

Assisted-by: Claude (claude-fable-5-2, claude-opus-5-5). I reviewed the report and this text.

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

Reply via email to