[llvm] [BOLT][DWARF][NFC] Add option to specify DW_AT_comp_dir (PR #79395)

Alexander Yermolovich via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 17:20:21 PST 2024


https://github.com/ayermolo updated https://github.com/llvm/llvm-project/pull/79395

>From eb780ac6a19e7aa4cf106f01180a157c45205480 Mon Sep 17 00:00:00 2001
From: Alexander Yermolovich <ayermolo at meta.com>
Date: Wed, 24 Jan 2024 16:44:19 -0800
Subject: [PATCH] [BOLT][DWARF][NFC] Add option to specify DW_AT_comp_dir.

Added an --comp-dir-override option that overrides DW_AT_comp_dir in the unit
die. This allows for llvm-bolt to be invoked from any category and still find
.dwo files.
---
 bolt/lib/Core/BinaryContext.cpp             | 20 ++++++++++++++-----
 bolt/lib/Rewrite/DWARFRewriter.cpp          | 22 ++++++++++++++++-----
 bolt/test/X86/dwarf4-override-comp-dir.test | 17 ++++++++++++++++
 bolt/test/X86/dwarf5-override-comp-dir.test | 17 ++++++++++++++++
 4 files changed, 66 insertions(+), 10 deletions(-)
 create mode 100644 bolt/test/X86/dwarf4-override-comp-dir.test
 create mode 100644 bolt/test/X86/dwarf5-override-comp-dir.test

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 3f96ea265e425f7..723938d28a8809a 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -72,6 +72,7 @@ PrintMemData("print-mem-data",
   cl::ZeroOrMore,
   cl::cat(BoltCategory));
 
+extern cl::opt<std::string> CompDirOverride;
 } // namespace opts
 
 namespace llvm {
@@ -1574,12 +1575,21 @@ void BinaryContext::preprocessDWODebugInfo() {
   for (const std::unique_ptr<DWARFUnit> &CU : DwCtx->compile_units()) {
     DWARFUnit *const DwarfUnit = CU.get();
     if (std::optional<uint64_t> DWOId = DwarfUnit->getDWOId()) {
-      DWARFUnit *DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit();
+      DWARFUnit *DWOCU = nullptr;
+      std::string DWOName = dwarf::toString(
+          DwarfUnit->getUnitDIE().find(
+              {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
+          "");
+      if (opts::CompDirOverride.empty()) {
+        DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit();
+      } else {
+        SmallString<16> AbsolutePath;
+        sys::path::append(AbsolutePath, opts::CompDirOverride);
+        sys::path::append(AbsolutePath, DWOName);
+        DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath)
+                    .getDwarfUnit();
+      }
       if (!DWOCU->isDWOUnit()) {
-        std::string DWOName = dwarf::toString(
-            DwarfUnit->getUnitDIE().find(
-                {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
-            "");
         outs() << "BOLT-WARNING: Debug Fission: DWO debug information for "
                << DWOName
                << " was not retrieved and won't be updated. Please check "
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 8e20306925feac3..3b1d40478e002bd 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -366,6 +366,12 @@ static cl::opt<bool> AlwaysConvertToRanges(
     cl::desc("This option is for testing purposes only. It forces BOLT to "
              "convert low_pc/high_pc to ranges always."),
     cl::ReallyHidden, cl::init(false), cl::cat(BoltCategory));
+
+cl::opt<std::string> CompDirOverride(
+    "comp-dir-override",
+    cl::desc("Overrides DW_AT_comp_dir which is used with DW_AT_dwo_name to "
+             "construct a path to *.dwo files."),
+    cl::init(""), cl::cat(BoltCategory));
 } // namespace opts
 
 static bool getLowAndHighPC(const DIE &Die, const DWARFUnit &DU,
@@ -1969,14 +1975,20 @@ void DWARFRewriter::writeDWOFiles(
     return;
   }
 
-  std::string CompDir = opts::DwarfOutputPath.empty()
-                            ? CU.getCompilationDir()
-                            : opts::DwarfOutputPath.c_str();
-  auto FullPath = CompDir.append("/").append(DWOName);
+  std::string CompDir = CU.getCompilationDir();
+
+  if (!opts::DwarfOutputPath.empty())
+    CompDir = opts::DwarfOutputPath.c_str();
+  else if (!opts::CompDirOverride.empty())
+    CompDir = opts::CompDirOverride;
+
+  SmallString<16> AbsolutePath;
+  sys::path::append(AbsolutePath, CompDir);
+  sys::path::append(AbsolutePath, DWOName);
 
   std::error_code EC;
   std::unique_ptr<ToolOutputFile> TempOut =
-      std::make_unique<ToolOutputFile>(FullPath, EC, sys::fs::OF_None);
+      std::make_unique<ToolOutputFile>(AbsolutePath, EC, sys::fs::OF_None);
 
   const DWARFUnitIndex::Entry *CUDWOEntry = nullptr;
   if (IsDWP)
diff --git a/bolt/test/X86/dwarf4-override-comp-dir.test b/bolt/test/X86/dwarf4-override-comp-dir.test
new file mode 100644
index 000000000000000..6033550ab8072bf
--- /dev/null
+++ b/bolt/test/X86/dwarf4-override-comp-dir.test
@@ -0,0 +1,17 @@
+; REQUIRES: system-linux
+
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; RUN: cd %t
+; RUN: mkdir temp
+; RUN: llvm-mc -dwarf-version=4 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf4-df-basic.s \
+; RUN: -split-dwarf-file=main.dwo -o main.o
+; RUN: %clang %cflags -gdwarf-4 -gsplit-dwarf=split main.o -o main.exe
+; RUN: cd temp
+; RUN: llvm-bolt ../main.exe -o ../main.exe.bolt --update-debug-sections -v 1 --comp-dir-override=%t
+; RUN: cd ..
+; RUN: ls -lat | FileCheck %s -check-prefix=BOLT-CHECK
+
+;; Tests that bolt processes .dwo files with --comp-dir-override.
+
+; BOLT-CHECK: main.dwo.dwo
diff --git a/bolt/test/X86/dwarf5-override-comp-dir.test b/bolt/test/X86/dwarf5-override-comp-dir.test
new file mode 100644
index 000000000000000..9cfb28eea638b22
--- /dev/null
+++ b/bolt/test/X86/dwarf5-override-comp-dir.test
@@ -0,0 +1,17 @@
+; REQUIRES: system-linux
+
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; RUN: cd %t
+; RUN: mkdir temp
+; RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %p/Inputs/dwarf5-df-input-lowpc-ranges-main.s \
+; RUN: -split-dwarf-file=main.dwo -o main.o
+; RUN: %clang %cflags -gdwarf-5 -gsplit-dwarf=split main.o -o main.exe
+; RUN: cd temp
+; RUN: llvm-bolt ../main.exe -o ../main.exe.bolt --update-debug-sections -v 1 --comp-dir-override=%t
+; RUN: cd ..
+; RUN: ls -lat | FileCheck %s -check-prefix=BOLT-CHECK
+
+;; Tests that bolt processes .dwo files with --comp-dir-override.
+
+; BOLT-CHECK: main.dwo.dwo



More information about the llvm-commits mailing list