[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
Thu Jan 25 13:39:29 PST 2024


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

>From e4bee722e5a79faeeedcb4edd5a4cd2e4a8c6b75 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             | 22 ++++++++++++++++-----
 bolt/lib/Rewrite/DWARFRewriter.cpp          | 18 ++++++++++++-----
 bolt/test/X86/dwarf4-override-comp-dir.test | 17 ++++++++++++++++
 bolt/test/X86/dwarf5-override-comp-dir.test | 17 ++++++++++++++++
 4 files changed, 64 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 3f96ea265e425f..118276a2bc2bdf 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -72,6 +72,12 @@ PrintMemData("print-mem-data",
   cl::ZeroOrMore,
   cl::cat(BoltCategory));
 
+cl::opt<std::string> CompDirOverride(
+    "comp-dir-override",
+    cl::desc("Overrides DW_AT_comp_dir, and provides an alterantive base "
+             "location, which is used with DW_AT_dwo_name to construct a path "
+             "to *.dwo files."),
+    cl::init(""), cl::cat(BoltCategory));
 } // namespace opts
 
 namespace llvm {
@@ -1574,12 +1580,18 @@ 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();
+      std::string DWOName = dwarf::toString(
+          DwarfUnit->getUnitDIE().find(
+              {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
+          "");
+      SmallString<16> AbsolutePath;
+      if (!opts::CompDirOverride.empty()) {
+        sys::path::append(AbsolutePath, opts::CompDirOverride);
+        sys::path::append(AbsolutePath, DWOName);
+      }
+      DWARFUnit *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 3e63e3318420ec..cefccbdfa2f012 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -366,6 +366,8 @@ 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));
+
+extern cl::opt<std::string> CompDirOverride;
 } // namespace opts
 
 static bool getLowAndHighPC(const DIE &Die, const DWARFUnit &DU,
@@ -1991,14 +1993,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 00000000000000..919ff24919e5db
--- /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 00000000000000..3910656c29ff28
--- /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