Skip to content

Commit 53b7875

Browse files
TNorthoveraschwaighofer
authored andcommitted
Merge pull request #3191 from apple/eng/PR-79357449-2
SwiftAsync: use runtime-provided flag for extended frame if back-deploying
1 parent edd85e5 commit 53b7875

File tree

14 files changed

+134
-20
lines changed

14 files changed

+134
-20
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,11 +1110,23 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
11101110
// ORR is sufficient, it is assumed a Swift kernel would initialize the TBI
11111111
// bits so that is still true.
11121112
if (HasFP && AFI->hasSwiftAsyncContext()) {
1113-
// ORR x29, x29, #0x1000_0000_0000_0000
1114-
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXri), AArch64::FP)
1115-
.addUse(AArch64::FP)
1116-
.addImm(0x1100)
1117-
.setMIFlag(MachineInstr::FrameSetup);
1113+
if (Subtarget.swiftAsyncContextIsDynamicallySet()) {
1114+
// The special symbol below is absolute and has a *value* that can be
1115+
// combined with the frame pointer to signal an extended frame.
1116+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LOADgot), AArch64::X16)
1117+
.addExternalSymbol("swift_async_extendedFramePointerFlags",
1118+
AArch64II::MO_GOT);
1119+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::FP)
1120+
.addUse(AArch64::FP)
1121+
.addUse(AArch64::X16)
1122+
.addImm(Subtarget.isTargetILP32() ? 32 : 0);
1123+
} else {
1124+
// ORR x29, x29, #0x1000_0000_0000_0000
1125+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXri), AArch64::FP)
1126+
.addUse(AArch64::FP)
1127+
.addImm(0x1100)
1128+
.setMIFlag(MachineInstr::FrameSetup);
1129+
}
11181130
}
11191131

11201132
// All calls are tail calls in GHC calling conv, and functions have no

llvm/lib/Target/AArch64/AArch64Subtarget.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,31 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
566566
}
567567
}
568568

569+
/// Return whether FrameLowering should always set the "extended frame
570+
/// present" bit in FP, or set it based on a symbol in the runtime.
571+
bool swiftAsyncContextIsDynamicallySet() const {
572+
// Older OS versions (particularly system unwinders) are confused by the
573+
// Swift extended frame, so when building code that might be run on them we
574+
// must dynamically query the concurrency library to determine whether
575+
// extended frames should be flagged as present.
576+
const Triple &TT = getTargetTriple();
577+
578+
unsigned Major, Minor, Micro;
579+
TT.getOSVersion(Major, Minor, Micro);
580+
switch(TT.getOS()) {
581+
default:
582+
return false;
583+
case Triple::IOS:
584+
case Triple::TvOS:
585+
return Major < 15;
586+
case Triple::WatchOS:
587+
return Major < 8;
588+
case Triple::MacOSX:
589+
case Triple::Darwin:
590+
return Major < 12;
591+
}
592+
}
593+
569594
void mirFileLoaded(MachineFunction &MF) const override;
570595

571596
// Return the known range for the bit length of SVE data registers. A value

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,11 +1342,23 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
13421342
unsigned StackProbeSize = STI.getTargetLowering()->getStackProbeSize(MF);
13431343

13441344
if (HasFP && X86FI->hasSwiftAsyncContext()) {
1345-
BuildMI(MBB, MBBI, DL, TII.get(X86::BTS64ri8),
1346-
MachineFramePtr)
1347-
.addUse(MachineFramePtr)
1348-
.addImm(60)
1349-
.setMIFlag(MachineInstr::FrameSetup);
1345+
if (STI.swiftAsyncContextIsDynamicallySet()) {
1346+
// The special symbol below is absolute and has a *value* suitable to be
1347+
// combined with the frame pointer directly.
1348+
BuildMI(MBB, MBBI, DL, TII.get(X86::OR64rm), MachineFramePtr)
1349+
.addUse(MachineFramePtr)
1350+
.addUse(X86::RIP)
1351+
.addImm(1)
1352+
.addUse(X86::NoRegister)
1353+
.addExternalSymbol("swift_async_extendedFramePointerFlags",
1354+
X86II::MO_GOTPCREL)
1355+
.addUse(X86::NoRegister);
1356+
} else {
1357+
BuildMI(MBB, MBBI, DL, TII.get(X86::BTS64ri8), MachineFramePtr)
1358+
.addUse(MachineFramePtr)
1359+
.addImm(60)
1360+
.setMIFlag(MachineInstr::FrameSetup);
1361+
}
13501362
}
13511363

13521364
// Re-align the stack on 64-bit if the x86-interrupt calling convention is

llvm/lib/Target/X86/X86Subtarget.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,31 @@ class X86Subtarget final : public X86GenSubtargetInfo {
924924
/// Return true if the subtarget allows calls to immediate address.
925925
bool isLegalToCallImmediateAddr() const;
926926

927+
/// Return whether FrameLowering should always set the "extended frame
928+
/// present" bit in FP, or set it based on a symbol in the runtime.
929+
bool swiftAsyncContextIsDynamicallySet() const {
930+
// Older OS versions (particularly system unwinders) are confused by the
931+
// Swift extended frame, so when building code that might be run on them we
932+
// must dynamically query the concurrency library to determine whether
933+
// extended frames should be flagged as present.
934+
const Triple &TT = getTargetTriple();
935+
936+
unsigned Major, Minor, Micro;
937+
TT.getOSVersion(Major, Minor, Micro);
938+
switch(TT.getOS()) {
939+
default:
940+
return false;
941+
case Triple::IOS:
942+
case Triple::TvOS:
943+
return Major < 15;
944+
case Triple::WatchOS:
945+
return Major < 8;
946+
case Triple::MacOSX:
947+
case Triple::Darwin:
948+
return Major < 12;
949+
}
950+
}
951+
927952
/// If we are using indirect thunks, we need to expand indirectbr to avoid it
928953
/// lowering to an actual indirect jump.
929954
bool enableIndirectBrExpand() const override {

llvm/test/CodeGen/AArch64/swift-async.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
; RUN: llc -mtriple=arm64-apple-ios %s -o - | FileCheck %s --check-prefixes=CHECK-NOAUTH,CHECK
2-
; RUN: llc -mtriple=arm64-apple-ios -mcpu=apple-a13 %s -o - | FileCheck %s --check-prefixes=CHECK-NOAUTH,CHECK
3-
; RUN: llc -mtriple=arm64e-apple-ios %s -o - | FileCheck %s --check-prefixes=CHECK-AUTH,CHECK
1+
; RUN: llc -mtriple=arm64-apple-ios15 %s -o - | FileCheck %s --check-prefixes=CHECK-NOAUTH,CHECK
2+
; RUN: llc -mtriple=arm64-apple-ios15 -mcpu=apple-a13 %s -o - | FileCheck %s --check-prefixes=CHECK-NOAUTH,CHECK
3+
; RUN: llc -mtriple=arm64e-apple-ios15 %s -o - | FileCheck %s --check-prefixes=CHECK-AUTH,CHECK
44

55
; Important details in prologue:
66
; * x22 is stored just below x29
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llc -mtriple arm64-apple-ios15.0.0 %s -o - | FileCheck %s --check-prefix=CHECK-STATIC
2+
; RUN: llc -mtriple arm64-apple-ios14.9.0 %s -o - | FileCheck %s --check-prefix=CHECK-DYNAMIC
3+
; RUN: llc -mtriple arm64-apple-tvos15.0.0 %s -o - | FileCheck %s --check-prefix=CHECK-STATIC
4+
; RUN: llc -mtriple arm64-apple-tvos14.9.0 %s -o - | FileCheck %s --check-prefix=CHECK-DYNAMIC
5+
; RUN: llc -mtriple arm64-apple-macosx12.0.0 %s -o - | FileCheck %s --check-prefix=CHECK-STATIC
6+
; RUN: llc -mtriple arm64-apple-macosx11.9.0 %s -o - | FileCheck %s --check-prefix=CHECK-DYNAMIC
7+
; RUN: llc -mtriple arm64_32-apple-watchos8.0.0 %s -o - | FileCheck %s --check-prefix=CHECK-STATIC
8+
; RUN: llc -mtriple arm64_32-apple-watchos7.9.0 %s -o - | FileCheck %s --check-prefix=CHECK-DYNAMIC-32
9+
10+
; CHECK-STATIC-LABEL: foo:
11+
; CHECK-STATIC: orr x29, x29, #0x1000000000000000
12+
13+
; CHECK-DYNAMIC-LABEL: foo:
14+
; CHECK-DYNAMIC: adrp x16, _swift_async_extendedFramePointerFlags@GOTPAGE
15+
; CHECK-DYNAMIC: ldr x16, [x16, _swift_async_extendedFramePointerFlags@GOTPAGEOFF]
16+
; CHECK-DYNAMIC: orr x29, x29, x16
17+
18+
; CHECK-DYNAMIC-32-LABEL: foo:
19+
; CHECK-DYNAMIC-32: adrp x16, _swift_async_extendedFramePointerFlags@GOTPAGE
20+
; CHECK-DYNAMIC-32: ldr w16, [x16, _swift_async_extendedFramePointerFlags@GOTPAGEOFF]
21+
; CHECK-DYNAMIC-32: orr x29, x29, x16, lsl #32
22+
23+
define void @foo(i8* swiftasync) "frame-pointer"="all" {
24+
ret void
25+
}

llvm/test/CodeGen/X86/swift-async.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -mtriple=x86_64-apple-darwin %s -o - | FileCheck %s
1+
; RUN: llc -mtriple=x86_64-apple-macosx12.0 %s -o - | FileCheck %s
22
; RUN: llc -mtriple=i686-apple-darwin %s -o - | FileCheck %s --check-prefix=CHECK-32
33

44

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: llc -mtriple x86_64-apple-macosx12.0.0 %s -o - | FileCheck %s --check-prefix=CHECK-STATIC
2+
; RUN: llc -mtriple x86_64-apple-macosx11.9.0 %s -o - | FileCheck %s --check-prefix=CHECK-DYNAMIC
3+
4+
; CHECK-STATIC-LABEL: foo:
5+
; CHECK-STATIC: btsq $60, %rbp
6+
7+
; CHECK-DYNAMIC-LABEL: foo:
8+
; CHECK-DYNAMIC: orq _swift_async_extendedFramePointerFlags@GOTPCREL(%rip), %rbp
9+
10+
define void @foo(i8* swiftasync) "frame-pointer"="all" {
11+
ret void
12+
}

llvm/test/lit.cfg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def get_asan_rtlib():
118118
asan_rtlib = get_asan_rtlib()
119119
if asan_rtlib:
120120
ld64_cmd = 'DYLD_INSERT_LIBRARIES={} {}'.format(asan_rtlib, ld64_cmd)
121+
if config.osx_sysroot:
122+
ld64_cmd = '{} -syslibroot {}'.format(ld64_cmd, config.osx_sysroot)
121123

122124
ocamlc_command = '%s ocamlc -cclib -L%s %s' % (
123125
config.ocamlfind_executable, config.llvm_lib_dir, config.ocaml_flags)

llvm/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ config.lit_tools_dir = path(r"@LLVM_LIT_TOOLS_DIR@")
1515
config.python_executable = "@Python3_EXECUTABLE@"
1616
config.gold_executable = "@GOLD_EXECUTABLE@"
1717
config.ld64_executable = "@LD64_EXECUTABLE@"
18+
config.osx_sysroot = "@CMAKE_OSX_SYSROOT@"
1819
config.ocamlfind_executable = "@OCAMLFIND@"
1920
config.have_ocamlopt = @HAVE_OCAMLOPT@
2021
config.have_ocaml_ounit = @HAVE_OCAML_OUNIT@

0 commit comments

Comments
 (0)