diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -677,9 +677,6 @@ ~CUDAConstantEvalContextRAII() { Ctx.CUDAConstantEvalCtx = SavedCtx; } }; - /// Current CUDA name mangling is for device name in host compilation. - bool CUDAMangleDeviceNameInHostCompilation = false; - /// Returns the dynamic AST node parent map context. ParentMapContext &getParentMapContext(); @@ -3044,7 +3041,8 @@ DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD); void setManglingNumber(const NamedDecl *ND, unsigned Number); - unsigned getManglingNumber(const NamedDecl *ND) const; + unsigned getManglingNumber(const NamedDecl *ND, + bool ForAuxTarget = false) const; void setStaticLocalNumber(const VarDecl *VD, unsigned Number); unsigned getStaticLocalNumber(const VarDecl *VD) const; diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h --- a/clang/include/clang/AST/Mangle.h +++ b/clang/include/clang/AST/Mangle.h @@ -54,6 +54,9 @@ ASTContext &Context; DiagnosticsEngine &Diags; const ManglerKind Kind; + /// For aux target. If true, uses mangling number for aux target from + /// ASTContext. + bool IsAux = false; llvm::DenseMap GlobalBlockIds; llvm::DenseMap LocalBlockIds; @@ -62,10 +65,11 @@ public: ManglerKind getKind() const { return Kind; } - explicit MangleContext(ASTContext &Context, - DiagnosticsEngine &Diags, - ManglerKind Kind) - : Context(Context), Diags(Diags), Kind(Kind) {} + bool isAux() const { return IsAux; } + + explicit MangleContext(ASTContext &Context, DiagnosticsEngine &Diags, + ManglerKind Kind, bool IsAux = false) + : Context(Context), Diags(Diags), Kind(Kind), IsAux(IsAux) {} virtual ~MangleContext() { } @@ -172,8 +176,9 @@ public: using DiscriminatorOverrideTy = llvm::Optional (*)(ASTContext &, const NamedDecl *); - explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D) - : MangleContext(C, D, MK_Itanium) {} + explicit ItaniumMangleContext(ASTContext &C, DiagnosticsEngine &D, + bool IsAux = false) + : MangleContext(C, D, MK_Itanium, IsAux) {} virtual void mangleCXXVTable(const CXXRecordDecl *RD, raw_ostream &) = 0; virtual void mangleCXXVTT(const CXXRecordDecl *RD, raw_ostream &) = 0; @@ -201,17 +206,19 @@ return C->getKind() == MK_Itanium; } - static ItaniumMangleContext *create(ASTContext &Context, - DiagnosticsEngine &Diags); + static ItaniumMangleContext * + create(ASTContext &Context, DiagnosticsEngine &Diags, bool IsAux = false); static ItaniumMangleContext *create(ASTContext &Context, DiagnosticsEngine &Diags, - DiscriminatorOverrideTy Discriminator); + DiscriminatorOverrideTy Discriminator, + bool IsAux = false); }; class MicrosoftMangleContext : public MangleContext { public: - explicit MicrosoftMangleContext(ASTContext &C, DiagnosticsEngine &D) - : MangleContext(C, D, MK_Microsoft) {} + explicit MicrosoftMangleContext(ASTContext &C, DiagnosticsEngine &D, + bool IsAux = false) + : MangleContext(C, D, MK_Microsoft, IsAux) {} /// Mangle vftable symbols. Only a subset of the bases along the path /// to the vftable are included in the name. It's up to the caller to pick @@ -270,8 +277,8 @@ return C->getKind() == MK_Microsoft; } - static MicrosoftMangleContext *create(ASTContext &Context, - DiagnosticsEngine &Diags); + static MicrosoftMangleContext * + create(ASTContext &Context, DiagnosticsEngine &Diags, bool IsAux = false); }; class ASTNameGenerator { diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -11692,9 +11692,11 @@ if (const auto *RD = dyn_cast(ND)) return RD->getDeviceLambdaManglingNumber(); return llvm::None; - }); + }, + /*IsAux=*/true); case TargetCXXABI::Microsoft: - return MicrosoftMangleContext::create(*this, getDiagnostics()); + return MicrosoftMangleContext::create(*this, getDiagnostics(), + /*IsAux=*/true); } llvm_unreachable("Unsupported ABI"); } @@ -11760,15 +11762,18 @@ MangleNumbers[ND] = Number; } -unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { +unsigned ASTContext::getManglingNumber(const NamedDecl *ND, + bool ForAuxTarget) const { auto I = MangleNumbers.find(ND); unsigned Res = I != MangleNumbers.end() ? I->second : 1; - if (!LangOpts.CUDA || LangOpts.CUDAIsDevice) - return Res; - // CUDA/HIP host compilation encodes host and device mangling numbers // as lower and upper half of 32 bit integer. - Res = CUDAMangleDeviceNameInHostCompilation ? Res >> 16 : Res & 0xFFFF; + if (LangOpts.CUDA && !LangOpts.CUDAIsDevice) { + Res = ForAuxTarget ? Res >> 16 : Res & 0xFFFF; + } else { + assert(!ForAuxTarget && "Only CUDA/HIP host compilation supports mangling " + "number for aux target"); + } return Res > 1 ? Res : 1; } diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -78,8 +78,8 @@ public: explicit ItaniumMangleContextImpl( ASTContext &Context, DiagnosticsEngine &Diags, - DiscriminatorOverrideTy DiscriminatorOverride) - : ItaniumMangleContext(Context, Diags), + DiscriminatorOverrideTy DiscriminatorOverride, bool IsAux = false) + : ItaniumMangleContext(Context, Diags, IsAux), DiscriminatorOverride(DiscriminatorOverride) {} /// @name Mangler Entry Points @@ -143,7 +143,7 @@ // Use the canonical number for externally visible decls. if (ND->isExternallyVisible()) { - unsigned discriminator = getASTContext().getManglingNumber(ND); + unsigned discriminator = getASTContext().getManglingNumber(ND, isAux()); if (discriminator == 1) return false; disc = discriminator - 2; @@ -1570,7 +1570,8 @@ } if (TD->isExternallyVisible()) { - unsigned UnnamedMangle = getASTContext().getManglingNumber(TD); + unsigned UnnamedMangle = + getASTContext().getManglingNumber(TD, Context.isAux()); Out << "Ut"; if (UnnamedMangle > 1) Out << UnnamedMangle - 2; @@ -6531,16 +6532,20 @@ } ItaniumMangleContext *ItaniumMangleContext::create(ASTContext &Context, - DiagnosticsEngine &Diags) { + DiagnosticsEngine &Diags, + bool IsAux) { return new ItaniumMangleContextImpl( Context, Diags, [](ASTContext &, const NamedDecl *) -> llvm::Optional { return llvm::None; - }); + }, + IsAux); } ItaniumMangleContext * ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags, - DiscriminatorOverrideTy DiscriminatorOverride) { - return new ItaniumMangleContextImpl(Context, Diags, DiscriminatorOverride); + DiscriminatorOverrideTy DiscriminatorOverride, + bool IsAux) { + return new ItaniumMangleContextImpl(Context, Diags, DiscriminatorOverride, + IsAux); } diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -147,7 +147,8 @@ SmallString<16> AnonymousNamespaceHash; public: - MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags); + MicrosoftMangleContextImpl(ASTContext &Context, DiagnosticsEngine &Diags, + bool IsAux = false); bool shouldMangleCXXName(const NamedDecl *D) override; bool shouldMangleStringLiteral(const StringLiteral *SL) override; void mangleCXXName(GlobalDecl GD, raw_ostream &Out) override; @@ -221,7 +222,7 @@ // Use the canonical number for externally visible decls. if (ND->isExternallyVisible()) { - disc = getASTContext().getManglingNumber(ND); + disc = getASTContext().getManglingNumber(ND, isAux()); return true; } @@ -459,8 +460,9 @@ } MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context, - DiagnosticsEngine &Diags) - : MicrosoftMangleContext(Context, Diags) { + DiagnosticsEngine &Diags, + bool IsAux) + : MicrosoftMangleContext(Context, Diags, IsAux) { // To mangle anonymous namespaces, hash the path to the main source file. The // path should be whatever (probably relative) path was passed on the command // line. The goal is for the compiler to produce the same output regardless of @@ -3944,7 +3946,8 @@ Mangler.getStream() << '@'; } -MicrosoftMangleContext * -MicrosoftMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) { - return new MicrosoftMangleContextImpl(Context, Diags); +MicrosoftMangleContext *MicrosoftMangleContext::create(ASTContext &Context, + DiagnosticsEngine &Diags, + bool IsAux) { + return new MicrosoftMangleContextImpl(Context, Diags, IsAux); } diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -24,7 +24,6 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/ReplaceConstant.h" #include "llvm/Support/Format.h" -#include "llvm/Support/SaveAndRestore.h" using namespace clang; using namespace CodeGen; @@ -261,8 +260,6 @@ } std::string CGNVCUDARuntime::getDeviceSideName(const NamedDecl *ND) { - llvm::SaveAndRestore MangleAsDevice( - CGM.getContext().CUDAMangleDeviceNameInHostCompilation, true); GlobalDecl GD; // D could be either a kernel or a variable. if (auto *FD = dyn_cast(ND))