Author: Fraser Cormack Date: 2025-07-03T08:30:33+01:00 New Revision: 85d09de5fa19a32bbcc400928d55f9d633077640
URL: https://github.com/llvm/llvm-project/commit/85d09de5fa19a32bbcc400928d55f9d633077640 DIFF: https://github.com/llvm/llvm-project/commit/85d09de5fa19a32bbcc400928d55f9d633077640.diff LOG: [libclc] Add prepare-<triple> targets (#146700) This target provides a unified build target for all devices under the single triple. This way a user doesn't have to know device names to build a specific target's bytecode libraries. Device names may be considered as internal implementation details as they are not exposed to users of CMake; users only specify triples to build. Now, instead of `prepare-{barts,cayman,cedar,cypress}-r600--.bc`, for example, a user may now build simply `prepare-r600--` and have all four of those libraries built. This commit also refactors the CMake somewhat. We were previously diverging between the SPIR-V and other targets, and duplicating a bit of logic like the creation of the 'prepare' targets, the targets' properties, and the installation directory. It's cleaner and hopefully more robust to share this code between all targets. This commit also takes this opportunity to improve some comments around this code. Added: Modified: libclc/cmake/modules/AddLibclc.cmake Removed: ################################################################################ diff --git a/libclc/cmake/modules/AddLibclc.cmake b/libclc/cmake/modules/AddLibclc.cmake index 597bb642655e4..c521ea1589484 100644 --- a/libclc/cmake/modules/AddLibclc.cmake +++ b/libclc/cmake/modules/AddLibclc.cmake @@ -356,55 +356,72 @@ function(add_libclc_builtin_set) set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> ) + # 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( spv_suffix ${ARG_ARCH_SUFFIX}.spv ) - add_custom_command( OUTPUT ${spv_suffix} - COMMAND ${llvm-spirv_exe} ${spvflags} -o ${spv_suffix} ${builtins_link_lib} + set( obj_suffix ${ARG_ARCH_SUFFIX}.spv ) + add_custom_command( OUTPUT ${obj_suffix} + COMMAND ${llvm-spirv_exe} ${spvflags} -o ${obj_suffix} ${builtins_link_lib} DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt} ) - add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" ) - set_target_properties( "prepare-${spv_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" ) - install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix} - DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) - - return() - endif() + else() + # Non-SPIR-V targets add an extra step to optimize the bytecode + set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} ) - set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} ) + add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc + COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${builtins_opt_lib_tgt}.bc + ${builtins_link_lib} + DEPENDS ${opt_target} ${builtins_link_lib} ${builtins_link_lib_tgt} + ) + add_custom_target( ${builtins_opt_lib_tgt} + ALL DEPENDS ${builtins_opt_lib_tgt}.bc + ) + set_target_properties( ${builtins_opt_lib_tgt} PROPERTIES + TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/${builtins_opt_lib_tgt}.bc + FOLDER "libclc/Device IR/Opt" + ) - # Add opt target - add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc - COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${builtins_opt_lib_tgt}.bc - ${builtins_link_lib} - DEPENDS ${opt_target} ${builtins_link_lib} ${builtins_link_lib_tgt} - ) - add_custom_target( ${builtins_opt_lib_tgt} - ALL DEPENDS ${builtins_opt_lib_tgt}.bc - ) - set_target_properties( ${builtins_opt_lib_tgt} PROPERTIES - TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/${builtins_opt_lib_tgt}.bc - FOLDER "libclc/Device IR/Opt" - ) + set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> ) - set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> ) + set( obj_suffix ${ARG_ARCH_SUFFIX}.bc ) + add_custom_command( OUTPUT ${obj_suffix} + COMMAND ${prepare_builtins_exe} -o ${obj_suffix} ${builtins_opt_lib} + DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} ) + endif() - # Add prepare target - set( obj_suffix ${ARG_ARCH_SUFFIX}.bc ) - add_custom_command( OUTPUT ${obj_suffix} - COMMAND ${prepare_builtins_exe} -o ${obj_suffix} ${builtins_opt_lib} - DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} ) + # Add a 'prepare' target add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} ) set_target_properties( "prepare-${obj_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" ) - # nvptx-- targets don't include workitem builtins, and clspv targets don't - # include all OpenCL builtins + # Also add a 'prepare' target for the triple. Since a triple may have + # multiple devices, ensure we only try to create the triple target once. The + # triple's target will build all of the bytecode for its constituent devices. + if( NOT TARGET prepare-${ARG_TRIPLE} ) + add_custom_target( prepare-${ARG_TRIPLE} ALL ) + endif() + add_dependencies( prepare-${ARG_TRIPLE} prepare-${obj_suffix} ) + + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} + DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" + ) + + # SPIR-V targets can exit early here + if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 ) + return() + endif() + + # Add a test for whether or not the libraries contain unresolved calls which + # would usually indicate a build problem. Note that we don't perform this + # test for all libclc targets: + # * nvptx-- 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-calls-${obj_suffix} COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) endif() - install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) foreach( a ${ARG_ALIASES} ) set( alias_suffix "${a}-${ARG_TRIPLE}.bc" ) add_custom_command( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits