[llvm] da27c25 - [LLVM[NFC] Refactor to allow debug_names entries to conatain DIE offset (#69399)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 25 12:39:33 PDT 2023


Author: Alexander Yermolovich
Date: 2023-10-25T12:39:28-07:00
New Revision: da27c25c19faef008c992a68387efcdc9fc0adb4

URL: https://github.com/llvm/llvm-project/commit/da27c25c19faef008c992a68387efcdc9fc0adb4
DIFF: https://github.com/llvm/llvm-project/commit/da27c25c19faef008c992a68387efcdc9fc0adb4.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/AccelTable.h
    llvm/include/llvm/DWARFLinker/DWARFLinker.h
    llvm/include/llvm/DWARFLinker/DWARFStreamer.h
    llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
    llvm/lib/DWARFLinker/DWARFStreamer.cpp
    llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.cpp
    llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h
    llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/AccelTable.h b/llvm/include/llvm/CodeGen/AccelTable.h
index d521b31e3d16ab4..d4e21b2ac8e7ebc 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<uint64_t>(OffsetVal) &&
+           "Accessing DIE Offset before normalizing.");
+    return std::get<uint64_t>(OffsetVal);
+  }
+  unsigned getDieTag() const { return DieTag; }
+  unsigned getUnitID() const { return UnitID; }
+  void normalizeDIEToOffset() {
+    assert(std::holds_alternative<const DIE *>(OffsetVal) &&
+           "Accessing offset after normalizing.");
+    OffsetVal = std::get<const DIE *>(OffsetVal)->getOffset();
+  }
 
 protected:
-  const DIE &Die;
+  std::variant<const DIE *, uint64_t> 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<DWARF5AccelTableData> {
 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<DWARF5AccelTableData *>(Value)->normalizeDIEToOffset();
+      }
+    }
+  }
 };
 
 void emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
@@ -306,8 +308,7 @@ void emitAppleAccelTable(AsmPrinter *Asm, AccelTable<DataT> &Contents,
   emitAppleAccelTableImpl(Asm, Contents, Prefix, SecBegin, DataT::Atoms);
 }
 
-void emitDWARF5AccelTable(AsmPrinter *Asm,
-                          AccelTable<DWARF5AccelTableData> &Contents,
+void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents,
                           const DwarfDebug &DD,
                           ArrayRef<std::unique_ptr<DwarfCompileUnit>> 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<DWARF5AccelTableStaticData> &Contents,
+    AsmPrinter *Asm, DWARF5AccelTable &Contents,
     ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
-    llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
+    llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
         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 e5797514165a22b..6887e441ce8ff62 100644
--- a/llvm/include/llvm/DWARFLinker/DWARFLinker.h
+++ b/llvm/include/llvm/DWARFLinker/DWARFLinker.h
@@ -119,8 +119,7 @@ class DwarfEmitter {
   virtual void emitLineStrings(const NonRelocatableStringpool &Pool) = 0;
 
   /// Emit DWARF debug names.
-  virtual void
-  emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;
+  virtual void emitDebugNames(DWARF5AccelTable &Table) = 0;
 
   /// Emit Apple namespaces accelerator table.
   virtual void
@@ -880,7 +879,7 @@ class DWARFLinker {
   uint32_t LastCIEOffset = 0;
 
   /// Apple accelerator tables.
-  AccelTable<DWARF5AccelTableStaticData> DebugNames;
+  DWARF5AccelTable DebugNames;
   AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
   AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
   AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;

diff  --git a/llvm/include/llvm/DWARFLinker/DWARFStreamer.h b/llvm/include/llvm/DWARFLinker/DWARFStreamer.h
index 63e4b28a8d2c997..18eb7277bfa2df2 100644
--- a/llvm/include/llvm/DWARFLinker/DWARFStreamer.h
+++ b/llvm/include/llvm/DWARFLinker/DWARFStreamer.h
@@ -163,7 +163,7 @@ class DwarfStreamer : public DwarfEmitter {
                StringRef Bytes) override;
 
   /// Emit DWARF debug names.
-  void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &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 88d5487427774c1..e393951744117eb 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 <typename DataT>
 void Dwarf5AccelTableWriter<DataT>::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<DWARF5AccelTableData> &Contents,
-    const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
+    AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD,
+    ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
   std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits;
   SmallVector<unsigned, 1> CUIndex(CUs.size());
   int Count = 0;
@@ -575,20 +579,19 @@ void llvm::emitDWARF5AccelTable(
   Dwarf5AccelTableWriter<DWARF5AccelTableData>(
       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<DWARF5AccelTableStaticData> &Contents,
+    AsmPrinter *Asm, DWARF5AccelTable &Contents,
     ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
-    llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
+    llvm::function_ref<unsigned(const DWARF5AccelTableData &)>
         getCUIndexForEntry) {
   Contents.finalize(Asm, "names");
-  Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
-                                                     getCUIndexForEntry)
+  Dwarf5AccelTableWriter<DWARF5AccelTableData>(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 887044b871e4460..61ac492a9097063 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 75649a747602ef5..b41d2be5eb9b8c9 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
@@ -496,7 +496,7 @@ class DwarfDebug : public DebugHandlerBase {
   AddressPool AddrPool;
 
   /// Accelerator tables.
-  AccelTable<DWARF5AccelTableData> AccelDebugNames;
+  DWARF5AccelTable AccelDebugNames;
   AccelTable<AppleAccelTableOffsetData> AccelNames;
   AccelTable<AppleAccelTableOffsetData> AccelObjC;
   AccelTable<AppleAccelTableOffsetData> AccelNamespace;

diff  --git a/llvm/lib/DWARFLinker/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/DWARFStreamer.cpp
index ff719d0a59bafaa..2c6aba7225adcce 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<DWARF5AccelTableStaticData> &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 7885e3013a51d3f..88038824c9dcb4d 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<DWARF5AccelTableStaticData> &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 d5c847908ba53be..1849442678d320b 100644
--- a/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h
+++ b/llvm/lib/DWARFLinkerParallel/DWARFEmitterImpl.h
@@ -87,7 +87,7 @@ class DwarfEmitterImpl : public ExtraDwarfEmitter {
   uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize; }
 
   /// Emits .debug_names section according to the specified \p Table.
-  void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table,
+  void emitDebugNames(DWARF5AccelTable &Table,
                       DebugNamesUnitsOffsets &CUOffsets,
                       CompUnitIDToIdx &UnitIDToIdxMap);
 

diff  --git a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp
index 78f553a12544516..a755d540aef99e1 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<AccelTable<DWARF5AccelTableStaticData>> DebugNames;
+  std::unique_ptr<DWARF5AccelTable> 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<AccelTable<DWARF5AccelTableStaticData>>();
+        DebugNames = std::make_unique<DWARF5AccelTable>();
 
       switch (Info.Type) {
       case DwarfUnit::AccelType::Name:


        


More information about the llvm-commits mailing list