Repository navigation
[llvm][AArch64] Ensure stack alignment in non-sibcall tail calls with FPDiff - #217156
Conversation
|
@llvm/pr-subscribers-backend-aarch64 Author: Jon Roelofs (jroelofs) ChangesWhen 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 2 Files Affected:
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 5957d51005386..c6bbc950a3082 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -9680,13 +9680,15 @@ SDValue AArch64TargetLowering::LowerFormalArguments(
unsigned StackArgSize = CCInfo.getStackSize();
bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) {
+ const Align StackAlign = Subtarget->getFrameLowering()->getStackAlign();
+
// This is a non-standard ABI so by fiat I say we're allowed to make full
- // use of the stack area to be popped, which must be aligned to 16 bytes in
- // any case:
- StackArgSize = alignTo(StackArgSize, 16);
+ // use of the stack area to be popped, which must be aligned to the stack
+ // alignment in any case:
+ StackArgSize = alignTo(StackArgSize, StackAlign);
// If we're expected to restore the stack (e.g. fastcc) then we'll be adding
- // a multiple of 16.
+ // a multiple of the stack alignment.
FuncInfo->setArgumentStackToRestore(StackArgSize);
// This realignment carries over to the available bytes below. Our own
@@ -10519,19 +10521,33 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
// caller will deallocate the entire stack and the callee still expects its
// arguments to begin at SP+0. Completely unused for non-tail calls.
int FPDiff = 0;
+ const Align StackAlign = Subtarget->getFrameLowering()->getStackAlign();
if (IsTailCall && !IsSibCall) {
unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
// Since callee will pop argument stack as a tail call, we must keep the
- // popped size 16-byte aligned.
- NumBytes = alignTo(NumBytes, 16);
+ // popped size aligned to the stack alignment.
+ NumBytes = alignTo(NumBytes, StackAlign);
// FPDiff will be negative if this tail call requires more space than we
// would automatically have in our incoming argument space. Positive if we
// can actually shrink the stack.
FPDiff = NumReusableBytes - NumBytes;
+ // Round FPDiff away from zero to a multiple of the minimum stack alignment.
+ // While NumBytes was already aligned above, NumReusableBytes (which
+ // represents the incoming argument area, only guaranteed to be aligned in
+ // caller pops conventions e.g. fastcc/swifttailcc) may not be. We round
+ // FPDiff's magnitude to the next multiple of the minimum stack alignment so
+ // the delta applied for the tail call satisfies the same constraint. A
+ // positive FPDiff at most claims a few more bytes of the caller's own
+ // incoming argument area (which is already suitably aligned), and a
+ // negative FPDiff at most grows the reserved tail-call stack slightly.
+ FPDiff = FPDiff < 0 ? -alignTo(-FPDiff, StackAlign)
+ : alignTo(FPDiff, StackAlign);
+
+
// Update the required reserved area if this is the tail call requiring the
// most argument stack space.
if (FPDiff < 0 && FuncInfo->getTailCallReservedStack() < (unsigned)-FPDiff)
@@ -11089,8 +11105,9 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
MF.getFunction().getParent()->getModuleFlag("import-call-optimization"))
DAG.addCalledGlobal(Chain.getNode(), CalledGlobal, OpFlags);
- uint64_t CalleePopBytes =
- DoesCalleeRestoreStack(CallConv, TailCallOpt) ? alignTo(NumBytes, 16) : 0;
+ uint64_t CalleePopBytes = DoesCalleeRestoreStack(CallConv, TailCallOpt)
+ ? alignTo(NumBytes, StackAlign)
+ : 0;
Chain = DAG.getCALLSEQ_END(Chain, NumBytes, CalleePopBytes, InGlue, DL);
InGlue = Chain.getValue(1);
diff --git a/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll b/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll
new file mode 100644
index 0000000000000..82a6d5a0617fb
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll
@@ -0,0 +1,72 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=arm64-apple-ios -mattr=+sme -o - | FileCheck %s
+; RUN: llc < %s -mtriple=arm64-apple-ios -mattr=+sme -global-isel-abort=2 -o - 2>&1 | FileCheck %s --check-prefix=CHECK,GISEL
+
+; GlobalISel doesn't implement SME yet, but once it does, we should be mindful of this case.
+; GISEL: warning: Instruction selection used fallback path for caller_more_args
+; GISEL: warning: Instruction selection used fallback path for caller_same_args
+
+declare void @external_func() "aarch64_inout_za"
+
+; ZA state being live for both caller and callee forces the non-sibcall tail
+; call fast path. FPDiff = (caller's incoming argument stack) - (callee's
+; outgoing argument stack) can come out either positive or negative, and in
+; either case its magnitude must be rounded away from zero to a multiple of
+; 16 to keep the stack pointer 16-byte aligned at all times.
+declare void @callee_fewer_args(i64, i64, i64, i64, i64, i64, i64, i64) "aarch64_inout_za"
+define void @caller_more_args(i64, i64, i64, i64, i64, i64, i64, i64, i64) "aarch64_inout_za" {
+; CHECK-LABEL: caller_more_args:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: stp x29, x30, [sp, #-16]! ; 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: .cfi_offset w30, -8
+; CHECK-NEXT: .cfi_offset w29, -16
+; CHECK-NEXT: bl _external_func
+; 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 w4, #5 ; =0x5
+; CHECK-NEXT: mov w5, #6 ; =0x6
+; CHECK-NEXT: mov w6, #7 ; =0x7
+; CHECK-NEXT: mov w7, #8 ; =0x8
+; CHECK-NEXT: ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-NEXT: add sp, sp, #16
+; CHECK-NEXT: b _callee_fewer_args
+entry:
+ tail call void @external_func()
+ tail call void @callee_fewer_args(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8) "aarch64_inout_za"
+ ret void
+}
+
+; Likewise for a callee that has the same amount of argument stack as the
+; caller, the FPDiff becomes negative, and must be rounded away from zero to the
+; next multiple of 16 to keep the stack aligned for the callee.
+declare void @callee_same_args(i64, i64, i64, i64, i64, i64, i64, i64, i64) "aarch64_inout_za"
+define void @caller_same_args(i64, i64, i64, i64, i64, i64, i64, i64, i64) "aarch64_inout_za" {
+; CHECK-LABEL: caller_same_args:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: stp x29, x30, [sp, #-32]! ; 16-byte Folded Spill
+; CHECK-NEXT: .cfi_def_cfa_offset 32
+; CHECK-NEXT: .cfi_offset w30, -24
+; CHECK-NEXT: .cfi_offset w29, -32
+; CHECK-NEXT: bl _external_func
+; CHECK-NEXT: mov w8, #9 ; =0x9
+; CHECK-NEXT: mov w0, #1 ; =0x1
+; CHECK-NEXT: mov w1, #2 ; =0x2
+; CHECK-NEXT: str x8, [sp, #16]
+; CHECK-NEXT: mov w2, #3 ; =0x3
+; CHECK-NEXT: mov w3, #4 ; =0x4
+; CHECK-NEXT: mov w4, #5 ; =0x5
+; CHECK-NEXT: mov w5, #6 ; =0x6
+; CHECK-NEXT: mov w6, #7 ; =0x7
+; CHECK-NEXT: mov w7, #8 ; =0x8
+; CHECK-NEXT: ldp x29, x30, [sp], #16 ; 16-byte Folded Reload
+; CHECK-NEXT: b _callee_same_args
+entry:
+ tail call void @external_func()
+ tail call void @callee_same_args(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9) "aarch64_inout_za"
+ ret void
+}
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; GISEL: {{.*}}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
0dec7bc to
0edc262
Compare
… 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
0edc262 to
4c42713
Compare
🪟 Windows x64 Test Results
All executed tests passed, but another part of the build failed. Click on a failure below to see the details. [code=4294967295] bin/mlir-opt.exeIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/223/builds/10637 Here is the relevant piece of the build log for the reference |
|
/cherry-pick 234ce03 |
|
/pull-request #220313 |
| // 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); |
There was a problem hiding this comment.
Oops, this should have been done in the other order.
…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.
…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.
…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]>
… FPDiff (llvm#217156) 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
…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.
… FPDiff (llvm#217156) 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
…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.
… FPDiff (llvm#217156) 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
…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.
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
…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
…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
…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)
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