[PATCH] D135687: [AArch64] Fix aligning the stack after calling __chkstk

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 09:03:34 PDT 2022


mstorsjo created this revision.
mstorsjo added reviewers: efriedma, zzheng.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

Whenever a call to __chkstk was made, the frame lowering previously
omitted the aligning (as NumBytes was reset to zero before doing
alignment).

This fixes https://github.com/llvm/llvm-project/issues/56182.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135687

Files:
  llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
  llvm/test/CodeGen/AArch64/win-align-chkstk.ll


Index: llvm/test/CodeGen/AArch64/win-align-chkstk.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/win-align-chkstk.ll
@@ -0,0 +1,27 @@
+; RUN: llc < %s -mtriple=aarch64-windows | FileCheck %s
+
+define dso_local void @func() {
+entry:
+  %buf = alloca [8192 x i8], align 32
+  %arraydecay = getelementptr inbounds [8192 x i8], ptr %buf, i64 0, i64 0
+  call void @other(ptr noundef %arraydecay)
+  ret void
+}
+
+declare dso_local void @other(ptr noundef)
+
+; CHECK-LABEL: func:
+; CHECK-NEXT: .seh_proc func
+; CHECK-NEXT: // %bb.0:
+; CHECK-NEXT: str x28, [sp, #-32]!
+; CHECK-NEXT: .seh_save_reg_x x28, 32
+; CHECK-NEXT: stp x29, x30, [sp, #8]
+; CHECK-NEXT: .seh_save_fplr 8
+; CHECK-NEXT: add x29, sp, #8
+; CHECK-NEXT: .seh_add_fp 8
+; CHECK-NEXT: .seh_endprologue
+; CHECK-NEXT: mov x15, #512
+; CHECK-NEXT: bl __chkstk
+; CHECK-NEXT: sub sp, sp, x15, lsl #4
+; CHECK-NEXT: mov x9, sp
+; CHECK-NEXT: and sp, x9, #0xffffffffffffffe0
Index: llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
===================================================================
--- llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
+++ llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
@@ -1638,6 +1638,10 @@
   if (EmitCFI)
     emitCalleeSavedGPRLocations(MBB, MBBI);
 
+  // Alignment is required for the parent frame, not the funclet
+  const bool NeedsRealignment =
+      NumBytes && !IsFunclet && RegInfo->hasStackRealignment(MF);
+
   if (windowsRequiresStackProbe(MF, NumBytes)) {
     uint64_t NumWords = NumBytes >> 4;
     if (NeedsWinCFI) {
@@ -1768,10 +1772,7 @@
                                        (int64_t)MFI.getStackSize() - NumBytes));
 
   // Allocate space for the rest of the frame.
-  if (NumBytes) {
-    // Alignment is required for the parent frame, not the funclet
-    const bool NeedsRealignment =
-        !IsFunclet && RegInfo->hasStackRealignment(MF);
+  if (NumBytes || NeedsRealignment) {
     unsigned scratchSPReg = AArch64::SP;
 
     if (NeedsRealignment) {
@@ -1780,7 +1781,7 @@
     }
 
     // If we're a leaf function, try using the red zone.
-    if (!canUseRedZone(MF)) {
+    if (NumBytes && !canUseRedZone(MF)) {
       // FIXME: in the case of dynamic re-alignment, NumBytes doesn't have
       // the correct value here, as NumBytes also includes padding bytes,
       // which shouldn't be counted here.
@@ -1790,6 +1791,11 @@
           false, NeedsWinCFI, &HasWinCFI, EmitCFI && !HasFP,
           SVEStackSize +
               StackOffset::getFixed((int64_t)MFI.getStackSize() - NumBytes));
+    } else if (NeedsRealignment) {
+      BuildMI(MBB, MBBI, DL, TII->get(AArch64::ADDXri), scratchSPReg)
+          .addReg(AArch64::SP)
+          .addImm(0)
+          .addImm(0);
     }
     if (NeedsRealignment) {
       const unsigned NrBitsToZero = Log2(MFI.getMaxAlign());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135687.466834.patch
Type: text/x-patch
Size: 2880 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221011/02677cc0/attachment.bin>


More information about the llvm-commits mailing list