Skip to content

Commit 0ecd884

Browse files
authored
[AArch64][Win] Emit SEH instructions for the swift async context-related instructions in the prologue and the epilogue. (#66967)
This fixes an error from checkARM64Instructions() in MCWin64EH.cpp.
1 parent 59a67ea commit 0ecd884

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
@@ -1443,10 +1443,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14431443
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LOADgot), AArch64::X16)
14441444
.addExternalSymbol("swift_async_extendedFramePointerFlags",
14451445
AArch64II::MO_GOT);
1446+
if (NeedsWinCFI) {
1447+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1448+
.setMIFlags(MachineInstr::FrameSetup);
1449+
HasWinCFI = true;
1450+
}
14461451
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::FP)
14471452
.addUse(AArch64::FP)
14481453
.addUse(AArch64::X16)
14491454
.addImm(Subtarget.isTargetILP32() ? 32 : 0);
1455+
if (NeedsWinCFI) {
1456+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1457+
.setMIFlags(MachineInstr::FrameSetup);
1458+
HasWinCFI = true;
1459+
}
14501460
break;
14511461
}
14521462
[[fallthrough]];
@@ -1457,6 +1467,11 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
14571467
.addUse(AArch64::FP)
14581468
.addImm(0x1100)
14591469
.setMIFlag(MachineInstr::FrameSetup);
1470+
if (NeedsWinCFI) {
1471+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1472+
.setMIFlags(MachineInstr::FrameSetup);
1473+
HasWinCFI = true;
1474+
}
14601475
break;
14611476

14621477
case SwiftAsyncFramePointerMode::Never:
@@ -1580,11 +1595,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
15801595
bool HaveInitialContext = Attrs.hasAttrSomewhere(Attribute::SwiftAsync);
15811596
if (HaveInitialContext)
15821597
MBB.addLiveIn(AArch64::X22);
1598+
Register Reg = HaveInitialContext ? AArch64::X22 : AArch64::XZR;
15831599
BuildMI(MBB, MBBI, DL, TII->get(AArch64::StoreSwiftAsyncContext))
1584-
.addUse(HaveInitialContext ? AArch64::X22 : AArch64::XZR)
1600+
.addUse(Reg)
15851601
.addUse(AArch64::SP)
15861602
.addImm(FPOffset - 8)
15871603
.setMIFlags(MachineInstr::FrameSetup);
1604+
if (NeedsWinCFI) {
1605+
// WinCFI and arm64e, where StoreSwiftAsyncContext is expanded
1606+
// to multiple instructions, should be mutually-exclusive.
1607+
assert(Subtarget.getTargetTriple().getArchName() != "arm64e");
1608+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
1609+
.setMIFlags(MachineInstr::FrameSetup);
1610+
HasWinCFI = true;
1611+
}
15881612
}
15891613

15901614
if (HomPrologEpilog) {
@@ -2056,6 +2080,11 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
20562080
.addUse(AArch64::FP)
20572081
.addImm(0x10fe)
20582082
.setMIFlag(MachineInstr::FrameDestroy);
2083+
if (NeedsWinCFI) {
2084+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
2085+
.setMIFlags(MachineInstr::FrameDestroy);
2086+
HasWinCFI = true;
2087+
}
20592088
break;
20602089

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