Skip to content

Commit

Permalink
[AArch64][Win] Emit SEH instructions for the swift async context-rela…
Browse files Browse the repository at this point in the history
…ted instructions in the prologue and the epilogue. (#66967)

This fixes an error from checkARM64Instructions() in MCWin64EH.cpp.
  • Loading branch information
hjyamauchi authored Sep 28, 2023
1 parent 59a67ea commit 0ecd884
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
31 changes: 30 additions & 1 deletion llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,10 +1443,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
BuildMI(MBB, MBBI, DL, TII->get(AArch64::LOADgot), AArch64::X16)
.addExternalSymbol("swift_async_extendedFramePointerFlags",
AArch64II::MO_GOT);
if (NeedsWinCFI) {
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
.setMIFlags(MachineInstr::FrameSetup);
HasWinCFI = true;
}
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::FP)
.addUse(AArch64::FP)
.addUse(AArch64::X16)
.addImm(Subtarget.isTargetILP32() ? 32 : 0);
if (NeedsWinCFI) {
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
.setMIFlags(MachineInstr::FrameSetup);
HasWinCFI = true;
}
break;
}
[[fallthrough]];
Expand All @@ -1457,6 +1467,11 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
.addUse(AArch64::FP)
.addImm(0x1100)
.setMIFlag(MachineInstr::FrameSetup);
if (NeedsWinCFI) {
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
.setMIFlags(MachineInstr::FrameSetup);
HasWinCFI = true;
}
break;

case SwiftAsyncFramePointerMode::Never:
Expand Down Expand Up @@ -1580,11 +1595,20 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF,
bool HaveInitialContext = Attrs.hasAttrSomewhere(Attribute::SwiftAsync);
if (HaveInitialContext)
MBB.addLiveIn(AArch64::X22);
Register Reg = HaveInitialContext ? AArch64::X22 : AArch64::XZR;
BuildMI(MBB, MBBI, DL, TII->get(AArch64::StoreSwiftAsyncContext))
.addUse(HaveInitialContext ? AArch64::X22 : AArch64::XZR)
.addUse(Reg)
.addUse(AArch64::SP)
.addImm(FPOffset - 8)
.setMIFlags(MachineInstr::FrameSetup);
if (NeedsWinCFI) {
// WinCFI and arm64e, where StoreSwiftAsyncContext is expanded
// to multiple instructions, should be mutually-exclusive.
assert(Subtarget.getTargetTriple().getArchName() != "arm64e");
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
.setMIFlags(MachineInstr::FrameSetup);
HasWinCFI = true;
}
}

if (HomPrologEpilog) {
Expand Down Expand Up @@ -2056,6 +2080,11 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF,
.addUse(AArch64::FP)
.addImm(0x10fe)
.setMIFlag(MachineInstr::FrameDestroy);
if (NeedsWinCFI) {
BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_Nop))
.setMIFlags(MachineInstr::FrameDestroy);
HasWinCFI = true;
}
break;

case SwiftAsyncFramePointerMode::Never:
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/CodeGen/AArch64/swift-async-context-seh.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; RUN: rm -rf %t && mkdir -p %t
; RUN: llc -mtriple aarch64-unknown-windows-msvc %s -o - | FileCheck %s
; RUN: llc -mtriple aarch64-unknown-windows-msvc -filetype obj %s -o %t/a.o

; Check that the prologue/epilogue instructions for the swift async
; context have an associated SEH instruction and that it doesn't error
; when the output is an object file.

; CHECK: orr x29, x29, #0x1000000000000000
; CHECK-NEXT: .seh_nop
; CHECK: str x22, [sp, #16]
; CHECK-NEXT: .seh_nop
; CHECK: and x29, x29, #0xefffffffffffffff
; CHECK-NEXT: .seh_nop

declare ptr @llvm.swift.async.context.addr()

define internal swifttailcc void @test(ptr nocapture readonly swiftasync %0) {
entryresume.0:
%1 = load ptr, ptr %0, align 8
%2 = tail call ptr @llvm.swift.async.context.addr()
store ptr %1, ptr %2, align 8
ret void
}

0 comments on commit 0ecd884

Please sign in to comment.