Skip to content

Commit 73213ad

Browse files
committed
[AArch64][Win] Emit SEH instructions for the swift async
context-related instructions in the prologue and the epilogue. This fixes an error from checkARM64Instructions() in MCWin64EH.cpp.
1 parent 7421040 commit 73213ad

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
@@ -1476,10 +1476,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14761476
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LOADgot), AArch64::X16)
14771477
.addExternalSymbol("swift_async_extendedFramePointerFlags",
14781478
AArch64II::MO_GOT);
1479+
if (NeedsWinCFI) {
1480+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1481+
.setMIFlags(MachineInstr::FrameSetup);
1482+
HasWinCFI = true;
1483+
}
14791484
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::FP)
14801485
.addUse(AArch64::FP)
14811486
.addUse(AArch64::X16)
14821487
.addImm(Subtarget.isTargetILP32() ? 32 : 0);
1488+
if (NeedsWinCFI) {
1489+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1490+
.setMIFlags(MachineInstr::FrameSetup);
1491+
HasWinCFI = true;
1492+
}
14831493
break;
14841494
}
14851495
[[fallthrough]];
@@ -1490,6 +1500,11 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14901500
.addUse(AArch64::FP)
14911501
.addImm(0x1100)
14921502
.setMIFlag(MachineInstr::FrameSetup);
1503+
if (NeedsWinCFI) {
1504+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1505+
.setMIFlags(MachineInstr::FrameSetup);
1506+
HasWinCFI = true;
1507+
}
14931508
break;
14941509

14951510
case SwiftAsyncFramePointerMode::Never:
@@ -1613,11 +1628,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
16131628
bool HaveInitialContext = Attrs.hasAttrSomewhere(Attribute::SwiftAsync);
16141629
if (HaveInitialContext)
16151630
MBB.addLiveIn(AArch64::X22);
1631+
Register Reg = HaveInitialContext ? AArch64::X22 : AArch64::XZR;
16161632
BuildMI(MBB, MBBI, DL, TII->get(AArch64::StoreSwiftAsyncContext))
1617-
.addUse(HaveInitialContext ? AArch64::X22 : AArch64::XZR)
1633+
.addUse(Reg)
16181634
.addUse(AArch64::SP)
16191635
.addImm(FPOffset - 8)
16201636
.setMIFlags(MachineInstr::FrameSetup);
1637+
if (NeedsWinCFI) {
1638+
// WinCFI and arm64e, where StoreSwiftAsyncContext is expanded
1639+
// to multiple instructions, should be mutually-exclusive.
1640+
assert(Subtarget.getTargetTriple().getArchName() != "arm64e");
1641+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1642+
.setMIFlags(MachineInstr::FrameSetup);
1643+
HasWinCFI = true;
1644+
}
16211645
}
16221646

16231647
if (HomPrologEpilog) {
@@ -2132,6 +2156,11 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
21322156
.addUse(AArch64::FP)
21332157
.addImm(0x10fe)
21342158
.setMIFlag(MachineInstr::FrameDestroy);
2159+
if (NeedsWinCFI) {
2160+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
2161+
.setMIFlags(MachineInstr::FrameDestroy);
2162+
HasWinCFI = true;
2163+
}
21352164
break;
21362165

21372166
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)