Skip to content

Commit 85d09de

Browse files
authored
[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.
1 parent 0bfa0bc commit 85d09de

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -356,55 +356,72 @@ function(add_libclc_builtin_set)
356356

357357
set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> )
358358

359+
# For SPIR-V targets we diverage at this point and generate SPIR-V using the
360+
# llvm-spirv tool.
359361
if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
360-
set( spv_suffix ${ARG_ARCH_SUFFIX}.spv )
361-
add_custom_command( OUTPUT ${spv_suffix}
362-
COMMAND ${llvm-spirv_exe} ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
362+
set( obj_suffix ${ARG_ARCH_SUFFIX}.spv )
363+
add_custom_command( OUTPUT ${obj_suffix}
364+
COMMAND ${llvm-spirv_exe} ${spvflags} -o ${obj_suffix} ${builtins_link_lib}
363365
DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
364366
)
365-
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
366-
set_target_properties( "prepare-${spv_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" )
367-
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
368-
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
369-
370-
return()
371-
endif()
367+
else()
368+
# Non-SPIR-V targets add an extra step to optimize the bytecode
369+
set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} )
372370

373-
set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} )
371+
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
372+
COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${builtins_opt_lib_tgt}.bc
373+
${builtins_link_lib}
374+
DEPENDS ${opt_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
375+
)
376+
add_custom_target( ${builtins_opt_lib_tgt}
377+
ALL DEPENDS ${builtins_opt_lib_tgt}.bc
378+
)
379+
set_target_properties( ${builtins_opt_lib_tgt} PROPERTIES
380+
TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/${builtins_opt_lib_tgt}.bc
381+
FOLDER "libclc/Device IR/Opt"
382+
)
374383

375-
# Add opt target
376-
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
377-
COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${builtins_opt_lib_tgt}.bc
378-
${builtins_link_lib}
379-
DEPENDS ${opt_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
380-
)
381-
add_custom_target( ${builtins_opt_lib_tgt}
382-
ALL DEPENDS ${builtins_opt_lib_tgt}.bc
383-
)
384-
set_target_properties( ${builtins_opt_lib_tgt} PROPERTIES
385-
TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/${builtins_opt_lib_tgt}.bc
386-
FOLDER "libclc/Device IR/Opt"
387-
)
384+
set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> )
388385

389-
set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> )
386+
set( obj_suffix ${ARG_ARCH_SUFFIX}.bc )
387+
add_custom_command( OUTPUT ${obj_suffix}
388+
COMMAND ${prepare_builtins_exe} -o ${obj_suffix} ${builtins_opt_lib}
389+
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} )
390+
endif()
390391

391-
# Add prepare target
392-
set( obj_suffix ${ARG_ARCH_SUFFIX}.bc )
393-
add_custom_command( OUTPUT ${obj_suffix}
394-
COMMAND ${prepare_builtins_exe} -o ${obj_suffix} ${builtins_opt_lib}
395-
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} ${prepare_builtins_target} )
392+
# Add a 'prepare' target
396393
add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )
397394
set_target_properties( "prepare-${obj_suffix}" PROPERTIES FOLDER "libclc/Device IR/Prepare" )
398395

399-
# nvptx-- targets don't include workitem builtins, and clspv targets don't
400-
# include all OpenCL builtins
396+
# Also add a 'prepare' target for the triple. Since a triple may have
397+
# multiple devices, ensure we only try to create the triple target once. The
398+
# triple's target will build all of the bytecode for its constituent devices.
399+
if( NOT TARGET prepare-${ARG_TRIPLE} )
400+
add_custom_target( prepare-${ARG_TRIPLE} ALL )
401+
endif()
402+
add_dependencies( prepare-${ARG_TRIPLE} prepare-${obj_suffix} )
403+
404+
install(
405+
FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix}
406+
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc"
407+
)
408+
409+
# SPIR-V targets can exit early here
410+
if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
411+
return()
412+
endif()
413+
414+
# Add a test for whether or not the libraries contain unresolved calls which
415+
# would usually indicate a build problem. Note that we don't perform this
416+
# test for all libclc targets:
417+
# * nvptx-- targets don't include workitem builtins
418+
# * clspv targets don't include all OpenCL builtins
401419
if( NOT ARG_ARCH MATCHES "^(nvptx|clspv)(64)?$" )
402420
add_test( NAME external-calls-${obj_suffix}
403421
COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
404422
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
405423
endif()
406424

407-
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
408425
foreach( a ${ARG_ALIASES} )
409426
set( alias_suffix "${a}-${ARG_TRIPLE}.bc" )
410427
add_custom_command(

0 commit comments

Comments
 (0)