Skip to content

[BOLT][DWARF][NFC] Split processUnitDIE into two lambdas #99225

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

Merged
merged 4 commits into from
Jul 20, 2024

Conversation

sayhaan
Copy link
Member

@sayhaan sayhaan commented Jul 16, 2024

Split processUnitDIE into two lambdas to separate the processing of DWO CUs and CUs in the main binary.

Copy link

github-actions bot commented Jul 16, 2024

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

@sayhaan sayhaan marked this pull request as ready for review July 16, 2024 20:26
@llvmbot llvmbot added the BOLT label Jul 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2024

@llvm/pr-subscribers-clang-driver
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-lldb

@llvm/pr-subscribers-bolt

Author: Sayhaan Siddiqui (sayhaan)

Changes

Split processUnitDIE into two lambdas to separate the processing of DWO CUs and CUs in the main binary.


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

1 Files Affected:

  • (modified) bolt/lib/Rewrite/DWARFRewriter.cpp (+120-97)
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 042c39a574561..c7440c82f1abd 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -632,44 +632,47 @@ void DWARFRewriter::updateDebugInfo() {
   std::mutex AccessMutex;
   // Needs to be invoked in the same order as CUs are processed.
   auto createRangeLocListAddressWriters =
-      [&](DWARFUnit &CU) -> DebugLocWriter * {
-    std::lock_guard<std::mutex> Lock(AccessMutex);
-    const uint16_t DwarfVersion = CU.getVersion();
-    if (DwarfVersion >= 5) {
-      auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
-          &BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
-      RangeListsSectionWriter->setAddressWriter(AddrW.get());
-      LocListWritersByCU[CUIndex] =
-          std::make_unique<DebugLoclistWriter>(CU, DwarfVersion, false, *AddrW);
-
-      if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
-        assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
-               "RangeLists writer for DWO unit already exists.");
-        auto DWORangeListsSectionWriter =
-            std::make_unique<DebugRangeListsSectionWriter>();
-        DWORangeListsSectionWriter->initSection(CU);
-        DWORangeListsSectionWriter->setAddressWriter(AddrW.get());
-        RangeListsWritersByCU[*DWOId] = std::move(DWORangeListsSectionWriter);
-      }
-      AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
+      [&](DWARFUnit &CU,
+          llvm::DenseMap<uint64_t, uint64_t> &LocListWritersIndexByCU) {
+        std::lock_guard<std::mutex> Lock(AccessMutex);
 
-    } else {
-      auto AddrW =
-          std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
-      AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
-      LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
-      if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
-        assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
-               "LegacyRangeLists writer for DWO unit already exists.");
-        auto LegacyRangesSectionWriterByCU =
-            std::make_unique<DebugRangesSectionWriter>();
-        LegacyRangesSectionWriterByCU->initSection(CU);
-        LegacyRangesWritersByCU[*DWOId] =
-            std::move(LegacyRangesSectionWriterByCU);
-      }
-    }
-    return LocListWritersByCU[CUIndex++].get();
-  };
+        const uint16_t DwarfVersion = CU.getVersion();
+        if (DwarfVersion >= 5) {
+          auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
+              &BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
+          RangeListsSectionWriter->setAddressWriter(AddrW.get());
+          LocListWritersByCU[CUIndex] = std::make_unique<DebugLoclistWriter>(
+              CU, DwarfVersion, false, *AddrW);
+
+          if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
+            assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
+                   "RangeLists writer for DWO unit already exists.");
+            auto DWORangeListsSectionWriter =
+                std::make_unique<DebugRangeListsSectionWriter>();
+            DWORangeListsSectionWriter->initSection(CU);
+            DWORangeListsSectionWriter->setAddressWriter(AddrW.get());
+            RangeListsWritersByCU[*DWOId] =
+                std::move(DWORangeListsSectionWriter);
+          }
+          AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
+
+        } else {
+          auto AddrW =
+              std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
+          AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
+          LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
+          if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
+            assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
+                   "LegacyRangeLists writer for DWO unit already exists.");
+            auto LegacyRangesSectionWriterByCU =
+                std::make_unique<DebugRangesSectionWriter>();
+            LegacyRangesSectionWriterByCU->initSection(CU);
+            LegacyRangesWritersByCU[*DWOId] =
+                std::move(LegacyRangesSectionWriterByCU);
+          }
+        }
+        LocListWritersIndexByCU[CU.getOffset()] = CUIndex++;
+      };
 
   DWARF5AcceleratorTable DebugNamesTable(opts::CreateDebugNames, BC,
                                          *StrWriter);
@@ -677,75 +680,77 @@ void DWARFRewriter::updateDebugInfo() {
   DWPState State;
   if (opts::WriteDWP)
     initDWPState(State);
-  auto processUnitDIE = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder) {
-    // Check if the unit is a skeleton and we need special updates for it and
-    // its matching split/DWO CU.
+  auto processSplitCU = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder,
+                            std::optional<uint64_t> &RangesBase) {
     std::optional<DWARFUnit *> SplitCU;
-    std::optional<uint64_t> RangesBase;
     std::optional<uint64_t> DWOId = Unit->getDWOId();
     if (DWOId)
       SplitCU = BC.getDWOCU(*DWOId);
-    DebugLocWriter *DebugLocWriter = createRangeLocListAddressWriters(*Unit);
-    DebugRangesSectionWriter *RangesSectionWriter =
-        Unit->getVersion() >= 5 ? RangeListsSectionWriter.get()
-                                : LegacyRangesSectionWriter.get();
-    DebugAddrWriter *AddressWriter =
-        AddressWritersByCU[Unit->getOffset()].get();
-    // Skipping CUs that failed to load.
-    if (SplitCU) {
-      DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
-                               Unit);
-      DWODIEBuilder.buildDWOUnit(**SplitCU);
-      std::string DWOName = "";
-      std::optional<std::string> DwarfOutputPath =
-          opts::DwarfOutputPath.empty()
-              ? std::nullopt
-              : std::optional<std::string>(opts::DwarfOutputPath.c_str());
-      {
-        std::lock_guard<std::mutex> Lock(AccessMutex);
-        DWOName = DIEBlder->updateDWONameCompDir(
-            *StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt);
-      }
-      DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
-      DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
-      DWODIEBuilder.updateDWONameCompDirForTypes(DWOStrOffstsWriter,
-                                                 DWOStrWriter, **SplitCU,
-                                                 DwarfOutputPath, DWOName);
-      DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true,
-                                           *AddressWriter);
-      DebugRangesSectionWriter *TempRangesSectionWriter = RangesSectionWriter;
-      if (Unit->getVersion() >= 5) {
-        TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get();
-      } else {
-        TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get();
-        RangesBase = RangesSectionWriter->getSectionOffset();
-        setDwoRangesBase(*DWOId, *RangesBase);
-      }
-
-      updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
-                          *TempRangesSectionWriter, *AddressWriter);
-      DebugLocDWoWriter.finalize(DWODIEBuilder,
-                                 *DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
-      if (Unit->getVersion() >= 5)
-        TempRangesSectionWriter->finalizeSection();
-
-      emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
-                     DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
-                     GDBIndexSection);
+    if (!SplitCU)
+      return;
+    DebugAddrWriter &AddressWriter =
+        *AddressWritersByCU[Unit->getOffset()].get();
+    DIEBuilder DWODIEBuilder(BC, &(*SplitCU)->getContext(), DebugNamesTable,
+                             Unit);
+    DWODIEBuilder.buildDWOUnit(**SplitCU);
+    std::string DWOName = "";
+    std::optional<std::string> DwarfOutputPath =
+        opts::DwarfOutputPath.empty()
+            ? std::nullopt
+            : std::optional<std::string>(opts::DwarfOutputPath.c_str());
+    {
+      std::lock_guard<std::mutex> Lock(AccessMutex);
+      DWOName = DIEBlder->updateDWONameCompDir(
+          *StrOffstsWriter, *StrWriter, *Unit, DwarfOutputPath, std::nullopt);
     }
+    DebugStrOffsetsWriter DWOStrOffstsWriter(BC);
+    DebugStrWriter DWOStrWriter((*SplitCU)->getContext(), true);
+    DWODIEBuilder.updateDWONameCompDirForTypes(
+        DWOStrOffstsWriter, DWOStrWriter, **SplitCU, DwarfOutputPath, DWOName);
+    DebugLoclistWriter DebugLocDWoWriter(*Unit, Unit->getVersion(), true,
+                                         AddressWriter);
+    DebugRangesSectionWriter *TempRangesSectionWriter;
+    if (Unit->getVersion() >= 5) {
+      TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get();
+    } else {
+      TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get();
+      RangesBase = Unit->getVersion() >= 5
+                       ? RangeListsSectionWriter.get()->getSectionOffset()
+                       : LegacyRangesSectionWriter.get()->getSectionOffset();
+      setDwoRangesBase(*DWOId, *RangesBase);
+    }
+
+    updateUnitDebugInfo(*(*SplitCU), DWODIEBuilder, DebugLocDWoWriter,
+                        *TempRangesSectionWriter, AddressWriter);
+    DebugLocDWoWriter.finalize(DWODIEBuilder,
+                               *DWODIEBuilder.getUnitDIEbyUnit(**SplitCU));
+    if (Unit->getVersion() >= 5)
+      TempRangesSectionWriter->finalizeSection();
 
+    emitDWOBuilder(DWOName, DWODIEBuilder, *this, **SplitCU, *Unit, State,
+                   DebugLocDWoWriter, DWOStrOffstsWriter, DWOStrWriter,
+                   GDBIndexSection);
+  };
+  auto processMainBinaryCU = [&](DWARFUnit *Unit, DIEBuilder *DIEBlder,
+                                 DebugLocWriter *DebugLocWriter,
+                                 std::optional<uint64_t> &RangesBase) {
+    DebugAddrWriter &AddressWriter =
+        *AddressWritersByCU[Unit->getOffset()].get();
+    DebugRangesSectionWriter &RangesSectionWriter =
+        Unit->getVersion() >= 5 ? *RangeListsSectionWriter.get()
+                                : *LegacyRangesSectionWriter.get();
     if (Unit->getVersion() >= 5) {
-      RangesBase = RangesSectionWriter->getSectionOffset() +
+      RangesBase = RangesSectionWriter.getSectionOffset() +
                    getDWARF5RngListLocListHeaderSize();
-      RangesSectionWriter->initSection(*Unit);
+      RangesSectionWriter.initSection(*Unit);
       StrOffstsWriter->finalizeSection(*Unit, *DIEBlder);
     }
 
-    updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, *RangesSectionWriter,
-                        *AddressWriter, RangesBase);
+    updateUnitDebugInfo(*Unit, *DIEBlder, *DebugLocWriter, RangesSectionWriter,
+                        AddressWriter, RangesBase);
     DebugLocWriter->finalize(*DIEBlder, *DIEBlder->getUnitDIEbyUnit(*Unit));
     if (Unit->getVersion() >= 5)
-      RangesSectionWriter->finalizeSection();
+      RangesSectionWriter.finalizeSection();
   };
 
   DIEBuilder DIEBlder(BC, BC.DwCtx.get(), DebugNamesTable);
@@ -768,8 +773,26 @@ void DWARFRewriter::updateDebugInfo() {
     CUPartitionVector PartVec = partitionCUs(*BC.DwCtx);
     for (std::vector<DWARFUnit *> &Vec : PartVec) {
       DIEBlder.buildCompileUnits(Vec);
-      for (DWARFUnit *CU : DIEBlder.getProcessedCUs())
-        processUnitDIE(CU, &DIEBlder);
+      llvm::SmallVector<DWARFUnit *> CompileUnits(
+          std::begin(DIEBlder.getProcessedCUs()),
+          std::end(DIEBlder.getProcessedCUs()));
+      llvm::DenseMap<uint64_t, uint64_t> RangesBaseByCU;
+      llvm::DenseMap<uint64_t, uint64_t> LocListWritersIndexByCU;
+      for (DWARFUnit *CU : CompileUnits) {
+        createRangeLocListAddressWriters(*CU, LocListWritersIndexByCU);
+        std::optional<uint64_t> RangesBase;
+        processSplitCU(CU, &DIEBlder, RangesBase);
+        if (RangesBase)
+          RangesBaseByCU[CU->getOffset()] = *RangesBase;
+      }
+      for (DWARFUnit *CU : CompileUnits) {
+        std::optional<uint64_t> RangesBase;
+        if (RangesBaseByCU.count(CU->getOffset()))
+          RangesBase = RangesBaseByCU[CU->getOffset()];
+        DebugLocWriter *DebugLocWriter =
+            LocListWritersByCU[LocListWritersIndexByCU[CU->getOffset()]].get();
+        processMainBinaryCU(CU, &DIEBlder, DebugLocWriter, RangesBase);
+      }
       finalizeCompileUnits(DIEBlder, *Streamer, OffsetMap,
                            DIEBlder.getProcessedCUs(), *FinalAddrWriter);
     }
@@ -777,8 +800,8 @@ void DWARFRewriter::updateDebugInfo() {
     // Update unit debug info in parallel
     ThreadPoolInterface &ThreadPool = ParallelUtilities::getThreadPool();
     for (std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units())
-      ThreadPool.async(processUnitDIE, CU.get(), &DIEBlder);
-    ThreadPool.wait();
+      // ThreadPool.async(processUnitDIE, CU.get(), &DIEBlder);
+      ThreadPool.wait();
   }
 
   DebugNamesTable.emitAccelTable();

@sayhaan sayhaan requested a review from ayermolo July 18, 2024 17:58
@sayhaan sayhaan requested a review from ayermolo July 18, 2024 20:25
@sayhaan sayhaan requested a review from JDevlieghere as a code owner July 19, 2024 23:39
@llvmbot llvmbot added clang Clang issues not falling into any other category lldb clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jul 19, 2024
@sayhaan sayhaan removed the request for review from JDevlieghere July 19, 2024 23:40
@sayhaan sayhaan force-pushed the T187681160-1 branch 2 times, most recently from c995912 to bb76ea1 Compare July 20, 2024 00:24
sayhaan added 3 commits July 19, 2024 17:25
Summary: Remove deprecated DeterministicDebugInfo option and its uses.

Test Plan: Run `ninja llvm check-bolt.`

Reviewers: ayermolo, aaupov, #llvm-bolt

Reviewed By: ayermolo

Differential Revision: https://phabricator.intern.facebook.com/D59942763

Tasks: T187681160
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D59822423
@sayhaan sayhaan merged commit 6747f12 into llvm:main Jul 20, 2024
6 checks passed
@aaupov
Copy link
Contributor

aaupov commented Jul 20, 2024

Heads up that this diff is not NFC: https://lab.llvm.org/buildbot/#/builders/92/builds/2042

sayhaan added a commit that referenced this pull request Jul 22, 2024
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
Split processUnitDIE into two lambdas to separate the processing of DWO
CUs and CUs in the main binary.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251258
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…9904)

Summary: Reverts #99225

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BOLT clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category lldb
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants