[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 11 00:46:52 PDT 2026


================
@@ -610,45 +718,146 @@ getDWONameMap(DWARFContext &DwCtx) {
   }
   return DWOIDToNameMap;
 }
-void DWARFRewriter::updateDebugInfo() {
-  ErrorOr<BinarySection &> DebugInfo = BC.getUniqueSectionByName(".debug_info");
-  if (!DebugInfo)
+
+void DWARFRewriter::finalizeSkeletonAndStrSection(
+    DIEBuilder &PartDIEBlder, DWARFUnit &CU,
+    const std::unordered_map<uint64_t, std::string> &DWOToNameMap) {
+  const std::optional<uint64_t> DWOId = CU.getDWOId();
+  std::optional<DWARFUnit *> DWOCU = DWOId ? BC.getDWOCU(*DWOId) : std::nullopt;
+  const bool HasSplitCU = DWOCU && *DWOCU != nullptr;
+  const unsigned Version = CU.getVersion();
+
+  if (HasSplitCU) {
+    auto It = DWOToNameMap.find(*DWOId);
+    if (It != DWOToNameMap.end()) {
+      PartDIEBlder.updateDWONameCompDir(*StrOffstsWriter, *StrWriter, CU,
+                                        opts::DwarfOutputPath,
+                                        StringRef(It->second));
+      if (Version >= 5 && StrOffstsWriter->isStrOffsetsSectionModified())
+        StrOffstsWriter->finalizeSection(CU, PartDIEBlder);
+    }
+    //  split CU were just finalized above when the
+    // .debug_str_offsets section was modified;
     return;
+  }
 
-  ARangesSectionWriter = std::make_unique<DebugARangesSectionWriter>();
-  StrWriter = std::make_unique<DebugStrWriter>(*BC.DwCtx, false);
-  StrOffstsWriter = std::make_unique<DebugStrOffsetsWriter>(BC);
+  if (Version >= 5)
+    StrOffstsWriter->finalizeSection(CU, PartDIEBlder);
+}
 
-  /// Stores and serializes information that will be put into the
-  /// .debug_addr DWARF section.
-  std::unique_ptr<DebugAddrWriter> FinalAddrWriter;
+/// Merge: compute per-CU loclist/legacy-loc bases and in the
+/// same loop apply DWARF5 rnglists/loclists base fixups and legacy
+/// .debug_loc base fixups.
+void DWARFRewriter::mergePerBucketLocsAndRanges(
+    DIEBuilder &PartDIEBlder, BucketLocalWriter &LocalWriters,
+    const std::unordered_map<uint64_t, std::string> &DWOToNameMap,
+    BucketLocAccumOffset &Accum) {
+  const auto &Processed = PartDIEBlder.getProcessedCUs();
+  std::vector<DWARFUnit *> SortedCUs;
+  SortedCUs.assign(Processed.begin(), Processed.end());
+  // Ensure deterministic output by sorting CUs in section offset order.
+  llvm::sort(SortedCUs, [](const DWARFUnit *A, const DWARFUnit *B) {
+    return A->getOffset() < B->getOffset();
+  });
+
+  for (DWARFUnit *CU : SortedCUs) {
+    // record CUs order to make loc/loclist order correct
+    Accum.LocListCUOrder.push_back(CU->getOffset());
+    finalizeSkeletonAndStrSection(PartDIEBlder, *CU, DWOToNameMap);
+  }
+  for (DWARFUnit *CU : SortedCUs) {
+    const uint64_t CUOffset = CU->getOffset();
+    // Compute this CU's base and adjust loclists / legacy loc fixups inline.
+    uint64_t LoclistsBase = 0;
+    uint64_t LegacyLocBase = 0;
+
+    auto LocIt = LocListWritersByCU.find(CUOffset);
+    if (LocIt != LocListWritersByCU.end()) {
+      DebugLocWriter *LocWriter = LocIt->second.get();
+      auto *LocListWriter = dyn_cast<DebugLoclistWriter>(LocWriter);
+      const uint64_t BufferSize = LocWriter->getLocBufferSize();
+
+      if (LocListWriter && LocListWriter->getDwarfVersion() >= 5 &&
+          !LocListWriter->isSplitDwarf() && BufferSize != 0) {
+        LoclistsBase = Accum.LoclistsOffset;
+        Accum.LoclistsOffset += BufferSize;
+      } else if (!LocListWriter) {
+        LegacyLocBase = Accum.LegacyLocOffset;
+        // Each per-CU DWARF4 .debug_loc buffer begins with a 16-byte empty-list
+        // sentinel (offset 0). That sentinel is emitted once for the whole
+        // section in makeFinalLocListsSection, and each CU's leading 16 bytes
+        // are stripped, so a CU contributes BufferSize-16 bytes to the global
+        // section.
+
+        if (BufferSize > 16)
+          Accum.LegacyLocOffset += BufferSize - 16;
+      }
+    }
 
-  if (BC.isDWARF5Used()) {
-    FinalAddrWriter = std::make_unique<DebugAddrWriterDwarf5>(&BC);
-    RangeListsSectionWriter = std::make_unique<DebugRangeListsSectionWriter>();
-  } else {
-    FinalAddrWriter = std::make_unique<DebugAddrWriter>(&BC);
+    if (CU->getVersion() >= 5) {
+      const uint64_t RangsOffset = RangeListsSectionWriter->getSectionOffset();
+      fixupDWARFRanges(PartDIEBlder, *CU, RangsOffset);
----------------
Jinjie-Huang wrote:

Wondering why there is a separate helper function for processing ranges, but not for loc?

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


More information about the llvm-commits mailing list