Skip to content

Commit ee5bb0c

Browse files
hmeldertstellar
authored andcommitted
Fix Objective-C++ Sret of non-trivial data types on Windows ARM64 (#88671)
Linked to gnustep/libobjc2#289. More information can be found in issue: #88273. My solution involves creating a new message-send function for this calling convention when targeting MSVC. Additional information is available in the libobjc2 pull request. I am unsure whether we should check for a runtime version where objc_msgSend_stret2_np is guaranteed to be present or leave it as is, considering it remains a critical bug. What are your thoughts about this @davidchisnall? (cherry picked from commit 3dcd2cc)
1 parent 6dbaa89 commit ee5bb0c

File tree

4 files changed

+105
-14
lines changed

4 files changed

+105
-14
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,11 @@ bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
15811581
return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
15821582
}
15831583

1584+
bool CodeGenModule::ReturnTypeHasInReg(const CGFunctionInfo &FI) {
1585+
const auto &RI = FI.getReturnInfo();
1586+
return RI.getInReg();
1587+
}
1588+
15841589
bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
15851590
return ReturnTypeUsesSRet(FI) &&
15861591
getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();

clang/lib/CodeGen/CGObjCGNU.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,23 +2903,29 @@ CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
29032903
break;
29042904
case CodeGenOptions::Mixed:
29052905
case CodeGenOptions::NonLegacy:
2906+
StringRef name = "objc_msgSend";
29062907
if (CGM.ReturnTypeUsesFPRet(ResultType)) {
2907-
imp =
2908-
CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
2909-
"objc_msgSend_fpret")
2910-
.getCallee();
2908+
name = "objc_msgSend_fpret";
29112909
} else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
2912-
// The actual types here don't matter - we're going to bitcast the
2913-
// function anyway
2914-
imp =
2915-
CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
2916-
"objc_msgSend_stret")
2917-
.getCallee();
2918-
} else {
2919-
imp = CGM.CreateRuntimeFunction(
2920-
llvm::FunctionType::get(IdTy, IdTy, true), "objc_msgSend")
2921-
.getCallee();
2910+
name = "objc_msgSend_stret";
2911+
2912+
// The address of the memory block is be passed in x8 for POD type,
2913+
// or in x0 for non-POD type (marked as inreg).
2914+
bool shouldCheckForInReg =
2915+
CGM.getContext()
2916+
.getTargetInfo()
2917+
.getTriple()
2918+
.isWindowsMSVCEnvironment() &&
2919+
CGM.getContext().getTargetInfo().getTriple().isAArch64();
2920+
if (shouldCheckForInReg && CGM.ReturnTypeHasInReg(MSI.CallInfo)) {
2921+
name = "objc_msgSend_stret2";
2922+
}
29222923
}
2924+
// The actual types here don't matter - we're going to bitcast the
2925+
// function anyway
2926+
imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
2927+
name)
2928+
.getCallee();
29232929
}
29242930

29252931
// Reset the receiver in case the lookup modified it

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,9 @@ class CodeGenModule : public CodeGenTypeCache {
12391239
/// Return true iff the given type uses 'sret' when used as a return type.
12401240
bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
12411241

1242+
/// Return true iff the given type has `inreg` set.
1243+
bool ReturnTypeHasInReg(const CGFunctionInfo &FI);
1244+
12421245
/// Return true iff the given type uses an argument slot when 'sret' is used
12431246
/// as a return type.
12441247
bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %clang_cc1 -triple aarch64-pc-windows-msvc -fobjc-runtime=gnustep-2.2 -fobjc-dispatch-method=non-legacy -emit-llvm -o - %s | FileCheck %s
2+
3+
// Pass and return for type size <= 8 bytes.
4+
struct S1 {
5+
int a[2];
6+
};
7+
8+
// Pass and return hfa <= 8 bytes
9+
struct F1 {
10+
float a[2];
11+
};
12+
13+
// Pass and return for type size > 16 bytes.
14+
struct S2 {
15+
int a[5];
16+
};
17+
18+
// Pass and return aggregate (of size < 16 bytes) with non-trivial destructor.
19+
// Sret and inreg: Returned in x0
20+
struct S3 {
21+
int a[3];
22+
~S3();
23+
};
24+
S3::~S3() {
25+
}
26+
27+
28+
@interface MsgTest { id isa; } @end
29+
@implementation MsgTest
30+
- (S1) smallS1 {
31+
S1 x;
32+
x.a[0] = 0;
33+
x.a[1] = 1;
34+
return x;
35+
36+
}
37+
- (F1) smallF1 {
38+
F1 x;
39+
x.a[0] = 0.2f;
40+
x.a[1] = 0.5f;
41+
return x;
42+
}
43+
- (S2) stretS2 {
44+
S2 x;
45+
for (int i = 0; i < 5; i++) {
46+
x.a[i] = i;
47+
}
48+
return x;
49+
}
50+
- (S3) stretInRegS3 {
51+
S3 x;
52+
for (int i = 0; i < 3; i++) {
53+
x.a[i] = i;
54+
}
55+
return x;
56+
}
57+
+ (S3) msgTestStretInRegS3 {
58+
S3 x;
59+
for (int i = 0; i < 3; i++) {
60+
x.a[i] = i;
61+
}
62+
return x;
63+
}
64+
@end
65+
66+
void test0(MsgTest *t) {
67+
// CHECK: call {{.*}} @objc_msgSend
68+
S1 ret = [t smallS1];
69+
// CHECK: call {{.*}} @objc_msgSend
70+
F1 ret2 = [t smallF1];
71+
// CHECK: call {{.*}} @objc_msgSend_stret
72+
S2 ret3 = [t stretS2];
73+
// CHECK: call {{.*}} @objc_msgSend_stret2
74+
S3 ret4 = [t stretInRegS3];
75+
// CHECK: call {{.*}} @objc_msgSend_stret2
76+
S3 ret5 = [MsgTest msgTestStretInRegS3];
77+
}

0 commit comments

Comments
 (0)