Issue 239453
Summary [AArch64] Entry-block realignment scratch is always X9: clobbers the Arm64EC thunk argument and the callee-saved X9 of preserve_most/preserve_all
Labels new issue
Assignees
Reporter 20000419
    `AArch64FrameLowering::findScratchNonCalleeSaveRegister` returns `X9` for every entry block except `preserve_none`. Both Arm64EC thunk conventions (`ARM64EC_Thunk_X64` = cc 108 and `ARM64EC_Thunk_Native` = cc 109) pass their first argument in `X9` (`CC_AArch64_Arm64EC_Thunk*` in `AArch64CallingConvention.td`). When a thunk prologue needs a scratch register, e.g. to realign the stack, `X9` is overwritten before it is read.

This is reachable without hand-written thunks: Arm64EC call lowering generates an exit thunk that allocas the 64-byte return/argument vector with ABI alignment 64, and that thunk then calls the dispatcher with a stack address in `X9` instead of the x64 callee.

```llvm
target triple = "arm64ec-pc-windows-msvc"

declare <8 x i64> @ext(<8 x i64>)

define <8 x i64> @wrap(<8 x i64> %a) {
  %r = call <8 x i64> @ext(<8 x i64> %a)
  ret <8 x i64> %r
}
```

`llc -O2` (trunk), `$iexit_thunk$cdecl$m64$m64`:

```asm
        mov     x29, sp
        sub     x9, sp, #240                     // x9 = x64 callee, overwritten
        and     sp, x9, #0xffffffffffffffc0
        adrp    x8, __os_arm64x_dispatch_call_no_redirect
        ...
        ldr     x16, [x8, :lo12:__os_arm64x_dispatch_call_no_redirect]
        blr     x16                              // dispatcher reads the callee from x9
```

https://godbolt.org/z/o1a59GaM1 (llc trunk, `-O2`)

Smaller IR, also executed under qemu with a Linux ELF harness (asm driver sets `x9 = 0x12345678`, stub `__os_arm64x_dispatch_ret` returns `x8`):

```llvm
define cc 109 i64 @thunk_native(i64 %dest) {
  %buf = alloca i8, align 64
  store volatile i8 1, ptr %buf
  ret i64 %dest
}

define cc 109 i64 @thunk_native_plain(i64 %dest) {
  ret i64 %dest
}
```

```asm
thunk_native:
        stp     x29, x30, [sp, #-16]!
        sub     x9, sp, #48
        mov     x29, sp
        and     sp, x9, #0xffffffffffffffc0
        mov     x0, x9                           // returns the scratch, not %dest
```

`thunk_native_plain` returns `305419896`; `thunk_native` (and the cc 108 version) return a stack address, at `-O0` to `-Os`. https://godbolt.org/z/j7YcxMqh5 (llc trunk, `-O2`)

### Same early return, callee-saved X9: `preserve_most` / `preserve_all`

`preserve_most` and `preserve_all` make `X9` callee-saved, but the same early return hands it to the realignment code without saving it. Reachable from C (`clang --target=aarch64-linux-gnu`, any `-O`):

```c
__attribute__((preserve_most, noinline)) void f(void) {
  volatile char buf[128] __attribute__((aligned(64)));
  buf[0] = 1;
  buf[127] = 2;
}
```

A caller that keeps a value in `x9` across `bl f` (test harness `pmost_x9.c`) gets a stack address back under qemu-aarch64; `x10` in the same harness is preserved. Wrong with clang 19 to 24 and trunk at `-O0` to `-Os` (rechecked 2026-11-06); earlier notes also had 15–17 wrong for this C case. An `aligned(16)` local is fine.

Versions: wrong with llc/clang 19, 20, 21, 22, 23 and 24 (cf44dd9e317e) and godbolt trunk (rechecked 2026-11-06). 15 to 17 do not support cc 108/109. (18 not tested.)

Where: `llvm/lib/Target/AArch64/AArch64FrameLowering.cpp` line 905 at cf44dd9e317e:

```cpp
  // If MBB is an entry block, use X9 as the scratch register
  // preserve_none functions may be using X9 to pass arguments,
  // so prefer to pick an available register below.
  if (&MF->front() == MBB &&
      MF->getFunction().getCallingConv() != CallingConv::PreserveNone)
    return AArch64::X9;
```

The realignment in `AArch64PrologueEpilogue.cpp` (`allocateStackSpace`) takes this register. f05fa6e0cfdc added the `PreserveNone` exception for the same reason but did not cover the Arm64EC thunk conventions, where `X9` is always an argument.

Possible fix (not tested): only take the early return when `X9` is neither a live-in of the entry block nor callee-saved for the function's calling convention (that covers `PreserveNone`, `ARM64EC_Thunk_X64`, `ARM64EC_Thunk_Native`, `PreserveMost` and `PreserveAll`); otherwise fall through to the `LiveRegUnits` scan below.

cc @efriedma-quic @dpaoliello

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

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

Reply via email to