[llvm] [BOLT][DWARF][NFC] Replace usages of GDBIndex functions (PR #94701)

Sayhaan Siddiqui via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 13:36:46 PDT 2024


https://github.com/sayhaan updated https://github.com/llvm/llvm-project/pull/94701

>From 394451898ea1a0e3b612a5c93eaeb3e7599afdda Mon Sep 17 00:00:00 2001
From: Sayhaan Siddiqui <sayhaan at meta.com>
Date: Tue, 4 Jun 2024 13:04:25 -0700
Subject: [PATCH 1/4] Replace usages of GDBIndex functions

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D58156978
---
 bolt/include/bolt/Rewrite/DWARFRewriter.h |  7 +-
 bolt/lib/Rewrite/DWARFRewriter.cpp        | 86 +++++++++++++----------
 2 files changed, 52 insertions(+), 41 deletions(-)

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();

>From a449f6c4ffecc279146e64232e87f7a21e564754 Mon Sep 17 00:00:00 2001
From: Sayhaan Siddiqui <sayhaan at meta.com>
Date: Fri, 7 Jun 2024 13:19:36 -0700
Subject: [PATCH 2/4] Refactoring functions

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
---
 bolt/include/bolt/Rewrite/DWARFRewriter.h |  3 +-
 bolt/lib/Rewrite/DWARFRewriter.cpp        | 65 +++++++++++------------
 2 files changed, 31 insertions(+), 37 deletions(-)

diff --git a/bolt/include/bolt/Rewrite/DWARFRewriter.h b/bolt/include/bolt/Rewrite/DWARFRewriter.h
index 1718bda286446..3cc9d823c815b 100644
--- a/bolt/include/bolt/Rewrite/DWARFRewriter.h
+++ b/bolt/include/bolt/Rewrite/DWARFRewriter.h
@@ -138,8 +138,7 @@ class DWARFRewriter {
   /// Process and write out CUs that are passsed in.
   void finalizeCompileUnits(DIEBuilder &DIEBlder, DIEStreamer &Streamer,
                             CUOffsetMap &CUMap,
-                            const std::list<DWARFUnit *> &CUs,
-                            GDBIndex &GDBIndexSection);
+                            const std::list<DWARFUnit *> &CUs);
 
   /// 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 70d6d662a6848..45b7b363ab29f 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -186,7 +186,7 @@ namespace bolt {
 class DIEStreamer : public DwarfStreamer {
   DIEBuilder *DIEBldr;
   DWARFRewriter &Rewriter;
-
+  GDBIndex &GDBIndexSection;
 private:
   /// Emit the compilation unit header for \p Unit in the debug_info
   /// section.
@@ -242,8 +242,7 @@ class DIEStreamer : public DwarfStreamer {
     }
   }
 
-  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE, unsigned DwarfVersion,
-                          GDBIndex &GDBIndexSection) {
+  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE, unsigned DwarfVersion) {
     AsmPrinter &Asm = getAsmPrinter();
     const uint64_t TypeSignature = cast<DWARFTypeUnit>(Unit).getTypeHash();
     DIE *TypeDIE = DIEBldr->getTypeDIE(Unit);
@@ -266,10 +265,9 @@ class DIEStreamer : public DwarfStreamer {
     Asm.emitDwarfLengthOrOffset(TypeDIE ? TypeDIE->getOffset() : 0);
   }
 
-  void emitUnitHeader(DWARFUnit &Unit, DIE &UnitDIE,
-                      GDBIndex &GDBIndexSection) {
+  void emitUnitHeader(DWARFUnit &Unit, DIE &UnitDIE) {
     if (Unit.isTypeUnit())
-      emitTypeUnitHeader(Unit, UnitDIE, Unit.getVersion(), GDBIndexSection);
+      emitTypeUnitHeader(Unit, UnitDIE, Unit.getVersion());
     else
       emitCompileUnitHeader(Unit, UnitDIE, Unit.getVersion());
   }
@@ -280,17 +278,17 @@ class DIEStreamer : public DwarfStreamer {
   }
 
 public:
-  DIEStreamer(DIEBuilder *DIEBldr, DWARFRewriter &Rewriter,
+  DIEStreamer(DIEBuilder *DIEBldr, DWARFRewriter &Rewriter, GDBIndex &GDBIndexSection,
               DWARFLinkerBase::OutputFileType OutFileType,
               raw_pwrite_stream &OutFile,
               DWARFLinkerBase::MessageHandlerTy Warning)
-      : DwarfStreamer(OutFileType, OutFile, Warning), DIEBldr(DIEBldr),
-        Rewriter(Rewriter){};
+      : DwarfStreamer(OutFileType, OutFile, Warning), DIEBldr(DIEBldr), Rewriter(Rewriter),
+        GDBIndexSection(GDBIndexSection){};
 
   using DwarfStreamer::emitCompileUnitHeader;
 
-  void emitUnit(DWARFUnit &Unit, DIE &UnitDIE, GDBIndex &GDBIndexSection) {
-    emitUnitHeader(Unit, UnitDIE, GDBIndexSection);
+  void emitUnit(DWARFUnit &Unit, DIE &UnitDIE) {
+    emitUnitHeader(Unit, UnitDIE);
     emitDIE(UnitDIE);
   }
 };
@@ -436,9 +434,9 @@ static bool getLowAndHighPC(const DIE &Die, const DWARFUnit &DU,
 
 static Expected<llvm::DWARFAddressRangesVector>
 getDIEAddressRanges(const DIE &Die, DWARFUnit &DU) {
-  uint64_t LowPC, HighPC, GDBIndexSection;
-  if (getLowAndHighPC(Die, DU, LowPC, HighPC, GDBIndexSection))
-    return DWARFAddressRangesVector{{LowPC, HighPC, GDBIndexSection}};
+  uint64_t LowPC, HighPC, Index;
+  if (getLowAndHighPC(Die, DU, LowPC, HighPC, Index))
+    return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
   if (DIEValue Dval = Die.findAttribute(dwarf::DW_AT_ranges)) {
     if (Dval.getForm() == dwarf::DW_FORM_rnglistx)
       return DU.findRnglistFromIndex(Dval.getDIEInteger().getValue());
@@ -461,10 +459,10 @@ static std::optional<uint64_t> getAsAddress(const DWARFUnit &DU,
 static std::unique_ptr<DIEStreamer>
 createDIEStreamer(const Triple &TheTriple, raw_pwrite_stream &OutFile,
                   StringRef Swift5ReflectionSegmentName, DIEBuilder &DIEBldr,
-                  DWARFRewriter &Rewriter) {
+                  DWARFRewriter &Rewriter, GDBIndex &GDBIndexSection) {
 
   std::unique_ptr<DIEStreamer> Streamer = std::make_unique<DIEStreamer>(
-      &DIEBldr, Rewriter, DWARFLinkerBase::OutputFileType::Object, OutFile,
+      &DIEBldr, Rewriter, GDBIndexSection, DWARFLinkerBase::OutputFileType::Object, OutFile,
       [&](const Twine &Warning, StringRef Context, const DWARFDie *) {});
   Error Err = Streamer->init(TheTriple, Swift5ReflectionSegmentName);
   if (Err)
@@ -475,11 +473,10 @@ createDIEStreamer(const Triple &TheTriple, raw_pwrite_stream &OutFile,
 }
 
 static DWARFRewriter::UnitMeta emitUnit(DIEBuilder &DIEBldr,
-                                        DIEStreamer &Streamer, DWARFUnit &Unit,
-                                        GDBIndex &GDBIndexSection) {
+                                        DIEStreamer &Streamer, DWARFUnit &Unit) {
   DIE *UnitDIE = DIEBldr.getUnitDIEbyUnit(Unit);
   const DIEBuilder::DWARFUnitInfo &U = DIEBldr.getUnitInfoByDwarfUnit(Unit);
-  Streamer.emitUnit(Unit, *UnitDIE, GDBIndexSection);
+  Streamer.emitUnit(Unit, *UnitDIE);
   uint64_t TypeHash = 0;
   if (DWARFTypeUnit *DTU = dyn_cast_or_null<DWARFTypeUnit>(&Unit))
     TypeHash = DTU->getTypeHash();
@@ -502,7 +499,7 @@ emitDWOBuilder(const std::string &DWOName, DIEBuilder &DWODIEBuilder,
   const object::ObjectFile *File = SplitCU.getContext().getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
   std::unique_ptr<DIEStreamer> Streamer = createDIEStreamer(
-      *TheTriple, *ObjOS, "DwoStreamerInitAug2", DWODIEBuilder, Rewriter);
+      *TheTriple, *ObjOS, "DwoStreamerInitAug2", DWODIEBuilder, Rewriter, GDBIndexSection);
   DWARFRewriter::UnitMetaVectorType TUMetaVector;
   DWARFRewriter::UnitMeta CUMI = {0, 0, 0};
   if (SplitCU.getContext().getMaxDWOVersion() >= 5) {
@@ -511,19 +508,19 @@ emitDWOBuilder(const std::string &DWOName, DIEBuilder &DWODIEBuilder,
       if (!CU->isTypeUnit())
         continue;
       DWARFRewriter::UnitMeta MI =
-          emitUnit(DWODIEBuilder, *Streamer, *CU.get(), GDBIndexSection);
+          emitUnit(DWODIEBuilder, *Streamer, *CU.get());
       TUMetaVector.emplace_back(MI);
     }
-    CUMI = emitUnit(DWODIEBuilder, *Streamer, SplitCU, GDBIndexSection);
+    CUMI = emitUnit(DWODIEBuilder, *Streamer, SplitCU);
   } else {
     for (std::unique_ptr<llvm::DWARFUnit> &CU :
          SplitCU.getContext().dwo_compile_units())
-      emitUnit(DWODIEBuilder, *Streamer, *CU.get(), GDBIndexSection);
+      emitUnit(DWODIEBuilder, *Streamer, *CU.get());
 
     // emit debug_types sections for dwarf4
     for (DWARFUnit *CU : DWODIEBuilder.getDWARF4TUVector()) {
       DWARFRewriter::UnitMeta MI =
-          emitUnit(DWODIEBuilder, *Streamer, *CU, GDBIndexSection);
+          emitUnit(DWODIEBuilder, *Streamer, *CU);
       TUMetaVector.emplace_back(MI);
     }
   }
@@ -654,8 +651,8 @@ void DWARFRewriter::updateDebugInfo() {
 
   DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
                                          *StrWriter);
-  DWPState State;
   GDBIndex GDBIndexSection(BC);
+  DWPState State;
   if (opts::WriteDWP)
     initDWPState(State);
   auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
@@ -707,8 +704,7 @@ void DWARFRewriter::updateDebugInfo() {
         TempRangesSectionWriter->finalizeSection();
 
       emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
-                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
-                     GDBIndexSection);
+                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter, GDBIndexSection);
     }
 
     if (Unit->getVersion() >= 5) {
@@ -734,7 +730,7 @@ void DWARFRewriter::updateDebugInfo() {
   const object::ObjectFile *File = BC.DwCtx->getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
   std::unique_ptr<DIEStreamer> Streamer =
-      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this);
+      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
   CUOffsetMap OffsetMap =
       finalizeTypeSections(DIEBlder, *Streamer, GDBIndexSection);
 
@@ -749,7 +745,7 @@ void DWARFRewriter::updateDebugInfo() {
       for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
         processUnitDIE(CU, &DIEBlder);
       finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
-                           DIEBlder.getProcessedCUs(), GDBIndexSection);
+                           DIEBlder.getProcessedCUs());
     }
   } else {
     // Update unit debug info in parallel
@@ -1457,7 +1453,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
   const object::ObjectFile *File = BC.DwCtx->getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
   std::unique_ptr<DIEStreamer> TypeStreamer =
-      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this);
+      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
 
   // generate debug_info and CUMap
   CUOffsetMap CUMap;
@@ -1465,7 +1461,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
     if (!CU->isTypeUnit())
       continue;
     updateLineTable(*CU.get());
-    emitUnit(DIEBlder, Streamer, *CU.get(), GDBIndexSection);
+    emitUnit(DIEBlder, Streamer, *CU.get());
     uint32_t StartOffset = CUOffset;
     DIE *UnitDIE = DIEBlder.getUnitDIEbyUnit(*CU.get());
     CUOffset += CU.get()->getHeaderSize();
@@ -1476,7 +1472,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, GDBIndexSection);
+    emitUnit(DIEBlder, *TypeStreamer, *TU);
   }
 
   TypeStreamer->finish();
@@ -1611,13 +1607,12 @@ void DWARFRewriter::finalizeDebugSections(
 void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder,
                                          DIEStreamer &Streamer,
                                          CUOffsetMap &CUMap,
-                                         const std::list<DWARFUnit *> &CUs,
-                                         GDBIndex &GDBIndexSection) {
+                                         const std::list<DWARFUnit *> &CUs) {
   DIEBlder.generateAbbrevs();
   DIEBlder.finish();
   // generate debug_info and CUMap
   for (DWARFUnit *CU : CUs) {
-    emitUnit(DIEBlder, Streamer, *CU, GDBIndexSection);
+    emitUnit(DIEBlder, Streamer, *CU);
     const uint32_t StartOffset = CUOffset;
     DIE *UnitDIE = DIEBlder.getUnitDIEbyUnit(*CU);
     CUOffset += CU->getHeaderSize();

>From fb7a6d7a9ffc95413805d5f02e4c5ccae06a377a Mon Sep 17 00:00:00 2001
From: Sayhaan Siddiqui <sayhaan at meta.com>
Date: Fri, 7 Jun 2024 13:21:30 -0700
Subject: [PATCH 3/4] Formatting changes

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
---
 bolt/lib/Rewrite/DWARFRewriter.cpp | 37 +++++++++++++++++-------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 45b7b363ab29f..875018ec9ba71 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -187,6 +187,7 @@ class DIEStreamer : public DwarfStreamer {
   DIEBuilder *DIEBldr;
   DWARFRewriter &Rewriter;
   GDBIndex &GDBIndexSection;
+
 private:
   /// Emit the compilation unit header for \p Unit in the debug_info
   /// section.
@@ -242,7 +243,8 @@ class DIEStreamer : public DwarfStreamer {
     }
   }
 
-  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE, unsigned DwarfVersion) {
+  void emitTypeUnitHeader(DWARFUnit &Unit, DIE &UnitDIE,
+                          unsigned DwarfVersion) {
     AsmPrinter &Asm = getAsmPrinter();
     const uint64_t TypeSignature = cast<DWARFTypeUnit>(Unit).getTypeHash();
     DIE *TypeDIE = DIEBldr->getTypeDIE(Unit);
@@ -278,12 +280,13 @@ class DIEStreamer : public DwarfStreamer {
   }
 
 public:
-  DIEStreamer(DIEBuilder *DIEBldr, DWARFRewriter &Rewriter, GDBIndex &GDBIndexSection,
+  DIEStreamer(DIEBuilder *DIEBldr, DWARFRewriter &Rewriter,
+              GDBIndex &GDBIndexSection,
               DWARFLinkerBase::OutputFileType OutFileType,
               raw_pwrite_stream &OutFile,
               DWARFLinkerBase::MessageHandlerTy Warning)
-      : DwarfStreamer(OutFileType, OutFile, Warning), DIEBldr(DIEBldr), Rewriter(Rewriter),
-        GDBIndexSection(GDBIndexSection){};
+      : DwarfStreamer(OutFileType, OutFile, Warning), DIEBldr(DIEBldr),
+        Rewriter(Rewriter), GDBIndexSection(GDBIndexSection){};
 
   using DwarfStreamer::emitCompileUnitHeader;
 
@@ -462,7 +465,8 @@ createDIEStreamer(const Triple &TheTriple, raw_pwrite_stream &OutFile,
                   DWARFRewriter &Rewriter, GDBIndex &GDBIndexSection) {
 
   std::unique_ptr<DIEStreamer> Streamer = std::make_unique<DIEStreamer>(
-      &DIEBldr, Rewriter, GDBIndexSection, DWARFLinkerBase::OutputFileType::Object, OutFile,
+      &DIEBldr, Rewriter, GDBIndexSection,
+      DWARFLinkerBase::OutputFileType::Object, OutFile,
       [&](const Twine &Warning, StringRef Context, const DWARFDie *) {});
   Error Err = Streamer->init(TheTriple, Swift5ReflectionSegmentName);
   if (Err)
@@ -472,8 +476,8 @@ 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) {
   DIE *UnitDIE = DIEBldr.getUnitDIEbyUnit(Unit);
   const DIEBuilder::DWARFUnitInfo &U = DIEBldr.getUnitInfoByDwarfUnit(Unit);
   Streamer.emitUnit(Unit, *UnitDIE);
@@ -498,8 +502,9 @@ emitDWOBuilder(const std::string &DWOName, DIEBuilder &DWODIEBuilder,
       std::make_shared<raw_svector_ostream>(OutBuffer);
   const object::ObjectFile *File = SplitCU.getContext().getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
-  std::unique_ptr<DIEStreamer> Streamer = createDIEStreamer(
-      *TheTriple, *ObjOS, "DwoStreamerInitAug2", DWODIEBuilder, Rewriter, GDBIndexSection);
+  std::unique_ptr<DIEStreamer> Streamer =
+      createDIEStreamer(*TheTriple, *ObjOS, "DwoStreamerInitAug2",
+                        DWODIEBuilder, Rewriter, GDBIndexSection);
   DWARFRewriter::UnitMetaVectorType TUMetaVector;
   DWARFRewriter::UnitMeta CUMI = {0, 0, 0};
   if (SplitCU.getContext().getMaxDWOVersion() >= 5) {
@@ -519,8 +524,7 @@ emitDWOBuilder(const std::string &DWOName, DIEBuilder &DWODIEBuilder,
 
     // 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);
       TUMetaVector.emplace_back(MI);
     }
   }
@@ -704,7 +708,8 @@ void DWARFRewriter::updateDebugInfo() {
         TempRangesSectionWriter->finalizeSection();
 
       emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
-                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter, GDBIndexSection);
+                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
+                     GDBIndexSection);
     }
 
     if (Unit->getVersion() >= 5) {
@@ -729,8 +734,8 @@ void DWARFRewriter::updateDebugInfo() {
       std::make_unique<raw_svector_ostream>(OutBuffer);
   const object::ObjectFile *File = BC.DwCtx->getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
-  std::unique_ptr<DIEStreamer> Streamer =
-      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
+  std::unique_ptr<DIEStreamer> Streamer = createDIEStreamer(
+      *TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
   CUOffsetMap OffsetMap =
       finalizeTypeSections(DIEBlder, *Streamer, GDBIndexSection);
 
@@ -1452,8 +1457,8 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
       std::make_shared<raw_svector_ostream>(OutBuffer);
   const object::ObjectFile *File = BC.DwCtx->getDWARFObj().getFile();
   auto TheTriple = std::make_unique<Triple>(File->makeTriple());
-  std::unique_ptr<DIEStreamer> TypeStreamer =
-      createDIEStreamer(*TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
+  std::unique_ptr<DIEStreamer> TypeStreamer = createDIEStreamer(
+      *TheTriple, *ObjOS, "TypeStreamer", DIEBlder, *this, GDBIndexSection);
 
   // generate debug_info and CUMap
   CUOffsetMap CUMap;

>From 2c2f78dc4947b7a21e18e5e49f8e46616eee4300 Mon Sep 17 00:00:00 2001
From: Sayhaan Siddiqui <sayhaan at meta.com>
Date: Fri, 7 Jun 2024 13:36:09 -0700
Subject: [PATCH 4/4] Formatting

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
---
 bolt/lib/Rewrite/DWARFRewriter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 875018ec9ba71..c3ed4d330677f 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -286,7 +286,7 @@ class DIEStreamer : public DwarfStreamer {
               raw_pwrite_stream &OutFile,
               DWARFLinkerBase::MessageHandlerTy Warning)
       : DwarfStreamer(OutFileType, OutFile, Warning), DIEBldr(DIEBldr),
-        Rewriter(Rewriter), GDBIndexSection(GDBIndexSection){};
+        Rewriter(Rewriter), GDBIndexSection(GDBIndexSection) {};
 
   using DwarfStreamer::emitCompileUnitHeader;
 



More information about the llvm-commits mailing list