[llvm] [BOLT][DWARF] Avoid invalid work if DWO id is zero (PR #154749)

Jinjie Huang via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 31 10:23:01 PDT 2025


https://github.com/Jinjie-Huang updated https://github.com/llvm/llvm-project/pull/154749

>From b6c3f3c5f3b2651b989462cc4f0d2614bc418f76 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Thu, 21 Aug 2025 21:29:01 +0800
Subject: [PATCH 1/3] Avoid unnecessary work if DWO id is zero

---
 bolt/lib/Rewrite/DWARFRewriter.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 0c1a1bac6c72e..bcde3ccfa691e 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -613,6 +613,7 @@ void DWARFRewriter::updateDebugInfo() {
   auto createRangeLocListAddressWriters = [&](DWARFUnit &CU) {
     std::lock_guard<std::mutex> Lock(AccessMutex);
     const uint16_t DwarfVersion = CU.getVersion();
+    std::optional<uint64_t> DWOId = CU.getDWOId();
     if (DwarfVersion >= 5) {
       auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
           &BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
@@ -620,7 +621,7 @@ void DWARFRewriter::updateDebugInfo() {
       LocListWritersByCU[CUIndex] =
           std::make_unique<DebugLoclistWriter>(CU, DwarfVersion, false, *AddrW);
 
-      if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
+      if (DWOId && *DWOId != 0) {
         assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
                "RangeLists writer for DWO unit already exists.");
         auto DWORangeListsSectionWriter =
@@ -635,7 +636,7 @@ void DWARFRewriter::updateDebugInfo() {
           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()) {
+      if (DWOId && *DWOId != 0) {
         assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
                "LegacyRangeLists writer for DWO unit already exists.");
         auto LegacyRangesSectionWriterByCU =

>From f8c96a9654a51edbe476eb9a5ec265e8db61d8f9 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Thu, 28 Aug 2025 21:57:48 +0800
Subject: [PATCH 2/3] Skip updating DWO CUs whose DWO ID is 0.

---
 bolt/lib/Rewrite/DWARFRewriter.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index bcde3ccfa691e..1eb7932edbc2b 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -614,6 +614,8 @@ void DWARFRewriter::updateDebugInfo() {
     std::lock_guard<std::mutex> Lock(AccessMutex);
     const uint16_t DwarfVersion = CU.getVersion();
     std::optional<uint64_t> DWOId = CU.getDWOId();
+    if (DWOId && *DWOId == 0)
+      return;
     if (DwarfVersion >= 5) {
       auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
           &BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
@@ -621,7 +623,7 @@ void DWARFRewriter::updateDebugInfo() {
       LocListWritersByCU[CUIndex] =
           std::make_unique<DebugLoclistWriter>(CU, DwarfVersion, false, *AddrW);
 
-      if (DWOId && *DWOId != 0) {
+      if (DWOId) {
         assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
                "RangeLists writer for DWO unit already exists.");
         auto DWORangeListsSectionWriter =
@@ -636,7 +638,7 @@ void DWARFRewriter::updateDebugInfo() {
           std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
       AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
       LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
-      if (DWOId && *DWOId != 0) {
+      if (DWOId) {
         assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
                "LegacyRangeLists writer for DWO unit already exists.");
         auto LegacyRangesSectionWriterByCU =
@@ -733,7 +735,7 @@ void DWARFRewriter::updateDebugInfo() {
       createRangeLocListAddressWriters(*CU);
       std::optional<DWARFUnit *> SplitCU;
       std::optional<uint64_t> DWOId = CU->getDWOId();
-      if (DWOId)
+      if (DWOId && *DWOId != 0)
         SplitCU = BC.getDWOCU(*DWOId);
       if (!SplitCU)
         continue;

>From bb173687ce493485f691122fd4c7950aaedb4de9 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Mon, 1 Sep 2025 01:22:07 +0800
Subject: [PATCH 3/3] Do not generate information for DWO ID 0

---
 bolt/lib/Core/DIEBuilder.cpp       |  3 +++
 bolt/lib/Rewrite/DWARFRewriter.cpp | 10 +++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
index b041dc5ea1cce..a313b298161d6 100644
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -583,6 +583,9 @@ DWARFDie DIEBuilder::resolveDIEReference(
   uint64_t TmpRefOffset = RefOffset;
   if ((RefCU =
            getUnitForOffset(*this, *DwarfContext, TmpRefOffset, AttrSpec))) {
+    std::optional<uint64_t> DWOId = RefCU->getDWOId();
+    if (DWOId && *DWOId == 0)
+      return DWARFDie();
     /// Trying to add to current working set in case it's cross CU reference.
     registerUnit(*RefCU, true);
     DWARFDataExtractor DebugInfoData = RefCU->getDebugInfoExtractor();
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 1eb7932edbc2b..32647bd6449b4 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -551,6 +551,9 @@ static CUPartitionVector partitionCUs(DWARFContext &DwCtx) {
   unsigned Counter = 0;
   const DWARFDebugAbbrev *Abbr = DwCtx.getDebugAbbrev();
   for (std::unique_ptr<DWARFUnit> &CU : DwCtx.compile_units()) {
+    std::optional<uint64_t> DWOId = CU->getDWOId();
+    if (DWOId && *DWOId == 0)
+      continue;
     Expected<const DWARFAbbreviationDeclarationSet *> AbbrDeclSet =
         Abbr->getAbbreviationDeclarationSet(CU->getAbbreviationsOffset());
     if (!AbbrDeclSet) {
@@ -613,9 +616,6 @@ void DWARFRewriter::updateDebugInfo() {
   auto createRangeLocListAddressWriters = [&](DWARFUnit &CU) {
     std::lock_guard<std::mutex> Lock(AccessMutex);
     const uint16_t DwarfVersion = CU.getVersion();
-    std::optional<uint64_t> DWOId = CU.getDWOId();
-    if (DWOId && *DWOId == 0)
-      return;
     if (DwarfVersion >= 5) {
       auto AddrW = std::make_unique<DebugAddrWriterDwarf5>(
           &BC, CU.getAddressByteSize(), CU.getAddrOffsetSectionBase());
@@ -623,7 +623,7 @@ void DWARFRewriter::updateDebugInfo() {
       LocListWritersByCU[CUIndex] =
           std::make_unique<DebugLoclistWriter>(CU, DwarfVersion, false, *AddrW);
 
-      if (DWOId) {
+      if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
         assert(RangeListsWritersByCU.count(*DWOId) == 0 &&
                "RangeLists writer for DWO unit already exists.");
         auto DWORangeListsSectionWriter =
@@ -638,7 +638,7 @@ void DWARFRewriter::updateDebugInfo() {
           std::make_unique<DebugAddrWriter>(&BC, CU.getAddressByteSize());
       AddressWritersByCU[CU.getOffset()] = std::move(AddrW);
       LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
-      if (DWOId) {
+      if (std::optional<uint64_t> DWOId = CU.getDWOId()) {
         assert(LegacyRangesWritersByCU.count(*DWOId) == 0 &&
                "LegacyRangeLists writer for DWO unit already exists.");
         auto LegacyRangesSectionWriterByCU =



More information about the llvm-commits mailing list