[llvm] [LLVM] Add `LLVM_<proj>_RUNTIME_TARGETS` to set targets per-project (PR #81557)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 05:40:29 PST 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/81557

>From 48e738f8c8679e4db93ae8896c7665734e790eee Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 12 Feb 2024 18:08:39 -0600
Subject: [PATCH] [LLVM] Add `LLVM_<proj>_RUNTIME_TARGETS` to set targets
 per-project

Summary:
Currently, the runtimes build allow users to optionally build for
multiple target architectures via the `LLVM_RUNTIME_TARGETS` option.
Once problem is that this applies to every single runtime the user has
enabled, when it's possible that we may wish to enable a different set
for just one target.

The motivating example here is for handling GPU targets as
cross-compiling. We want to be able to perform something like
`LLVM_RUNTIME_TARGETS=amdgcn-amd-amdhsa;nvptx64-nvidia-cuda` to build
runtimes targeting the GPU. However, the current support would force the
other runtimes to be built for the GPU, when this definitely will not
work.

This patch attempts to work around this in a generic way by letting
individual targets overload the set of enabled runtimes triples. The
expected use would be to enable something like the following for
targeting the GPU `libc` and in the future the GPU `libcxx`.

```
-DLLVM_ENABLE_PROJECTS='openmp;libcxx;libcxx-abi;libc'
-DLLVM_LIBC_RUNTIME_TARGETS='nvptx64-nvidia-cuda;amdgcn-amd-amdhsa'
```
---
 llvm/runtimes/CMakeLists.txt | 102 ++++++++++++++++++++++-------------
 1 file changed, 64 insertions(+), 38 deletions(-)

diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index 8c48d85a4346f4..d1e6fcb7bc4f0c 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -209,12 +209,13 @@ foreach(entry ${runtimes})
 endforeach()
 
 function(runtime_default_target)
-  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS;PREFIXES" ${ARGN})
+  cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS;PREFIXES;RUNTIMES" ${ARGN})
 
   include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
   set(SUB_CHECK_TARGETS ${SUB_CHECK_TARGETS} PARENT_SCOPE)
   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
 
+  set(LLVM_ENABLE_RUNTIMES ${ARG_RUNTIMES})
   foreach(runtime_name ${RUNTIME_NAMES})
     list(APPEND extra_targets
       ${runtime_name}
@@ -269,11 +270,12 @@ endfunction()
 # runtime_register_target(name)
 #   Utility function to register external runtime target.
 function(runtime_register_target name)
-  cmake_parse_arguments(ARG "" "BASE_NAME" "DEPENDS;CMAKE_ARGS;EXTRA_ARGS" ${ARGN})
+  cmake_parse_arguments(ARG "" "BASE_NAME" "DEPENDS;CMAKE_ARGS;EXTRA_ARGS;RUNTIMES" ${ARGN})
   include(${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake OPTIONAL)
   set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake)
 
   set(runtime_names ${RUNTIME_NAMES})
+  set(LLVM_ENABLE_RUNTIMES ${ARG_RUNTIMES})
   foreach(_name IN ITEMS ${ARG_BASE_NAME} ${name})
     if(RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES)
       set(runtime_names)
@@ -449,39 +451,62 @@ if(runtimes)
       endforeach()
     endif()
   endif()
-  if(NOT LLVM_RUNTIME_TARGETS)
-    runtime_default_target(
-      DEPENDS ${builtins_dep} ${extra_deps}
-      CMAKE_ARGS ${libc_cmake_args}
-      PREFIXES ${prefixes})
-    set(test_targets check-runtimes)
-  else()
-    if("default" IN_LIST LLVM_RUNTIME_TARGETS)
+
+  # Get the list of all target triples requested for this build.
+  set(runtime_targets "default")
+  if (LLVM_RUNTIME_TARGETS)
+    set(runtime_targets ${LLVM_RUNTIME_TARGETS})
+  endif()
+  foreach(proj ${LLVM_ENABLE_RUNTIMES})
+    string(TOUPPER "${proj}" canon_name)
+    string(REGEX REPLACE "-" "_" canon_name ${canon_name})
+    if(LLVM_${canon_name}_RUNTIME_TARGETS)
+      list(APPEND extra_targets ${LLVM_${canon_name}_RUNTIME_TARGETS})
+    endif()
+  endforeach()
+  list(REMOVE_DUPLICATES extra_targets)
+
+  # If we have any non-default triples we need to create custom targets.
+  if(NOT "default" IN_LIST runtime_targets AND NOT "default" IN_LIST extra_targets)
+    add_custom_target(runtimes)
+    add_custom_target(runtimes-configure)
+    add_custom_target(install-runtimes)
+    add_custom_target(install-runtimes-stripped)
+    if(LLVM_INCLUDE_TESTS)
+      add_custom_target(check-runtimes)
+      add_custom_target(runtimes-test-depends)
+      set(test_targets "")
+    endif()
+    if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
+      foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
+        add_custom_target(${component})
+        add_custom_target(install-${component})
+        add_custom_target(install-${component}-stripped)
+      endforeach()
+    endif()
+  endif()
+
+  # Register each requested target triple using the projects that requested that
+  # target.
+  foreach(name ${runtime_targets} ${extra_targets})
+    set(enabled_runtimes "")
+    foreach(proj ${LLVM_ENABLE_RUNTIMES})
+      string(TOUPPER "${proj}" canon_name)
+      string(REGEX REPLACE "-" "_" canon_name ${canon_name})
+      if((NOT LLVM_${canon_name}_RUNTIME_TARGETS AND ${name} IN_LIST runtime_targets) 
+          OR ${name} IN_LIST LLVM_${canon_name}_RUNTIME_TARGETS)
+        list(APPEND enabled_runtimes ${proj})
+      endif()
+    endforeach()
+
+    if("${name}" STREQUAL "default")
       runtime_default_target(
         DEPENDS ${builtins_dep} ${extra_deps}
         CMAKE_ARGS ${libc_cmake_args}
-        PREFIXES ${prefixes})
-      list(REMOVE_ITEM LLVM_RUNTIME_TARGETS "default")
+        PREFIXES ${prefixes}
+        RUNTIMES ${enabled_runtimes})
+      set(test_targets check-runtimes)
     else()
-      add_custom_target(runtimes)
-      add_custom_target(runtimes-configure)
-      add_custom_target(install-runtimes)
-      add_custom_target(install-runtimes-stripped)
-      if(LLVM_INCLUDE_TESTS)
-        add_custom_target(check-runtimes)
-        add_custom_target(runtimes-test-depends)
-        set(test_targets "")
-      endif()
-      if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
-        foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
-          add_custom_target(${component})
-          add_custom_target(install-${component})
-          add_custom_target(install-${component}-stripped)
-        endforeach()
-      endif()
-    endif()
-
-    foreach(name ${LLVM_RUNTIME_TARGETS})
       if(builtins_dep)
         if (LLVM_BUILTIN_TARGETS)
           set(builtins_dep_name "${builtins_dep}-${name}")
@@ -495,21 +520,22 @@ if(runtimes)
       runtime_register_target(${name}
         DEPENDS ${builtins_dep_name} ${hdrgen_deps}
         CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${name} ${libc_cmake_args}
+        RUNTIMES ${enabled_runtimes}
         EXTRA_ARGS TARGET_TRIPLE ${name})
-    endforeach()
 
-    foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
-      foreach(name ${LLVM_RUNTIME_MULTILIB_${multilib}_TARGETS})
+      foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
         runtime_register_target(${name}+${multilib}
           DEPENDS runtimes-${name}
           CMAKE_ARGS -DLLVM_DEFAULT_TARGET_TRIPLE=${name}
-                     -DLLVM_RUNTIMES_PREFIX=${name}/
-                     -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib}
+          -DLLVM_ENABLE_RUNTIMES="${enabled_runtimes}"
+          -DLLVM_RUNTIMES_PREFIX=${name}/
+          -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib}
           BASE_NAME ${name}
+          RUNTIMES ${enabled_runtimes}
           EXTRA_ARGS TARGET_TRIPLE ${name})
       endforeach()
-    endforeach()
-  endif()
+    endif()
+  endforeach()
 
   if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
     # TODO: This is a hack needed because the libcxx headers are copied into the



More information about the llvm-commits mailing list