[binutils-gdb] gdb/testsuite: replace hipcc with amdclang++ as the HIP compiler

Sarang Patrange via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=382e5c4d32f6d4d38f0e48916a852dd57d100743

commit 382e5c4d32f6d4d38f0e48916a852dd57d100743
Author: Sarang Patrange <[email protected]>
Date:   Tue Jun 30 09:53:23 2026 -0500

    gdb/testsuite: replace hipcc with amdclang++ as the HIP compiler
    
    The GDB testsuite discovers and invokes hipcc to compile the HIP
    testcases.  hipcc is deprecated and slated for removal, so switch the
    testsuite over to amdclang++.  Unlike the hipcc wrapper, amdclang++ is
    the plain clang driver, so a few HIP options that hipcc handled
    implicitly now have to be passed explicitly.
    
    Compiler discovery (lib/future.exp):
    - Look for amdclang++ under $ROCM_PATH/lib/llvm/bin instead of hipcc
      under $ROCM_PATH/bin.
    - Rename the HIPCC_FOR_TARGET environment override to
      HIP_COMPILER_FOR_TARGET, and gdb_find_hipcc / find_hipcc to
      gdb_find_hip_compiler / find_hip_compiler.  The "hipcompiler"
      board-info key is left unchanged for backwards compatibility.
    
    Compile and link flags (lib/gdb.exp):
    - amdclang++ infers the input language from the file extension and
      compiles .cpp as C++, not HIP.  The HIP testcases are kept named
      .cpp (rather than renamed to .hip), so tag each input explicitly:
      "-x hip" before C/C++/HIP source files and "-x none" before object
      files, archives and other inputs.  This lets a single invocation
      handle a source list that mixes sources with prebuilt objects.
    - Pass --hip-link when producing an executable, to bring in the HIP
      runtime.
    - Pass --rocm-path only when ROCM_PATH is set in the environment;
      otherwise defer to amdclang++'s own HIP discovery.
    - Pass one --offload-arch per available device, unless the testcase
      already selects an architecture.
    - Drop the explicit -O0: it was only needed because the hipcc wrapper
      defaulted to -O2, whereas amdclang++ already defaults to -O0.  An
      explicit "optimize" option still overrides it.
    
    Testcases (lib/rocm.exp, gdb.rocm/*.exp):
    - Rename allow_hipcc_tests to allow_hip_tests.
    - Replace hipcc's --genco with amdclang++'s --cuda-device-only in
      gdb.rocm/code-object-load-while-breakpoint-hit.exp, which builds a
      standalone device code object.
    
    Signed-off-by: Sarang Patrange <[email protected]>
    Approved-by: Lancelot Six <[email protected]> (amdgpu)

Diff:
---
 gdb/testsuite/gdb.rocm/addr-bp-gpu-no-deb-info.exp |  2 +-
 .../gdb.rocm/break-kernel-no-debug-info.exp        |  2 +-
 .../code-object-load-while-breakpoint-hit.exp      |  4 +-
 gdb/testsuite/gdb.rocm/displaced-stepping.exp      |  2 +-
 .../gdb.rocm/fork-exec-gpu-to-non-gpu.exp          |  2 +-
 .../gdb.rocm/fork-exec-non-gpu-to-gpu.exp          |  2 +-
 gdb/testsuite/gdb.rocm/interrupt-twice.exp         |  2 +-
 gdb/testsuite/gdb.rocm/mi-attach.exp               |  2 +-
 gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp      |  2 +-
 gdb/testsuite/gdb.rocm/precise-memory-exec.exp     |  2 +-
 gdb/testsuite/gdb.rocm/precise-memory-fork.exp     |  2 +-
 .../gdb.rocm/precise-memory-multi-inferiors.exp    |  2 +-
 .../gdb.rocm/precise-memory-warning-sigsegv.exp    |  2 +-
 .../gdb.rocm/precise-memory-warning-watchpoint.exp |  2 +-
 gdb/testsuite/gdb.rocm/precise-memory.exp          |  2 +-
 gdb/testsuite/gdb.rocm/simple.exp                  |  2 +-
 gdb/testsuite/gdb.rocm/step-abort.exp              |  2 +-
 .../gdb.rocm/watchpoint-at-end-of-shader.exp       |  2 +-
 gdb/testsuite/gdb.rocm/watchpoint-basic.exp        |  2 +-
 gdb/testsuite/lib/future.exp                       | 30 ++++++------
 gdb/testsuite/lib/gdb.exp                          | 55 +++++++++++++++++++---
 gdb/testsuite/lib/rocm.exp                         |  2 +-
 22 files changed, 85 insertions(+), 42 deletions(-)

diff --git a/gdb/testsuite/gdb.rocm/addr-bp-gpu-no-deb-info.exp b/gdb/testsuite/gdb.rocm/addr-bp-gpu-no-deb-info.exp
index 039a208a5fa..a77c628f81d 100644
--- a/gdb/testsuite/gdb.rocm/addr-bp-gpu-no-deb-info.exp
+++ b/gdb/testsuite/gdb.rocm/addr-bp-gpu-no-deb-info.exp
@@ -20,7 +20,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 if {[build_executable "failed to prepare" $testfile $srcfile {hip}]} {
     return
diff --git a/gdb/testsuite/gdb.rocm/break-kernel-no-debug-info.exp b/gdb/testsuite/gdb.rocm/break-kernel-no-debug-info.exp
index d9e35d36d3d..7fc608c2913 100644
--- a/gdb/testsuite/gdb.rocm/break-kernel-no-debug-info.exp
+++ b/gdb/testsuite/gdb.rocm/break-kernel-no-debug-info.exp
@@ -25,7 +25,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 # Build for hip, explicitly without debug infos
 if {[build_executable "failed to prepare" $testfile $srcfile {hip nodebug}]} {
diff --git a/gdb/testsuite/gdb.rocm/code-object-load-while-breakpoint-hit.exp b/gdb/testsuite/gdb.rocm/code-object-load-while-breakpoint-hit.exp
index 842a3211090..9575c923605 100644
--- a/gdb/testsuite/gdb.rocm/code-object-load-while-breakpoint-hit.exp
+++ b/gdb/testsuite/gdb.rocm/code-object-load-while-breakpoint-hit.exp
@@ -34,7 +34,7 @@
 
 load_lib rocm.exp
 standard_testfile .cpp
-require allow_hipcc_tests
+require allow_hip_tests
 
 # Build the host executable.
 if { [build_executable "failed to prepare" \
@@ -47,7 +47,7 @@ set hipmodule_path [standard_output_file ${testfile}.co]
 # Build the kernel object file.
 if { [gdb_compile $srcdir/$subdir/$srcfile \
 	$hipmodule_path object \
-	{ debug hip additional_flags=--genco additional_flags=-DDEVICE } ] != "" } {
+	{ debug hip additional_flags=--cuda-device-only additional_flags=-DDEVICE } ] != "" } {
     return
 }
 
diff --git a/gdb/testsuite/gdb.rocm/displaced-stepping.exp b/gdb/testsuite/gdb.rocm/displaced-stepping.exp
index 383972bbb33..2466a678f19 100644
--- a/gdb/testsuite/gdb.rocm/displaced-stepping.exp
+++ b/gdb/testsuite/gdb.rocm/displaced-stepping.exp
@@ -19,7 +19,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 # Since GDB doesn't yet understand DWARF expressions generated by the HIP
 # compiler, purposefully generate the binary without debug info.
diff --git a/gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu.exp b/gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu.exp
index aa85face813..efc27572a5d 100644
--- a/gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu.exp
+++ b/gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu.exp
@@ -20,7 +20,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 require allow_fork_tests
 
 standard_testfile -execer.cpp -execee.cpp
diff --git a/gdb/testsuite/gdb.rocm/fork-exec-non-gpu-to-gpu.exp b/gdb/testsuite/gdb.rocm/fork-exec-non-gpu-to-gpu.exp
index ca104e1cb07..3e1d43bbb7c 100644
--- a/gdb/testsuite/gdb.rocm/fork-exec-non-gpu-to-gpu.exp
+++ b/gdb/testsuite/gdb.rocm/fork-exec-non-gpu-to-gpu.exp
@@ -19,7 +19,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 require allow_fork_tests
 
 standard_testfile -execer.cpp -execee.cpp
diff --git a/gdb/testsuite/gdb.rocm/interrupt-twice.exp b/gdb/testsuite/gdb.rocm/interrupt-twice.exp
index 0e87b5c66e8..cfefdff9ff0 100644
--- a/gdb/testsuite/gdb.rocm/interrupt-twice.exp
+++ b/gdb/testsuite/gdb.rocm/interrupt-twice.exp
@@ -19,7 +19,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/gdb.rocm/mi-attach.exp b/gdb/testsuite/gdb.rocm/mi-attach.exp
index c62a3a1cd97..a9b3a971099 100644
--- a/gdb/testsuite/gdb.rocm/mi-attach.exp
+++ b/gdb/testsuite/gdb.rocm/mi-attach.exp
@@ -17,7 +17,7 @@ load_lib rocm.exp
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-require can_spawn_for_attach allow_hipcc_tests
+require can_spawn_for_attach allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp
index 5e07e4d982c..4a06b642cb0 100644
--- a/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp
+++ b/gdb/testsuite/gdb.rocm/multi-inferior-gpu.exp
@@ -20,7 +20,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 require hip_devices_support_debug_multi_process
 require allow_fork_tests
 
diff --git a/gdb/testsuite/gdb.rocm/precise-memory-exec.exp b/gdb/testsuite/gdb.rocm/precise-memory-exec.exp
index 0f9f618d686..bdd423cd65d 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory-exec.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory-exec.exp
@@ -20,7 +20,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .c
 
diff --git a/gdb/testsuite/gdb.rocm/precise-memory-fork.exp b/gdb/testsuite/gdb.rocm/precise-memory-fork.exp
index bcf94c36eec..06ae518d5a3 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory-fork.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory-fork.exp
@@ -20,7 +20,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 require allow_fork_tests
 
 standard_testfile .c
diff --git a/gdb/testsuite/gdb.rocm/precise-memory-multi-inferiors.exp b/gdb/testsuite/gdb.rocm/precise-memory-multi-inferiors.exp
index d0b50a69e91..e8a7b5802ca 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory-multi-inferiors.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory-multi-inferiors.exp
@@ -20,7 +20,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 clean_restart
 
diff --git a/gdb/testsuite/gdb.rocm/precise-memory-warning-sigsegv.exp b/gdb/testsuite/gdb.rocm/precise-memory-warning-sigsegv.exp
index 4bf5bd71e15..460d14a2b30 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory-warning-sigsegv.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory-warning-sigsegv.exp
@@ -20,7 +20,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/gdb.rocm/precise-memory-warning-watchpoint.exp b/gdb/testsuite/gdb.rocm/precise-memory-warning-watchpoint.exp
index 15ab9a91e4c..373b95fecde 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory-warning-watchpoint.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory-warning-watchpoint.exp
@@ -18,7 +18,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 if { ![istarget "*-linux*"] } then {
     continue
diff --git a/gdb/testsuite/gdb.rocm/precise-memory.exp b/gdb/testsuite/gdb.rocm/precise-memory.exp
index fc6993e7d7b..8b92af19d29 100644
--- a/gdb/testsuite/gdb.rocm/precise-memory.exp
+++ b/gdb/testsuite/gdb.rocm/precise-memory.exp
@@ -19,7 +19,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/gdb.rocm/simple.exp b/gdb/testsuite/gdb.rocm/simple.exp
index dba81c2c1e6..e928aa55bee 100644
--- a/gdb/testsuite/gdb.rocm/simple.exp
+++ b/gdb/testsuite/gdb.rocm/simple.exp
@@ -20,7 +20,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 if {[build_executable "failed to prepare" $testfile $srcfile {debug hip}]} {
     return
diff --git a/gdb/testsuite/gdb.rocm/step-abort.exp b/gdb/testsuite/gdb.rocm/step-abort.exp
index 56bcdfc8534..510161020e9 100644
--- a/gdb/testsuite/gdb.rocm/step-abort.exp
+++ b/gdb/testsuite/gdb.rocm/step-abort.exp
@@ -20,7 +20,7 @@ load_lib rocm.exp
 
 standard_testfile .cpp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 # We want to have a small kernel as we are going to single step all the way
 # to our abort instruction (s_trap 2).  Using -O1 allows the compiler to inline
diff --git a/gdb/testsuite/gdb.rocm/watchpoint-at-end-of-shader.exp b/gdb/testsuite/gdb.rocm/watchpoint-at-end-of-shader.exp
index 298b5be9461..6e31e5c3752 100644
--- a/gdb/testsuite/gdb.rocm/watchpoint-at-end-of-shader.exp
+++ b/gdb/testsuite/gdb.rocm/watchpoint-at-end-of-shader.exp
@@ -18,7 +18,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/gdb.rocm/watchpoint-basic.exp b/gdb/testsuite/gdb.rocm/watchpoint-basic.exp
index 558a1f8472c..cd29d856d56 100644
--- a/gdb/testsuite/gdb.rocm/watchpoint-basic.exp
+++ b/gdb/testsuite/gdb.rocm/watchpoint-basic.exp
@@ -17,7 +17,7 @@
 
 load_lib rocm.exp
 
-require allow_hipcc_tests
+require allow_hip_tests
 
 standard_testfile .cpp
 
diff --git a/gdb/testsuite/lib/future.exp b/gdb/testsuite/lib/future.exp
index 3ab160a05fc..fbe517f116b 100644
--- a/gdb/testsuite/lib/future.exp
+++ b/gdb/testsuite/lib/future.exp
@@ -120,20 +120,20 @@ proc gdb_find_rustc {} {
     return $rustc
 }
 
-proc gdb_find_hipcc {} {
+proc gdb_find_hip_compiler {} {
     global tool_root_dir
     if {![is_remote host]} {
-	set hipcc [lookfor_file $tool_root_dir hipcc]
-	if {$hipcc eq "" && [info exists ::env(ROCM_PATH)]} {
-	    set hipcc [lookfor_file $::env(ROCM_PATH)/bin hipcc]
+	set compiler [lookfor_file $tool_root_dir amdclang++]
+	if {$compiler eq "" && [info exists ::env(ROCM_PATH)]} {
+	    set compiler [lookfor_file $::env(ROCM_PATH)/lib/llvm/bin amdclang++]
 	}
-	if {$hipcc eq ""} {
-	    set hipcc hipcc
+	if {$compiler eq ""} {
+	    set compiler amdclang++
 	}
     } else {
-	set hipcc ""
+	set compiler ""
     }
-    return $hipcc
+    return $compiler
 }
 
 proc gdb_find_ldd {} {
@@ -323,7 +323,7 @@ proc gdb_default_target_compile_1 {source destfile type options} {
 	    if {[board_info $dest exists hipcompiler]} {
 		set compiler [target_info hipcompiler]
 	    } else {
-		set compiler [find_hipcc]
+		set compiler [find_hip_compiler]
 	    }
 	}
 
@@ -389,7 +389,7 @@ proc gdb_default_target_compile_1 {source destfile type options} {
     global GO_FOR_TARGET
     global GO_LD_FOR_TARGET
     global RUSTC_FOR_TARGET
-    global HIPCC_FOR_TARGET
+    global HIP_COMPILER_FOR_TARGET
 
     if {[info exists GNATMAKE_FOR_TARGET]} {
 	if { $compiler_type == "ada" } {
@@ -436,9 +436,9 @@ proc gdb_default_target_compile_1 {source destfile type options} {
 	}
     }
 
-    if {[info exists HIPCC_FOR_TARGET]} {
+    if {[info exists HIP_COMPILER_FOR_TARGET]} {
 	if {$compiler_type == "hip"} {
-	    set compiler $HIPCC_FOR_TARGET
+	    set compiler $HIP_COMPILER_FOR_TARGET
 	}
     }
 
@@ -742,12 +742,12 @@ if {[info procs find_rustc] == ""} {
     rename gdb_find_rustc ""
 }
 
-if {[info procs find_hipcc] == ""} {
-    rename gdb_find_hipcc find_hipcc
+if {[info procs find_hip_compiler] == ""} {
+    rename gdb_find_hip_compiler find_hip_compiler
     set use_gdb_compile(hip) 1
     gdb_note [join [list $note_prefix "HIP" $note_suffix] ""]
 } else {
-    rename gdb_find_hipcc ""
+    rename gdb_find_hip_compiler ""
 }
 
 # If dejagnu's default_target_compile is missing support for any language,
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 6fb04869605..1a6438f36a5 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6882,14 +6882,35 @@ proc gdb_compile {source dest type options} {
 	#    '-mllvm=-amdgpu-spill-cfi-saved-regs' [-Wunused-command-line-argument]
 	#
 	# These happen when we're compiling an executable _and_ all
-	# the input files are .o files, like: hipcc foo.o -o foo ...
+	# the input files are .o files, like: amdclang++ foo.o -o foo ...
 	#
-	# HIPCC defaults to -O2, so add -O0 to early flags for the HIP
-	# language.  If "optimize" is also requested, another -O flag
-	# (e.g. -O2) will be added to the flags, overriding this -O0.
-	lappend new_options "early_flags=-O0\
-			     -mllvm=-amdgpu-spill-cfi-saved-regs\
+	# --hip-link is required when linking to bring in the HIP
+	# runtime.
+	set hip_early_flags "-mllvm=-amdgpu-spill-cfi-saved-regs\
 			     -Wno-unused-command-line-argument"
+	if {$type eq "executable"} {
+	    set hip_early_flags "--hip-link $hip_early_flags"
+	}
+	# When ROCM_PATH is set in the environment, pass --rocm-path
+	# so amdclang++ uses that specific ROCm install for HIP
+	# headers and device libraries.  When ROCM_PATH is not set,
+	# defer to amdclang++'s own HIP discovery, which looks under
+	# the parent of its own LLVM directory and falls back on
+	# /opt/rocm; overriding that here would silently mask the
+	# user's actual compiler-side install.
+	if {[info exists ::env(ROCM_PATH)] && $::env(ROCM_PATH) ne ""} {
+	    set hip_early_flags "--rocm-path=$::env(ROCM_PATH) $hip_early_flags"
+	}
+	lappend new_options "early_flags=$hip_early_flags"
+
+	# amdclang++ requires explicit --offload-arch.  Explicitly
+	# pass one --offload-arch for each available device.  But
+	# don't do it if the testcase explicitly used --offload-arch.
+	if {[lsearch -regexp $options "--offload-arch="] == -1} {
+	    foreach gpu_target [hcc_amdgpu_targets] {
+		lappend new_options "early_flags=--offload-arch=$gpu_target"
+	    }
+	}
     }
 
     # Because we link with libraries using their basename, we may need
@@ -7095,6 +7116,28 @@ proc gdb_compile {source dest type options} {
     # Automatically handle includes in testsuite/lib/.
     auto_lappend_include_files options $source
 
+    # amdclang++ infers the input language from the file extension and
+    # compiles .cpp as C++, not HIP.  Since the HIP testcases are named
+    # .cpp, explicitly tag each input: "-x hip" before C/C++/HIP source
+    # files, and "-x none" before everything else (object files,
+    # archives, shared libraries), so the compiler does not try to
+    # parse those as HIP source.  This lets a single amdclang++
+    # invocation correctly handle a mix of sources and non-source
+    # inputs (e.g. when set_unbuffered_mode.o is appended via ldflags
+    # later in the command line).
+    if {[lsearch -exact $options hip] != -1 && !$getting_compiler_info} {
+	set new_source {}
+	foreach src $source {
+	    set ext [string tolower [file extension $src]]
+	    if {$ext in {".c" ".cc" ".cp" ".cxx" ".cpp" ".c++" ".hip"}} {
+		lappend new_source "-x" "hip" $src
+	    } else {
+		lappend new_source "-x" "none" $src
+	    }
+	}
+	set source $new_source
+    }
+
     cond_wrap [expr {$pie != -1 || $nopie != -1}] \
 	with_PIE_multilib_flags_filtered {
 	set result [target_compile $source $dest $type $options]
diff --git a/gdb/testsuite/lib/rocm.exp b/gdb/testsuite/lib/rocm.exp
index ca1ae3f1a8b..f8a3b233fae 100644
--- a/gdb/testsuite/lib/rocm.exp
+++ b/gdb/testsuite/lib/rocm.exp
@@ -110,7 +110,7 @@ proc hcc_amdgpu_targets {} {
     return $unique
 }
 
-gdb_caching_proc allow_hipcc_tests {} {
+gdb_caching_proc allow_hip_tests {} {
     # Only the native target supports ROCm debugging.  E.g., when
     # testing against GDBserver, there's no point in running the ROCm
     # tests.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.