Repository navigation
[llvm][AArch64] Fix FPDiff rounding direction in non-sibcall tail calls - #223545
Conversation
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
|
@llvm/pr-subscribers-backend-aarch64 Author: Jon Roelofs (jroelofs) ChangesThis 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 4 Files Affected:
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 26ca67ed132e5..3d3138e53cc7e 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -10629,20 +10629,24 @@ AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
if (IsTailCall && !IsSibCall) {
unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
+ // In general, neither NumBytes nor NumReusableBytes is guaranteed to be
+ // aligned, so we round NumBytes up to the same residue mod StackAlign as
+ // NumReusableBytes, which keeps their difference (FPDiff) a multiple of
+ // StackAlign, and therefore preserve the required stack aligned going into
+ // the callee. When the callee's convention can guarantee TCO,
+ // LowerFormalArguments will have force-aligned the stack arg area for us
+ // already, so we can count on our own alignment of NumBytes below to result
+ // in an aligned FPDiff.
+ assert((!DoesCalleeRestoreStack(CallConv, TailCallOpt) ||
+ isAligned(StackAlign, NumReusableBytes)) &&
+ "expected LowerFormalArguments to force-align stack arg area");
+ NumBytes += offsetToAlignment(NumBytes - NumReusableBytes, 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;
- // Since callee will pop the argument stack as a tail call, we must keep the
- // popped size aligned to the stack alignment. Either or both of NumBytes
- // 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.
- 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.
if (FPDiff < 0 && FuncInfo->getTailCallReservedStack() < (unsigned)-FPDiff)
diff --git a/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll b/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll
index 9c449b12f2127..5fbb4ecfad5a2 100644
--- a/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll
+++ b/llvm/test/CodeGen/AArch64/sme-za-tailcall-fpdiff-align.ll
@@ -75,5 +75,40 @@ entry:
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
}
+
+; The caller's first 8 formal arguments land in registers, followed by i64 %9 on
+; the stack, which leaves those 8 bytes available for reuse. However the callee
+; only needs 1 byte of outgoing stack arg area for the i1. To keep the FPDiff
+; aligned, we must grow NumBytes by an additional 8 bytes, covering the entire
+; reusable area.
+declare void @callee_last_stack_arg_i1(i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, i64 %8, i1) "aarch64_inout_za"
+define void @caller_last_stack_arg_i64(i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, i64 %8, i64 %9) "aarch64_inout_za" uwtable {
+; CHECK-LABEL: caller_last_stack_arg_i64:
+; 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 w8, #1 ; =0x1
+; CHECK-NEXT: mov w0, #1 ; =0x1
+; CHECK-NEXT: mov w1, #2 ; =0x2
+; CHECK-NEXT: strb w8, [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: .cfi_def_cfa_offset 0
+; CHECK-NEXT: .cfi_restore w30
+; CHECK-NEXT: .cfi_restore w29
+; CHECK-NEXT: b _callee_last_stack_arg_i1
+entry:
+ tail call void @external_func()
+ tail call void @callee_last_stack_arg_i1(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i1 1) "aarch64_inout_za"
+ ret void
+}
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; GISEL: {{.*}}
diff --git a/llvm/test/CodeGen/AArch64/swifttail-fpdiff-align.ll b/llvm/test/CodeGen/AArch64/swifttail-fpdiff-align.ll
new file mode 100644
index 0000000000000..70c390b23de3c
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/swifttail-fpdiff-align.ll
@@ -0,0 +1,69 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=arm64-apple-ios -o - | FileCheck %s
+
+declare swiftcc ptr @swift_task_alloc(i64)
+
+; A musttail call modelling a swiftasync context reallocation thunk, whose 10
+; ordinary arguments overflow the 8 GPR argument registers, leaving 9 bytes of
+; stack arg area:
+; * Formal arguments %1-%8 are forwarded in registers x0-x7
+; * swiftasync and swiftself don't count agaginst the usual 8 GPR argument
+; list, and are are passed in dedicated registers (x22/x20)
+; * ptr %9 (8 bytes) and i1 %10 (1 byte) are passed on the stack, packed in 9
+; contiguous bytes
+; We round up that stack arg area to keep the FPDiff (and therefore the stack)
+; 16-byte aligned going into the callee.
+define swifttailcc void @thunk(ptr %1, ptr swiftasync %sa, i64 %2, i64 %3, i64 %4, ptr %5, i64 %6, ptr %7, ptr %8, ptr %9, i1 %10, ptr swiftself %self) #0 {
+; CHECK-LABEL: thunk:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: orr x29, x29, #0x1000000000000000
+; CHECK-NEXT: sub sp, sp, #96
+; CHECK-NEXT: stp x28, x27, [sp, #8] ; 16-byte Folded Spill
+; CHECK-NEXT: mov x27, x1
+; CHECK-NEXT: mov x28, x0
+; CHECK-NEXT: stp x26, x25, [sp, #24] ; 16-byte Folded Spill
+; CHECK-NEXT: mov w0, #64 ; =0x40
+; CHECK-NEXT: mov x25, x3
+; CHECK-NEXT: stp x24, x23, [sp, #40] ; 16-byte Folded Spill
+; CHECK-NEXT: mov x23, x5
+; CHECK-NEXT: mov x24, x4
+; CHECK-NEXT: stp x21, x19, [sp, #56] ; 16-byte Folded Spill
+; CHECK-NEXT: mov x19, x7
+; CHECK-NEXT: mov x21, x6
+; CHECK-NEXT: stp x29, x30, [sp, #80] ; 16-byte Folded Spill
+; CHECK-NEXT: add x29, sp, #80
+; CHECK-NEXT: mov x26, x2
+; CHECK-NEXT: str x22, [sp, #72]
+; CHECK-NEXT: ldr x1, [x20]
+; CHECK-NEXT: str x1, [sp] ; 8-byte Spill
+; CHECK-NEXT: bl _swift_task_alloc
+; CHECK-NEXT: ldp x29, x30, [sp, #80] ; 16-byte Folded Reload
+; CHECK-NEXT: mov x8, x0
+; CHECK-NEXT: str x0, [x22, #16]
+; CHECK-NEXT: mov x0, x28
+; CHECK-NEXT: mov x1, x27
+; CHECK-NEXT: mov x2, x26
+; CHECK-NEXT: mov x3, x25
+; CHECK-NEXT: mov x4, x24
+; CHECK-NEXT: mov x5, x23
+; CHECK-NEXT: mov x6, x21
+; CHECK-NEXT: mov x7, x19
+; CHECK-NEXT: ldp x21, x19, [sp, #56] ; 16-byte Folded Reload
+; CHECK-NEXT: mov x22, x8
+; CHECK-NEXT: ldp x24, x23, [sp, #40] ; 16-byte Folded Reload
+; CHECK-NEXT: ldr x8, [sp] ; 8-byte Reload
+; CHECK-NEXT: ldp x26, x25, [sp, #24] ; 16-byte Folded Reload
+; CHECK-NEXT: and x29, x29, #0xefffffffffffffff
+; CHECK-NEXT: ldp x28, x27, [sp, #8] ; 16-byte Folded Reload
+; CHECK-NEXT: add sp, sp, #96
+; CHECK-NEXT: br x8
+entry:
+ %oldCtx = getelementptr inbounds i8, ptr %sa, i64 16
+ %fn = load ptr, ptr %self, align 8
+ %newCtx = call swiftcc ptr @swift_task_alloc(i64 64)
+ store ptr %newCtx, ptr %oldCtx, align 8
+ musttail call swifttailcc void %fn(ptr %1, ptr swiftasync %newCtx, i64 %2, i64 %3, i64 %4, ptr %5, i64 %6, ptr %7, ptr %8, ptr %9, i1 %10, ptr swiftself %self)
+ ret void
+}
+
+attributes #0 = { nounwind "frame-pointer"="non-leaf-no-reserve" }
diff --git a/llvm/test/CodeGen/AArch64/tail-call-stack-args.ll b/llvm/test/CodeGen/AArch64/tail-call-stack-args.ll
index bbeea28f18bda..5bf3bffdac1a8 100644
--- a/llvm/test/CodeGen/AArch64/tail-call-stack-args.ll
+++ b/llvm/test/CodeGen/AArch64/tail-call-stack-args.ll
@@ -78,3 +78,24 @@ define void @wrapper_func_i64(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i3
tail call void @func_i64(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h, i32 %i, i64 %conv)
ret void
}
+
+; A musttail call with 9 bytes of stack arg area: the first 8 formal arguments
+; land in registers, followed by ptr %8 (8 bytes) and i1 %9 (1 byte) packed
+; tightly on the stack without padding. 9 is neither 8 nor 16-byte aligned, so
+; we need to round up NumBytes of frame size in order to keep the FPDiff aligned
+; for a stack-restoring (tailcc) callee.
+declare tailcc void @func_9_bytes_argspace(i64, i64, i64, i64, i64, i64, i64, i64, ptr, i1)
+
+define tailcc void @wrapper_func_9_bytes_argspace(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, ptr %8, i1 %9) {
+; SDAG-LABEL: wrapper_func_9_bytes_argspace:
+; SDAG: // %bb.0:
+; SDAG-NEXT: b func_9_bytes_argspace
+;
+; GI-LABEL: wrapper_func_9_bytes_argspace:
+; GI: // %bb.0:
+; GI-NEXT: ldrb w8, [sp, #8]
+; GI-NEXT: strb w8, [sp, #8]
+; GI-NEXT: b func_9_bytes_argspace
+ musttail call tailcc void @func_9_bytes_argspace(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, ptr %8, i1 %9)
+ ret void
+}
|
|
Can we add a test that uses a negative FPDiff? If I got this right this should be a tailcc caller without any reusable arguments on the stack and a callee with a < 16 bytes argument so you align to |
dtellenbach
left a comment
There was a problem hiding this comment.
LGTM with the small nits and request for another test.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
/cherry-pick ec4d25a |
|
Failed to cherry-pick: ec4d25a Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request |
|
…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)
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