]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/testsuite/rocm.exp: Use system GPU(s) to detect features
authorShahab Vahedi <shahab.vahedi@amd.com>
Wed, 12 Feb 2025 14:58:15 +0000 (15:58 +0100)
committerShahab Vahedi <shahab.vahedi@amd.com>
Fri, 21 Feb 2025 17:53:17 +0000 (18:53 +0100)
gdb/testsuite/rocm.exp: Use system GPU(s) to detect features

Background
----------
This patch revisits the purpose of hcc_amdgpu_targets{} in
order to address the separation of concerns between:

- GPU targets passed to the compiler.  This kind of target
  is passed as an argument to flags like "--offload-arch=...",
  "--targets=...", etc.

- GPU targets as in available GPU devices on the system.  This
  is crucial for finding which capabilities are available,
  and therefore which tests should be executed or skipped.

Code change
-----------
- A new "find_amdgpu_devices{}" procedure is added.  It is
  responsible for listing the GPU devices that are available
  on the system.

- "hcc_amdgpu_targets{}" is rewritten to use the newly added
  "find_amdgpu_devices{}" when there's no environment variable
  (HCC_AMDGPU_TARGET) set.

- The output of "hcc_amdgpu_targets{}" is now only used in
  places that set the target for the building toolchains.

- The output of "find_amdgpu_devices{}" is used anywhere that
  needs to evaluate the GPU features.

Approved-By: Lancelot Six <lancelot.six@amd.com> (amdgpu)
Change-Id: Ib11021dbe674aa40192737ede78284a1bc531513

gdb/testsuite/lib/rocm.exp

index b2db0d5c783f35c0a350a25e6d3d3feef83eb1f9..f358758f283108cbdb4376629a9fb0af20c8f009 100644 (file)
 #
 # Support library for testing ROCm (AMD GPU) GDB features.
 
-# Get the list of gpu targets to compile for.
-#
-# If HCC_AMDGPU_TARGET is set in the environment, use it.  Otherwise,
-# try reading it from the system using the rocm_agent_enumerator
-# utility.
+# ROCM_PATH is used by hipcc as well.
+if {[info exists ::env(ROCM_PATH)]} {
+    set rocm_path $::env(ROCM_PATH)
+} else {
+    set rocm_path "/opt/rocm"
+}
 
-proc hcc_amdgpu_targets {} {
-    # Look for HCC_AMDGPU_TARGET (same env var hipcc uses).  If
-    # that fails, try using rocm_agent_enumerator (again, same as
-    # hipcc does).
-    if {[info exists ::env(HCC_AMDGPU_TARGET)]} {
-       return [split $::env(HCC_AMDGPU_TARGET) ","]
+# Act as a drop-in replacement for "remote_exec host"
+# that logs the failures.
+
+proc log_host_exec { cmd } {
+    set result [remote_exec host "$cmd"]
+    set exit_status [lindex $result 0]
+    if {$exit_status != 0} {
+       # -1 indicates that $cmd could not be executed at all.
+       if {$exit_status == -1} {
+           verbose -log "Cannot execute $cmd."
+       } else {
+           verbose -log "$cmd returned an error."
+       }
     }
 
-    set rocm_agent_enumerator "rocm_agent_enumerator"
+    return $result
+}
 
-    # If available, use ROCM_PATH to locate rocm_agent_enumerator.
-    if { [info exists ::env(ROCM_PATH)] } {
-       set rocm_agent_enumerator \
-           "$::env(ROCM_PATH)/bin/rocm_agent_enumerator"
+# Detect available AMDGPU devices.
+#
+# Return a list of GPU devices that do exist on the system.
+# The list will be empty when there's no GPU or the execution
+# of rocm_agent_enumerator does not succeed.  It is up to the
+# caller of this procedure that what should happen when an empty
+# list is returned.
+
+gdb_caching_proc find_amdgpu_devices {} {
+    global rocm_path
+    set hip_gpu_devices [list]
+    set enumerator "rocm_agent_enumerator"
+    set targets ""
+
+    # Try the PATH first
+    set result [log_host_exec "$enumerator"]
+    if {[lindex $result 0] == 0} {
+       set targets [lindex $result 1]
+    } else {
+       # Now try the ROCM_PATH
+       set result [log_host_exec "$rocm_path/bin/$enumerator"]
+       if {[lindex $result 0] == 0} {
+           set targets [lindex $result 1]
+       }
     }
 
-    # If we fail to locate the rocm_agent_enumerator, just return an empty
-    # list of targets and let the caller decide if this should be an error.
-    if { [which $rocm_agent_enumerator] == 0 } {
-       return [list]
+    if {$targets != ""} {
+       foreach dev $targets {
+           # Ignore the 'gfx000' device which identifies the host.
+           if {$dev != "gfx000"} {
+               lappend hip_gpu_devices $dev
+           }
+       }
     }
 
-    set result [remote_exec host $rocm_agent_enumerator]
-    if { [lindex $result 0] != 0 } {
-       error "rocm_agent_enumerator failed"
-    }
+    return $hip_gpu_devices
+}
 
-    set targets [list]
-    foreach target [lindex $result 1] {
-       # Ignore gfx000 which is the host CPU.
-       if { $target ne "gfx000" } {
-           lappend targets $target
-       }
+# Get the list of GPU targets to compile for.
+#
+# If HCC_AMDGPU_TARGET is set in the environment, use it.
+# Otherwise, consider the devices available on the system.
+
+proc hcc_amdgpu_targets {} {
+    # First, look for HCC_AMDGPU_TARGET (same env var hipcc uses).
+    if {[info exists ::env(HCC_AMDGPU_TARGET)]} {
+       # We don't verify the contents of HCC_AMDGPU_TARGET.
+       # That's the toolchain's job.
+       return [split $::env(HCC_AMDGPU_TARGET) ","]
     }
 
-    return $targets
+    return [find_amdgpu_devices]
 }
 
 gdb_caching_proc allow_hipcc_tests {} {
@@ -77,12 +112,15 @@ gdb_caching_proc allow_hipcc_tests {} {
        return {0 "amd-dbgapi not supported"}
     }
 
-    # Check we have a working hipcc compiler available.
-    set targets [hcc_amdgpu_targets]
-    if { [llength $targets] == 0} {
+    # Check if there's any GPU device to run the tests on.
+    set devices [find_amdgpu_devices]
+    if {[llength $devices] == 0} {
        return {0 "no suitable amdgpu targets found"}
     }
 
+    # Check we have a working hipcc compiler available.
+    # TARGETS won't be empty, because there's at least one GPU device.
+    set targets [hcc_amdgpu_targets]
     set flags [list hip additional_flags=--offload-arch=[join $targets ","]]
     if {![gdb_simple_compile hipprobe {
            #include <hip/hip_runtime.h>
@@ -126,7 +164,7 @@ proc hip_devices_support_debug_multi_process {} {
     set unsupported_targets \
        {gfx900 gfx906 gfx908 gfx1010 gfx1011 gfx1012 gfx1030 gfx1031 gfx1032}
 
-    set targets [hcc_amdgpu_targets]
+    set targets [find_amdgpu_devices]
     if { [llength $targets] == 0 } {
        return 0
     }
@@ -145,7 +183,7 @@ proc hip_devices_support_precise_memory {} {
     set unsupported_targets \
        {gfx900 gfx906 gfx908 gfx1010 gfx1011 gfx1012 gfx1030 gfx1031 gfx1032}
 
-    set targets [hcc_amdgpu_targets]
+    set targets [find_amdgpu_devices]
     if { [llength $targets] == 0 } {
        return 0
     }