Index: include/clang/AST/ASTContext.h =================================================================== --- include/clang/AST/ASTContext.h +++ include/clang/AST/ASTContext.h @@ -24,6 +24,7 @@ #include "clang/AST/DeclarationName.h" #include "clang/AST/Expr.h" #include "clang/AST/ExternalASTSource.h" +#include "clang/AST/Mangle.h" #include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/PrettyPrinter.h" #include "clang/AST/RawCommentList.h" @@ -96,7 +97,6 @@ class DiagnosticsEngine; class Expr; class FixedPointSemantics; -class MangleContext; class MangleNumberingContext; class MaterializeTemporaryExpr; class MemberSpecializationInfo; @@ -2254,6 +2254,14 @@ /// If \p T is null pointer, assume the target in ASTContext. MangleContext *createMangleContext(const TargetInfo *T = nullptr); +private: + llvm::DenseMap::type, + std::unique_ptr> SharedMangleContexts; + +public: + /// If \p T is null pointer, assume the target in ASTContext. + MangleContext &getSharedMangleContext(const TargetInfo *T = nullptr); + void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, SmallVectorImpl &Ivars) const; Index: lib/AST/ASTContext.cpp =================================================================== --- lib/AST/ASTContext.cpp +++ lib/AST/ASTContext.cpp @@ -10048,10 +10048,8 @@ return VTContext.get(); } -MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { - if (!T) - T = Target; - switch (T->getCXXABI().getKind()) { +MangleContext::ManglerKind getManglerKindForABI(TargetCXXABI::Kind K) { + switch (K) { case TargetCXXABI::GenericAArch64: case TargetCXXABI::GenericItanium: case TargetCXXABI::GenericARM: @@ -10060,13 +10058,36 @@ case TargetCXXABI::iOS64: case TargetCXXABI::WebAssembly: case TargetCXXABI::WatchOS: - return ItaniumMangleContext::create(*this, getDiagnostics()); + return MangleContext::MK_Itanium; case TargetCXXABI::Microsoft: - return MicrosoftMangleContext::create(*this, getDiagnostics()); + return MangleContext::MK_Microsoft; } llvm_unreachable("Unsupported ABI"); } +MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { + if (!T) + T = Target; + switch (getManglerKindForABI(T->getCXXABI().getKind())) { + case MangleContext::MK_Itanium: + return ItaniumMangleContext::create(*this, getDiagnostics()); + case MangleContext::MK_Microsoft: + return MicrosoftMangleContext::create(*this, getDiagnostics()); + } + llvm_unreachable("Unsupported MangleContext"); +} + +MangleContext &ASTContext::getSharedMangleContext(const TargetInfo *T) { + if (!T) + T = Target; + auto Kind = getManglerKindForABI(T->getCXXABI().getKind()); + auto I = SharedMangleContexts.find(Kind); + if (I == SharedMangleContexts.end()) + I = SharedMangleContexts.insert({Kind, + std::unique_ptr(createMangleContext(T))}).first; + return *I->second; +} + CXXABI::~CXXABI() = default; size_t ASTContext::getSideTableAllocatedMemory() const { Index: lib/CodeGen/CGCUDANV.cpp =================================================================== --- lib/CodeGen/CGCUDANV.cpp +++ lib/CodeGen/CGCUDANV.cpp @@ -60,7 +60,7 @@ /// Whether we generate relocatable device code. bool RelocatableDeviceCode; /// Mangle context for device. - std::unique_ptr DeviceMC; + MangleContext &DeviceMC; llvm::FunctionCallee getSetupArgumentFn() const; llvm::FunctionCallee getLaunchFn() const; @@ -154,7 +154,7 @@ : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()), TheModule(CGM.getModule()), RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode), - DeviceMC(CGM.getContext().createMangleContext( + DeviceMC(CGM.getContext().getSharedMangleContext( CGM.getContext().getAuxTargetInfo())) { CodeGen::CodeGenTypes &Types = CGM.getTypes(); ASTContext &Ctx = CGM.getContext(); @@ -207,10 +207,10 @@ std::string CGNVCUDARuntime::getDeviceSideName(const Decl *D) { auto *ND = cast(D); std::string DeviceSideName; - if (DeviceMC->shouldMangleDeclName(ND)) { + if (DeviceMC.shouldMangleDeclName(ND)) { SmallString<256> Buffer; llvm::raw_svector_ostream Out(Buffer); - DeviceMC->mangleName(ND, Out); + DeviceMC.mangleName(ND, Out); DeviceSideName = Out.str(); } else DeviceSideName = ND->getIdentifier()->getName(); Index: lib/CodeGen/CGCXXABI.h =================================================================== --- lib/CodeGen/CGCXXABI.h +++ lib/CodeGen/CGCXXABI.h @@ -43,10 +43,10 @@ class CGCXXABI { protected: CodeGenModule &CGM; - std::unique_ptr MangleCtx; + MangleContext &MangleCtx; CGCXXABI(CodeGenModule &CGM) - : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} + : CGM(CGM), MangleCtx(CGM.getContext().getSharedMangleContext()) {} protected: ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) { @@ -94,7 +94,7 @@ /// Gets the mangle context. MangleContext &getMangleContext() { - return *MangleCtx; + return MangleCtx; } /// Returns true if the given constructor or destructor is one of the