From 1635c9f0a045570754fadc744ee7aa16fd482158 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 12 May 2017 16:23:53 +0000 Subject: [PATCH] Simplify DINamespace caching in CGDebugInfo This addresses review feedback from r302840. By not canonicalizing namespace decls and using lexical decl context instead of lookuing up the semantic decl context we can take advantage of the fact that DINamespaces a reuniqued. This way non-module debug info is unchanged and module debug info still gets distinct namespace declarations when they ocur in different modules. Thanks to Richard Smith for pointing this out! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@302915 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDebugInfo.cpp | 36 ++++++++++++++---------------------- lib/CodeGen/CGDebugInfo.h | 11 ++--------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index d15ae06b45..9d77c61bd5 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -208,10 +208,8 @@ llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, } // Check namespace. - if (const auto *NSDecl = dyn_cast(Context)) { - auto *ParentModule = dyn_cast(Default); - return getOrCreateNamespace(NSDecl, ParentModule); - } + if (const auto *NSDecl = dyn_cast(Context)) + return getOrCreateNamespace(NSDecl); if (const auto *RDecl = dyn_cast(Context)) if (!RDecl->isDependentType()) @@ -2862,8 +2860,8 @@ void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, if (DebugKind >= codegenoptions::LimitedDebugInfo) { if (const NamespaceDecl *NSDecl = - dyn_cast_or_null(FD->getDeclContext())) - FDContext = getOrCreateNamespace(NSDecl, getParentModuleOrNull(FD)); + dyn_cast_or_null(FD->getLexicalDeclContext())) + FDContext = getOrCreateNamespace(NSDecl); else if (const RecordDecl *RDecl = dyn_cast_or_null(FD->getDeclContext())) { llvm::DIScope *Mod = getParentModuleOrNull(RDecl); @@ -3963,7 +3961,7 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { CGM.getCodeGenOpts().DebugExplicitImport) { DBuilder.createImportedModule( getCurrentContextDescriptor(cast(UD.getDeclContext())), - getOrCreateNamespace(NSDecl, getParentModuleOrNull(&UD)), + getOrCreateNamespace(NSDecl), getLineNumber(UD.getLocation())); } } @@ -4023,32 +4021,26 @@ CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { else R = DBuilder.createImportedDeclaration( getCurrentContextDescriptor(cast(NA.getDeclContext())), - getOrCreateNamespace(cast(NA.getAliasedNamespace()), - getParentModuleOrNull(&NA)), + getOrCreateNamespace(cast(NA.getAliasedNamespace())), getLineNumber(NA.getLocation()), NA.getName()); VH.reset(R); return R; } llvm::DINamespace * -CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl, - llvm::DIModule *ParentModule) { - NSDecl = NSDecl->getCanonicalDecl(); - // The AST merges NamespaceDecls, but for module debug info it is important to - // put a namespace decl (or rather its children) into the correct - // (sub-)module, so use the parent module of the decl that triggered this - // namespace to be serialized as a second key. - NamespaceKey Key = {NSDecl, ParentModule}; - auto I = NamespaceCache.find(Key); +CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) { + // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued + // if necessary, and this way multiple declarations of the same namespace in + // different parent modules stay distinct. + auto I = NamespaceCache.find(NSDecl); if (I != NamespaceCache.end()) return cast(I->second); llvm::DIScope *Context = getDeclContextDescriptor(NSDecl); // Don't trust the context if it is a DIModule (see comment above). - llvm::DINamespace *NS = DBuilder.createNameSpace( - isa(Context) ? ParentModule : Context, NSDecl->getName(), - NSDecl->isInline()); - NamespaceCache[Key].reset(NS); + llvm::DINamespace *NS = + DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline()); + NamespaceCache[NSDecl].reset(NS); return NS; } diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h index beafd25f9b..7de48f2789 100644 --- a/lib/CodeGen/CGDebugInfo.h +++ b/lib/CodeGen/CGDebugInfo.h @@ -125,8 +125,7 @@ class CGDebugInfo { /// Cache declarations relevant to DW_TAG_imported_declarations (C++ /// using declarations) that aren't covered by other more specific caches. llvm::DenseMap DeclCache; - typedef std::pair NamespaceKey; - llvm::DenseMap NamespaceCache; + llvm::DenseMap NamespaceCache; llvm::DenseMap NamespaceAliasCache; llvm::DenseMap> @@ -197,13 +196,7 @@ class CGDebugInfo { llvm::DIType *getOrCreateVTablePtrType(llvm::DIFile *F); /// \return namespace descriptor for the given namespace decl. - /// - /// \return namespace descriptor for the given namespace decl. - /// \param ParentModule The parent module (or nullptr) of this particular - /// namespace decl. This needs to be passed in because - /// the AST merges namespace decls. - llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N, - llvm::DIModule *ParentModule); + llvm::DINamespace *getOrCreateNamespace(const NamespaceDecl *N); llvm::DIType *CreatePointerLikeType(llvm::dwarf::Tag Tag, const Type *Ty, QualType PointeeTy, llvm::DIFile *F); llvm::DIType *getOrCreateStructPtrType(StringRef Name, llvm::DIType *&Cache); -- 2.40.0