[llvm] [BOLT][DWARF] Get DWO file via relative path if the CompilationDir does not exist (PR #154515)

Jinjie Huang via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 6 08:37:00 PDT 2025


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

>From a21d0a85de4ce030a13600a37ae4bce85cfcf984 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Wed, 20 Aug 2025 20:24:08 +0800
Subject: [PATCH 1/5] support dwo relative path in BOLT

---
 bolt/lib/Core/BinaryContext.cpp    | 5 ++++-
 bolt/lib/Rewrite/DWARFRewriter.cpp | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index dd0d041692484..e20c7fc113007 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -33,6 +33,7 @@
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Regex.h"
 #include <algorithm>
 #include <functional>
@@ -1636,7 +1637,9 @@ void BinaryContext::preprocessDWODebugInfo() {
       if (!opts::CompDirOverride.empty()) {
         sys::path::append(AbsolutePath, opts::CompDirOverride);
         sys::path::append(AbsolutePath, DWOName);
-      }
+      } else if (!sys::fs::exists(DwarfUnit->getCompilationDir()) &&
+                 sys::fs::exists(DWOName))
+        AbsolutePath = DWOName;
       DWARFUnit *DWOCU =
           DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
       if (!DWOCU->isDWOUnit()) {
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 0c1a1bac6c72e..80b24a5561e81 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -1851,6 +1851,8 @@ void DWARFRewriter::writeDWOFiles(
     CompDir = opts::DwarfOutputPath.c_str();
   else if (!opts::CompDirOverride.empty())
     CompDir = opts::CompDirOverride;
+  else if (!sys::fs::exists(CompDir))
+    CompDir = ".";
 
   SmallString<16> AbsolutePath;
   sys::path::append(AbsolutePath, CompDir);

>From 85193436e42783672e5bdb07f50b9580b68564d7 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Thu, 4 Sep 2025 22:50:48 +0800
Subject: [PATCH 2/5] add test

---
 bolt/lib/Core/BinaryContext.cpp    |  6 +++++-
 bolt/lib/Rewrite/DWARFRewriter.cpp |  4 ++--
 bolt/test/dwo-name-retrieving.c    | 16 ++++++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)
 create mode 100755 bolt/test/dwo-name-retrieving.c

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index e20c7fc113007..06dc1e9245064 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1638,8 +1638,12 @@ void BinaryContext::preprocessDWODebugInfo() {
         sys::path::append(AbsolutePath, opts::CompDirOverride);
         sys::path::append(AbsolutePath, DWOName);
       } else if (!sys::fs::exists(DwarfUnit->getCompilationDir()) &&
-                 sys::fs::exists(DWOName))
+                 sys::fs::exists(DWOName)) {
+        this->outs()
+            << "BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for "
+            << DWOName << ". DWO Name will be directly used for retrieving.\n";
         AbsolutePath = DWOName;
+      }
       DWARFUnit *DWOCU =
           DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
       if (!DWOCU->isDWOUnit()) {
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 80b24a5561e81..a4a614e9dcd8e 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -1851,8 +1851,8 @@ void DWARFRewriter::writeDWOFiles(
     CompDir = opts::DwarfOutputPath.c_str();
   else if (!opts::CompDirOverride.empty())
     CompDir = opts::CompDirOverride;
-  else if (!sys::fs::exists(CompDir))
-    CompDir = ".";
+  else if (!sys::fs::exists(CompDir) && sys::fs::exists(DWOName))
+    CompDir = "";
 
   SmallString<16> AbsolutePath;
   sys::path::append(AbsolutePath, CompDir);
diff --git a/bolt/test/dwo-name-retrieving.c b/bolt/test/dwo-name-retrieving.c
new file mode 100755
index 0000000000000..a3da78fc88ed2
--- /dev/null
+++ b/bolt/test/dwo-name-retrieving.c
@@ -0,0 +1,16 @@
+// This test checks retrieving dwo file diercetly with dwo name,
+// if the Debug Compilation Dir does not exist.
+
+int main() { return 0; }
+
+// RUN: rm -rf %t && mkdir -p %t && cd %t
+// RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %s -o main.exe
+// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections  2>&1 | FileCheck %s -check-prefix=NOT-EXIST
+
+// NOT-EXIST: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
+
+// RUN: rm -rf %t && mkdir -p %t && cd %t
+// RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %s -o %t/main.exe
+// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections  2>&1 | FileCheck %s -check-prefix=FOUND
+
+// FOUND-NOT: Debug Fission: DWO debug information for

>From 2d35fcb42f426983ab6eaf9aa8c67f47c9fb1e33 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Fri, 5 Sep 2025 16:55:28 +0800
Subject: [PATCH 3/5] clang format

---
 bolt/test/dwo-name-retrieving.c    | 17 ++++++++++-------
 bolt/test/dwo-name-retrieving.test | 17 +++++++++++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)
 mode change 100755 => 100644 bolt/test/dwo-name-retrieving.c
 create mode 100755 bolt/test/dwo-name-retrieving.test

diff --git a/bolt/test/dwo-name-retrieving.c b/bolt/test/dwo-name-retrieving.c
old mode 100755
new mode 100644
index a3da78fc88ed2..306f1261abd23
--- a/bolt/test/dwo-name-retrieving.c
+++ b/bolt/test/dwo-name-retrieving.c
@@ -4,13 +4,16 @@
 int main() { return 0; }
 
 // RUN: rm -rf %t && mkdir -p %t && cd %t
-// RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %s -o main.exe
-// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections  2>&1 | FileCheck %s -check-prefix=NOT-EXIST
+// RUN: %clang %cflags -g -gsplit-dwarf  \
+// RUN:   -fdebug-compilation-dir=/path/does/not/exist %s -o main.exe
+// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
+// RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
+// RUN: %clang %cflags -g -gsplit-dwarf  \
+// RUN:   -fdebug-compilation-dir=/path/does/not/exist %s -o %t/main.exe
+// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
+// RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
 
-// NOT-EXIST: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
+// DWO-NAME: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
 
-// RUN: rm -rf %t && mkdir -p %t && cd %t
-// RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %s -o %t/main.exe
-// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections  2>&1 | FileCheck %s -check-prefix=FOUND
+// DWO-NAMET-NOT: Debug Fission: DWO debug information for
 
-// FOUND-NOT: Debug Fission: DWO debug information for
diff --git a/bolt/test/dwo-name-retrieving.test b/bolt/test/dwo-name-retrieving.test
new file mode 100755
index 0000000000000..c227f4c119536
--- /dev/null
+++ b/bolt/test/dwo-name-retrieving.test
@@ -0,0 +1,17 @@
+## This test checks retrieving dwo file diercetly with dwo name,
+## if the Debug Compilation Dir does not exist.
+
+# RUN: rm -rf %t && mkdir -p %t && cd %t
+# RUN: %clang %cflags -g -gsplit-dwarf  \
+# RUN:   -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o main.exe
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
+# RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
+# RUN: %clang %cflags -g -gsplit-dwarf  \
+# RUN:   -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o %t/main.exe
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
+# RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
+
+# DWO-NAME: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
+# DWO-NAMET-NOT: Debug Fission: DWO debug information for/data00/home/huangjinjie/llvm-project/bolt/test/dwo-name-retrieving.test
+
+

>From 6324588c2b9db55cb22486e42d1030c159f6e081 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Fri, 5 Sep 2025 20:46:20 +0800
Subject: [PATCH 4/5] use make_absolute to prevent failures when DWOName is
 already an absolute path

---
 bolt/lib/Core/BinaryContext.cpp    | 18 ++++++++++--------
 bolt/lib/Rewrite/DWARFRewriter.cpp | 11 +++++------
 bolt/test/dwo-name-retrieving.c    | 19 -------------------
 bolt/test/dwo-name-retrieving.test | 16 ++++++----------
 4 files changed, 21 insertions(+), 43 deletions(-)
 delete mode 100644 bolt/test/dwo-name-retrieving.c

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 06dc1e9245064..dfb4e1812d674 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1633,17 +1633,18 @@ void BinaryContext::preprocessDWODebugInfo() {
           DwarfUnit->getUnitDIE().find(
               {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
           "");
-      SmallString<16> AbsolutePath;
+      SmallString<16> AbsolutePath(DWOName);
+      std::string DWOCompDir = DwarfUnit->getCompilationDir();
       if (!opts::CompDirOverride.empty()) {
-        sys::path::append(AbsolutePath, opts::CompDirOverride);
-        sys::path::append(AbsolutePath, DWOName);
-      } else if (!sys::fs::exists(DwarfUnit->getCompilationDir()) &&
-                 sys::fs::exists(DWOName)) {
+        DWOCompDir = opts::CompDirOverride;
+      } else if (!sys::fs::exists(DWOCompDir) && sys::fs::exists(DWOName)) {
+        DWOCompDir = ".";
         this->outs()
             << "BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for "
-            << DWOName << ". DWO Name will be directly used for retrieving.\n";
-        AbsolutePath = DWOName;
+            << DWOName << ". Relative path will be used for retrieving.\n";
       }
+      // Prevent failures when DWOName is already an absolute path.
+      sys::fs::make_absolute(DWOCompDir, AbsolutePath);
       DWARFUnit *DWOCU =
           DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
       if (!DWOCU->isDWOUnit()) {
@@ -1651,7 +1652,8 @@ void BinaryContext::preprocessDWODebugInfo() {
             << "BOLT-WARNING: Debug Fission: DWO debug information for "
             << DWOName
             << " was not retrieved and won't be updated. Please check "
-               "relative path.\n";
+               "relative path or use '--comp-dir-override' to specify the base "
+               "location.\n";
         continue;
       }
       DWOCUs[*DWOId] = DWOCU;
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index a4a614e9dcd8e..6752489ad562a 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -1846,17 +1846,16 @@ void DWARFRewriter::writeDWOFiles(
   }
 
   std::string CompDir = CU.getCompilationDir();
+  SmallString<16> AbsolutePath(DWOName);
 
   if (!opts::DwarfOutputPath.empty())
     CompDir = opts::DwarfOutputPath.c_str();
   else if (!opts::CompDirOverride.empty())
     CompDir = opts::CompDirOverride;
-  else if (!sys::fs::exists(CompDir) && sys::fs::exists(DWOName))
-    CompDir = "";
-
-  SmallString<16> AbsolutePath;
-  sys::path::append(AbsolutePath, CompDir);
-  sys::path::append(AbsolutePath, DWOName);
+  else if (!sys::fs::exists(CompDir))
+    CompDir = ".";
+  // Prevent failures when DWOName is already an absolute path.
+  sys::fs::make_absolute(CompDir, AbsolutePath);
 
   std::error_code EC;
   std::unique_ptr<ToolOutputFile> TempOut =
diff --git a/bolt/test/dwo-name-retrieving.c b/bolt/test/dwo-name-retrieving.c
deleted file mode 100644
index 306f1261abd23..0000000000000
--- a/bolt/test/dwo-name-retrieving.c
+++ /dev/null
@@ -1,19 +0,0 @@
-// This test checks retrieving dwo file diercetly with dwo name,
-// if the Debug Compilation Dir does not exist.
-
-int main() { return 0; }
-
-// RUN: rm -rf %t && mkdir -p %t && cd %t
-// RUN: %clang %cflags -g -gsplit-dwarf  \
-// RUN:   -fdebug-compilation-dir=/path/does/not/exist %s -o main.exe
-// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
-// RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
-// RUN: %clang %cflags -g -gsplit-dwarf  \
-// RUN:   -fdebug-compilation-dir=/path/does/not/exist %s -o %t/main.exe
-// RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
-// RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
-
-// DWO-NAME: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
-
-// DWO-NAMET-NOT: Debug Fission: DWO debug information for
-
diff --git a/bolt/test/dwo-name-retrieving.test b/bolt/test/dwo-name-retrieving.test
index c227f4c119536..38f547d2f5589 100755
--- a/bolt/test/dwo-name-retrieving.test
+++ b/bolt/test/dwo-name-retrieving.test
@@ -2,16 +2,12 @@
 ## if the Debug Compilation Dir does not exist.
 
 # RUN: rm -rf %t && mkdir -p %t && cd %t
-# RUN: %clang %cflags -g -gsplit-dwarf  \
-# RUN:   -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o main.exe
-# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
-# RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
-# RUN: %clang %cflags -g -gsplit-dwarf  \
-# RUN:   -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o %t/main.exe
-# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections \
-# RUN:   2>&1 | FileCheck %s -check-prefix=DWO-NAME
+# RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o main.exe
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections 2>&1 | FileCheck %s -check-prefix=DWO-NAME
+# RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o %t/main.exe
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections 2>&1 | FileCheck %s -check-prefix=DWO-NAME
 
 # DWO-NAME: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
-# DWO-NAMET-NOT: Debug Fission: DWO debug information for/data00/home/huangjinjie/llvm-project/bolt/test/dwo-name-retrieving.test
-
+# DWO-NAMET-NOT: Debug Fission: DWO debug information for
+# DWO-NAMET-NOT: Assertion `FD >= 0 && "File not yet open!"' failed.
 

>From b9bd7d2b7b59ae34beaf1716ff4212251a814236 Mon Sep 17 00:00:00 2001
From: huangjinjie <huangjinjie at bytedance.com>
Date: Sat, 6 Sep 2025 23:36:44 +0800
Subject: [PATCH 5/5] add test clarification

---
 bolt/lib/Core/BinaryContext.cpp    |  6 ++++--
 bolt/test/dwo-name-retrieving.test | 22 ++++++++++++++--------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index dfb4e1812d674..a0804f28e8fc6 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1640,8 +1640,10 @@ void BinaryContext::preprocessDWODebugInfo() {
       } else if (!sys::fs::exists(DWOCompDir) && sys::fs::exists(DWOName)) {
         DWOCompDir = ".";
         this->outs()
-            << "BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for "
-            << DWOName << ". Relative path will be used for retrieving.\n";
+            << "BOLT-WARNING: Debug Fission: Debug Compilation Directory of "
+            << DWOName
+            << " does not exist. Relative path will be used to process .dwo "
+               "files.\n";
       }
       // Prevent failures when DWOName is already an absolute path.
       sys::fs::make_absolute(DWOCompDir, AbsolutePath);
diff --git a/bolt/test/dwo-name-retrieving.test b/bolt/test/dwo-name-retrieving.test
index 38f547d2f5589..39193ccc6637a 100755
--- a/bolt/test/dwo-name-retrieving.test
+++ b/bolt/test/dwo-name-retrieving.test
@@ -1,13 +1,19 @@
-## This test checks retrieving dwo file diercetly with dwo name,
-## if the Debug Compilation Dir does not exist.
+## Test DWO retrieval via relative path with a missing CompDir.
+## Also, verify no crash for an absolute DWOName path.
 
+## The case where DWOName is a relative path, and debug compilation directory does not exist.
 # RUN: rm -rf %t && mkdir -p %t && cd %t
 # RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o main.exe
-# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections 2>&1 | FileCheck %s -check-prefix=DWO-NAME
-# RUN: %clang %cflags -g -gsplit-dwarf -fdebug-compilation-dir=/path/does/not/exist %p/Inputs/hello.c -o %t/main.exe
-# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections 2>&1 | FileCheck %s -check-prefix=DWO-NAME
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections 2>&1 | FileCheck %s -check-prefix=DWO-NAME-REL
 
-# DWO-NAME: BOLT-WARNING: Debug Fission: Debug Compilation Dir wrong for
-# DWO-NAMET-NOT: Debug Fission: DWO debug information for
-# DWO-NAMET-NOT: Assertion `FD >= 0 && "File not yet open!"' failed.
+# DWO-NAME-REL: BOLT-WARNING: Debug Fission: Debug Compilation Directory of main.exe-hello.dwo does not exist.
+# DWO-NAME-REL-NOT: Debug Fission: DWO debug information for
 
+## The case where DWOName is a absolute path, and a dwp file is provided.
+# RUN: %clang %cflags -g -gsplit-dwarf %p/Inputs/hello.c -o %t/main.exe
+# RUN: llvm-dwp -e %t/main.exe -o  %t/main.exe.dwp
+# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -update-debug-sections -dwp=%t/main.exe.dwp 2>&1 | FileCheck %s -check-prefix=DWO-NAME-ABS
+
+# DWO-NAME-ABS-NOT: BOLT-WARNING: Debug Fission: Debug Compilation Directory of {{.*}}/main.exe-hello.dwo does not exist.
+# DWO-NAME-ABS-NOT: Debug Fission: DWO debug information for
+# DWO-NAME-ABS-NOT: Assertion `FD >= 0 && "File not yet open!"' failed.



More information about the llvm-commits mailing list