Pārlūkot izejas kodu

HIP: Add CMAKE_HIP_PLATFORM variable to specify GPU platform

For now, require the value to be `amd`, since that is the only
platform we currently support.
Brad King 2 gadi atpakaļ
vecāks
revīzija
127b6fa06b

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -590,6 +590,7 @@ Variables for Languages
    /variable/CMAKE_Fortran_MODOUT_FLAG
    /variable/CMAKE_HIP_ARCHITECTURES
    /variable/CMAKE_HIP_EXTENSIONS
+   /variable/CMAKE_HIP_PLATFORM
    /variable/CMAKE_HIP_STANDARD
    /variable/CMAKE_HIP_STANDARD_REQUIRED
    /variable/CMAKE_ISPC_HEADER_DIRECTORY

+ 2 - 1
Help/prop_tgt/HIP_ARCHITECTURES.rst

@@ -3,7 +3,8 @@ HIP_ARCHITECTURES
 
 .. versionadded:: 3.21
 
-List of AMD GPU architectures to generate device code for.
+List of GPU architectures to for which to generate device code.
+Architecture names are interpreted based on :variable:`CMAKE_HIP_PLATFORM`.
 
 A non-empty false value (e.g. ``OFF``) disables adding architectures.
 This is intended to support packagers and rare cases where full control

+ 6 - 0
Help/release/dev/hip-nvidia.rst

@@ -0,0 +1,6 @@
+hip-nvidia
+----------
+
+* The :variable:`CMAKE_HIP_PLATFORM` variable was added to specify
+  the GPU platform for which HIP language sources are to be compiled
+  (``amd``).

+ 7 - 3
Help/variable/CMAKE_HIP_ARCHITECTURES.rst

@@ -3,10 +3,14 @@ CMAKE_HIP_ARCHITECTURES
 
 .. versionadded:: 3.21
 
-Default value for :prop_tgt:`HIP_ARCHITECTURES` property of targets.
+List of GPU architectures to for which to generate device code.
+Architecture names are interpreted based on :variable:`CMAKE_HIP_PLATFORM`.
 
-This is initialized to the architectures reported by ``rocm_agent_enumerator``,
-if available, and otherwise to the default chosen by the compiler.
+This is initialized based on the value of :variable:`CMAKE_HIP_PLATFORM`:
+
+``amd``
+  Uses architectures reported by ``rocm_agent_enumerator``, if available,
+  and otherwise to a default chosen by the compiler.
 
 This variable is used to initialize the :prop_tgt:`HIP_ARCHITECTURES` property
 on all targets. See the target property for additional information.

+ 19 - 0
Help/variable/CMAKE_HIP_PLATFORM.rst

@@ -0,0 +1,19 @@
+CMAKE_HIP_PLATFORM
+------------------
+
+.. versionadded:: 3.28
+
+GPU platform for which HIP language sources are to be compiled.
+
+The value must be one of:
+
+``amd``
+  AMD GPUs
+
+If not specified, the default is ``amd``.
+
+:variable:`CMAKE_HIP_ARCHITECTURES` entries are interpreted with
+as architectures of the GPU platform.
+
+:variable:`CMAKE_HIP_COMPILER <CMAKE_<LANG>_COMPILER>` must target
+the same GPU platform.

+ 20 - 8
Modules/CMakeDetermineHIPCompiler.cmake

@@ -10,6 +10,16 @@ if( NOT ( ("${CMAKE_GENERATOR}" MATCHES "Make") OR
   message(FATAL_ERROR "HIP language not currently supported by \"${CMAKE_GENERATOR}\" generator")
 endif()
 
+if(NOT CMAKE_HIP_PLATFORM)
+  set(CMAKE_HIP_PLATFORM "amd" CACHE STRING "HIP platform" FORCE)
+endif()
+if(NOT CMAKE_HIP_PLATFORM MATCHES "^(amd)$")
+  message(FATAL_ERROR
+    "The CMAKE_HIP_PLATFORM has unsupported value:\n"
+    " '${CMAKE_HIP_PLATFORM}'\n"
+    "It must be 'amd'."
+    )
+endif()
 
 if(NOT CMAKE_HIP_COMPILER)
   set(CMAKE_HIP_COMPILER_INIT NOTFOUND)
@@ -34,15 +44,17 @@ if(NOT CMAKE_HIP_COMPILER)
 
   # finally list compilers to try
   if(NOT CMAKE_HIP_COMPILER_INIT)
-    set(CMAKE_HIP_COMPILER_LIST clang++)
+    if(CMAKE_HIP_PLATFORM STREQUAL "amd")
+      set(CMAKE_HIP_COMPILER_LIST clang++)
 
-    # Look for the Clang coming with ROCm to support HIP.
-    execute_process(COMMAND hipconfig --hipclangpath
-      OUTPUT_VARIABLE _CMAKE_HIPCONFIG_CLANGPATH
-      RESULT_VARIABLE _CMAKE_HIPCONFIG_RESULT
-    )
-    if(_CMAKE_HIPCONFIG_RESULT EQUAL 0 AND EXISTS "${_CMAKE_HIPCONFIG_CLANGPATH}")
-      set(CMAKE_HIP_COMPILER_HINTS "${_CMAKE_HIPCONFIG_CLANGPATH}")
+      # Look for the Clang coming with ROCm to support HIP.
+      execute_process(COMMAND hipconfig --hipclangpath
+        OUTPUT_VARIABLE _CMAKE_HIPCONFIG_CLANGPATH
+        RESULT_VARIABLE _CMAKE_HIPCONFIG_RESULT
+      )
+      if(_CMAKE_HIPCONFIG_RESULT EQUAL 0 AND EXISTS "${_CMAKE_HIPCONFIG_CLANGPATH}")
+        set(CMAKE_HIP_COMPILER_HINTS "${_CMAKE_HIPCONFIG_CLANGPATH}")
+      endif()
     endif()
   endif()
 

+ 2 - 0
Source/cmCoreTryCompile.cxx

@@ -78,6 +78,7 @@ std::string const kCMAKE_EXECUTABLE_ENABLE_EXPORTS =
 std::string const kCMAKE_SHARED_LIBRARY_ENABLE_EXPORTS =
   "CMAKE_SHARED_LIBRARY_ENABLE_EXPORTS";
 std::string const kCMAKE_HIP_ARCHITECTURES = "CMAKE_HIP_ARCHITECTURES";
+std::string const kCMAKE_HIP_PLATFORM = "CMAKE_HIP_PLATFORM";
 std::string const kCMAKE_HIP_RUNTIME_LIBRARY = "CMAKE_HIP_RUNTIME_LIBRARY";
 std::string const kCMAKE_ISPC_INSTRUCTION_SETS = "CMAKE_ISPC_INSTRUCTION_SETS";
 std::string const kCMAKE_ISPC_HEADER_SUFFIX = "CMAKE_ISPC_HEADER_SUFFIX";
@@ -1081,6 +1082,7 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
     vars.insert(kCMAKE_EXECUTABLE_ENABLE_EXPORTS);
     vars.insert(kCMAKE_SHARED_LIBRARY_ENABLE_EXPORTS);
     vars.insert(kCMAKE_HIP_ARCHITECTURES);
+    vars.insert(kCMAKE_HIP_PLATFORM);
     vars.insert(kCMAKE_HIP_RUNTIME_LIBRARY);
     vars.insert(kCMAKE_ISPC_INSTRUCTION_SETS);
     vars.insert(kCMAKE_ISPC_HEADER_SUFFIX);

+ 5 - 1
Tests/HIP/ArchitectureOff/CMakeLists.txt

@@ -3,7 +3,11 @@ project(HIPArchitecture HIP)
 
 # Make sure CMake doesn't pass architectures if HIP_ARCHITECTURES is OFF.
 set(CMAKE_HIP_ARCHITECTURES OFF)
-string(APPEND CMAKE_HIP_FLAGS " --offload-arch=gfx908")
+
+# Pass our own architecture flags instead.
+if(CMAKE_HIP_PLATFORM STREQUAL "amd")
+  string(APPEND CMAKE_HIP_FLAGS " --offload-arch=gfx908")
+endif()
 
 add_executable(HIPOnlyArchitectureOff main.hip)
 get_property(hip_archs TARGET HIPOnlyArchitectureOff PROPERTY HIP_ARCHITECTURES)

+ 4 - 1
Tests/HIP/CompileFlags/CMakeLists.txt

@@ -3,6 +3,9 @@ project(CompileFlags HIP)
 
 add_executable(HIPOnlyCompileFlags main.hip)
 
-set_property(TARGET HIPOnlyCompileFlags PROPERTY HIP_ARCHITECTURES gfx803)
+if(CMAKE_HIP_PLATFORM STREQUAL "amd")
+  set(hip_archs gfx803)
+endif()
+set_property(TARGET HIPOnlyCompileFlags PROPERTY HIP_ARCHITECTURES ${hip_archs})
 
 target_compile_options(HIPOnlyCompileFlags PRIVATE -DALWAYS_DEFINE)

+ 4 - 1
Tests/HIP/TryCompile/CMakeLists.txt

@@ -4,7 +4,10 @@ project (TryCompile HIP)
 #Goal for this example:
 # Verify try_compile with HIP language works
 set(CMAKE_HIP_STANDARD 14)
-set(CMAKE_HIP_ARCHITECTURES gfx803 gfx900)
+
+if(CMAKE_HIP_PLATFORM STREQUAL "amd")
+  set(CMAKE_HIP_ARCHITECTURES gfx803 gfx900)
+endif()
 
 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
 try_compile(result "${CMAKE_CURRENT_BINARY_DIR}"