Skip to content

Commit b63e74b

Browse files
authored
Merge pull request #7533 from hjyamauchi/backport-66967
[AArch64][Win] Emit SEH instructions for the swift async context-related instructions in the prologue and the epilogue.
2 parents 570c7a4 + 09294ec commit b63e74b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,10 +1483,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14831483
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LOADgot), AArch64::X16)
14841484
.addExternalSymbol("swift_async_extendedFramePointerFlags",
14851485
AArch64II::MO_GOT);
1486+
if (NeedsWinCFI) {
1487+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1488+
.setMIFlags(MachineInstr::FrameSetup);
1489+
HasWinCFI = true;
1490+
}
14861491
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::FP)
14871492
.addUse(AArch64::FP)
14881493
.addUse(AArch64::X16)
14891494
.addImm(Subtarget.isTargetILP32() ? 32 : 0);
1495+
if (NeedsWinCFI) {
1496+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1497+
.setMIFlags(MachineInstr::FrameSetup);
1498+
HasWinCFI = true;
1499+
}
14901500
break;
14911501
}
14921502
[[fallthrough]];
@@ -1497,6 +1507,11 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14971507
.addUse(AArch64::FP)
14981508
.addImm(0x1100)
14991509
.setMIFlag(MachineInstr::FrameSetup);
1510+
if (NeedsWinCFI) {
1511+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1512+
.setMIFlags(MachineInstr::FrameSetup);
1513+
HasWinCFI = true;
1514+
}
15001515
break;
15011516

15021517
case SwiftAsyncFramePointerMode::Never:
@@ -1626,11 +1641,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
16261641
bool HaveInitialContext = Attrs.hasAttrSomewhere(Attribute::SwiftAsync);
16271642
if (HaveInitialContext)
16281643
MBB.addLiveIn(AArch64::X22);
1644+
Register Reg = HaveInitialContext ? AArch64::X22 : AArch64::XZR;
16291645
BuildMI(MBB, MBBI, DL, TII->get(AArch64::StoreSwiftAsyncContext))
1630-
.addUse(HaveInitialContext ? AArch64::X22 : AArch64::XZR)
1646+
.addUse(Reg)
16311647
.addUse(AArch64::SP)
16321648
.addImm(FPOffset - 8)
16331649
.setMIFlags(MachineInstr::FrameSetup);
1650+
if (NeedsWinCFI) {
1651+
// WinCFI and arm64e, where StoreSwiftAsyncContext is expanded
1652+
// to multiple instructions, should be mutually-exclusive.
1653+
assert(Subtarget.getTargetTriple().getArchName() != "arm64e");
1654+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1655+
.setMIFlags(MachineInstr::FrameSetup);
1656+
HasWinCFI = true;
1657+
}
16341658
}
16351659

16361660
// Issue sub fp, sp, FPOffset or
@@ -2165,6 +2189,11 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
21652189
.addUse(AArch64::FP)
21662190
.addImm(0x10fe)
21672191
.setMIFlag(MachineInstr::FrameDestroy);
2192+
if (NeedsWinCFI) {
2193+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
2194+
.setMIFlags(MachineInstr::FrameDestroy);
2195+
HasWinCFI = true;
2196+
}
21682197
break;
21692198

21702199
case SwiftAsyncFramePointerMode::Never:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; RUN: rm -rf %t && mkdir -p %t
2+
; RUN: llc -mtriple aarch64-unknown-windows-msvc %s -o - | FileCheck %s
3+
; RUN: llc -mtriple aarch64-unknown-windows-msvc -filetype obj %s -o %t/a.o
4+
5+
; Check that the prologue/epilogue instructions for the swift async
6+
; context have an associated SEH instruction and that it doesn't error
7+
; when the output is an object file.
8+
9+
; CHECK: orr x29, x29, #0x1000000000000000
10+
; CHECK-NEXT: .seh_nop
11+
; CHECK: str x22, [sp, #16]
12+
; CHECK-NEXT: .seh_nop
13+
; CHECK: and x29, x29, #0xefffffffffffffff
14+
; CHECK-NEXT: .seh_nop
15+
16+
declare ptr @llvm.swift.async.context.addr()
17+
18+
define internal swifttailcc void @test(ptr nocapture readonly swiftasync %0) {
19+
entryresume.0:
20+
%1 = load ptr, ptr %0, align 8
21+
%2 = tail call ptr @llvm.swift.async.context.addr()
22+
store ptr %1, ptr %2, align 8
23+
ret void
24+
}

0 commit comments

Comments
 (0)