[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.
This commit is contained in:
Fraser Cormack 2025-07-03 08:30:33 +01:00 committed by GitHub
parent 0bfa0bcd79
commit 85d09de5fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -356,55 +356,72 @@ function(add_libclc_builtin_set)
set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> ) 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 ) if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
set( spv_suffix ${ARG_ARCH_SUFFIX}.spv ) set( obj_suffix ${ARG_ARCH_SUFFIX}.spv )
add_custom_command( OUTPUT ${spv_suffix} add_custom_command( OUTPUT ${obj_suffix}
COMMAND ${llvm-spirv_exe} ${spvflags} -o ${spv_suffix} ${builtins_link_lib} COMMAND ${llvm-spirv_exe} ${spvflags} -o ${obj_suffix} ${builtins_link_lib}
DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt} DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
) )
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" ) else()
set_target_properties( "prepare-${spv_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" ) # Non-SPIR-V targets add an extra step to optimize the bytecode
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix} set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} )
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
return() 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( 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() endif()
set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} ) # Add a 'prepare' target
# 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> )
# 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_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} ) add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )
set_target_properties( "prepare-${obj_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" ) set_target_properties( "prepare-${obj_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" )
# nvptx-- targets don't include workitem builtins, and clspv targets don't # Also add a 'prepare' target for the triple. Since a triple may have
# include all OpenCL builtins # 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)?$" ) if( NOT ARG_ARCH MATCHES "^(nvptx|clspv)(64)?$" )
add_test( NAME external-calls-${obj_suffix} add_test( NAME external-calls-${obj_suffix}
COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR} COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
endif() endif()
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
foreach( a ${ARG_ALIASES} ) foreach( a ${ARG_ALIASES} )
set( alias_suffix "${a}-${ARG_TRIPLE}.bc" ) set( alias_suffix "${a}-${ARG_TRIPLE}.bc" )
add_custom_command( add_custom_command(