[libclc] c5cb48c - [libclc] Rework libclc naming convention to use the triple (#177465)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 26 05:43:31 PST 2026
Author: Joseph Huber
Date: 2026-01-26T07:43:26-06:00
New Revision: c5cb48c39701086393d1177929a328868849fc72
URL: https://github.com/llvm/llvm-project/commit/c5cb48c39701086393d1177929a328868849fc72
DIFF: https://github.com/llvm/llvm-project/commit/c5cb48c39701086393d1177929a328868849fc72.diff
LOG: [libclc] Rework libclc naming convention to use the triple (#177465)
Summary:
Right now all these libraries are installed in the `libclc/` directory
in the compiler's resource directory. We should instead follow the
per-target approach and install it according to the triple. The
sub-architectures will be put in a subdirectory as well.
I will do refactorings on this later to remove all the redundant targets
and pull this into common handling.
Also we should accept `--libclc-lib` without an argument to just find it
by default. I don't know what the plan here is since AMDGCN is the only
triple that uses this flag.
Added:
clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/gfx90a/libclc.bc
clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/libclc.bc
Modified:
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/test/Driver/opencl-libclc.cl
libclc/CMakeLists.txt
libclc/cmake/modules/AddLibclc.cmake
Removed:
################################################################################
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 157c0675815da..57d19a73c5d2b 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -3049,38 +3049,55 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
void tools::addOpenCLBuiltinsLib(const Driver &D,
const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) {
- // Check whether user specifies a libclc bytecode library
const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib_EQ);
if (!A)
return;
- // Find device libraries in <LLVM_DIR>/lib/clang/<ver>/lib/libclc/
- SmallString<128> LibclcPath(D.ResourceDir);
- llvm::sys::path::append(LibclcPath, "lib", "libclc");
-
- // If the namespec is of the form :filename, search for that file.
+ // If the namespec is of the form :filename we use it exactly.
StringRef LibclcNamespec(A->getValue());
bool FilenameSearch = LibclcNamespec.consume_front(":");
- SmallString<128> LibclcTargetFile(LibclcNamespec);
-
- if (FilenameSearch && llvm::sys::fs::exists(LibclcTargetFile)) {
- CC1Args.push_back("-mlink-builtin-bitcode");
- CC1Args.push_back(DriverArgs.MakeArgString(LibclcTargetFile));
- } else {
- // Search the library paths for the file
- if (!FilenameSearch)
- LibclcTargetFile += ".bc";
-
- llvm::sys::path::append(LibclcPath, LibclcTargetFile);
- if (llvm::sys::fs::exists(LibclcPath)) {
+ if (FilenameSearch) {
+ SmallString<128> LibclcFile(LibclcNamespec);
+ if (llvm::sys::fs::exists(LibclcFile)) {
CC1Args.push_back("-mlink-builtin-bitcode");
- CC1Args.push_back(DriverArgs.MakeArgString(LibclcPath));
- } else {
- // Since the user requested a library, if we haven't one then report an
- // error.
- D.Diag(diag::err_drv_libclc_not_found) << LibclcTargetFile;
+ CC1Args.push_back(DriverArgs.MakeArgString(LibclcFile));
+ return;
}
+ D.Diag(diag::err_drv_libclc_not_found) << LibclcFile;
+ return;
}
+
+ // The OpenCL libraries are stored in <ResourceDir>/lib/<triple>.
+ SmallString<128> BasePath(D.ResourceDir);
+ llvm::sys::path::append(BasePath, "lib");
+ llvm::sys::path::append(BasePath, D.getTargetTriple());
+
+ // First check for a CPU-specific library in <ResourceDir>/lib/<triple>/<CPU>.
+ // TODO: Factor this into common logic that checks for valid subtargets.
+ if (const Arg *CPUArg =
+ DriverArgs.getLastArg(options::OPT_mcpu_EQ, options::OPT_march_EQ)) {
+ StringRef CPU = CPUArg->getValue();
+ if (!CPU.empty()) {
+ SmallString<128> CPUPath(BasePath);
+ llvm::sys::path::append(CPUPath, CPU, "libclc.bc");
+ if (llvm::sys::fs::exists(CPUPath)) {
+ CC1Args.push_back("-mlink-builtin-bitcode");
+ CC1Args.push_back(DriverArgs.MakeArgString(CPUPath));
+ return;
+ }
+ }
+ }
+
+ // Fall back to the generic library for the triple.
+ SmallString<128> GenericPath(BasePath);
+ llvm::sys::path::append(GenericPath, "libclc.bc");
+ if (llvm::sys::fs::exists(GenericPath)) {
+ CC1Args.push_back("-mlink-builtin-bitcode");
+ CC1Args.push_back(DriverArgs.MakeArgString(GenericPath));
+ return;
+ }
+
+ D.Diag(diag::err_drv_libclc_not_found) << "libclc.bc";
}
void tools::addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,
diff --git a/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/gfx90a/libclc.bc b/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/gfx90a/libclc.bc
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/libclc.bc b/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/amdgcn-amd-amdhsa/libclc.bc
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/opencl-libclc.cl b/clang/test/Driver/opencl-libclc.cl
index 185690768c75b..f7428aeb9f922 100644
--- a/clang/test/Driver/opencl-libclc.cl
+++ b/clang/test/Driver/opencl-libclc.cl
@@ -7,3 +7,15 @@
// CHECK-SUBDIR: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}libclc{{/|\\\\}}subdir{{/|\\\\}}libclc.bc
// CHECK-ERROR: no libclc library{{.*}}not-here.bc' found in the clang resource directory
+
+// RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib \
+// RUN: --libclc-lib= \
+// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
+// RUN: -march=gfx90a %s 2>&1 | FileCheck %s --check-prefix=CHECK-GFX90A
+// CHECK-GFX90A: -mlink-builtin-bitcode{{.*}}resource_dir_with_per_target_subdir{{/|\\\\}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}gfx90a{{/|\\\\}}libclc.bc
+
+// RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib \
+// RUN: --libclc-lib= \
+// RUN: -resource-dir %S/Inputs/resource_dir_with_per_target_subdir \
+// RUN: %s 2>&1 | FileCheck %s --check-prefix=CHECK-GENERIC
+// CHECK-GENERIC: -mlink-builtin-bitcode{{.*}}resource_dir_with_per_target_subdir{{/|\\\\}}lib{{/|\\\\}}amdgcn-amd-amdhsa{{/|\\\\}}libclc.bc
diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 88a32797d5915..09a1d6ef4feb7 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -110,12 +110,10 @@ else()
# in-tree build we place the libraries in clang's resource driectory.
include(GetClangResourceDir)
get_clang_resource_dir( LIBCLC_INSTALL_DIR )
- cmake_path( APPEND LIBCLC_INSTALL_DIR "lib" "libclc" )
+ cmake_path( APPEND LIBCLC_INSTALL_DIR "lib" )
- # Note we do not adhere to LLVM_ENABLE_PER_TARGET_RUNTIME_DIR.
cmake_path( GET LLVM_LIBRARY_OUTPUT_INTDIR PARENT_PATH LIBCLC_OUTPUT_LIBRARY_DIR )
cmake_path( APPEND LIBCLC_OUTPUT_LIBRARY_DIR ${LIBCLC_INSTALL_DIR} )
- file( MAKE_DIRECTORY ${LIBCLC_OUTPUT_LIBRARY_DIR} )
endif()
if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
@@ -446,6 +444,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
add_libclc_builtin_set(
CLC_INTERNAL
ARCH ${ARCH}
+ CPU ${cpu}
ARCH_SUFFIX clc-${arch_suffix}
TRIPLE ${clang_triple}
COMPILE_FLAGS ${build_flags}
diff --git a/libclc/cmake/modules/AddLibclc.cmake b/libclc/cmake/modules/AddLibclc.cmake
index 1ba53a9528f82..ef72243c91845 100644
--- a/libclc/cmake/modules/AddLibclc.cmake
+++ b/libclc/cmake/modules/AddLibclc.cmake
@@ -226,33 +226,6 @@ function(get_libclc_device_info)
endif()
endfunction()
-# Install libclc artifacts.
-#
-# Arguments:
-# * FILES <string> ...
-# List of libclc artifact files to be installed.
-function(libclc_install)
- cmake_parse_arguments(ARG "" "" "FILES" ${ARGN})
-
- if( NOT ARG_FILES )
- message( FATAL_ERROR "Must provide FILES" )
- endif()
-
- if( NOT CMAKE_CFG_INTDIR STREQUAL "." )
- # Replace CMAKE_CFG_INTDIR with CMAKE_INSTALL_CONFIG_NAME for multiple-
- # configuration generators.
- string( REPLACE ${CMAKE_CFG_INTDIR} "\$\{CMAKE_INSTALL_CONFIG_NAME\}"
- files ${ARG_FILES} )
- else()
- set( files ${ARG_FILES} )
- endif()
-
- install(
- FILES ${files}
- DESTINATION ${LIBCLC_INSTALL_DIR}
- )
-endfunction()
-
# Compiles a list of library source files (provided by LIB_FILES) and compiles
# them to LLVM bytecode (or SPIR-V), links them together and optimizes them.
#
@@ -262,6 +235,8 @@ endfunction()
# Arguments:
# * ARCH <string>
# libclc architecture being built
+# * CPU <string>
+# libclc microarchitecture being built
# * ARCH_SUFFIX <string>
# libclc architecture/triple suffix
# * TRIPLE <string>
@@ -399,13 +374,21 @@ function(add_libclc_builtin_set)
return()
endif()
+ set( LIBCLC_OUTPUT_FILENAME libclc )
set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> )
+ # We store the library according to its triple and cpu if present.
+ if (ARG_CPU)
+ set (library_dir ${LIBCLC_OUTPUT_LIBRARY_DIR}/${ARG_TRIPLE}/${ARG_CPU})
+ else()
+ set (library_dir ${LIBCLC_OUTPUT_LIBRARY_DIR}/${ARG_TRIPLE})
+ endif()
+ file( MAKE_DIRECTORY ${library_dir} )
+
# For SPIR-V targets we diverage at this point and generate SPIR-V using the
# llvm-spirv tool.
if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
- set( obj_suffix ${ARG_ARCH_SUFFIX}.spv )
- set( libclc_builtins_lib ${LIBCLC_OUTPUT_LIBRARY_DIR}/${obj_suffix} )
+ set( libclc_builtins_lib ${library_dir}/${LIBCLC_OUTPUT_FILENAME}.spv )
if ( LIBCLC_USE_SPIRV_BACKEND )
add_custom_command( OUTPUT ${libclc_builtins_lib}
COMMAND ${clang_exe} -c --target=${ARG_TRIPLE} -x ir -o ${libclc_builtins_lib} ${builtins_link_lib}
@@ -419,8 +402,7 @@ function(add_libclc_builtin_set)
endif()
else()
# Non-SPIR-V targets add an extra step to optimize the bytecode
- set( obj_suffix ${ARG_ARCH_SUFFIX}.bc )
- set( libclc_builtins_lib ${LIBCLC_OUTPUT_LIBRARY_DIR}/${obj_suffix} )
+ set( libclc_builtins_lib ${library_dir}/${LIBCLC_OUTPUT_FILENAME}.bc )
add_custom_command( OUTPUT ${libclc_builtins_lib}
COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${libclc_builtins_lib}
@@ -430,8 +412,8 @@ function(add_libclc_builtin_set)
endif()
# Add a 'library' target
- add_custom_target( library-${obj_suffix} ALL DEPENDS ${libclc_builtins_lib} )
- set_target_properties( "library-${obj_suffix}" PROPERTIES
+ add_custom_target( library-${ARG_ARCH_SUFFIX} ALL DEPENDS ${libclc_builtins_lib} )
+ set_target_properties( "library-${ARG_ARCH_SUFFIX}" PROPERTIES
TARGET_FILE ${libclc_builtins_lib}
FOLDER "libclc/Device IR/Library"
)
@@ -442,12 +424,16 @@ function(add_libclc_builtin_set)
if( NOT TARGET library-${ARG_TRIPLE} )
add_custom_target( library-${ARG_TRIPLE} ALL )
endif()
- add_dependencies( library-${ARG_TRIPLE} library-${obj_suffix} )
+ add_dependencies( library-${ARG_TRIPLE} library-${ARG_ARCH_SUFFIX} )
# Add dependency to top-level pseudo target to ease making other
# targets dependent on libclc.
add_dependencies( ${ARG_PARENT_TARGET} library-${ARG_TRIPLE} )
- libclc_install(FILES ${libclc_builtins_lib})
+ # Install the created library.
+ install(
+ FILES ${libclc_builtins_lib}
+ DESTINATION ${LIBCLC_INSTALL_DIR}/${ARG_TRIPLE}
+ )
# SPIR-V targets can exit early here
if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
@@ -460,7 +446,7 @@ function(add_libclc_builtin_set)
# * nvptx64-- targets don't include workitem builtins
# * clspv targets don't include all OpenCL builtins
if( NOT ARG_ARCH MATCHES "^(nvptx|clspv)(64)?$" )
- add_test( NAME external-funcs-${obj_suffix}
+ add_test( NAME external-funcs-${ARG_ARCH_SUFFIX}
COMMAND ./check_external_funcs.sh ${libclc_builtins_lib} ${LLVM_TOOLS_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
endif()
@@ -476,20 +462,26 @@ function(add_libclc_builtin_set)
set(LIBCLC_LINK_OR_COPY copy)
endif()
- set( alias_suffix "${a}-${ARG_TRIPLE}.bc" )
+ file( MAKE_DIRECTORY ${LIBCLC_OUTPUT_LIBRARY_DIR}/${ARG_TRIPLE}/${a} )
+ set( libclc_alias_lib ${LIBCLC_OUTPUT_LIBRARY_DIR}/${ARG_TRIPLE}/${a}/${LIBCLC_OUTPUT_FILENAME}.bc )
add_custom_command(
- OUTPUT ${LIBCLC_OUTPUT_LIBRARY_DIR}/${alias_suffix}
- COMMAND ${CMAKE_COMMAND} -E ${LIBCLC_LINK_OR_COPY} ${LIBCLC_LINK_OR_COPY_SOURCE} ${LIBCLC_OUTPUT_LIBRARY_DIR}/${alias_suffix}
- DEPENDS library-${obj_suffix}
+ OUTPUT ${libclc_alias_lib}
+ COMMAND ${CMAKE_COMMAND} -E ${LIBCLC_LINK_OR_COPY} ${LIBCLC_LINK_OR_COPY_SOURCE} ${libclc_alias_lib}
+ DEPENDS library-${ARG_ARCH_SUFFIX}
)
- add_custom_target( alias-${alias_suffix} ALL
- DEPENDS ${LIBCLC_OUTPUT_LIBRARY_DIR}/${alias_suffix}
+ add_custom_target( alias-${a}-${ARG_TRIPLE} ALL
+ DEPENDS ${libclc_alias_lib}
)
- add_dependencies( ${ARG_PARENT_TARGET} alias-${alias_suffix} )
- set_target_properties( alias-${alias_suffix}
+ add_dependencies( ${ARG_PARENT_TARGET} alias-${a}-${ARG_TRIPLE} )
+ set_target_properties( alias-${a}-${ARG_TRIPLE}
PROPERTIES FOLDER "libclc/Device IR/Aliases"
)
- libclc_install(FILES ${LIBCLC_OUTPUT_LIBRARY_DIR}/${alias_suffix})
+
+ # Install the library
+ install(
+ FILES ${libclc_alias_lib}
+ DESTINATION ${LIBCLC_INSTALL_DIR}/${ARG_TRIPLE}/${a}
+ )
endforeach( a )
endfunction(add_libclc_builtin_set)
More information about the cfe-commits
mailing list