-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[win][aarch64] Place catch objects in the fixed object area #147421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
; RUN: llc %s --mtriple=aarch64-pc-windows-msvc -o - | FileCheck %s | ||
|
||
; Regression test for handling MSVC C++ exceptions when there's an aligned | ||
; object on the stack. | ||
|
||
; Generated from this C++ code: | ||
; https://godbolt.org/z/cGzGfqq34 | ||
; > clang --target=aarch64-pc-windows-msvc test.cpp | ||
; ``` | ||
; // Large object: alignment seems to be important? | ||
; struct alignas(128) BigObj { | ||
; int value; | ||
; // Destructor so it's kept alive. | ||
; ~BigObj() { } | ||
; }; | ||
; | ||
; // Exception type need to be large enough to not fit in a register. | ||
; struct Error { | ||
; int value; | ||
; int padding[3]; | ||
; }; | ||
; | ||
; int main() { | ||
; BigObj bo{}; | ||
; | ||
; try { | ||
; throw Error { 42, {0, 0, 0} }; | ||
; } catch (const Error& e) { | ||
; return e.value; | ||
; } | ||
; return 0; | ||
; } | ||
; ``` | ||
|
||
; CHECK-LABEL: main: | ||
; CHECK: sub x[[SPTMP:[0-9]+]], sp, #336 | ||
; CHECK: and sp, x[[SPTMP]], #0xffffffffffffff80 | ||
; CHECK: mov x[[FP:[0-9]+]], sp | ||
; CHECK: str wzr, [x[[FP]], #332] | ||
|
||
; CHECK-LABEL: "?catch$3@?0?main@4HA": | ||
; CHECK: str w8, [x[[FP]], #332] | ||
; CHECK-NEXT: .seh_startepilogue | ||
; CHECK: ret | ||
|
||
; CHECK-LABEL: $cppxdata$main: | ||
; CHECK: .word -16 // UnwindHelp | ||
; CHECK-LABEL: $handlerMap$0$main: | ||
; CHECK-NEXT: .word 8 // Adjectives | ||
; CHECK-NEXT: .word "??_R0?AUError@@@8"@IMGREL // Type | ||
; CHECK-NEXT: .word -8 // CatchObjOffset | ||
; CHECK-NEXT: .word "?catch$3@?0?main@4HA"@IMGREL // Handler | ||
|
||
%rtti.TypeDescriptor11 = type { ptr, ptr, [12 x i8] } | ||
%eh.CatchableType = type { i32, i32, i32, i32, i32, i32, i32 } | ||
%eh.CatchableTypeArray.1 = type { i32, [1 x i32] } | ||
%eh.ThrowInfo = type { i32, i32, i32, i32 } | ||
%struct.BigObj = type { i32, [124 x i8] } | ||
%struct.Error = type { i32, [3 x i32] } | ||
|
||
$"??1BigObj@@QEAA@XZ" = comdat any | ||
|
||
$"??_R0?AUError@@@8" = comdat any | ||
|
||
$"_CT??_R0?AUError@@@816" = comdat any | ||
|
||
$"_CTA1?AUError@@" = comdat any | ||
|
||
$"_TI1?AUError@@" = comdat any | ||
|
||
@"??_7type_info@@6B@" = external constant ptr | ||
@"??_R0?AUError@@@8" = linkonce_odr global %rtti.TypeDescriptor11 { ptr @"??_7type_info@@6B@", ptr null, [12 x i8] c".?AUError@@\00" }, comdat | ||
@__ImageBase = external dso_local constant i8 | ||
@"_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 | ||
@"_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 | ||
@"_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 | ||
|
||
define dso_local noundef i32 @main() personality ptr @__CxxFrameHandler3 { | ||
entry: | ||
%retval = alloca i32, align 4 | ||
%bo = alloca %struct.BigObj, align 128 | ||
%tmp = alloca %struct.Error, align 4 | ||
%e = alloca ptr, align 8 | ||
%cleanup.dest.slot = alloca i32, align 4 | ||
store i32 0, ptr %retval, align 4 | ||
call void @llvm.memset.p0.i64(ptr align 128 %bo, i8 0, i64 128, i1 false) | ||
%value = getelementptr inbounds nuw %struct.BigObj, ptr %bo, i32 0, i32 0 | ||
%value1 = getelementptr inbounds nuw %struct.Error, ptr %tmp, i32 0, i32 0 | ||
store i32 42, ptr %value1, align 4 | ||
%padding = getelementptr inbounds nuw %struct.Error, ptr %tmp, i32 0, i32 1 | ||
store i32 0, ptr %padding, align 4 | ||
%arrayinit.element = getelementptr inbounds i32, ptr %padding, i64 1 | ||
store i32 0, ptr %arrayinit.element, align 4 | ||
%arrayinit.element2 = getelementptr inbounds i32, ptr %padding, i64 2 | ||
store i32 0, ptr %arrayinit.element2, align 4 | ||
invoke void @_CxxThrowException(ptr %tmp, ptr @"_TI1?AUError@@") #3 | ||
to label %unreachable unwind label %catch.dispatch | ||
|
||
catch.dispatch: | ||
%0 = catchswitch within none [label %catch] unwind label %ehcleanup | ||
|
||
catch: | ||
%1 = catchpad within %0 [ptr @"??_R0?AUError@@@8", i32 8, ptr %e] | ||
%2 = load ptr, ptr %e, align 8 | ||
%value3 = getelementptr inbounds nuw %struct.Error, ptr %2, i32 0, i32 0 | ||
%3 = load i32, ptr %value3, align 4 | ||
store i32 %3, ptr %retval, align 4 | ||
store i32 1, ptr %cleanup.dest.slot, align 4 | ||
catchret from %1 to label %catchret.dest | ||
|
||
catchret.dest: | ||
br label %cleanup | ||
|
||
try.cont: | ||
store i32 0, ptr %retval, align 4 | ||
store i32 1, ptr %cleanup.dest.slot, align 4 | ||
br label %cleanup | ||
|
||
cleanup: | ||
call void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %bo) #4 | ||
%4 = load i32, ptr %retval, align 4 | ||
ret i32 %4 | ||
|
||
ehcleanup: | ||
%5 = cleanuppad within none [] | ||
call void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %bo) [ "funclet"(token %5) ] | ||
cleanupret from %5 unwind to caller | ||
|
||
unreachable: | ||
unreachable | ||
} | ||
|
||
declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #1 | ||
|
||
declare dso_local void @_CxxThrowException(ptr, ptr) | ||
|
||
declare dso_local i32 @__CxxFrameHandler3(...) | ||
|
||
define linkonce_odr dso_local void @"??1BigObj@@QEAA@XZ"(ptr noundef nonnull align 128 dereferenceable(4) %this) unnamed_addr comdat { | ||
entry: | ||
%this.addr = alloca ptr, align 8 | ||
store ptr %this, ptr %this.addr, align 8 | ||
%this1 = load ptr, ptr %this.addr, align 8 | ||
ret void | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
; RUN: llc %s --mtriple=aarch64-pc-windows-msvc -o - | FileCheck %s | ||
|
||
; Tests the fixed object layouts when two catchpads re-use the same stack | ||
; allocation for this catch objects. | ||
|
||
; Generated from this C++ code, with modifications to the IR (see comments in | ||
; IR): | ||
; https://godbolt.org/z/9qv5Yn68j | ||
; > clang --target=aarch64-pc-windows-msvc test.cpp | ||
; ``` | ||
; extern "C" void boom(); | ||
; extern "C" int calls_boom(); | ||
; { | ||
; try { boom(); } | ||
; catch (int& i) { return i; } | ||
; catch (long& l) { return l; } | ||
; return 0; | ||
; } | ||
; ``` | ||
|
||
; Only need 48 bytes on the stack, not 64. | ||
; CHECK-LABEL: calls_boom: | ||
; CHECK: sub sp, sp, #48 | ||
; CHECK: .seh_stackalloc 48 | ||
|
||
; Both the catch blocks load from the same address. | ||
; CHECK-LABEL: "?catch$3@?0?calls_boom@4HA": | ||
; CHECK: ldr x8, [x29, #24] | ||
; CHECK-LABEL: "?catch$4@?0?calls_boom@4HA": | ||
; CHECK: ldr x8, [x29, #24] | ||
|
||
; There's enough space for the UnwindHelp to be at -16 instead of -32 | ||
; CHECK-LABEL: $cppxdata$calls_boom: | ||
; CHECK: .word -16 // UnwindHelp | ||
|
||
; Both catches have the same object offset. | ||
; CHECK-LABEL: $handlerMap$0$calls_boom: | ||
; CHECK: .word -8 // CatchObjOffset | ||
; CHECK-NEXT: .word "?catch$3@?0?calls_boom@4HA"@IMGREL // Handler | ||
; CHECK: .word -8 // CatchObjOffset | ||
; CHECK-NEXT: .word "?catch$4@?0?calls_boom@4HA"@IMGREL // Handler | ||
|
||
%rtti.TypeDescriptor2 = type { ptr, ptr, [3 x i8] } | ||
|
||
$"??_R0H@8" = comdat any | ||
|
||
$"??_R0J@8" = comdat any | ||
|
||
@"??_7type_info@@6B@" = external constant ptr | ||
@"??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { ptr @"??_7type_info@@6B@", ptr null, [3 x i8] c".H\00" }, comdat | ||
@"??_R0J@8" = linkonce_odr global %rtti.TypeDescriptor2 { ptr @"??_7type_info@@6B@", ptr null, [3 x i8] c".J\00" }, comdat | ||
|
||
define dso_local i32 @calls_boom() personality ptr @__CxxFrameHandler3 { | ||
entry: | ||
%retval = alloca i32, align 4 | ||
; MODIFICATION: Remove unusued alloca | ||
; %l = alloca ptr, align 8 | ||
%i = alloca ptr, align 8 | ||
invoke void @boom() | ||
to label %invoke.cont unwind label %catch.dispatch | ||
|
||
catch.dispatch: | ||
%0 = catchswitch within none [label %catch1, label %catch] unwind to caller | ||
|
||
catch1: | ||
%1 = catchpad within %0 [ptr @"??_R0H@8", i32 8, ptr %i] | ||
%2 = load ptr, ptr %i, align 8 | ||
%3 = load i32, ptr %2, align 4 | ||
store i32 %3, ptr %retval, align 4 | ||
catchret from %1 to label %catchret.dest2 | ||
|
||
catch: | ||
; MODIFICATION: Use %i instead of %l | ||
%4 = catchpad within %0 [ptr @"??_R0J@8", i32 8, ptr %i] | ||
%5 = load ptr, ptr %i, align 8 | ||
%6 = load i32, ptr %5, align 4 | ||
store i32 %6, ptr %retval, align 4 | ||
catchret from %4 to label %catchret.dest | ||
|
||
invoke.cont: | ||
br label %try.cont | ||
|
||
catchret.dest: | ||
br label %return | ||
|
||
catchret.dest2: | ||
br label %return | ||
|
||
try.cont: | ||
store i32 0, ptr %retval, align 4 | ||
br label %return | ||
|
||
return: | ||
%7 = load i32, ptr %retval, align 4 | ||
ret i32 %7 | ||
} | ||
|
||
declare dso_local void @boom() #1 | ||
|
||
declare dso_local i32 @__CxxFrameHandler3(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it can allocate space for the same FrameIndex multiple times? I think a given FrameIndex can show up multple times in the TryBlockMap. (See FunctionLoweringInfo::set)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch: yes, if the catchpads reuse the same alloca then we'd allocate too many fixed objects. I've fixed it for AArch64 and added a test, I'll create a followup PR to fix x64.
I couldn't figure out a way to make Clang generate this IR, so that could be a potential optimization (i.e., only emit one alloca for all by-reference and by-pointer catch blocks).