From da27c25c19faef008c992a68387efcdc9fc0adb4 Mon Sep 17 00:00:00 2001 From: Alexander Yermolovich <43973793+ayermolo@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:39:28 -0700 Subject: [PATCH] [LLVM[NFC] Refactor to allow debug_names entries to conatain DIE offset (#69399) This is pre-cursor patch to enabling type units with DWARF5 acceleration tables. With this change it allows for entries to contain offsets directly, this way type units do not need to be preserved until .debug_names is written out. --- llvm/include/llvm/CodeGen/AccelTable.h | 63 ++++++++++--------- llvm/include/llvm/DWARFLinker/DWARFLinker.h | 5 +- llvm/include/llvm/DWARFLinker/DWARFStreamer.h | 2 +- llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp | 24 ++++--- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 10 ++- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h | 2 +- llvm/lib/DWARFLinker/DWARFStreamer.cpp | 12 ++-- .../DWARFLinkerParallel/DWARFEmitterImpl.cpp | 10 +-- .../DWARFLinkerParallel/DWARFEmitterImpl.h | 2 +- .../DWARFLinkerParallel/DWARFLinkerImpl.cpp | 4 +- 10 files changed, 68 insertions(+), 66 deletions(-) diff --git a/llvm/include/llvm/CodeGen/AccelTable.h b/llvm/include/llvm/CodeGen/AccelTable.h index d521b31e3d16..d4e21b2ac8e7 100644 --- a/llvm/include/llvm/CodeGen/AccelTable.h +++ b/llvm/include/llvm/CodeGen/AccelTable.h @@ -252,44 +252,46 @@ class DWARF5AccelTableData : public AccelTableData { public: static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); } - DWARF5AccelTableData(const DIE &Die) : Die(Die) {} + DWARF5AccelTableData(const DIE &Die, const DwarfCompileUnit &CU); + DWARF5AccelTableData(uint64_t DieOffset, unsigned DieTag, unsigned CUIndex) + : OffsetVal(DieOffset), DieTag(DieTag), UnitID(CUIndex) {} #ifndef NDEBUG void print(raw_ostream &OS) const override; #endif - const DIE &getDie() const { return Die; } - uint64_t getDieOffset() const { return Die.getOffset(); } - unsigned getDieTag() const { return Die.getTag(); } + uint64_t getDieOffset() const { + assert(std::holds_alternative(OffsetVal) && + "Accessing DIE Offset before normalizing."); + return std::get(OffsetVal); + } + unsigned getDieTag() const { return DieTag; } + unsigned getUnitID() const { return UnitID; } + void normalizeDIEToOffset() { + assert(std::holds_alternative(OffsetVal) && + "Accessing offset after normalizing."); + OffsetVal = std::get(OffsetVal)->getOffset(); + } protected: - const DIE &Die; + std::variant OffsetVal; + unsigned DieTag; + unsigned UnitID; - uint64_t order() const override { return Die.getOffset(); } + uint64_t order() const override { return getDieOffset(); } }; -class DWARF5AccelTableStaticData : public AccelTableData { +class DWARF5AccelTable : public AccelTable { public: - static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); } - - DWARF5AccelTableStaticData(uint64_t DieOffset, unsigned DieTag, - unsigned CUIndex) - : DieOffset(DieOffset), DieTag(DieTag), CUIndex(CUIndex) {} - -#ifndef NDEBUG - void print(raw_ostream &OS) const override; -#endif - - uint64_t getDieOffset() const { return DieOffset; } - unsigned getDieTag() const { return DieTag; } - unsigned getCUIndex() const { return CUIndex; } - -protected: - uint64_t DieOffset; - unsigned DieTag; - unsigned CUIndex; - - uint64_t order() const override { return DieOffset; } + /// Convert DIE entries to explicit offset. + /// Needs to be called after DIE offsets are computed. + void convertDieToOffset() { + for (auto &Entry : Entries) { + for (AccelTableData *Value : Entry.second.Values) { + static_cast(Value)->normalizeDIEToOffset(); + } + } + } }; void emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents, @@ -306,8 +308,7 @@ void emitAppleAccelTable(AsmPrinter *Asm, AccelTable &Contents, emitAppleAccelTableImpl(Asm, Contents, Prefix, SecBegin, DataT::Atoms); } -void emitDWARF5AccelTable(AsmPrinter *Asm, - AccelTable &Contents, +void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD, ArrayRef> CUs); @@ -316,9 +317,9 @@ void emitDWARF5AccelTable(AsmPrinter *Asm, /// start of compilation unit, either offsets to the start of compilation /// unit themselves. void emitDWARF5AccelTable( - AsmPrinter *Asm, AccelTable &Contents, + AsmPrinter *Asm, DWARF5AccelTable &Contents, ArrayRef> CUs, - llvm::function_ref + llvm::function_ref getCUIndexForEntry); /// Accelerator table data implementation for simple Apple accelerator tables diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinker.h b/llvm/include/llvm/DWARFLinker/DWARFLinker.h index e5797514165a..6887e441ce8f 100644 --- a/llvm/include/llvm/DWARFLinker/DWARFLinker.h +++ b/llvm/include/llvm/DWARFLinker/DWARFLinker.h @@ -119,8 +119,7 @@ public: virtual void emitLineStrings(const NonRelocatableStringpool &Pool) = 0; /// Emit DWARF debug names. - virtual void - emitDebugNames(AccelTable &Table) = 0; + virtual void emitDebugNames(DWARF5AccelTable &Table) = 0; /// Emit Apple namespaces accelerator table. virtual void @@ -880,7 +879,7 @@ private: uint32_t LastCIEOffset = 0; /// Apple accelerator tables. - AccelTable DebugNames; + DWARF5AccelTable DebugNames; AccelTable AppleNames; AccelTable AppleNamespaces; AccelTable AppleObjc; diff --git a/llvm/include/llvm/DWARFLinker/DWARFStreamer.h b/llvm/include/llvm/DWARFLinker/DWARFStreamer.h index 63e4b28a8d2c..18eb7277bfa2 100644 --- a/llvm/include/llvm/DWARFLinker/DWARFStreamer.h +++ b/llvm/include/llvm/DWARFLinker/DWARFStreamer.h @@ -163,7 +163,7 @@ public: StringRef Bytes) override; /// Emit DWARF debug names. - void emitDebugNames(AccelTable &Table) override; + void emitDebugNames(DWARF5AccelTable &Table) override; /// Emit Apple namespaces accelerator table. void emitAppleNamespaces( diff --git a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp index 88d548742777..e39395174411 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp @@ -357,6 +357,10 @@ void AppleAccelTableWriter::emit() const { emitData(); } +DWARF5AccelTableData::DWARF5AccelTableData(const DIE &Die, + const DwarfCompileUnit &CU) + : OffsetVal(&Die), DieTag(Die.getTag()), UnitID(CU.getUniqueID()) {} + template void Dwarf5AccelTableWriter::Header::emit(Dwarf5AccelTableWriter &Ctx) { assert(CompUnitCount > 0 && "Index must have at least one CU."); @@ -545,8 +549,8 @@ void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents, } void llvm::emitDWARF5AccelTable( - AsmPrinter *Asm, AccelTable &Contents, - const DwarfDebug &DD, ArrayRef> CUs) { + AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD, + ArrayRef> CUs) { std::vector> CompUnits; SmallVector CUIndex(CUs.size()); int Count = 0; @@ -575,20 +579,19 @@ void llvm::emitDWARF5AccelTable( Dwarf5AccelTableWriter( Asm, Contents, CompUnits, [&](const DWARF5AccelTableData &Entry) { - const DIE *CUDie = Entry.getDie().getUnitDie(); - return CUIndex[DD.lookupCU(CUDie)->getUniqueID()]; + return CUIndex[Entry.getUnitID()]; }) .emit(); } void llvm::emitDWARF5AccelTable( - AsmPrinter *Asm, AccelTable &Contents, + AsmPrinter *Asm, DWARF5AccelTable &Contents, ArrayRef> CUs, - llvm::function_ref + llvm::function_ref getCUIndexForEntry) { Contents.finalize(Asm, "names"); - Dwarf5AccelTableWriter(Asm, Contents, CUs, - getCUIndexForEntry) + Dwarf5AccelTableWriter(Asm, Contents, CUs, + getCUIndexForEntry) .emit(); } @@ -687,11 +690,6 @@ void DWARF5AccelTableData::print(raw_ostream &OS) const { OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n"; } -void DWARF5AccelTableStaticData::print(raw_ostream &OS) const { - OS << " Offset: " << getDieOffset() << "\n"; - OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n"; -} - void AppleAccelTableOffsetData::print(raw_ostream &OS) const { OS << " Offset: " << Die.getOffset() << "\n"; } diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index 887044b871e4..61ac492a9097 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -1389,6 +1389,10 @@ void DwarfDebug::finalizeModuleInfo() { InfoHolder.computeSizeAndOffsets(); if (useSplitDwarf()) SkeletonHolder.computeSizeAndOffsets(); + + // Now that offsets are computed, can replace DIEs in debug_names Entry with + // an actual offset. + AccelDebugNames.convertDieToOffset(); } // Emit all Dwarf sections that should come after the content. @@ -3547,9 +3551,11 @@ void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU, case AccelTableKind::Apple: AppleAccel.addName(Ref, Die); break; - case AccelTableKind::Dwarf: - AccelDebugNames.addName(Ref, Die); + case AccelTableKind::Dwarf: { + DwarfCompileUnit *DCU = CUMap.lookup(&CU); + AccelDebugNames.addName(Ref, Die, *DCU); break; + } case AccelTableKind::Default: llvm_unreachable("Default should have already been resolved."); case AccelTableKind::None: diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h index 75649a747602..b41d2be5eb9b 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -496,7 +496,7 @@ private: AddressPool AddrPool; /// Accelerator tables. - AccelTable AccelDebugNames; + DWARF5AccelTable AccelDebugNames; AccelTable AccelNames; AccelTable AccelObjC; AccelTable AccelNamespace; diff --git a/llvm/lib/DWARFLinker/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/DWARFStreamer.cpp index ff719d0a59ba..2c6aba7225ad 100644 --- a/llvm/lib/DWARFLinker/DWARFStreamer.cpp +++ b/llvm/lib/DWARFLinker/DWARFStreamer.cpp @@ -291,8 +291,7 @@ void DwarfStreamer::emitLineStrings(const NonRelocatableStringpool &Pool) { } } -void DwarfStreamer::emitDebugNames( - AccelTable &Table) { +void DwarfStreamer::emitDebugNames(DWARF5AccelTable &Table) { if (EmittedUnits.empty()) return; @@ -307,11 +306,10 @@ void DwarfStreamer::emitDebugNames( } Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection()); - emitDWARF5AccelTable( - Asm.get(), Table, CompUnits, - [&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) { - return UniqueIdToCuMap[Entry.getCUIndex()]; - }); + emitDWARF5AccelTable(Asm.get(), Table, CompUnits, + [&UniqueIdToCuMap](const DWARF5AccelTableData &Entry) { + return UniqueIdToCuMap[Entry.getUnitID()]; + }); } void DwarfStreamer::emitAppleNamespaces( diff --git a/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp b/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp index 7885e3013a51..88038824c9dc 100644 --- a/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp +++ b/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp @@ -223,16 +223,16 @@ void DwarfEmitterImpl::emitDIE(DIE &Die) { DebugInfoSectionSize += Die.getSize(); } -void DwarfEmitterImpl::emitDebugNames( - AccelTable &Table, - DebugNamesUnitsOffsets &CUOffsets, CompUnitIDToIdx &CUidToIdx) { +void DwarfEmitterImpl::emitDebugNames(DWARF5AccelTable &Table, + DebugNamesUnitsOffsets &CUOffsets, + CompUnitIDToIdx &CUidToIdx) { if (CUOffsets.empty()) return; Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection()); emitDWARF5AccelTable(Asm.get(), Table, CUOffsets, - [&CUidToIdx](const DWARF5AccelTableStaticData &Entry) { - return CUidToIdx[Entry.getCUIndex()]; + [&CUidToIdx](const DWARF5AccelTableData &Entry) { + return CUidToIdx[Entry.getUnitID()]; }); } diff --git a/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h b/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h index d5c847908ba5..1849442678d3 100644 --- a/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h +++ b/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h @@ -87,7 +87,7 @@ public: uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize; } /// Emits .debug_names section according to the specified \p Table. - void emitDebugNames(AccelTable &Table, + void emitDebugNames(DWARF5AccelTable &Table, DebugNamesUnitsOffsets &CUOffsets, CompUnitIDToIdx &UnitIDToIdxMap); diff --git a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp index 78f553a12544..a755d540aef9 100644 --- a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp +++ b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp @@ -1129,7 +1129,7 @@ void DWARFLinkerImpl::emitAppleAcceleratorSections(const Triple &TargetTriple) { } void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) { - std::unique_ptr> DebugNames; + std::unique_ptr DebugNames; DebugNamesUnitsOffsets CompUnits; CompUnitIDToIdx CUidToIdx; @@ -1144,7 +1144,7 @@ void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) { CU->AcceleratorRecords.forEach([&](const DwarfUnit::AccelInfo &Info) { if (DebugNames.get() == nullptr) - DebugNames = std::make_unique>(); + DebugNames = std::make_unique(); switch (Info.Type) { case DwarfUnit::AccelType::Name: