https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/174022
>From c4fd2be49ceb0e25d0dfcf0daef618f2abe85046 Mon Sep 17 00:00:00 2001 From: Joseph Huber <[email protected]> Date: Tue, 30 Dec 2025 15:38:45 -0600 Subject: [PATCH 1/3] [libclc] Initial support for cross-compiling OpenCL libraries Summary: The other GPU enabled libraries, (openmp, flang-rt, compiler-rt, libc, libcxx, libcxx-abi) all support builds through a runtime cross-build. In these builds we use a separate CMake build that cross-compiles to a single target. This patch provides basic support for this with the `libclc` libraries. Changes include adding support for the more standard GPU compute triples (amdgcn-amd-amdhsa, nvptx64-nvidia-cuda) and building only one target in this mode. Some things left to do: This patch does not change the compiler invocations, this method would allow us to use standard CMake routines but this keeps it minimal. The prebuild support is questionable and doesn't fit into this scheme because it's a host executable, I'm ignoring it for now. The installed location should just use the triple with no `libclc/` subdirectory handling I believe. --- libclc/CMakeLists.txt | 20 +++++++++++++++++--- libclc/cmake/modules/AddLibclc.cmake | 20 ++++++++++++++++---- libclc/utils/CMakeLists.txt | 6 ++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt index f9ee80645b70f..ca1f135c097ef 100644 --- a/libclc/CMakeLists.txt +++ b/libclc/CMakeLists.txt @@ -38,7 +38,12 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS set( LIBCLC_MIN_LLVM 3.9.0 ) -set( LIBCLC_TARGETS_TO_BUILD "all" +# A runtimes cross-build should only use the requested target. +set( LIBCLC_DEFAULT_TARGET "all" ) +if( LLVM_RUNTIMES_BUILD AND LLVM_DEFAULT_TARGET_TRIPLE MATCHES "^nvptx|^amdgcn" ) + set( LIBCLC_DEFAULT_TARGET ${LLVM_DEFAULT_TARGET_TRIPLE} ) +endif() +set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_DEFAULT_TARGET} CACHE STRING "Semicolon-separated list of libclc targets to build, or 'all'." ) option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF ) @@ -82,7 +87,7 @@ else() # In-tree configuration set( LIBCLC_STANDALONE_BUILD FALSE ) - set( LLVM_PACKAGE_VERSION ${LLVM_VERSION} ) + set( PACKAGE_VERSION ${LLVM_PACKAGE_VERSION} ) # Note that we check this later (for both build types) but we can provide a # more useful error message when built in-tree. We assume that LLVM tools are @@ -158,6 +163,12 @@ set( LIBCLC_TARGETS_ALL nvptx64--nvidiacl ) +# Targets not built under 'all' but are still valid +set( LIBCLC_TARGETS_EXTRA + amdgcn-amd-amdhsa + nvptx64-nvidia-cuda +) + # The mesa3d environment is only available since LLVM 4.0 if( LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL 4.0.0 ) list( APPEND LIBCLC_TARGETS_ALL amdgcn-mesa-mesa3d ) @@ -183,7 +194,8 @@ if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" ) set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} ) else() foreach(TARGET_TO_BUILD ${LIBCLC_TARGETS_TO_BUILD}) - if (NOT ${TARGET_TO_BUILD} IN_LIST LIBCLC_TARGETS_ALL) + if ((NOT ${TARGET_TO_BUILD} IN_LIST LIBCLC_TARGETS_ALL) AND + (NOT ${TARGET_TO_BUILD} IN_LIST LIBCLC_TARGETS_EXTRA)) message ( FATAL_ERROR "Unknown target in LIBCLC_TARGETS_TO_BUILD: \"${TARGET_TO_BUILD}\"\n" "Valid targets are: ${LIBCLC_TARGETS_ALL}\n") endif() @@ -206,10 +218,12 @@ set( r600--_devices cedar cypress barts cayman ) set( amdgcn--_devices tahiti ) set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} ) set( amdgcn--amdhsa_devices none ) +set( amdgcn-amd-amdhsa_devices none ) set( clspv--_devices none ) set( clspv64--_devices none ) set( nvptx--_devices none ) set( nvptx64--_devices none ) +set( nvptx64-nvidia-cuda_devices none ) set( nvptx--nvidiacl_devices none ) set( nvptx64--nvidiacl_devices none ) set( spirv-mesa3d-_devices none ) diff --git a/libclc/cmake/modules/AddLibclc.cmake b/libclc/cmake/modules/AddLibclc.cmake index 53b8fa91bf3fe..370b0b8dedf3c 100644 --- a/libclc/cmake/modules/AddLibclc.cmake +++ b/libclc/cmake/modules/AddLibclc.cmake @@ -454,10 +454,22 @@ function(add_libclc_builtin_set) set( obj_suffix ${ARG_ARCH_SUFFIX}.bc ) set( libclc_builtins_lib ${LIBCLC_OUTPUT_LIBRARY_DIR}/${obj_suffix} ) - add_custom_command( OUTPUT ${libclc_builtins_lib} - COMMAND ${prepare_builtins_exe} -o ${libclc_builtins_lib} ${builtins_opt_lib} - DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} - ) + if( TARGET prepare_builtins ) + # FIXME: Is this utility even necessary? The `-mlink-builtin-bitcode` + # option used to link the library in discards the modified linkage. + add_custom_command( OUTPUT ${libclc_builtins_lib} + COMMAND ${prepare_builtins_exe} -o ${libclc_builtins_lib} ${builtins_opt_lib} + DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} + ) + else() + add_custom_command( + OUTPUT ${libclc_builtins_lib} + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${builtins_opt_lib} + ${libclc_builtins_lib} + DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} + ) + endif() endif() # Add a 'prepare' target diff --git a/libclc/utils/CMakeLists.txt b/libclc/utils/CMakeLists.txt index a14d133985a64..66f542e50403b 100644 --- a/libclc/utils/CMakeLists.txt +++ b/libclc/utils/CMakeLists.txt @@ -1,3 +1,9 @@ +# This is a host utility only. +if( DEFINED CMAKE_CXX_COMPILER_TARGET AND + NOT "${CMAKE_CXX_COMPILER_TARGET}" STREQUAL "${LLVM_HOST_TRIPLE}" ) + return() +endif () + # Setup prepare_builtins tools set( LLVM_LINK_COMPONENTS BitReader >From 8fa70c56b5dc21514d05377ef60c6d8a6273dd51 Mon Sep 17 00:00:00 2001 From: Joseph Huber <[email protected]> Date: Sun, 4 Jan 2026 15:10:44 -0600 Subject: [PATCH 2/3] Make alias more clear - will remove more in another PR --- libclc/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt index ca1f135c097ef..6d09485df3aa4 100644 --- a/libclc/CMakeLists.txt +++ b/libclc/CMakeLists.txt @@ -218,12 +218,12 @@ set( r600--_devices cedar cypress barts cayman ) set( amdgcn--_devices tahiti ) set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} ) set( amdgcn--amdhsa_devices none ) -set( amdgcn-amd-amdhsa_devices none ) +set( amdgcn-amd-amdhsa_devices ${amdgcn--amdhsa_devices} ) set( clspv--_devices none ) set( clspv64--_devices none ) set( nvptx--_devices none ) set( nvptx64--_devices none ) -set( nvptx64-nvidia-cuda_devices none ) +set( nvptx64-nvidia-cuda_devices ${nvptx64--_devices} ) set( nvptx--nvidiacl_devices none ) set( nvptx64--nvidiacl_devices none ) set( spirv-mesa3d-_devices none ) >From af79034243247b30b59f0daa97e19abb7fce6d87 Mon Sep 17 00:00:00 2001 From: Joseph Huber <[email protected]> Date: Sun, 4 Jan 2026 22:41:21 -0600 Subject: [PATCH 3/3] comments --- libclc/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt index 6d09485df3aa4..27bce1f066a6e 100644 --- a/libclc/CMakeLists.txt +++ b/libclc/CMakeLists.txt @@ -87,6 +87,9 @@ else() # In-tree configuration set( LIBCLC_STANDALONE_BUILD FALSE ) + if( NOT LLVM_PACKAGE_VERSION ) + set( LLVM_PACKAGE_VERSION ${LLVM_VERSION} ) + endif() set( PACKAGE_VERSION ${LLVM_PACKAGE_VERSION} ) # Note that we check this later (for both build types) but we can provide a _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
