Skip to content

Reland "[llvm][AArch64] Ensure stack alignment in non-sibcall tail calls with FPDiff" - #220406

Merged
jroelofs merged 6 commits into
llvm:mainfrom
jroelofs:jroelofs/rdar184474075
Sep 11, 2026
Merged

jroelofs merged 6 commits into
llvm:mainfrom
jroelofs:jroelofs/rdar184474075

Conversation

@jroelofs

@jroelofs jroelofs commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

When a caller has a non-multiple of the minimum stack alignment more argument
stack space than the callee it is tail calling, we need to ensure that the
resulting FPDiff's magnitude has been rounded up to a multiple of the platform's
minimum stack alignment.

rdar://184474075

In the re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after FPDiff
had been re-aligned.

…lls with FPDiff"

When a caller has a non-multiple of the minimum stack alignment more argument
stack space than the callee it is tail calling, we need to ensure that the
resulting FPDiff's magnitude has been rounded up to a multiple of the platform's
minimum stack alignment.

rdar://184474075

In the re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after FPDiff
had been re-aligned.
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-backend-aarch64

Author: Jon Roelofs (jroelofs)

Changes

When a caller has a non-multiple of the minimum stack alignment more argument
stack space than the callee it is tail calling, we need to ensure that the
resulting FPDiff's magnitude has been rounded up to a multiple of the platform's
minimum stack alignment.

rdar://184474075

In the re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after FPDiff
had been re-aligned.


Full diff: https://github.com/llvm/llvm-project/pull/220406.diff

2 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+3-2)
  • (modified) llvm/test/CodeGen/AArch64/arm64ec-varargs.ll (+31)
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 721ec0222cee0..ec68e8c55323b 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -10563,8 +10563,9 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
     // and NumReusableBytes may not have been aligned, so we further increase by
     // the amount needed to keep FPDiff aligned, and therefore preserve the
     // required alignment going into the callee.
-    FPDiff -= offsetToAlignment(FPDiff, StackAlign);
-    NumBytes += offsetToAlignment(FPDiff, StackAlign);
+    uint64_t Realign = offsetToAlignment(FPDiff, StackAlign);
+    FPDiff -= Realign;
+    NumBytes += Realign;
 
     // Update the required reserved area if this is the tail call requiring the
     // most argument stack space.
diff --git a/llvm/test/CodeGen/AArch64/arm64ec-varargs.ll b/llvm/test/CodeGen/AArch64/arm64ec-varargs.ll
index 979e09cfb5fac..6f13b9606afeb 100644
--- a/llvm/test/CodeGen/AArch64/arm64ec-varargs.ll
+++ b/llvm/test/CodeGen/AArch64/arm64ec-varargs.ll
@@ -157,3 +157,34 @@ define void @varargs_thunk(ptr noundef %0, ...) "thunk" {
   musttail call void (ptr, ...) %vtablefn(ptr noundef %0, ...)
   ret void
 }
+
+declare tailcc void @callee_9_variadic(i64, i64, i64, i64, i64, i64, i64, i64, i64, ...)
+
+define tailcc void @caller_8_args(i64, i64, i64, i64, i64, i64, i64, i64) {
+; CHECK-LABEL: caller_8_args:
+; CHECK:       .seh_proc caller_8_args
+; CHECK-NEXT:  // %bb.0: // %entry
+; CHECK-NEXT:    sub sp, sp, #16
+; CHECK-NEXT:    .seh_stackalloc 16
+; CHECK-NEXT:    .seh_endprologue
+; CHECK-NEXT:    mov w8, #9 // =0x9
+; CHECK-NEXT:    mov x4, sp
+; CHECK-NEXT:    mov w0, #1 // =0x1
+; CHECK-NEXT:    mov w1, #2 // =0x2
+; CHECK-NEXT:    mov w2, #3 // =0x3
+; CHECK-NEXT:    mov w3, #4 // =0x4
+; CHECK-NEXT:    mov w6, #7 // =0x7
+; CHECK-NEXT:    mov w7, #8 // =0x8
+; CHECK-NEXT:    mov w5, #16 // =0x10
+; CHECK-NEXT:    str x8, [sp]
+; CHECK-NEXT:    .weak_anti_dep callee_9_variadic
+; CHECK-NEXT:  callee_9_variadic = "#callee_9_variadic"
+; CHECK-NEXT:    .weak_anti_dep "#callee_9_variadic"
+; CHECK-NEXT:  "#callee_9_variadic" = callee_9_variadic
+; CHECK-NEXT:    b "#callee_9_variadic"
+; CHECK-NEXT:    .seh_endfunclet
+; CHECK-NEXT:    .seh_endproc
+entry:
+  tail call tailcc void (i64, i64, i64, i64, i64, i64, i64, i64, i64, ...) @callee_9_variadic(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9)
+  ret void
+}

jroelofs added a commit that referenced this pull request Sep 1, 2026
…lls with FPDiff" (#220419)

Reverts #217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
#220406 for the re-land.
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request Sep 1, 2026
…all tail calls with FPDiff" (#220419)

Reverts llvm/llvm-project#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm/llvm-project#220406 for the re-land.
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 1, 2026
…all tail calls with FPDiff" (#220419)

Reverts llvm/llvm-project#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm/llvm-project#220406 for the re-land.
nekoshirro pushed a commit to nekoshirro/Alchemist-LLVM that referenced this pull request Sep 2, 2026
…lls with FPDiff" (#220419)

Reverts llvm/llvm-project#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm/llvm-project#220406 for the re-land.
Signed-off-by: Hafidz Muzakky <[email protected]>
Iasonaskrpr pushed a commit to Iasonaskrpr/llvm-project that referenced this pull request Sep 4, 2026
…lls with FPDiff" (llvm#220419)

Reverts llvm#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm#220406 for the re-land.
asudarsa-qti pushed a commit to asudarsa-qti/llvm-project that referenced this pull request Sep 4, 2026
…lls with FPDiff" (llvm#220419)

Reverts llvm#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm#220406 for the re-land.
addmisol pushed a commit to addmisol/llvm-project2 that referenced this pull request Sep 6, 2026
…lls with FPDiff" (llvm#220419)

Reverts llvm#217156

I noticed an issue with it right after it landed, and to simplify
cherry-picking, I'm going to revert and re-land. See:
llvm#220406 for the re-land.
@dtellenbach
dtellenbach self-requested a review September 11, 2026 02:48

@dtellenbach dtellenbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jroelofs
jroelofs merged commit 393710b into llvm:main Sep 11, 2026
12 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Sep 11, 2026
@jroelofs

Copy link
Copy Markdown
Contributor Author

/cherry-pick 393710b

@jroelofs
jroelofs deleted the jroelofs/rdar184474075 branch September 11, 2026 21:08
@llvmbot

llvmbot commented Sep 11, 2026

Copy link
Copy Markdown
Member

/pull-request #223063

jroelofs added a commit to jroelofs/llvm-project that referenced this pull request Sep 14, 2026
This fixes another subtle bug in frame accounting (see: llvm#217156 / llvm#220406), for
tail calls that have a non-multiple of 16 bytes worth of stack arg area, and
need that stack arg re-use to be increased to cover the alignment requirement.
This is best illustrated with callers containing 8 formal arguments covering the
first 8 GPRs (x0-x7), followed by 9 bytes of argument passed on the stack.

In a callee-pops tail call (e.g. tailcc/swifttailcc), the set of reusable stack
arg area bytes has already been sufficiently aligned by LowerFormalArguments, so
growing NumBytes up to StackAlign is enough to consume that excess. Otherwise
(e.g. a plain C-convention call, forced off the sibcall path, as in the
aarch64_inout_za tests), we can't rely on either having been pre-aligned, so we
round NumBytes up to the same residue mod StackAlign as NumReusableBytes, which
cancels the residue out of their difference (FPDiff), thus keeping the stack
aligned going into the callee.

Concretely, llvm#220406 computed the Realign distance needed to round FPDiff up to
the next multiple of StackAlign, then subtracted that from FPDiff. Subtracting
that round-up distance overshoots rounding down to the nearest multiple, and in
general arrives at a value that may not even be a multiple of StackAlign. For
example, in @caller_last_stack_arg_i64 we had NumReusableBytes=8 and NumBytes=1
for an FPDiff of 7, which computed Realign=9, FPDiff=-2, NumBytes=10 instead of
the intended NumBytes=8, FPDiff=0.

rdar://187331378
aobolensk pushed a commit to aobolensk/llvm-project that referenced this pull request Sep 15, 2026
…lls with FPDiff" (llvm#220406)

When a caller has a non-multiple of the minimum stack alignment more
argument stack space than the callee it is tail calling, we need to ensure that
the resulting FPDiff's magnitude has been rounded up to a multiple of the
platform's minimum stack alignment.

In this re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after
FPDiff had been re-aligned.

rdar://184474075
anilkund pushed a commit to anilkund/llvm-project that referenced this pull request Sep 16, 2026
…lls with FPDiff" (llvm#220406)

When a caller has a non-multiple of the minimum stack alignment more
argument stack space than the callee it is tail calling, we need to ensure that
the resulting FPDiff's magnitude has been rounded up to a multiple of the
platform's minimum stack alignment.

In this re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after
FPDiff had been re-aligned.

rdar://184474075
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Sep 16, 2026
…lls with FPDiff" (llvm#220406)

When a caller has a non-multiple of the minimum stack alignment more
argument stack space than the callee it is tail calling, we need to ensure that
the resulting FPDiff's magnitude has been rounded up to a multiple of the
platform's minimum stack alignment.

In this re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after
FPDiff had been re-aligned.

rdar://184474075
(cherry picked from commit 393710b)
jroelofs added a commit that referenced this pull request Sep 16, 2026
…ls (#223545)

This fixes another subtle bug in frame accounting (see: #217156 /
#220406), for tail calls that have a non-multiple of 16 bytes worth of
stack arg area, and need that stack arg re-use to be increased to cover
the alignment requirement. This is best illustrated with callers
containing 8 formal arguments covering the first 8 GPRs (x0-x7),
followed by 9 bytes of argument passed on the stack.

In a callee-pops tail call (e.g. tailcc/swifttailcc), the set of
reusable stack arg area bytes has already been sufficiently aligned by
LowerFormalArguments, so growing NumBytes up to StackAlign is enough to
consume that excess. Otherwise (e.g. a plain C-convention call, forced
off the sibcall path, as in the aarch64_inout_za tests), we can't rely
on either having been pre-aligned, so we round NumBytes up to the same
residue mod StackAlign as NumReusableBytes, which cancels the residue
out of their difference (FPDiff), thus keeping the stack aligned going
into the callee.

Concretely, #220406 computed the Realign distance needed to round FPDiff
up to the next multiple of StackAlign, then subtracted that from FPDiff.
Subtracting that round-up distance overshoots rounding down to the
nearest multiple, and in general arrives at a value that may not even be
a multiple of StackAlign. For example, in @caller_last_stack_arg_i64 we
had NumReusableBytes=8 and NumBytes=1 for an FPDiff of 7, which computed
Realign=9, FPDiff=-2, NumBytes=10 instead of the intended NumBytes=8,
FPDiff=0.

rdar://187331378
yebinchon pushed a commit to yebinchon/llvm-project that referenced this pull request Sep 18, 2026
…ls (llvm#223545)

This fixes another subtle bug in frame accounting (see: llvm#217156 /
llvm#220406), for tail calls that have a non-multiple of 16 bytes worth of
stack arg area, and need that stack arg re-use to be increased to cover
the alignment requirement. This is best illustrated with callers
containing 8 formal arguments covering the first 8 GPRs (x0-x7),
followed by 9 bytes of argument passed on the stack.

In a callee-pops tail call (e.g. tailcc/swifttailcc), the set of
reusable stack arg area bytes has already been sufficiently aligned by
LowerFormalArguments, so growing NumBytes up to StackAlign is enough to
consume that excess. Otherwise (e.g. a plain C-convention call, forced
off the sibcall path, as in the aarch64_inout_za tests), we can't rely
on either having been pre-aligned, so we round NumBytes up to the same
residue mod StackAlign as NumReusableBytes, which cancels the residue
out of their difference (FPDiff), thus keeping the stack aligned going
into the callee.

Concretely, llvm#220406 computed the Realign distance needed to round FPDiff
up to the next multiple of StackAlign, then subtracted that from FPDiff.
Subtracting that round-up distance overshoots rounding down to the
nearest multiple, and in general arrives at a value that may not even be
a multiple of StackAlign. For example, in @caller_last_stack_arg_i64 we
had NumReusableBytes=8 and NumBytes=1 for an FPDiff of 7, which computed
Realign=9, FPDiff=-2, NumBytes=10 instead of the intended NumBytes=8,
FPDiff=0.

rdar://187331378
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Sep 18, 2026
…lls with FPDiff" (llvm#220406)

When a caller has a non-multiple of the minimum stack alignment more
argument stack space than the callee it is tail calling, we need to ensure that
the resulting FPDiff's magnitude has been rounded up to a multiple of the
platform's minimum stack alignment.

In this re-land there is a fixup to 234ce03,
which, because of the wrong order, wasn't actually moving NumBytes after
FPDiff had been re-aligned.

rdar://184474075
(cherry picked from commit 393710b)
jroelofs added a commit to jroelofs/llvm-project that referenced this pull request Sep 18, 2026
…ls (llvm#223545)

This fixes another subtle bug in frame accounting (see: llvm#217156 /
llvm#220406), for tail calls that have a non-multiple of 16 bytes worth of
stack arg area, and need that stack arg re-use to be increased to cover
the alignment requirement. This is best illustrated with callers
containing 8 formal arguments covering the first 8 GPRs (x0-x7),
followed by 9 bytes of argument passed on the stack.

In a callee-pops tail call (e.g. tailcc/swifttailcc), the set of
reusable stack arg area bytes has already been sufficiently aligned by
LowerFormalArguments, so growing NumBytes up to StackAlign is enough to
consume that excess. Otherwise (e.g. a plain C-convention call, forced
off the sibcall path, as in the aarch64_inout_za tests), we can't rely
on either having been pre-aligned, so we round NumBytes up to the same
residue mod StackAlign as NumReusableBytes, which cancels the residue
out of their difference (FPDiff), thus keeping the stack aligned going
into the callee.

Concretely, llvm#220406 computed the Realign distance needed to round FPDiff
up to the next multiple of StackAlign, then subtracted that from FPDiff.
Subtracting that round-up distance overshoots rounding down to the
nearest multiple, and in general arrives at a value that may not even be
a multiple of StackAlign. For example, in @caller_last_stack_arg_i64 we
had NumReusableBytes=8 and NumBytes=1 for an FPDiff of 7, which computed
Realign=9, FPDiff=-2, NumBytes=10 instead of the intended NumBytes=8,
FPDiff=0.

rdar://187331378
(cherry picked from commit ec4d25a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants