Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOLT][DWARF][NFC] Replace usages of GDBIndex functions #94701

Merged
merged 5 commits into from
Jun 10, 2024

Conversation

sayhaan
Copy link
Contributor

@sayhaan sayhaan commented Jun 7, 2024

Replace old usages of GDB Index functions to use the new class.

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D58156978
@llvmbot
Copy link
Collaborator

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-bolt

Author: Sayhaan Siddiqui (sayhaan)

Changes

Replace old usages of GDB Index functions to use the new class.


Full diff: https://github.com/llvm/llvm-project/pull/94701.diff

2 Files Affected:

  • (modified) bolt/include/bolt/Rewrite/DWARFRewriter.h (+5-2)
  • (modified) bolt/lib/Rewrite/DWARFRewriter.cpp (+47-39)
diff --git a/bolt/include/bolt/Rewrite/DWARFRewriter.h b/bolt/include/bolt/Rewrite/DWARFRewriter.h
index 8dec32de9008e..1718bda286446 100644
--- a/bolt/include/bolt/Rewrite/DWARFRewriter.h
+++ b/bolt/include/bolt/Rewrite/DWARFRewriter.h
@@ -12,6 +12,7 @@
 #include "bolt/Core/DIEBuilder.h"
 #include "bolt/Core/DebugData.h"
 #include "bolt/Core/DebugNames.h"
+#include "bolt/Core/GDBIndex.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/CodeGen/DIE.h"
 #include "llvm/DWP/DWP.h"
@@ -131,12 +132,14 @@ class DWARFRewriter {
   makeFinalLocListsSection(DWARFVersion Version);
 
   /// Finalize type sections in the main binary.
-  CUOffsetMap finalizeTypeSections(DIEBuilder &DIEBlder, DIEStreamer &Streamer);
+  CUOffsetMap finalizeTypeSections(DIEBuilder &DIEBlder, DIEStreamer &Streamer,
+                                   GDBIndex &GDBIndexSection);
 
   /// Process and write out CUs that are passsed in.
   void finalizeCompileUnits(DIEBuilder &DIEBlder, DIEStreamer &Streamer,
                             CUOffsetMap &CUMap,
-                            const std::list<DWARFUnit *> &CUs);
+                            const std::list<DWARFUnit *> &CUs,
+                            GDBIndex &GDBIndexSection);
 
   /// Finalize debug sections in the main binary.
   void finalizeDebugSections(DIEBuilder &DIEBlder,
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 8814ebbd10aa5..70d6d662a6848 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -12,6 +12,7 @@
 #include "bolt/Core/DIEBuilder.h"
 #include "bolt/Core/DebugData.h"
 #include "bolt/Core/DynoStats.h"
+#include "bolt/Core/GDBIndex.h"
 #include "bolt/Core/ParallelUtilities.h"
 #include "bolt/Rewrite/RewriteInstance.h"
 #include "llvm/ADT/STLExtras.h"
@@ -241,13 +242,13 @@ class DIEStreamer : public DwarfStreamer {
     }
   }
 
-  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE,
-                          unsigned DwarfVersion) {
+  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE, unsigned DwarfVersion,
+                          GDBIndex &GDBIndexSection) {
     AsmPrinter &Asm = getAsmPrinter();
     const uint64_t TypeSignature = cast<DWARFTypeUnit>(Unit).getTypeHash();
     DIE *TypeDIE = DIEBldr->getTypeDIE(Unit);
     const DIEBuilder::DWARFUnitInfo &UI = DIEBldr->getUnitInfoByDwarfUnit(Unit);
-    Rewriter.addGDBTypeUnitEntry(
+    GDBIndexSection.addGDBTypeUnitEntry(
         {UI.UnitOffset, TypeSignature, TypeDIE->getOffset()});
     if (Unit.getVersion() < 5) {
       // Switch the section to .debug_types section.
@@ -265,9 +266,10 @@ class DIEStreamer : public DwarfStreamer {
     Asm.emitDwarfLengthOrOffset(TypeDIE ? TypeDIE->getOffset() : 0);
   }
 
-  void emitUnitHeader(DWARFUnit &Unit, DIE &UnitDIE) {
+  void emitUnitHeader(DWARFUnit &Unit, DIE &UnitDIE,
+                      GDBIndex &GDBIndexSection) {
     if (Unit.isTypeUnit())
-      emitTypeUnitHeader(Unit, UnitDIE, Unit.getVersion());
+      emitTypeUnitHeader(Unit, UnitDIE, Unit.getVersion(), GDBIndexSection);
     else
       emitCompileUnitHeader(Unit, UnitDIE, Unit.getVersion());
   }
@@ -287,8 +289,8 @@ class DIEStreamer : public DwarfStreamer {
 
   using DwarfStreamer::emitCompileUnitHeader;
 
-  void emitUnit(DWARFUnit &Unit, DIE &UnitDIE) {
-    emitUnitHeader(Unit, UnitDIE);
+  void emitUnit(DWARFUnit &Unit, DIE &UnitDIE, GDBIndex &GDBIndexSection) {
+    emitUnitHeader(Unit, UnitDIE, GDBIndexSection);
     emitDIE(UnitDIE);
   }
 };
@@ -326,12 +328,11 @@ static cl::opt<bool> KeepARanges(
         "keep or generate .debug_aranges section if .gdb_index is written"),
     cl::Hidden, cl::cat(BoltCategory));
 
-static cl::opt<bool>
-DeterministicDebugInfo("deterministic-debuginfo",
-  cl::desc("disables parallel execution of tasks that may produce "
-           "nondeterministic debug info"),
-  cl::init(true),
-  cl::cat(BoltCategory));
+static cl::opt<bool> DeterministicDebugInfo(
+    "deterministic-debuginfo",
+    cl::desc("disables parallel execution of tasks that may produce "
+             "nondeterministic debug info"),
+    cl::init(true), cl::cat(BoltCategory));
 
 static cl::opt<std::string> DwarfOutputPath(
     "dwarf-output-path",
@@ -435,9 +436,9 @@ static bool getLowAndHighPC(const DIE &Die, const DWARFUnit &DU,
 
 static Expected<llvm::DWARFAddressRangesVector>
 getDIEAddressRanges(const DIE &Die, DWARFUnit &DU) {
-  uint64_t LowPC, HighPC, Index;
-  if (getLowAndHighPC(Die, DU, LowPC, HighPC, Index))
-    return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
+  uint64_t LowPC, HighPC, GDBIndexSection;
+  if (getLowAndHighPC(Die, DU, LowPC, HighPC, GDBIndexSection))
+    return DWARFAddressRangesVector{{LowPC, HighPC, GDBIndexSection}};
   if (DIEValue Dval = Die.findAttribute(dwarf::DW_AT_ranges)) {
     if (Dval.getForm() == dwarf::DW_FORM_rnglistx)
       return DU.findRnglistFromIndex(Dval.getDIEInteger().getValue());
@@ -473,24 +474,24 @@ createDIEStreamer(const Triple &TheTriple, raw_pwrite_stream &OutFile,
   return Streamer;
 }
 
-static DWARFRewriter::UnitMeta
-emitUnit(DIEBuilder &DIEBldr, DIEStreamer &Streamer, DWARFUnit &Unit) {
+static DWARFRewriter::UnitMeta emitUnit(DIEBuilder &DIEBldr,
+                                        DIEStreamer &Streamer, DWARFUnit &Unit,
+                                        GDBIndex &GDBIndexSection) {
   DIE *UnitDIE = DIEBldr.getUnitDIEbyUnit(Unit);
   const DIEBuilder::DWARFUnitInfo &U = DIEBldr.getUnitInfoByDwarfUnit(Unit);
-  Streamer.emitUnit(Unit, *UnitDIE);
+  Streamer.emitUnit(Unit, *UnitDIE, GDBIndexSection);
   uint64_t TypeHash = 0;
   if (DWARFTypeUnit *DTU = dyn_cast_or_null<DWARFTypeUnit>(&Unit))
     TypeHash = DTU->getTypeHash();
   return {U.UnitOffset, U.UnitLength, TypeHash};
 }
 
-static void emitDWOBuilder(const std::string &DWOName,
-                           DIEBuilder &DWODIEBuilder, DWARFRewriter &Rewriter,
-                           DWARFUnit &SplitCU, DWARFUnit &CU,
-                           DWARFRewriter::DWPState &State,
-                           DebugLocWriter &LocWriter,
-                           DebugStrOffsetsWriter &StrOffstsWriter,
-                           DebugStrWriter &StrWriter) {
+static void
+emitDWOBuilder(const std::string &DWOName, DIEBuilder &DWODIEBuilder,
+               DWARFRewriter &Rewriter, DWARFUnit &SplitCU, DWARFUnit &CU,
+               DWARFRewriter::DWPState &State, DebugLocWriter &LocWriter,
+               DebugStrOffsetsWriter &StrOffstsWriter,
+               DebugStrWriter &StrWriter, GDBIndex &GDBIndexSection) {
   // Populate debug_info and debug_abbrev for current dwo into StringRef.
   DWODIEBuilder.generateAbbrevs();
   DWODIEBuilder.finish();
@@ -510,18 +511,19 @@ static void emitDWOBuilder(const std::string &DWOName,
       if (!CU->isTypeUnit())
         continue;
       DWARFRewriter::UnitMeta MI =
-          emitUnit(DWODIEBuilder, *Streamer, *CU.get());
+          emitUnit(DWODIEBuilder, *Streamer, *CU.get(), GDBIndexSection);
       TUMetaVector.emplace_back(MI);
     }
-    CUMI = emitUnit(DWODIEBuilder, *Streamer, SplitCU);
+    CUMI = emitUnit(DWODIEBuilder, *Streamer, SplitCU, GDBIndexSection);
   } else {
     for (std::unique_ptr<llvm::DWARFUnit> &CU :
          SplitCU.getContext().dwo_compile_units())
-      emitUnit(DWODIEBuilder, *Streamer, *CU.get());
+      emitUnit(DWODIEBuilder, *Streamer, *CU.get(), GDBIndexSection);
 
     // emit debug_types sections for dwarf4
     for (DWARFUnit *CU : DWODIEBuilder.getDWARF4TUVector()) {
-      DWARFRewriter::UnitMeta MI = emitUnit(DWODIEBuilder, *Streamer, *CU);
+      DWARFRewriter::UnitMeta MI =
+          emitUnit(DWODIEBuilder, *Streamer, *CU, GDBIndexSection);
       TUMetaVector.emplace_back(MI);
     }
   }
@@ -653,6 +655,7 @@ void DWARFRewriter::updateDebugInfo() {
   DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
                                          *StrWriter);
   DWPState State;
+  GDBIndex GDBIndexSection(BC);
   if (opts::WriteDWP)
     initDWPState(State);
   auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
@@ -704,7 +707,8 @@ void DWARFRewriter::updateDebugInfo() {
         TempRangesSectionWriter->finalizeSection();
 
       emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
-                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter);
+                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
+                     GDBIndexSection);
     }
 
     if (Unit->getVersion() >= 5) {
@@ -731,7 +735,8 @@ void DWARFRewriter::updateDebugInfo() {
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
   std::unique_ptr<DIEStreamer> Streamer =
       createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this);
-  CUOffsetMap OffsetMap = finalizeTypeSections(DIEBlder, *Streamer);
+  CUOffsetMap OffsetMap =
+      finalizeTypeSections(DIEBlder, *Streamer, GDBIndexSection);
 
   const bool SingleThreadedMode =
       opts::NoThreads || opts::DeterministicDebugInfo;
@@ -744,7 +749,7 @@ void DWARFRewriter::updateDebugInfo() {
       for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
         processUnitDIE(CU, &DIEBlder);
       finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
-                           DIEBlder.getProcessedCUs());
+                           DIEBlder.getProcessedCUs(), GDBIndexSection);
     }
   } else {
     // Update unit debug info in parallel
@@ -761,7 +766,8 @@ void DWARFRewriter::updateDebugInfo() {
 
   finalizeDebugSections(DIEBlder, DebugNamesTable, *Streamer, *ObjOS,
                         OffsetMap);
-  updateGdbIndexSection(OffsetMap, CUIndex);
+  GDBIndexSection.updateGdbIndexSection(OffsetMap, CUIndex,
+                                        *ARangesSectionWriter);
 }
 
 void DWARFRewriter::updateUnitDebugInfo(
@@ -1429,7 +1435,8 @@ void DWARFRewriter::updateLineTableOffsets(const MCAsmLayout &Layout) {
 }
 
 CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
-                                                DIEStreamer &Streamer) {
+                                                DIEStreamer &Streamer,
+                                                GDBIndex &GDBIndexSection) {
   // update TypeUnit DW_AT_stmt_list with new .debug_line information.
   auto updateLineTable = [&](const DWARFUnit &Unit) -> void {
     DIE *UnitDIE = DIEBlder.getUnitDIEbyUnit(Unit);
@@ -1458,7 +1465,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
     if (!CU->isTypeUnit())
       continue;
     updateLineTable(*CU.get());
-    emitUnit(DIEBlder, Streamer, *CU.get());
+    emitUnit(DIEBlder, Streamer, *CU.get(), GDBIndexSection);
     uint32_t StartOffset = CUOffset;
     DIE *UnitDIE = DIEBlder.getUnitDIEbyUnit(*CU.get());
     CUOffset += CU.get()->getHeaderSize();
@@ -1469,7 +1476,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
   // Emit Type Unit of DWARF 4 to .debug_type section
   for (DWARFUnit *TU : DIEBlder.getDWARF4TUVector()) {
     updateLineTable(*TU);
-    emitUnit(DIEBlder, *TypeStreamer, *TU);
+    emitUnit(DIEBlder, *TypeStreamer, *TU, GDBIndexSection);
   }
 
   TypeStreamer->finish();
@@ -1604,12 +1611,13 @@ void DWARFRewriter::finalizeDebugSections(
 void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder,
                                          DIEStreamer &Streamer,
                                          CUOffsetMap &CUMap,
-                                         const std::list<DWARFUnit *> &CUs) {
+                                         const std::list<DWARFUnit *> &CUs,
+                                         GDBIndex &GDBIndexSection) {
   DIEBlder.generateAbbrevs();
   DIEBlder.finish();
   // generate debug_info and CUMap
   for (DWARFUnit *CU : CUs) {
-    emitUnit(DIEBlder, Streamer, *CU);
+    emitUnit(DIEBlder, Streamer, *CU, GDBIndexSection);
     const uint32_t StartOffset = CUOffset;
     DIE *UnitDIE = DIEBlder.getUnitDIEbyUnit(*CU);
     CUOffset += CU->getHeaderSize();

uint64_t LowPC, HighPC, Index;
if (getLowAndHighPC(Die, DU, LowPC, HighPC, Index))
return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
uint64_t LowPC, HighPC, GDBIndexSection;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this changed by mistake?

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
Copy link

github-actions bot commented Jun 7, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
@ayermolo ayermolo merged commit 61df854 into llvm:main Jun 10, 2024
6 checks passed
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
Replace old usages of GDB Index functions to use the new class.
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants