Skip to content

Commit e8a486e

Browse files
authored
[clang] Return larger CXX records in memory (#120670)
We incorrectly return CXX records in AVX registers when they should be returned in memory. This is violation of x86-64 psABI. Detailed discussion is here: https://groups.google.com/g/x86-64-abi/c/BjOOyihHuqg/m/KurXdUcWAgAJ
1 parent f634223 commit e8a486e

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ C++ Specific Potentially Breaking Changes
4545
ABI Changes in This Version
4646
---------------------------
4747

48+
- Return larger CXX records in memory instead of using AVX registers. Code compiled with older clang will be incompatible with newer version of the clang unless -fclang-abi-compat=20 is provided. (#GH120670)
49+
4850
AST Dumping Potentially Breaking Changes
4951
----------------------------------------
5052

clang/include/clang/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ class LangOptionsBase {
250250
/// passing them as if they had a size of 1 byte.
251251
Ver19,
252252

253+
/// Attempt to be ABI-compatible with code generated by Clang 20.0.x.
254+
/// This causes clang to:
255+
/// - Incorrectly return C++ records in AVX registers on x86_64.
256+
Ver20,
257+
253258
/// Conform to the underlying platform's C and C++ ABIs as closely
254259
/// as we can.
255260
Latest

clang/lib/CodeGen/Targets/X86.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,15 @@ class X86_64ABIInfo : public ABIInfo {
13341334
return T.isOSLinux() || T.isOSNetBSD();
13351335
}
13361336

1337+
bool returnCXXRecordGreaterThan128InMem() const {
1338+
// Clang <= 20.0 did not do this.
1339+
if (getContext().getLangOpts().getClangABICompat() <=
1340+
LangOptions::ClangABI::Ver20)
1341+
return false;
1342+
1343+
return true;
1344+
}
1345+
13371346
X86AVXABILevel AVXLevel;
13381347
// Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
13391348
// 64-bit hardware.
@@ -2067,6 +2076,13 @@ void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, Class &Lo,
20672076
classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
20682077
Lo = merge(Lo, FieldLo);
20692078
Hi = merge(Hi, FieldHi);
2079+
if (returnCXXRecordGreaterThan128InMem() &&
2080+
(Size > 128 && (Size != getContext().getTypeSize(I.getType()) ||
2081+
Size > getNativeVectorSizeForAVXABI(AVXLevel)))) {
2082+
// The only case a 256(or 512)-bit wide vector could be used to return
2083+
// is when CXX record contains a single 256(or 512)-bit element.
2084+
Lo = Memory;
2085+
}
20702086
if (Lo == Memory || Hi == Memory) {
20712087
postMerge(Size, Lo, Hi);
20722088
return;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang %s -S --target=x86_64-unknown-linux-gnu -emit-llvm -O2 -march=x86-64-v3 -o - | FileCheck %s
2+
3+
using UInt64x2 = unsigned long long __attribute__((__vector_size__(16), may_alias));
4+
5+
template<int id>
6+
struct XMM1 {
7+
UInt64x2 x;
8+
};
9+
10+
struct XMM2 : XMM1<0>, XMM1<1> {
11+
};
12+
13+
// CHECK: define{{.*}} @_Z3foov({{.*}} [[ARG:%.*]]){{.*}}
14+
// CHECK-NEXT: entry:
15+
// CHECK-NEXT: store {{.*}}, ptr [[ARG]]{{.*}}
16+
// CHECK-NEXT: [[TMP1:%.*]] = getelementptr {{.*}}, ptr [[ARG]]{{.*}}
17+
// CHECK-NEXT: store {{.*}}, ptr [[TMP1]]{{.*}}
18+
XMM2 foo() {
19+
XMM2 result;
20+
((XMM1<0>*)&result)->x = UInt64x2{1, 2};
21+
((XMM1<1>*)&result)->x = UInt64x2{3, 4};
22+
return result;
23+
}

0 commit comments

Comments
 (0)