diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 518121e200190..bfcc01829bce0 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -178,7 +178,7 @@ unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) { DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( const DIGlobalVariable *GV, ArrayRef GlobalExprs) { // Check for pre-existence. - if (DIE *Die = getDIE(GV)) + if (DIE *Die = getDIEs(GV).getVariableDIE(GV)) return Die; assert(GV); @@ -795,7 +795,9 @@ DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { auto *VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); - insertDIE(DV.getVariable(), VariableDie); + getDIEs(DV.getVariable()) + .getLVs() + .insertDIE(DV.getVariable(), &DV, VariableDie, Abstract); DV.setDIE(*VariableDie); // Abstract variables don't get common attributes later, so apply them now. if (Abstract) { @@ -1010,7 +1012,9 @@ DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope) { auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag()); - insertDIE(DL.getLabel(), LabelDie); + getDIEs(DL.getLabel()) + .getLabels() + .insertDIE(DL.getLabel(), &DL, LabelDie, Scope.isAbstractScope()); DL.setDIE(*LabelDie); if (Scope.isAbstractScope()) diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h index a3bbc8364599d..902cd8cac8d78 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h @@ -79,16 +79,10 @@ class DwarfCompileUnit final : public DwarfUnit { // List of concrete lexical block scopes belong to subprograms within this CU. DenseMap LexicalBlockDIEs; - // List of abstract local scopes (either DISubprogram or DILexicalBlock). - DenseMap AbstractLocalScopeDIEs; - SmallPtrSet FinalizedAbstractSubprograms; - // List of inlined lexical block scopes that belong to subprograms within this // CU. DenseMap> InlinedLocalScopeDIEs; - DenseMap> AbstractEntities; - /// DWO ID for correlating skeleton and split units. uint64_t DWOId = 0; @@ -126,22 +120,20 @@ class DwarfCompileUnit final : public DwarfUnit { bool isDwoUnit() const override; + DwarfInfoHolder &getDIEs(const DINode *N) { return DwarfUnit::getDIEs(N); } + + DwarfInfoHolder &getDIEs() { return getDIEs(nullptr); } + DenseMap &getAbstractScopeDIEs() { - if (isDwoUnit() && !DD->shareAcrossDWOCUs()) - return AbstractLocalScopeDIEs; - return DU->getAbstractScopeDIEs(); + return getDIEs().getAbstractScopeDIEs(); } DenseMap> &getAbstractEntities() { - if (isDwoUnit() && !DD->shareAcrossDWOCUs()) - return AbstractEntities; - return DU->getAbstractEntities(); + return getDIEs().getAbstractEntities(); } auto &getFinalizedAbstractSubprograms() { - if (isDwoUnit() && !DD->shareAcrossDWOCUs()) - return FinalizedAbstractSubprograms; - return DU->getFinalizedAbstractSubprograms(); + return getDIEs().getFinalizedAbstractSubprograms(); } void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override; diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index d751a7f9f01ef..2080b592d2fb8 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -502,7 +502,8 @@ void DwarfDebug::addSubprogramNames( // well into the name table. Only do that if we are going to actually emit // that name. if (LinkageName != "" && SP->getName() != LinkageName && - (useAllLinkageNames() || InfoHolder.getAbstractScopeDIEs().lookup(SP))) + (useAllLinkageNames() || + InfoHolder.getDIEs().getAbstractScopeDIEs().lookup(SP))) addAccelName(Unit, NameTableKind, LinkageName, Die); // If this is an Objective-C selector name add it to the ObjC accelerator diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h index ef1524d875c84..18388c3b7c1e9 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfFile.h @@ -15,9 +15,12 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/CodeGen/DIE.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/Support/Allocator.h" +#include #include #include +#include #include namespace llvm { @@ -26,9 +29,6 @@ class AsmPrinter; class DbgEntity; class DbgVariable; class DbgLabel; -class DINode; -class DILocalScope; -class DISubprogram; class DwarfCompileUnit; class DwarfUnit; class LexicalScope; @@ -53,6 +53,137 @@ struct RangeSpanList { SmallVector Ranges; }; +/// Tracks abstract and concrete DIEs for debug info entities of a certain type. +template class DINodeInfoHolder { +public: + using AbstractMapT = DenseMap; + using ConcreteMapT = + DenseMap>; + +private: + AbstractMapT AbstractMap; + ConcreteMapT ConcreteMap; + +public: + void insertAbstractDIE(const DINodeT *N, DIE *D) { + auto [_, Inserted] = AbstractMap.try_emplace(N, D); + assert(Inserted && "Duplicate abstract DIE for debug info node"); + } + + void insertConcreteDIE(const DINodeT *N, const DbgEntityT *E, DIE *D) { + auto [_, Inserted] = ConcreteMap[N].try_emplace(E, D); + assert(Inserted && "Duplicate concrete DIE for debug info node"); + } + + void insertDIE(const DINodeT *N, const DbgEntityT *E, DIE *D, bool Abstract) { + if (Abstract) + insertAbstractDIE(N, D); + else + insertConcreteDIE(N, E, D); + } + + DIE *getAbstractDIE(const DINodeT *N) const { return AbstractMap.lookup(N); } + + std::optional< + std::reference_wrapper> + getConcreteDIEs(const DINodeT *N) const { + if (auto I = ConcreteMap.find(N); I != ConcreteMap.end()) + return std::make_optional(std::ref(I->second)); + return std::nullopt; + } + + DIE *getConcreteDIE(const DINodeT *N, const DbgEntityT *E) const { + if (auto I = getConcreteDIEs(N)) + return I->get().lookup(E); + return nullptr; + } + + DIE *getAnyConcreteDIE(const DINodeT *N) const { + if (auto I = getConcreteDIEs(N)) + return I->get().empty() ? nullptr : I->get().begin()->second; + return nullptr; + } + + /// Returns abstract DIE for the entity. + /// If no abstract DIE was created, returns any concrete DIE for the entity. + DIE *getDIE(const DINodeT *N) const { + if (DIE *D = getAbstractDIE(N)) + return D; + + return getAnyConcreteDIE(N); + } + + AbstractMapT &getAbstractDIEs() { return AbstractMap; } +}; + +/// Tracks DIEs for debug info entites. +/// These DIEs can be shared across CUs, that is why we keep the map here +/// instead of in DwarfCompileUnit. +class DwarfInfoHolder { + /// DIEs of local DbgVariables. + DINodeInfoHolder LVHolder; + /// DIEs of labels. + DINodeInfoHolder LabelHolder; + DenseMap> AbstractEntities; + // List of abstract local scopes (either DISubprogram or DILexicalBlock). + DenseMap AbstractLocalScopeDIEs; + /// Keeps track of abstract subprograms to populate them only once. + // FIXME: merge creation and population of abstract scopes. + SmallPtrSet FinalizedAbstractSubprograms; + + /// Other DINodes with the corresponding DIEs. + DenseMap MDNodeToDieMap; + +public: + void insertDIE(const DINode *N, DIE *Die) { + assert((!isa(N) && !isa(N)) && + "Use getLabels().insertDIE() for labels or getLVs().insertDIE() for " + "local variables"); + auto [_, Inserted] = MDNodeToDieMap.try_emplace(N, Die); + assert((Inserted || isa(N) || isa(N)) && + "DIE for this DINode has already been added"); + } + + void insertDIE(DIE *D) { MDNodeToDieMap.try_emplace(nullptr, D); } + + DIE *getDIE(const DINode *N) const { + DIE *D = MDNodeToDieMap.lookup(N); + assert((!D || (!isa(N) && !isa(N))) && + "Use getLabels().getDIE() for labels or getLVs().getDIE() for " + "local variables"); + return D; + } + + auto &getLVs() { return LVHolder; } + auto &getLVs() const { return LVHolder; } + + auto &getLabels() { return LabelHolder; } + auto &getLabels() const { return LabelHolder; } + + /// For a global variable, returns DIE of the variable. + /// + /// For a local variable, returns abstract DIE of the variable. + /// If no abstract DIE was created, returns any concrete DIE of the variable. + DIE *getVariableDIE(const DIVariable *V) const { + if (auto *LV = dyn_cast(V)) + if (DIE *D = getLVs().getDIE(LV)) + return D; + return getDIE(V); + } + + DenseMap &getAbstractScopeDIEs() { + return AbstractLocalScopeDIEs; + } + + DenseMap> &getAbstractEntities() { + return AbstractEntities; + } + + auto &getFinalizedAbstractSubprograms() { + return FinalizedAbstractSubprograms; + } +}; + class DwarfFile { // Target of Dwarf emission, used for sizing of abbreviations. AsmPrinter *Asm; @@ -93,17 +224,7 @@ class DwarfFile { using LabelList = SmallVector; DenseMap ScopeLabels; - // Collection of abstract subprogram DIEs. - DenseMap AbstractLocalScopeDIEs; - DenseMap> AbstractEntities; - /// Keeps track of abstract subprograms to populate them only once. - // FIXME: merge creation and population of abstract scopes. - SmallPtrSet FinalizedAbstractSubprograms; - - /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can - /// be shared across CUs, that is why we keep the map here instead - /// of in DwarfCompileUnit. - DenseMap DITypeNodeToDieMap; + DwarfInfoHolder InfoHolder; public: DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA); @@ -171,25 +292,7 @@ class DwarfFile { return ScopeLabels; } - DenseMap &getAbstractScopeDIEs() { - return AbstractLocalScopeDIEs; - } - - DenseMap> &getAbstractEntities() { - return AbstractEntities; - } - - auto &getFinalizedAbstractSubprograms() { - return FinalizedAbstractSubprograms; - } - - void insertDIE(const MDNode *TypeMD, DIE *Die) { - DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die)); - } - - DIE *getDIE(const MDNode *TypeMD) { - return DITypeNodeToDieMap.lookup(TypeMD); - } + DwarfInfoHolder &getDIEs() { return InfoHolder; } }; } // end namespace llvm diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp index aa078f3f81d49..73579e41605e4 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -188,28 +188,16 @@ bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const { // together. if (isDwoUnit() && !DD->shareAcrossDWOCUs()) return false; - return (isa(D) || - (isa(D) && !cast(D)->isDefinition())) && - !DD->generateTypeUnits(); -} - -DIE *DwarfUnit::getDIE(const DINode *D) const { - if (isShareableAcrossCUs(D)) - return DU->getDIE(D); - return MDNodeToDieMap.lookup(D); + return !D || ((isa(D) || (isa(D) && + !cast(D)->isDefinition())) && + !DD->generateTypeUnits()); } void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) { - if (isShareableAcrossCUs(Desc)) { - DU->insertDIE(Desc, D); - return; - } - MDNodeToDieMap.insert(std::make_pair(Desc, D)); + getDIEs(Desc).insertDIE(Desc, D); } -void DwarfUnit::insertDIE(DIE *D) { - MDNodeToDieMap.insert(std::make_pair(nullptr, D)); -} +void DwarfUnit::insertDIE(DIE *D) { InfoHolder.insertDIE(D); } void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) { if (DD->getDwarfVersion() >= 4) @@ -803,7 +791,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) { addString(Buffer, dwarf::DW_AT_name, Name); if (DIVariable *Var = STy->getStringLength()) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE); } else if (DIExpression *Expr = STy->getStringLengthExp()) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1122,8 +1110,8 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { constructTypeDIE(VariantPart, Composite); } } else if (Tag == dwarf::DW_TAG_namelist) { - auto *Var = dyn_cast(Element); - auto *VarDIE = getDIE(Var); + auto *Var = dyn_cast(Element); + auto *VarDIE = getDIEs(Var).getVariableDIE(Var); if (VarDIE) { DIE &ItemDie = createAndAddDIE(dwarf::DW_TAG_namelist_item, Buffer); addDIEEntry(ItemDie, dwarf::DW_AT_namelist_item, *VarDIE); @@ -1185,7 +1173,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { if (auto *Var = dyn_cast_or_null(CTy->getRawSizeInBits())) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(Buffer, dwarf::DW_AT_bit_size, *VarDIE); } else if (auto *Exp = dyn_cast_or_null(CTy->getRawSizeInBits())) { @@ -1416,7 +1404,8 @@ bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP, StringRef LinkageName = SP->getLinkageName(); // Always emit linkage name for abstract subprograms. if (DeclLinkageName != LinkageName && - (DD->useAllLinkageNames() || DU->getAbstractScopeDIEs().lookup(SP))) + (DD->useAllLinkageNames() || + DU->getDIEs().getAbstractScopeDIEs().lookup(SP))) addLinkageName(SPDie, LinkageName); if (!DeclDie) @@ -1586,7 +1575,7 @@ void DwarfUnit::constructSubrangeDIE(DIE &DW_Subrange, const DISubrangeType *SR, auto AddBoundTypeEntry = [&](dwarf::Attribute Attr, DISubrangeType::BoundType Bound) -> void { if (auto *BV = dyn_cast_if_present(Bound)) { - if (auto *VarDIE = getDIE(BV)) + if (auto *VarDIE = getDIEs(BV).getVariableDIE(BV)) addDIEEntry(DW_Subrange, Attr, *VarDIE); } else if (auto *BE = dyn_cast_if_present(Bound)) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1628,7 +1617,7 @@ void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR) { auto AddBoundTypeEntry = [&](dwarf::Attribute Attr, DISubrange::BoundType Bound) -> void { if (auto *BV = dyn_cast_if_present(Bound)) { - if (auto *VarDIE = getDIE(BV)) + if (auto *VarDIE = getDIEs(BV).getVariableDIE(BV)) addDIEEntry(DW_Subrange, Attr, *VarDIE); } else if (auto *BE = dyn_cast_if_present(Bound)) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1670,7 +1659,7 @@ void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer, auto AddBoundTypeEntry = [&](dwarf::Attribute Attr, DIGenericSubrange::BoundType Bound) -> void { if (auto *BV = dyn_cast_if_present(Bound)) { - if (auto *VarDIE = getDIE(BV)) + if (auto *VarDIE = getDIEs(BV).getVariableDIE(BV)) addDIEEntry(DwGenericSubrange, Attr, *VarDIE); } else if (auto *BE = dyn_cast_if_present(Bound)) { if (BE->isConstant() && @@ -1749,7 +1738,7 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { } if (DIVariable *Var = CTy->getDataLocation()) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE); } else if (DIExpression *Expr = CTy->getDataLocationExp()) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1760,7 +1749,7 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { } if (DIVariable *Var = CTy->getAssociated()) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE); } else if (DIExpression *Expr = CTy->getAssociatedExp()) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1771,7 +1760,7 @@ void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { } if (DIVariable *Var = CTy->getAllocated()) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE); } else if (DIExpression *Expr = CTy->getAllocatedExp()) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1896,7 +1885,7 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { if (DT->getRawSizeInBits() == nullptr) { // No size, just ignore. } else if (auto *Var = dyn_cast(DT->getRawSizeInBits())) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(MemberDie, dwarf::DW_AT_bit_size, *VarDIE); } else if (auto *Exp = dyn_cast(DT->getRawSizeInBits())) { DIELoc *Loc = new (DIEValueAllocator) DIELoc; @@ -1921,7 +1910,7 @@ DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { // See https://dwarfstd.org/issues/250501.1.html if (auto *Var = dyn_cast_or_null(DT->getRawOffsetInBits())) { if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) { - if (auto *VarDIE = getDIE(Var)) + if (auto *VarDIE = getDIEs(Var).getVariableDIE(Var)) addDIEEntry(MemberDie, dwarf::DW_AT_data_bit_offset, *VarDIE); } } else if (auto *Expr = diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h index 9288d7edbf156..6c297e1d0667f 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h @@ -61,7 +61,7 @@ class DwarfUnit : public DIEUnit { /// Tracks the mapping of unit level debug information variables to debug /// information entries. - DenseMap MDNodeToDieMap; + DwarfInfoHolder InfoHolder; /// A list of all the DIEBlocks in use. std::vector DIEBlocks; @@ -139,7 +139,7 @@ class DwarfUnit : public DIEUnit { /// We delegate the request to DwarfDebug when the MDNode can be part of the /// type system, since DIEs for the type system can be shared across CUs and /// the mappings are kept in DwarfDebug. - DIE *getDIE(const DINode *D) const; + DIE *getDIE(const DINode *D) const { return getDIEs(D).getDIE(D); } /// Returns a fresh newly allocated DIELoc. DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; } @@ -153,6 +153,18 @@ class DwarfUnit : public DIEUnit { void insertDIE(DIE *D); + const DwarfInfoHolder &getDIEs(const DINode *N) const { + if (isShareableAcrossCUs(N)) + return DU->getDIEs(); + + return InfoHolder; + } + + DwarfInfoHolder &getDIEs(const DINode *N) { + return const_cast( + const_cast(this)->getDIEs(N)); + } + /// Add a flag that is true to the DIE. void addFlag(DIE &Die, dwarf::Attribute Attribute);