Issue 242966
Summary [AArch64][Windows] preserve_all function places a local in a callee-saved register's save slot
Labels new issue
Assignees
Reporter athei
    On `aarch64-w64-mingw32` and `arm64ec-w64-mingw32`, a `preserve_all` function stores a local into the stack slot where its prologue saved a callee-saved register. The epilogue then loads the local's value into that register, so the caller gets the register back changed.

### Reproduce

`pa1.cpp`:

```c++
struct State {
  unsigned flags;
  void* frame;
};

extern "C" void consume(unsigned* flags, double x);

__attribute__((preserve_all)) double handle(unsigned short fcw, double x, char* frame) {
  State s {fcw, frame};
  consume(&s.flags, x);
  if (s.flags & 0x10) {
 static_cast<char*>(s.frame)[0x410] = 1;
  }
  return x;
}
```

```
clang version 23.1.5 (https://github.com/llvm/llvm-project.git 392dfff97d29b59d77e5cac6d4b6ef2585fe7117)

clang++ --target=aarch64-w64-mingw32 -O2 -c pa1.cpp -o pa1.o
llvm-objdump -d pa1.o
```

- The same with `--target=arm64ec-w64-mingw32` and `--target=aarch64-linux-gnu`. No headers or sysroot are needed.
- The compiler is the `llvmorg-23.1.5` release from an llvm-mingw toolchain on an arm64 macOS host.
- `main` is not tested.

### Observed

`aarch64-w64-mingw32`, excerpt without instruction bytes, comments added:

```
   4:  str  x28, [sp, #0x10]     // saves x28
 ...
  5c:  str  x1, [sp, #0x10]      // s.frame, into the x28 slot
  ...
 6c:  ldr  x8, [sp, #0x10]      // reads s.frame back
  ...
  c0:  ldr  x28, [sp, #0x10]     // x28 = frame on return
```

`arm64ec-w64-mingw32` does the same with x27:

```
   4:  stp  x27, x30, [sp, #0x10]
  58:  str  x1, [sp, #0x10]
  b8:  ldp  x27, x30, [sp, #0x10]
```

The function uses x28 (x27 on ARM64EC) only in these save and restore instructions.

### Expected

The save slot of x28 (x27) is not reused before the epilogue restores the register.

- `aarch64-linux-gnu` is correct: `s.flags` at `[sp]`, `s.frame` at `[sp, #0x8]`, x28 saved at `[sp, #0x1d8]` and restored from there.
- `__attribute__((preserve_all, nouwtable))` gives the same listing on both Windows targets.
- Without `preserve_all`, both Windows targets put `s.frame` at `[sp, #0x8]` and save only x30 and d8.

Found in an x86 emulator's x87 fallback handlers built for ARM64EC, where the overwritten registers hold guest registers.

Disclosure: this report was prepared with an AI tool. The reproducer and the output shown were rerun on the clang version above before filing.

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

Reply via email to