[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:32:33 PST 2024


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

>From b78afe535f0a5d744680f4ab464765faa1346234 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 1/2] [BOLT][DWARF][NFC] Add option to specify DW_AT_comp_dir.

Summary:
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.

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:


Differential Revision: https://phabricator.intern.facebook.com/D53098074
---
 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 3f96ea265e425f..723938d28a8809 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 3e63e3318420ec..1870788378b91a 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,
@@ -1991,14 +1997,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..6033550ab8072b
--- /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..9cfb28eea638b2
--- /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

>From 344ce6be374dd79ddd52874084753f857ba03bde Mon Sep 17 00:00:00 2001
From: Alexander Yermolovich <ayermolo at meta.com>
Date: Thu, 25 Jan 2024 11:59:22 -0800
Subject: [PATCH 2/2] [BOLT][NFC] fixed shared library build, addressed
 comments

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Differential Revision: https://phabricator.intern.facebook.com/D53098071
---
 bolt/lib/Core/BinaryContext.cpp             | 18 ++++++++++--------
 bolt/lib/Rewrite/DWARFRewriter.cpp          |  6 +-----
 bolt/test/X86/dwarf4-override-comp-dir.test |  2 +-
 bolt/test/X86/dwarf5-override-comp-dir.test |  2 +-
 4 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index 723938d28a8809..118276a2bc2bdf 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -72,7 +72,12 @@ PrintMemData("print-mem-data",
   cl::ZeroOrMore,
   cl::cat(BoltCategory));
 
-extern cl::opt<std::string> CompDirOverride;
+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 {
@@ -1575,20 +1580,17 @@ 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 = 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;
+      SmallString<16> AbsolutePath;
+      if (!opts::CompDirOverride.empty()) {
         sys::path::append(AbsolutePath, opts::CompDirOverride);
         sys::path::append(AbsolutePath, DWOName);
-        DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath)
-                    .getDwarfUnit();
       }
+      DWARFUnit *DWOCU =
+          DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
       if (!DWOCU->isDWOUnit()) {
         outs() << "BOLT-WARNING: Debug Fission: DWO debug information for "
                << DWOName
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 1870788378b91a..cefccbdfa2f012 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -367,11 +367,7 @@ static cl::opt<bool> AlwaysConvertToRanges(
              "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));
+extern cl::opt<std::string> CompDirOverride;
 } // namespace opts
 
 static bool getLowAndHighPC(const DIE &Die, const DWARFUnit &DU,
diff --git a/bolt/test/X86/dwarf4-override-comp-dir.test b/bolt/test/X86/dwarf4-override-comp-dir.test
index 6033550ab8072b..919ff24919e5db 100644
--- a/bolt/test/X86/dwarf4-override-comp-dir.test
+++ b/bolt/test/X86/dwarf4-override-comp-dir.test
@@ -12,6 +12,6 @@
 ; RUN: cd ..
 ; RUN: ls -lat | FileCheck %s -check-prefix=BOLT-CHECK
 
-;; Tests that bolt processes .dwo files with --comp-dir-override.
+;; 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
index 9cfb28eea638b2..3910656c29ff28 100644
--- a/bolt/test/X86/dwarf5-override-comp-dir.test
+++ b/bolt/test/X86/dwarf5-override-comp-dir.test
@@ -12,6 +12,6 @@
 ; RUN: cd ..
 ; RUN: ls -lat | FileCheck %s -check-prefix=BOLT-CHECK
 
-;; Tests that bolt processes .dwo files with --comp-dir-override.
+;; Tests that BOLT processes .dwo files with --comp-dir-override.
 
 ; BOLT-CHECK: main.dwo.dwo



More information about the llvm-commits mailing list