[binutils-gdb] gdb/solib-rocm: avoid expensive gdbarch_from_bfd call in rocm_solib_relocate_section_addresses

Simon Marchi simark@sourceware.org
Tue Oct 28 15:55:48 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=338796db769342bffe41e362cb62bf2155345b2f

commit 338796db769342bffe41e362cb62bf2155345b2f
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Mon Oct 27 15:41:51 2025 -0400

    gdb/solib-rocm: avoid expensive gdbarch_from_bfd call in rocm_solib_relocate_section_addresses
    
    Loading a library containing a lot (> 100k) sections proved very slow
    with whenever the support for ROCm was built into gdb.  The culprit is
    the gdbarch_from_bfd call in rocm_solib_relocate_section_addresses:
    
        if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd.get ())))
    
    This function gets called for every section, and gdbarch_from_bfd is
    somewhat slow.  It turns out that we can skip the gdbarch_from_bfd call,
    since all is_amdgpu_arch needs is the bfd_architecture value, which we
    can directly extract from the `bfd *`, without going through the
    gdbarch.
    
    Add an overload of is_amdgpu_arch that takes a `bfd *`, and use it in
    rocm_solib_relocate_section_addresses.
    
    Update a call site in rocm_solib_bfd_open to use the new overload as
    well.  That call site is not as much in a hot path, but there is no
    point in paying the extra cost of looking up the gdbarch there.  I
    removed the other assert that checked that gdbarch_from_bfd returned a
    non-nullptr value.  If that was the case, something would be very wrong
    with ROCgdb, and the problem would manifest very soon after anyway.
    
    Change-Id: I55e9e68af59903b1b9727ff57388f9469d0e0002
    Approved-by: Lancelot Six <lancelot.six@amd.com> (AMDGPU)

Diff:
---
 gdb/amdgpu-tdep.c | 19 +++++++++++++++++--
 gdb/amdgpu-tdep.h |  5 +++++
 gdb/solib-rocm.c  |  5 ++---
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/gdb/amdgpu-tdep.c b/gdb/amdgpu-tdep.c
index 07ad307afc1..a2cb7d8984a 100644
--- a/gdb/amdgpu-tdep.c
+++ b/gdb/amdgpu-tdep.c
@@ -31,13 +31,28 @@
 #include "producer.h"
 #include "reggroups.h"
 
+/* Return true if INFO is of an AMDGPU architecture.  */
+
+static bool
+is_amdgpu_arch (const bfd_arch_info *info)
+{
+  return info->arch == bfd_arch_amdgcn;
+}
+
 /* See amdgpu-tdep.h.  */
 
 bool
 is_amdgpu_arch (struct gdbarch *arch)
 {
-  gdb_assert (arch != nullptr);
-  return gdbarch_bfd_arch_info (arch)->arch == bfd_arch_amdgcn;
+  return is_amdgpu_arch (gdbarch_bfd_arch_info (arch));
+}
+
+/* See amdgpu-tdep.h.  */
+
+bool
+is_amdgpu_arch (bfd *abfd)
+{
+  return is_amdgpu_arch (abfd->arch_info);
 }
 
 /* See amdgpu-tdep.h.  */
diff --git a/gdb/amdgpu-tdep.h b/gdb/amdgpu-tdep.h
index b468c6c098c..67bcaaf243a 100644
--- a/gdb/amdgpu-tdep.h
+++ b/gdb/amdgpu-tdep.h
@@ -84,8 +84,13 @@ struct amdgpu_gdbarch_tdep : gdbarch_tdep_base
 };
 
 /* Return true if GDBARCH is of an AMDGPU architecture.  */
+
 bool is_amdgpu_arch (struct gdbarch *gdbarch);
 
+/* Return true if ABFD is of an AMDGPU architecture.  */
+
+bool is_amdgpu_arch (bfd *abfd);
+
 /* Return the amdgpu-specific data associated to ARCH.  */
 
 amdgpu_gdbarch_tdep *get_amdgpu_gdbarch_tdep (gdbarch *arch);
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index 3b121e67cc2..b48e4426fd4 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -245,7 +245,7 @@ void
 rocm_solib_ops::relocate_section_addresses (solib &so,
 					    struct target_section *sec) const
 {
-  if (!is_amdgpu_arch (gdbarch_from_bfd (so.abfd.get ())))
+  if (!is_amdgpu_arch (so.abfd.get ()))
     {
       m_host_ops->relocate_section_addresses (so, sec);
       return;
@@ -728,8 +728,7 @@ rocm_solib_ops::bfd_open (const char *pathname) const
 	}
     }
 
-  gdb_assert (gdbarch_from_bfd (abfd.get ()) != nullptr);
-  gdb_assert (is_amdgpu_arch (gdbarch_from_bfd (abfd.get ())));
+  gdb_assert (is_amdgpu_arch (abfd.get ()));
 
   return abfd;
 }


More information about the Gdb-cvs mailing list