Skip to content

Commit 99f0e05

Browse files
committed
[win][aarch64] Place catch objects in fixed stack locations
1 parent 9b1bf4c commit 99f0e05

File tree

4 files changed

+199
-20
lines changed

4 files changed

+199
-20
lines changed

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -428,19 +428,38 @@ AArch64FrameLowering::getStackIDForScalableVectors() const {
428428
static unsigned getFixedObjectSize(const MachineFunction &MF,
429429
const AArch64FunctionInfo *AFI, bool IsWin64,
430430
bool IsFunclet) {
431+
assert(AFI->getTailCallReservedStack() % 16 == 0 &&
432+
"Tail call reserved stack must be aligned to 16 bytes");
431433
if (!IsWin64 || IsFunclet) {
432434
return AFI->getTailCallReservedStack();
433435
} else {
434436
if (AFI->getTailCallReservedStack() != 0 &&
435437
!MF.getFunction().getAttributes().hasAttrSomewhere(
436438
Attribute::SwiftAsync))
437439
report_fatal_error("cannot generate ABI-changing tail call for Win64");
440+
unsigned FixedObjectSize = AFI->getTailCallReservedStack();
441+
438442
// Var args are stored here in the primary function.
439-
const unsigned VarArgsArea = AFI->getVarArgsGPRSize();
440-
// To support EH funclets we allocate an UnwindHelp object
441-
const unsigned UnwindHelpObject = (MF.hasEHFunclets() ? 8 : 0);
442-
return AFI->getTailCallReservedStack() +
443-
alignTo(VarArgsArea + UnwindHelpObject, 16);
443+
FixedObjectSize += AFI->getVarArgsGPRSize();
444+
445+
if (MF.hasEHFunclets()) {
446+
// Catch objects are stored here in the primary function.
447+
const MachineFrameInfo &MFI = MF.getFrameInfo();
448+
const WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo();
449+
for (const WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
450+
for (const WinEHHandlerType &H : TBME.HandlerArray) {
451+
int FrameIndex = H.CatchObj.FrameIndex;
452+
if (FrameIndex != INT_MAX) {
453+
FixedObjectSize = alignTo(FixedObjectSize,
454+
MFI.getObjectAlign(FrameIndex).value()) +
455+
MFI.getObjectSize(FrameIndex);
456+
}
457+
}
458+
}
459+
// To support EH funclets we allocate an UnwindHelp object
460+
FixedObjectSize += 8;
461+
}
462+
return alignTo(FixedObjectSize, 16);
444463
}
445464
}
446465

@@ -4494,30 +4513,47 @@ void AArch64FrameLowering::processFunctionBeforeFrameFinalized(
44944513
// anything.
44954514
if (!MF.hasEHFunclets())
44964515
return;
4497-
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
4498-
WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo();
44994516

4500-
MachineBasicBlock &MBB = MF.front();
4501-
auto MBBI = MBB.begin();
4502-
while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
4503-
++MBBI;
4517+
// Win64 C++ EH needs to allocate space for the catch objects in the fixed
4518+
// object area right next to the UnwindHelp object.
4519+
WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo();
4520+
int64_t CurrentOffset =
4521+
AFI->getVarArgsGPRSize() + AFI->getTailCallReservedStack();
4522+
for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
4523+
for (WinEHHandlerType &H : TBME.HandlerArray) {
4524+
int FrameIndex = H.CatchObj.FrameIndex;
4525+
if (FrameIndex != INT_MAX) {
4526+
CurrentOffset =
4527+
alignTo(CurrentOffset, MFI.getObjectAlign(FrameIndex).value());
4528+
CurrentOffset += MFI.getObjectSize(FrameIndex);
4529+
MFI.setObjectOffset(FrameIndex, -CurrentOffset);
4530+
}
4531+
}
4532+
}
45044533

45054534
// Create an UnwindHelp object.
45064535
// The UnwindHelp object is allocated at the start of the fixed object area
4507-
int64_t FixedObject =
4508-
getFixedObjectSize(MF, AFI, /*IsWin64*/ true, /*IsFunclet*/ false);
4509-
int UnwindHelpFI = MFI.CreateFixedObject(/*Size*/ 8,
4510-
/*SPOffset*/ -FixedObject,
4536+
int64_t UnwindHelpOffset = alignTo(CurrentOffset + 8, Align(16));
4537+
assert(UnwindHelpOffset == getFixedObjectSize(MF, AFI, /*IsWin64*/ true,
4538+
/*IsFunclet*/ false) &&
4539+
"UnwindHelpOffset must be at the start of the fixed object area");
4540+
int UnwindHelpFI = MFI.CreateFixedObject(/*Size*/ 8, -UnwindHelpOffset,
45114541
/*IsImmutable=*/false);
45124542
EHInfo.UnwindHelpFrameIdx = UnwindHelpFI;
45134543

4544+
MachineBasicBlock &MBB = MF.front();
4545+
auto MBBI = MBB.begin();
4546+
while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup))
4547+
++MBBI;
4548+
45144549
// We need to store -2 into the UnwindHelp object at the start of the
45154550
// function.
45164551
DebugLoc DL;
45174552
RS->enterBasicBlockEnd(MBB);
45184553
RS->backward(MBBI);
45194554
Register DstReg = RS->FindUnusedReg(&AArch64::GPR64commonRegClass);
45204555
assert(DstReg && "There must be a free register after frame setup");
4556+
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
45214557
BuildMI(MBB, MBBI, DL, TII.get(AArch64::MOVi64imm), DstReg).addImm(-2);
45224558
BuildMI(MBB, MBBI, DL, TII.get(AArch64::STURXi))
45234559
.addReg(DstReg, getKillRegState(true))

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28237,9 +28237,7 @@ void AArch64TargetLowering::finalizeLowering(MachineFunction &MF) const {
2823728237
}
2823828238

2823928239
// Unlike X86, we let frame lowering assign offsets to all catch objects.
28240-
bool AArch64TargetLowering::needsFixedCatchObjects() const {
28241-
return false;
28242-
}
28240+
bool AArch64TargetLowering::needsFixedCatchObjects() const { return true; }
2824328241

2824428242
bool AArch64TargetLowering::shouldLocalize(
2824528243
const MachineInstr &MI, const TargetTransformInfo *TTI) const {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
; RUN: llc %s --mtriple=aarch64-pc-windows-msvc -o - | FileCheck %s
2+
3+
; Regression test for handling MSVC C++ exceptions when there's an aligned
4+
; object on the stack.
5+
6+
; Generated from this C++ code:
7+
; https://godbolt.org/z/cGzGfqq34
8+
; > clang --target=aarch64-pc-windows-msvc test.cpp
9+
; ```
10+
; // Large object: alignment seems to be important?
11+
; struct alignas(128) BigObj {
12+
; int value;
13+
; // Destructor so it's kept alive.
14+
; ~BigObj() { }
15+
; };
16+
;
17+
; // Exception type need to be large enough to not fit in a register.
18+
; struct Error {
19+
; int value;
20+
; int padding[3];
21+
; };
22+
;
23+
; int main() {
24+
; BigObj bo{};
25+
;
26+
; try {
27+
; throw Error { 42, {0, 0, 0} };
28+
; } catch (const Error& e) {
29+
; return e.value;
30+
; }
31+
; return 0;
32+
; }
33+
; ```
34+
35+
; CHECK-LABEL: main:
36+
; CHECK: sub x[[SPTMP:[0-9]+]], sp, #336
37+
; CHECK: and sp, x[[SPTMP]], #0xffffffffffffff80
38+
; CHECK: mov x[[FP:[0-9]+]], sp
39+
; CHECK: str wzr, [x[[FP]], #332]
40+
41+
; CHECK-LABEL: "?catch$3@?0?main@4HA":
42+
; CHECK: str w8, [x[[FP]], #332]
43+
; CHECK-NEXT: .seh_startepilogue
44+
; CHECK: ret
45+
46+
; CHECK-LABEL: $cppxdata$main:
47+
; CHECK: .word -16 // UnwindHelp
48+
; CHECK-LABEL: $handlerMap$0$main:
49+
; CHECK-NEXT: .word 8 // Adjectives
50+
; CHECK-NEXT: .word "??_R0?AUError@@@8"@IMGREL // Type
51+
; CHECK-NEXT: .word -8 // CatchObjOffset
52+
; CHECK-NEXT: .word "?catch$3@?0?main@4HA"@IMGREL // Handler
53+
54+
%rtti.TypeDescriptor11 = type { ptr, ptr, [12 x i8] }
55+
%eh.CatchableType = type { i32, i32, i32, i32, i32, i32, i32 }
56+
%eh.CatchableTypeArray.1 = type { i32, [1 x i32] }
57+
%eh.ThrowInfo = type { i32, i32, i32, i32 }
58+
%struct.BigObj = type { i32, [124 x i8] }
59+
%struct.Error = type { i32, [3 x i32] }
60+
61+
$"??1BigObj@@QEAA@XZ" = comdat any
62+
63+
$"??_R0?AUError@@@8" = comdat any
64+
65+
$"_CT??_R0?AUError@@@816" = comdat any
66+
67+
$"_CTA1?AUError@@" = comdat any
68+
69+
$"_TI1?AUError@@" = comdat any
70+
71+
@"??_7type_info@@6B@" = external constant ptr
72+
@"??_R0?AUError@@@8" = linkonce_odr global %rtti.TypeDescriptor11 { ptr @"??_7type_info@@6B@", ptr null, [12 x i8] c".?AUError@@\00" }, comdat
73+
@__ImageBase = external dso_local constant i8
74+
@"_CT??_R0?AUError@@@816" = linkonce_odr unnamed_addr constant %eh.CatchableType { i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (ptr @"??_R0?AUError@@@8" to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32), i32 0, i32 -1, i32 0, i32 16, i32 0 }, section ".xdata", comdat
75+
@"_CTA1?AUError@@" = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x i32] [i32 trunc (i64 sub nuw nsw (i64 ptrtoint (ptr @"_CT??_R0?AUError@@@816" to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32)] }, section ".xdata", comdat
76+
@"_TI1?AUError@@" = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i32 0, i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (ptr @"_CTA1?AUError@@" to i64), i64 ptrtoint (ptr @__ImageBase to i64)) to i32) }, section ".xdata", comdat
77+
78+
define dso_local noundef i32 @main() personality ptr @__CxxFrameHandler3 {
79+
entry:
80+
%retval = alloca i32, align 4
81+
%bo = alloca %struct.BigObj, align 128
82+
%tmp = alloca %struct.Error, align 4
83+
%e = alloca ptr, align 8
84+
%cleanup.dest.slot = alloca i32, align 4
85+
store i32 0, ptr %retval, align 4
86+
call void @llvm.memset.p0.i64(ptr align 128 %bo, i8 0, i64 128, i1 false)
87+
%value = getelementptr inbounds nuw %struct.BigObj, ptr %bo, i32 0, i32 0
88+
%value1 = getelementptr inbounds nuw %struct.Error, ptr %tmp, i32 0, i32 0
89+
store i32 42, ptr %value1, align 4
90+
%padding = getelementptr inbounds nuw %struct.Error, ptr %tmp, i32 0, i32 1
91+
store i32 0, ptr %padding, align 4
92+
%arrayinit.element = getelementptr inbounds i32, ptr %padding, i64 1
93+
store i32 0, ptr %arrayinit.element, align 4
94+
%arrayinit.element2 = getelementptr inbounds i32, ptr %padding, i64 2
95+
store i32 0, ptr %arrayinit.element2, align 4
96+
invoke void @_CxxThrowException(ptr %tmp, ptr @"_TI1?AUError@@") #3
97+
to label %unreachable unwind label %catch.dispatch
98+
99+
catch.dispatch:
100+
%0 = catchswitch within none [label %catch] unwind label %ehcleanup
101+
102+
catch:
103+
%1 = catchpad within %0 [ptr @"??_R0?AUError@@@8", i32 8, ptr %e]
104+
%2 = load ptr, ptr %e, align 8
105+
%value3 = getelementptr inbounds nuw %struct.Error, ptr %2, i32 0, i32 0
106+
%3 = load i32, ptr %value3, align 4
107+
store i32 %3, ptr %retval, align 4
108+
store i32 1, ptr %cleanup.dest.slot, align 4
109+
catchret from %1 to label %catchret.dest
110+
111+
catchret.dest:
112+
br label %cleanup
113+
114+
try.cont:
115+
store i32 0, ptr %retval, align 4
116+
store i32 1, ptr %cleanup.dest.slot, align 4
117+
br label %cleanup
118+
119+
cleanup:
120+
call void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %bo) #4
121+
%4 = load i32, ptr %retval, align 4
122+
ret i32 %4
123+
124+
ehcleanup:
125+
%5 = cleanuppad within none []
126+
call void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %bo) [ "funclet"(token %5) ]
127+
cleanupret from %5 unwind to caller
128+
129+
unreachable:
130+
unreachable
131+
}
132+
133+
declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #1
134+
135+
declare dso_local void @_CxxThrowException(ptr, ptr)
136+
137+
declare dso_local i32 @__CxxFrameHandler3(...)
138+
139+
define linkonce_odr dso_local void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %this) unnamed_addr comdat {
140+
entry:
141+
%this.addr = alloca ptr, align 8
142+
store ptr %this, ptr %this.addr, align 8
143+
%this1 = load ptr, ptr %this.addr, align 8
144+
ret void
145+
}

llvm/test/CodeGen/AArch64/wineh-try-catch.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
; CHECK: str x28, [sp, #24]
2121
; CHECK: stp x29, x30, [sp, #32]
2222
; CHECK: add x29, sp, #32
23-
; CHECK: sub sp, sp, #624
23+
; CHECK: sub sp, sp, #608
2424
; CHECK: mov x19, sp
2525
; CHECK: mov x0, #-2
2626
; CHECK: stur x0, [x29, #16]
@@ -51,7 +51,7 @@
5151
; CHECK: str x21, [sp, #16]
5252
; CHECK: str x28, [sp, #24]
5353
; CHECK: stp x29, x30, [sp, #32]
54-
; CHECK: add x20, x19, #12
54+
; CHECK: add x20, x19, #0
5555

5656
; Check that there are no further stack updates.
5757
; CHECK-NOT: sub sp, sp

0 commit comments

Comments
 (0)