Skip to content

[libclc] Add prepare-<triple> targets #146700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 50 additions & 33 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code in the else block is a little long and makes the if-else-endif not easy to read, can it be hoisted into a helper function like optimize_bc? If yes, it could be done as a later refactor in follow-up pr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I can look into that as a follow-up. Thanks for the idea.

I'd quite like to avoid using opt altogether, to be honest. I wonder if using liboffload's approach of using clang with LTO to create a bytecode library would solve this problem somewhat. I started looking into it but got pulled in other directions.

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(
Expand Down
Loading