Skip to content

Commit 3ab4ae9

Browse files
[clang codegen] Fix MS ABI detection of user-provided constructors. (#90151)
In the context of determining whether a class counts as an "aggregate", a constructor template counts as a user-provided constructor. Fixes #86384
1 parent 7eac39f commit 3ab4ae9

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ ABI Changes in This Version
6363
MSVC uses a different mangling for these objects, compatibility is not affected.
6464
(#GH85423).
6565

66+
- Fixed Microsoft calling convention for returning certain classes with a
67+
templated constructor. If a class has a templated constructor, it should
68+
be returned indirectly even if it meets all the other requirements for
69+
returning a class in a register. This affects some uses of std::pair.
70+
(#GH86384).
71+
6672
AST Dumping Potentially Breaking Changes
6773
----------------------------------------
6874

clang/lib/CodeGen/MicrosoftCXXABI.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,9 +1131,15 @@ static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty,
11311131
return false;
11321132
if (RD->hasNonTrivialCopyAssignment())
11331133
return false;
1134-
for (const CXXConstructorDecl *Ctor : RD->ctors())
1135-
if (Ctor->isUserProvided())
1136-
return false;
1134+
for (const Decl *D : RD->decls()) {
1135+
if (auto *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1136+
if (Ctor->isUserProvided())
1137+
return false;
1138+
} else if (auto *Template = dyn_cast<FunctionTemplateDecl>(D)) {
1139+
if (isa<CXXConstructorDecl>(Template->getTemplatedDecl()))
1140+
return false;
1141+
}
1142+
}
11371143
if (RD->hasNonTrivialDestructor())
11381144
return false;
11391145
return true;

clang/test/CodeGen/arm64-microsoft-arguments.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,18 @@ S11 f11() {
201201
S11 x;
202202
return func11(x);
203203
}
204+
205+
// GH86384
206+
// Pass and return object with template constructor (pass directly,
207+
// return indirectly).
208+
// CHECK: define dso_local void @"?f12@@YA?AUS12@@XZ"(ptr dead_on_unwind inreg noalias writable sret(%struct.S12) align 4 {{.*}})
209+
// CHECK: call void @"?func12@@YA?AUS12@@U1@@Z"(ptr dead_on_unwind inreg writable sret(%struct.S12) align 4 {{.*}}, i64 {{.*}})
210+
struct S12 {
211+
template<typename T> S12(T*) {}
212+
int x;
213+
};
214+
S12 func12(S12 x);
215+
S12 f12() {
216+
S12 x((int*)0);
217+
return func12(x);
218+
}

0 commit comments

Comments
 (0)