Skip to content

Conversation

Jinjie-Huang
Copy link
Contributor

@Jinjie-Huang Jinjie-Huang commented Aug 20, 2025

In distributed builds, the DWARF CompilationDir is often invalid, causing BOLT to fail when locating DWO files. If the default path does not exist, it seems better to consider the DWOName as a relative path in this case?
And I think this approach should be more intuitive and aligns with DWARF-related tools like llvm-dwp(see: https://reviews.llvm.org/D133480#).

The implementation of this patch will try to search for the DWO file in the following order:

  1. CompDirOverride + DWOName (if CompDirOverride specified)
  2. CompilationDir + DWOName (if CompilationDir exists)
  3. Current directory + DWOName (relative path as a fallback)

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-bolt

Author: Jinjie Huang (Jinjie-Huang)

Changes

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

2 Files Affected:

  • (modified) bolt/lib/Core/BinaryContext.cpp (+6)
  • (modified) bolt/lib/Rewrite/DWARFRewriter.cpp (+2)
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 84f1853469709..6a56c8aa7cfa5 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>
@@ -1640,6 +1641,11 @@ void BinaryContext::preprocessDWODebugInfo() {
       if (!opts::CompDirOverride.empty()) {
         sys::path::append(AbsolutePath, opts::CompDirOverride);
         sys::path::append(AbsolutePath, DWOName);
+      } else {
+        std::string CompDir = DwarfUnit->getCompilationDir();
+        sys::path::append(AbsolutePath, CompDir);
+        if (!sys::fs::exists(AbsolutePath) && sys::fs::exists(DWOName))
+          AbsolutePath = DWOName;
       }
       DWARFUnit *DWOCU =
           DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
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);

@Jinjie-Huang Jinjie-Huang requested review from ayermolo and maksfb and removed request for aaupov, paschalis-mpeis, maksfb, rafaelauler, yota9, ayermolo and yozhu August 20, 2025 11:20
@Jinjie-Huang Jinjie-Huang changed the title [BOLT] Get DWO file via a relative path if the CompilationDir does not exist [BOLT][DWARF] Get DWO file via a relative path if the CompilationDir does not exist Aug 20, 2025
@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from 5d845ac to d4a2319 Compare August 20, 2025 12:06
@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from d4a2319 to e08166c Compare August 20, 2025 12:24
@Jinjie-Huang Jinjie-Huang marked this pull request as draft August 21, 2025 12:43
@Jinjie-Huang Jinjie-Huang marked this pull request as ready for review August 21, 2025 13:41
@Sockke Sockke self-requested a review August 26, 2025 08:23
@ayermolo ayermolo self-assigned this Aug 27, 2025
@ayermolo
Copy link
Contributor

  1. This sounds like a problem with the build system. We also use distributed builds. Our solution is to set comp_dir to ".", and then dwo_name is relative to whatever directory bolt is ran in.
    2). We also provide opts::CompDirOverride. Why is that not sufficient?

@Jinjie-Huang
Copy link
Contributor Author

Jinjie-Huang commented Aug 28, 2025

Thank you for your reply.

Yes, "-fdebug-compilation-dir=." for compiler or "CompDirOverride=." for llvm-bolt can perfectly resolve the issue here. But it seems that having the ability to get dwo from a relative path is also not bad? This consideration is that some other users might encounter this problem when launching distributed builds on local paths, and they may be faced with different build systems, or not familiar enough with BOLT-opts. And addressing this issue might also require some understanding of DWARF? which could require a bit of learning curve.

BTW, the behaviors of GDB and llvm-dwp also involve searching using both absolute and relative paths—probably based on similar considerations?

}
} else if (!sys::fs::exists(DwarfUnit->getCompilationDir()) &&
sys::fs::exists(DWOName))
AbsolutePath = DWOName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be appended to "."?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, DWOName might be an absolute path... But in this case, using CompilationDir + DWOName would also be incorrect...

@ayermolo
Copy link
Contributor

I guess if there is precedence...
Please add a test, and a warning that relative path will be used.

@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from e08166c to 8519343 Compare September 4, 2025 14:51
Copy link

github-actions bot commented Sep 4, 2025

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

@Jinjie-Huang Jinjie-Huang changed the title [BOLT][DWARF] Get DWO file via a relative path if the CompilationDir does not exist [BOLT][DWARF] Get DWO file via DWOName if the CompilationDir does not exist Sep 4, 2025
@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch 2 times, most recently from ba3785e to 26c696e Compare September 4, 2025 17:18
@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from 26c696e to 2d35fcb Compare September 5, 2025 08:56
@Jinjie-Huang Jinjie-Huang changed the title [BOLT][DWARF] Get DWO file via DWOName if the CompilationDir does not exist [BOLT][DWARF] Get DWO file via relative path if the CompilationDir does not exist Sep 5, 2025
@Jinjie-Huang
Copy link
Contributor Author

Jinjie-Huang commented Sep 5, 2025

@ayermolo Currently it's changed to use make_absolute() joining DWOCompDir and DWOName. This is helpful when DWOName is already an absolute path (it will prevent the concatenation). Otherwise, if a .dwp file is specified, directly concatenating DWOCompDir and DWOName could trigger an asserting crash when emitting dwos.

PTAL, thanks.

@@ -0,0 +1,13 @@
## This test checks retrieving dwo file diercetly with dwo name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify on why we are building main.exe twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from 6cc4f88 to 144b6fb Compare September 6, 2025 14:36
@Jinjie-Huang Jinjie-Huang force-pushed the users/huangjinjie/bolt_support_relative_dwo_path branch from 144b6fb to b9bd7d2 Compare September 6, 2025 15:36
@Jinjie-Huang
Copy link
Contributor Author

@ayermolo Please have a look at this too. Thanks.

Copy link
Contributor

@ayermolo ayermolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Jinjie-Huang Jinjie-Huang merged commit e2455bf into llvm:main Sep 15, 2025
9 checks passed
itzexpoexpo pushed a commit to itzexpoexpo/llvm-project that referenced this pull request Sep 21, 2025
…es not exist (llvm#154515)

In distributed builds, the DWARF CompilationDir is often invalid,
causing BOLT to fail when locating DWO files. If the default path does
not exist, it seems better to consider the DWOName as a relative path in
this case.

The implementation of this patch will try to search for the DWO file in
the following order:
1. CompDirOverride + DWOName (if CompDirOverride specified)
2. CompilationDir + DWOName (if CompilationDir exists)
3. **Current directory + DWOName (relative path as a fallback)**

This patch also fixes a crash that occurs when DWOName is an absolute path and a DWP file is provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants