Skip to content

Commit 3dcd2cc

Browse files
authored
Fix Objective-C++ Sret of non-trivial data types on Windows ARM64 (llvm#88671)
Linked to gnustep/libobjc2#289. More information can be found in issue: llvm#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?
1 parent a8fd0d0 commit 3dcd2cc

File tree

4 files changed

+105
-14
lines changed

4 files changed

+105
-14
lines changed

Diff for: clang/lib/CodeGen/CGCall.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,11 @@ bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
15851585
return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
15861586
}
15871587

1588+
bool CodeGenModule::ReturnTypeHasInReg(const CGFunctionInfo &FI) {
1589+
const auto &RI = FI.getReturnInfo();
1590+
return RI.getInReg();
1591+
}
1592+
15881593
bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
15891594
return ReturnTypeUsesSRet(FI) &&
15901595
getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();

Diff for: clang/lib/CodeGen/CGObjCGNU.cpp

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

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

Diff for: clang/lib/CodeGen/CodeGenModule.h

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

1244+
/// Return true iff the given type has `inreg` set.
1245+
bool ReturnTypeHasInReg(const CGFunctionInfo &FI);
1246+
12441247
/// Return true iff the given type uses an argument slot when 'sret' is used
12451248
/// as a return type.
12461249
bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI);

Diff for: clang/test/CodeGenObjCXX/msabi-stret-arm64.mm

+77
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)