[llvm] [Xtensa] Route windowed-ABI stack realignment through MOVSP (PR #230735)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 22:05:26 PDT 2026
https://github.com/chenqiant created https://github.com/llvm/llvm-project/pull/230735
For a windowed-ABI function whose MaxAlign exceeds the 16-byte SP guarantee, emitPrologue realigned SP after ENTRY with a plain ADD SP, SP, misalign. Any SP change after ENTRY must go through MOVSP, which relocates the caller a0-a3 base save area (or raises the Alloca exception); with ADD, a window spill in the ENTRY..ADD window saves the caller registers below the old SP and retw reloads stale data.
Also source the misalignment mask from SP instead of FP: FP still holds an incoming argument at that point (it is materialized below), and extend the realignment guard from >32 to >16 since SP is only 16-byte aligned.
Add windowed-stack-realign.ll covering align 64 (with and without a frame pointer), align 32, and align 16 (no realignment expected).
>From f62158dc27630b6a50ffbb1c6bdcc901535c39ff Mon Sep 17 00:00:00 2001
From: chenqian <chen.qian at espressif.com>
Date: Sat, 10 Oct 2026 13:00:19 +0800
Subject: [PATCH] [Xtensa] Route windowed-ABI stack realignment through MOVSP
For a windowed-ABI function whose MaxAlign exceeds the 16-byte SP
guarantee, emitPrologue realigned SP after ENTRY with a plain
ADD SP, SP, misalign. Any SP change after ENTRY must go through MOVSP,
which relocates the caller a0-a3 base save area (or raises the Alloca
exception); with ADD, a window spill in the ENTRY..ADD window saves the
caller registers below the old SP and retw reloads stale data.
Also source the misalignment mask from SP instead of FP: FP still holds
an incoming argument at that point (it is materialized below), and extend
the realignment guard from >32 to >16 since SP is only 16-byte aligned.
Add windowed-stack-realign.ll covering align 64 (with and without a frame
pointer), align 32, and align 16 (no realignment expected).
---
.../lib/Target/Xtensa/XtensaFrameLowering.cpp | 18 ++++-
.../CodeGen/Xtensa/windowed-stack-realign.ll | 73 +++++++++++++++++++
2 files changed, 87 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/CodeGen/Xtensa/windowed-stack-realign.ll
diff --git a/llvm/lib/Target/Xtensa/XtensaFrameLowering.cpp b/llvm/lib/Target/Xtensa/XtensaFrameLowering.cpp
index f7ea5f2e1720fe..66478c585cf1a3 100644
--- a/llvm/lib/Target/Xtensa/XtensaFrameLowering.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaFrameLowering.cpp
@@ -88,19 +88,29 @@ void XtensaFrameLowering::emitPrologue(MachineFunction &MF,
// new_offset = SP + diff_to_128_aligned_address
// This is safe to do because we increased the stack size by MaxAlignment.
MCRegister Reg, RegMisAlign;
- if (MaxAlignment > 32) {
+ if (MaxAlignment > 16) {
TII.loadImmediate(MBB, MBBI, &RegMisAlign, MaxAlignment - 1);
TII.loadImmediate(MBB, MBBI, &Reg, MaxAlignment);
+ // NB: align off SP, not FP: FP still holds an incoming argument at this
+ // point (it is materialized below).
BuildMI(MBB, MBBI, DL, TII.get(Xtensa::AND))
.addReg(RegMisAlign, RegState::Define)
- .addReg(FP)
+ .addReg(SP)
.addReg(RegMisAlign);
BuildMI(MBB, MBBI, DL, TII.get(Xtensa::SUB), RegMisAlign)
- .addReg(Reg)
+ .addReg(Reg, RegState::Kill)
.addReg(RegMisAlign);
- BuildMI(MBB, MBBI, DL, TII.get(Xtensa::ADD), SP)
+ // Any SP update after ENTRY must go through MOVSP so the register
+ // window save area is relocated (cf. XtensaInstrInfo::adjustStackPtr);
+ // a plain ADD corrupts the caller if a window spill lands between ENTRY
+ // and the update. Compute the aligned SP in a scratch register first.
+ MachineRegisterInfo &MRI = MF.getRegInfo();
+ MCRegister AlignedSP = MRI.createVirtualRegister(&Xtensa::ARRegClass);
+ BuildMI(MBB, MBBI, DL, TII.get(Xtensa::ADD), AlignedSP)
.addReg(SP)
.addReg(RegMisAlign, RegState::Kill);
+ BuildMI(MBB, MBBI, DL, TII.get(Xtensa::MOVSP), SP)
+ .addReg(AlignedSP, RegState::Kill);
}
// Store FP register in A8, because FP may be used to pass function
diff --git a/llvm/test/CodeGen/Xtensa/windowed-stack-realign.ll b/llvm/test/CodeGen/Xtensa/windowed-stack-realign.ll
new file mode 100644
index 00000000000000..7abc4f5121a287
--- /dev/null
+++ b/llvm/test/CodeGen/Xtensa/windowed-stack-realign.ll
@@ -0,0 +1,73 @@
+; RUN: llc -mtriple=xtensa -mcpu=esp32s3 -O2 -verify-machineinstrs < %s \
+; RUN: | FileCheck %s
+
+; Windowed ABI: any SP change after ENTRY must go through MOVSP so the
+; register-window save area is relocated (a plain ADD corrupts the caller
+; if a window spill lands between ENTRY and the update). Over-aligned
+; frames therefore realign via scratch ADD + MOVSP, computed off SP.
+
+declare void @use(ptr)
+
+define void @realign64() {
+; CHECK-LABEL: realign64:
+; CHECK: entry a1, 160
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: and a8, a1, a8
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: movsp a1, a8
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: retw
+entry:
+ %buf = alloca [64 x i8], align 64
+ call void @use(ptr %buf)
+ ret void
+}
+
+; With a frame pointer, FP (a7) still holds an incoming argument when the
+; misalignment is computed, so the AND must be sourced from SP (a1), and
+; FP is only materialized after the realignment.
+define void @realign64_fp(i32 %x0, i32 %x1, i32 %x2, i32 %x3, i32 %x4, i32 %x5, i32 %n) {
+; CHECK-LABEL: realign64_fp:
+; CHECK: entry a1, 160
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: and a8, a1, a8
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: movsp a1, a8
+; CHECK: or a7, a1, a1
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: retw
+entry:
+ %buf = alloca [64 x i8], align 64
+ %dyn = alloca i8, i32 %n
+ call void @use(ptr %buf)
+ call void @use(ptr %dyn)
+ ret void
+}
+
+; SP is only 16-byte aligned, so align 32 needs realignment too.
+define void @realign32() {
+; CHECK-LABEL: realign32:
+; CHECK: entry a1, 64
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: and a8, a1, a8
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: movsp a1, a8
+; CHECK-NOT: add{{(\.n)?}} a1, a1
+; CHECK: retw
+entry:
+ %buf = alloca [32 x i8], align 32
+ call void @use(ptr %buf)
+ ret void
+}
+
+; Align 16 needs no realignment: SP is already 16-byte aligned.
+define void @realign16() {
+; CHECK-LABEL: realign16:
+; CHECK: entry a1, 64
+; CHECK-NOT: movsp
+; CHECK: retw
+entry:
+ %buf = alloca [32 x i8], align 16
+ call void @use(ptr %buf)
+ ret void
+}
More information about the llvm-commits
mailing list