Skip to content

[ItaniumMangle] Add substitutions for record types when mangling vtables #109970

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 7 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ ABI Changes in This Version
---------------------------

- Fixed Microsoft name mangling of placeholder, auto and decltype(auto), return types for MSVC 1920+. This change resolves incompatibilities with code compiled by MSVC 1920+ but will introduce incompatibilities with code compiled by earlier versions of Clang unless such code is built with the compiler option -fms-compatibility-version=19.14 to imitate the MSVC 1914 mangling behavior.
- Fix the Itanium mangling of CXXCtorVTable (#GH108015).

AST Dumping Potentially Breaking Changes
----------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ class LangOptionsBase {
/// in the initializers of members of local classes.
Ver18,

/// Attempt to be ABI-compatible with code generated by Clang 19.0.x.
/// This causes clang to:
/// - Incorrect Mangling of CXXCtorVTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// - Incorrect Mangling of CXXCtorVTable
/// - Incorrectly mangle CXX vtable substitutions in some cases...

It would be great if you could flesh out the behavior changes here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Updated.

Ver19,

/// Conform to the underlying platform's C and C++ ABIs as closely
/// as we can.
Latest
Expand Down
20 changes: 12 additions & 8 deletions clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class CXXNameMangler {
void mangleSeqID(unsigned SeqID);
void mangleName(GlobalDecl GD);
void mangleType(QualType T);
void mangleNameOrStandardSubstitution(const NamedDecl *ND);
void mangleCXXRecordDecl(const CXXRecordDecl *Record);
void mangleLambdaSig(const CXXRecordDecl *Lambda);
void mangleModuleNamePrefix(StringRef Name, bool IsPartition = false);
void mangleVendorQualifier(StringRef Name);
Expand Down Expand Up @@ -3029,9 +3029,13 @@ void CXXNameMangler::mangleType(QualType T) {
addSubstitution(T);
}

void CXXNameMangler::mangleNameOrStandardSubstitution(const NamedDecl *ND) {
if (!mangleStandardSubstitution(ND))
mangleName(ND);
void CXXNameMangler::mangleCXXRecordDecl(const CXXRecordDecl *Record) {
if (mangleSubstitution(Record))
return;
mangleName(Record);
if (isCompatibleWith(LangOptions::ClangABI::Ver19))
return;
addSubstitution(Record);
}

void CXXNameMangler::mangleType(const BuiltinType *T) {
Expand Down Expand Up @@ -7309,15 +7313,15 @@ void ItaniumMangleContextImpl::mangleCXXVTable(const CXXRecordDecl *RD,
// <special-name> ::= TV <type> # virtual table
CXXNameMangler Mangler(*this, Out);
Mangler.getStream() << "_ZTV";
Mangler.mangleNameOrStandardSubstitution(RD);
Mangler.mangleCXXRecordDecl(RD);
}

void ItaniumMangleContextImpl::mangleCXXVTT(const CXXRecordDecl *RD,
raw_ostream &Out) {
// <special-name> ::= TT <type> # VTT structure
CXXNameMangler Mangler(*this, Out);
Mangler.getStream() << "_ZTT";
Mangler.mangleNameOrStandardSubstitution(RD);
Mangler.mangleCXXRecordDecl(RD);
}

void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
Expand All @@ -7327,10 +7331,10 @@ void ItaniumMangleContextImpl::mangleCXXCtorVTable(const CXXRecordDecl *RD,
// <special-name> ::= TC <type> <offset number> _ <base type>
CXXNameMangler Mangler(*this, Out);
Mangler.getStream() << "_ZTC";
Mangler.mangleNameOrStandardSubstitution(RD);
Mangler.mangleCXXRecordDecl(RD);
Mangler.getStream() << Offset;
Mangler.getStream() << '_';
Mangler.mangleNameOrStandardSubstitution(Type);
Mangler.mangleCXXRecordDecl(Type);
}

void ItaniumMangleContextImpl::mangleCXXRTTI(QualType Ty, raw_ostream &Out) {
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,9 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
case LangOptions::ClangABI::Ver18:
GenerateArg(Consumer, OPT_fclang_abi_compat_EQ, "18.0");
break;
case LangOptions::ClangABI::Ver19:
GenerateArg(Consumer, OPT_fclang_abi_compat_EQ, "19.0");
break;
case LangOptions::ClangABI::Latest:
break;
}
Expand Down Expand Up @@ -4332,6 +4335,8 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.setClangABICompat(LangOptions::ClangABI::Ver17);
else if (Major <= 18)
Opts.setClangABICompat(LangOptions::ClangABI::Ver18);
else if (Major <= 19)
Opts.setClangABICompat(LangOptions::ClangABI::Ver19);
} else if (Ver != "latest") {
Diags.Report(diag::err_drv_invalid_value)
<< A->getAsString(Args) << A->getValue();
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CodeGenCXX/mangle-subst.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 | FileCheck %s
// RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-apple-darwin9 -fclang-abi-compat=19 | FileCheck %s --check-prefix=CHECK-CLANG-19

//CHECK: @_ZTCN16MangleCtorVTable4InstE0_NS_1A4ImplINS1_4WrapEEE
//CHECK-CLANG-19: @_ZTCN16MangleCtorVTable4InstE0_NS_1A4ImplINS0_4WrapEEE
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these two checks directly above the corresponding codes? Thanks!

Copy link
Member Author

@tcwzxx tcwzxx Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The construction vtable is a global variable, so it cannot be moved above the corresponding code.


struct X {};

Expand Down Expand Up @@ -96,3 +100,26 @@ typename X<T>::template Y<T>::type f(typename X<T>::template Y<T>::type2) { retu
// CHECK: @_ZN12ManglePrefix1fIiEENS_1XIT_E1YIS2_E4typeENS5_5type2E
template int f<int>(int);
}

namespace MangleCtorVTable {
namespace A {

class VBase {
public:
virtual ~VBase() {};
};

struct Wrap {};

template <typename T>
class Impl : public virtual VBase {
public:
};

} // namespace A

struct Inst : public A::Impl<A::Wrap> {};

void Test() { Inst a; }

}
1 change: 1 addition & 0 deletions clang/test/CodeGenCXX/mangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Y { };
//CHECK: @pr5966_i = external global
//CHECK: @_ZL8pr5966_j = internal global


// CHECK-LABEL: define{{.*}} zeroext i1 @_ZplRK1YRA100_P1X
bool operator+(const Y&, X* (&xs)[100]) { return false; }

Expand Down
Loading