HIP: Fix search for hip-lang CMake package on multiarch distros

We need `CMAKE_LIBRARY_ARCHITECTURE` to find the package before HIP
compiler ABI detection.  However, if HIP is the first enabled language,
the value is not known until `CMAKE_HIP_LIBRARY_ARCHITECTURE` is
determined by the ABI detection step.  Resolve this by detecting
`CMAKE_HIP_LIBRARY_ARCHITECTURE` from the compiler id output.

Fixes: #24562
diff --git a/Modules/CMakeDetermineHIPCompiler.cmake b/Modules/CMakeDetermineHIPCompiler.cmake
index 6294d04..5e54502 100644
--- a/Modules/CMakeDetermineHIPCompiler.cmake
+++ b/Modules/CMakeDetermineHIPCompiler.cmake
@@ -3,6 +3,7 @@
 
 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
 include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
+include(${CMAKE_ROOT}/Modules/CMakeParseLibraryArchitecture.cmake)
 
 if( NOT ( ("${CMAKE_GENERATOR}" MATCHES "Make") OR
           ("${CMAKE_GENERATOR}" MATCHES "Ninja") ) )
@@ -102,13 +103,48 @@
 if(NOT CMAKE_HIP_COMPILER_ROCM_ROOT)
   message(FATAL_ERROR "Failed to find ROCm root directory.")
 endif()
-if(NOT EXISTS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang/hip-lang-config.cmake")
-  message(FATAL_ERROR
-    "The ROCm root directory:\n"
-    " ${CMAKE_HIP_COMPILER_ROCM_ROOT}\n"
-    "does not contain the HIP runtime CMake package, expected at:\n"
-    " ${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang/hip-lang-config.cmake\n"
-    )
+
+# Normally implicit link information is not detected until
+cmake_parse_implicit_link_info("${CMAKE_HIP_COMPILER_PRODUCED_OUTPUT}"
+  _CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS
+  _CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS
+  _CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS
+  _CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG
+  "" LANGUAGE HIP)
+message(CONFIGURE_LOG
+  "Parsed HIP implicit link information from compiler id output:\n${_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG}\n\n")
+cmake_parse_library_architecture(HIP "${_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS}" "" CMAKE_HIP_LIBRARY_ARCHITECTURE)
+if(CMAKE_HIP_LIBRARY_ARCHITECTURE)
+  message(CONFIGURE_LOG
+    "Parsed HIP library architecture from compiler id output: ${CMAKE_HIP_LIBRARY_ARCHITECTURE}\n")
+endif()
+unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LIBS)
+unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_DIRS)
+unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_FWKS)
+unset(_CMAKE_HIP_COMPILER_ID_IMPLICIT_LOG)
+
+if(NOT CMAKE_HIP_COMPILER_ROCM_LIB)
+  set(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib")
+  if(CMAKE_HIP_LIBRARY_ARCHITECTURE)
+    list(APPEND _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/${CMAKE_HIP_LIBRARY_ARCHITECTURE}")
+  endif()
+  foreach(dir IN LISTS _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS)
+    if(EXISTS "${dir}/cmake/hip-lang/hip-lang-config.cmake")
+      set(CMAKE_HIP_COMPILER_ROCM_LIB "${dir}")
+      break()
+    endif()
+  endforeach()
+  if(NOT CMAKE_HIP_COMPILER_ROCM_LIB)
+    list(TRANSFORM _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS APPEND "/cmake/hip-lang/hip-lang-config.cmake")
+    string(REPLACE ";" "\n " _CMAKE_HIP_COMPILER_ROCM_LIB_DIRS "${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}")
+    message(FATAL_ERROR
+      "The ROCm root directory:\n"
+      " ${CMAKE_HIP_COMPILER_ROCM_ROOT}\n"
+      "does not contain the HIP runtime CMake package, expected at one of:\n"
+      " ${_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS}\n"
+      )
+  endif()
+  unset(_CMAKE_HIP_COMPILER_ROCM_LIB_DIRS)
 endif()
 
 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
diff --git a/Modules/CMakeHIPCompiler.cmake.in b/Modules/CMakeHIPCompiler.cmake.in
index c94153b..0fa5bf0 100644
--- a/Modules/CMakeHIPCompiler.cmake.in
+++ b/Modules/CMakeHIPCompiler.cmake.in
@@ -18,6 +18,7 @@
 @SET_MSVC_HIP_ARCHITECTURE_ID@
 @_SET_CMAKE_HIP_COMPILER_SYSROOT@
 set(CMAKE_HIP_COMPILER_ROCM_ROOT "@CMAKE_HIP_COMPILER_ROCM_ROOT@")
+set(CMAKE_HIP_COMPILER_ROCM_LIB "@CMAKE_HIP_COMPILER_ROCM_LIB@")
 
 set(CMAKE_HIP_COMPILER_ENV_VAR "HIPCXX")
 
diff --git a/Modules/CMakeHIPInformation.cmake b/Modules/CMakeHIPInformation.cmake
index 33f8697..41a98db 100644
--- a/Modules/CMakeHIPInformation.cmake
+++ b/Modules/CMakeHIPInformation.cmake
@@ -142,7 +142,7 @@
 
 # Load the file and find the relevant HIP runtime.
 if(NOT DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)
-  set(hip-lang_DIR "${CMAKE_HIP_COMPILER_ROCM_ROOT}/lib/cmake/hip-lang")
+  set(hip-lang_DIR "${CMAKE_HIP_COMPILER_ROCM_LIB}/cmake/hip-lang")
   find_package(hip-lang CONFIG QUIET NO_DEFAULT_PATH REQUIRED)
 endif()
 if(DEFINED _CMAKE_HIP_DEVICE_RUNTIME_TARGET)