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,15 @@ /// If \p T is null pointer, assume the target in ASTContext. MangleContext *createMangleContext(const TargetInfo *T = nullptr); +private: + llvm::DenseMap::type, + std::shared_ptr> SharedMangleContexts; + +public: + /// If \p T is null pointer, assume the target in ASTContext. + std::shared_ptr + &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,37 @@ 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"); +} + +std::shared_ptr& +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::shared_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; + std::shared_ptr 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(); 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; + std::shared_ptr MangleCtx; CGCXXABI(CodeGenModule &CGM) - : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {} + : CGM(CGM), MangleCtx(CGM.getContext().getSharedMangleContext()) {} protected: ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) {