[llvm] [RFC][BOLT] Add a new parallel DWARF processing(2/2) (PR #197859)

Jinjie Huang via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 05:51:52 PDT 2026


================
@@ -674,86 +907,163 @@ void DWARFRewriter::updateDebugInfo() {
             std::move(LegacyRangesSectionWriterByCU);
       }
     }
-    LocListWritersIndexByCU[CU.getOffset()] = CUIndex++;
-  };
+  }
+}
 
-  DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
-                                         *StrWriter);
-  if (DebugNamesTable.isCreated())
-    DebugNamesTable.preAllocateUnits(*BC.DwCtx);
-  std::unordered_map<uint64_t, std::string> DWOToNameMap =
-      getDWONameMap(*BC.DwCtx);
-  GDBIndex GDBIndexSection(BC);
-  if (!opts::DwarfOutputPath.empty()) {
-    if (std::error_code EC =
-            sys::fs::create_directories(opts::DwarfOutputPath)) {
-      BC.errs() << "BOLT-ERROR: could not create directory '"
-                << opts::DwarfOutputPath << "': " << EC.message() << '\n';
-      return;
+void DWARFRewriter::processMainBinaryCU(DWARFUnit &Unit, DIEBuilder &DIEBlder,
+                                        BucketLocalWriter &LocalWriter) {
+  std::optional<DWARFUnit *> SplitCU;
+  std::optional<uint64_t> RangesBase;
+  std::optional<uint64_t> DWOId = Unit.getDWOId();
+  uint8_t DWARFVersion = Unit.getVersion();
+  if (DWOId)
+    SplitCU = BC.getDWOCU(*DWOId);
+  auto LocIt = LocListWritersByCU.find(Unit.getOffset());
+  assert(LocIt != LocListWritersByCU.end() &&
+         "LocListWriter does not exist for CU");
+  DebugLocWriter &DebugLocWriter = *LocIt->second.get();
+  auto AddrIt = AddressWritersByCU.find(Unit.getOffset());
+  assert(AddrIt != AddressWritersByCU.end() &&
+         "AddressWriter does not exist for CU");
+  DebugAddrWriter &AddressWriter = *AddrIt->second.get();
+  if (DWARFVersion >= 5) {
+    if (!LocalWriter.RngListsWriter)
+      LocalWriter.RngListsWriter =
+          std::make_unique<DebugRangeListsSectionWriter>();
+  } else {
+    if (!LocalWriter.LegacyRangesWriter) {
+      LocalWriter.LegacyRangesWriter =
+          std::make_unique<DebugRangesSectionWriter>();
+      LocalWriter.LegacyRangesWriter->initSection();
     }
   }
-  auto processSplitCU = [&](DWARFUnit &Unit, DWARFUnit &SplitCU,
+
+  DebugRangesSectionWriter &RangesSectionWriter =
+      DWARFVersion >= 5 ? *LocalWriter.RngListsWriter
+                        : *LocalWriter.LegacyRangesWriter;
+
+  if (DWARFVersion >= 5) {
+    LocalWriter.RngListsWriter->setAddressWriter(&AddressWriter);
+    RangesBase = RangesSectionWriter.getSectionOffset() +
+                 getDWARF5RngListLocListHeaderSize();
+    RangesSectionWriter.initSection(Unit);
+  } else if (SplitCU) {
+    // DWARF4: only split-dwarf CUs need a ranges base
+    RangesBase = LocalWriter.LegacyRangesWriter->getSectionOffset();
+  }
+  updateUnitDebugInfo(Unit, DIEBlder, DebugLocWriter, RangesSectionWriter,
+                      AddressWriter, RangesBase);
+  DebugLocWriter.finalize(DIEBlder, *DIEBlder.getUnitDIEbyUnit(Unit));
+  if (DWARFVersion >= 5)
+    RangesSectionWriter.finalizeSection();
+}
+
+void DWARFRewriter::processBucket(
+    size_t Idx, SmallVector<SmallVector<DWARFUnit *>> &PartVec,
+    std::vector<std::unique_ptr<DIEBuilder>> &BucketDIEBlders,
+    std::vector<BucketLocalWriter> &LocalWriters,
+    DWARF5AcceleratorTable &DebugNamesTable,
+    std::unordered_map<uint64_t, std::string> &DWOToNameMap,
+    GDBIndex &GDBIndexSection) {
+  SmallVector<DWARFUnit *> &Vec = PartVec[Idx];
+  std::unique_ptr<DIEBuilder> &BucketDIEBlder = BucketDIEBlders[Idx];
+  BucketDIEBlder =
+      std::make_unique<DIEBuilder>(BC, BC.DwCtx.get(), DebugNamesTable);
+  DIEBuilder &PartDIEBlder = *BucketDIEBlder;
+  PartDIEBlder.buildCompileUnits(Vec);
+  auto ProcessSplitCU = [&](DWARFUnit &Unit, DWARFUnit &SplitCU,
                             DebugRangesSectionWriter &TempRangesSectionWriter,
                             DebugAddrWriter &AddressWriter,
-                            const std::string DWOName,
+                            const std::string &DWOName,
                             DIEBuilder &DWODIEBuilder) {
     DWODIEBuilder.buildDWOUnit(SplitCU);
     DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
-    DebugStrWriter DWOStrWriter((SplitCU).getContext(), true);
+    DebugStrWriter DWOStrWriter(SplitCU.getContext(), true);
     DWODIEBuilder.updateDWONameCompDirForTypes(DWOStrOffstsWriter, DWOStrWriter,
                                                SplitCU, opts::DwarfOutputPath,
                                                DWOName);
     DebugLoclistWriter DebugLocDWoWriter(Unit, Unit.getVersion(), true,
                                          AddressWriter);
-
     updateUnitDebugInfo(SplitCU, DWODIEBuilder, DebugLocDWoWriter,
                         TempRangesSectionWriter, AddressWriter);
-    DIE *UnitDIE = DWODIEBuilder.getUnitDIEbyUnit(SplitCU);
-    if (!UnitDIE) {
-      errs() << "BOLT-WARNING: failed to construct DIE for split CU "
-             << Twine::utohexstr(*Unit.getDWOId()) << "\n";
-      return;
-    }
-    DebugLocDWoWriter.finalize(DWODIEBuilder, *UnitDIE);
+    DebugLocDWoWriter.finalize(DWODIEBuilder,
+                               *DWODIEBuilder.getUnitDIEbyUnit(SplitCU));
     if (Unit.getVersion() >= 5)
       TempRangesSectionWriter.finalizeSection();
-
     emitDWOBuilder(DWOName, DWODIEBuilder, *this, SplitCU, Unit,
                    DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
                    GDBIndexSection, TempRangesSectionWriter);
+    {
+      std::lock_guard<std::mutex> Lock(DebugNamesUpdateMutex);
+      DWODIEBuilder.updateDebugNamesTable();
+    }
   };
-  auto processMainBinaryCU = [&](DWARFUnit &Unit, DIEBuilder &DIEBlder) {
+
+  for (DWARFUnit *CU : PartDIEBlder.getProcessedCUs()) {
     std::optional<DWARFUnit *> SplitCU;
-    std::optional<uint64_t> RangesBase;
-    std::optional<uint64_t> DWOId = Unit.getDWOId();
+    std::optional<uint64_t> DWOId = CU->getDWOId();
     if (DWOId)
       SplitCU = BC.getDWOCU(*DWOId);
-    DebugLocWriter &DebugLocWriter =
-        *LocListWritersByCU[LocListWritersIndexByCU[Unit.getOffset()]].get();
-    DebugRangesSectionWriter &RangesSectionWriter =
-        Unit.getVersion() >= 5 ? *RangeListsSectionWriter
-                               : *LegacyRangesSectionWriter;
-    DebugAddrWriter &AddressWriter =
-        *AddressWritersByCU[Unit.getOffset()].get();
-    if (Unit.getVersion() >= 5)
-      RangeListsSectionWriter->setAddressWriter(&AddressWriter);
-    if (Unit.getVersion() >= 5) {
-      RangesBase = RangesSectionWriter.getSectionOffset() +
-                   getDWARF5RngListLocListHeaderSize();
-      RangesSectionWriter.initSection(Unit);
-      if (!SplitCU)
-        StrOffstsWriter->finalizeSection(Unit, DIEBlder);
-    } else if (SplitCU) {
-      RangesBase = LegacyRangesSectionWriter->getSectionOffset();
+    if (SplitCU) {
+      DebugAddrWriter &AddressWriter = *AddressWritersByCU.at(CU->getOffset());
+      DebugRangesSectionWriter &TempRangesSectionWriter =
+          CU->getVersion() >= 5 ? *RangeListsWritersByCU.at(*DWOId)
+                                : *LegacyRangesWritersByCU.at(*DWOId);
+      auto NameIt = DWOToNameMap.find(*DWOId);
+      if (NameIt == DWOToNameMap.end()) {
+        BC.errs() << "BOLT-WARNING: [internal-dwarf-warning]: Could not find "
+                     "DWO name for DWO ID "
+                  << Twine::utohexstr(*DWOId) << ".\n";
+        continue;
+      }
+      auto DWOName = NameIt->second;
+      auto DWODIEBuilderPtr = std::make_unique<DIEBuilder>(
+          BC, &(**SplitCU).getContext(), DebugNamesTable, CU);
+      DIEBuilder &DWODIEBuilder = *DWODIEBuilderPtr;
+      ProcessSplitCU(*CU, **SplitCU, TempRangesSectionWriter, AddressWriter,
+                     DWOName, DWODIEBuilder);
     }
+    processMainBinaryCU(*CU, PartDIEBlder, LocalWriters[Idx]);
+  }
+}
 
-    updateUnitDebugInfo(Unit, DIEBlder, DebugLocWriter, RangesSectionWriter,
-                        AddressWriter, RangesBase);
-    DebugLocWriter.finalize(DIEBlder, *DIEBlder.getUnitDIEbyUnit(Unit));
-    if (Unit.getVersion() >= 5)
-      RangesSectionWriter.finalizeSection();
-  };
+void DWARFRewriter::updateDebugInfo() {
+  ErrorOr<BinarySection &> DebugInfo = BC.getUniqueSectionByName(".debug_info");
+  if (!DebugInfo)
+    return;
+  ARangesSectionWriter = std::make_unique<DebugARangesSectionWriter>();
+  StrWriter = std::make_unique<DebugStrWriter>(*BC.DwCtx, false);
+  StrOffstsWriter = std::make_unique<DebugStrOffsetsWriter>(BC);
+  /// Stores and serializes information that will be put into the
+  /// .debug_addr DWARF section.
+  std::unique_ptr<DebugAddrWriter> FinalAddrWriter;
+  if (BC.isDWARF5Used()) {
+    FinalAddrWriter = std::make_unique<DebugAddrWriterDwarf5>(&BC);
+    RangeListsSectionWriter = std::make_unique<DebugRangeListsSectionWriter>();
+  } else {
+    FinalAddrWriter = std::make_unique<DebugAddrWriter>(&BC);
+  }
+  if (BC.isDWARFLegacyUsed()) {
+    LegacyRangesSectionWriter = std::make_unique<DebugRangesSectionWriter>();
+    LegacyRangesSectionWriter->initSection();
+  }
 
+  uint32_t CUSize = std::distance(BC.DwCtx->compile_units().begin(),
+                                  BC.DwCtx->compile_units().end());
+  // Pre-create per-CU writers in a single thread.
+  // Worker threads should only read from these maps.
+  createRangeLocListAndAddressWriters();
+  // If the user requested an output directory for rewritten .dwo files, make
+  // sure it exists before any worker attempt to create the files.
+  if (!opts::DwarfOutputPath.empty() && !sys::fs::exists(opts::DwarfOutputPath))
+    (void)sys::fs::create_directories(opts::DwarfOutputPath);
+  std::unordered_map<uint64_t, std::string> DWOToNameMap =
----------------
Jinjie-Huang wrote:

Guess what mentioned [here](https://github.com/llvm/llvm-project/pull/197670#issuecomment-4523733462) is the reason we don't use DenseMap here?

https://github.com/llvm/llvm-project/pull/197859


More information about the llvm-commits mailing list