Skip to content

Commit

Permalink
[CMake][libclc] Improve dependencies to avoid build errors (llvm#95018)
Browse files Browse the repository at this point in the history
With the Makefile generator and particularly high build parallelism some
intermediate dependencies may be generated redundantly and concurrently,
leading to build failures.

To fix this, arrange for libclc's add_custom_commands to depend on
targets in addition to files.

This follows CMake documentation's[^1] guidance on add_custom_command:

> Do not list the output in more than one independent target that may
> build in parallel or the instances of the rule may conflict. Instead,
> use the add_custom_target() command to drive the command and make the
> other targets depend on that one.

Eliminating the redundant commands also improves build times.

[^1]: https://cmake.org/cmake/help/v3.29/command/add_custom_command.html
  • Loading branch information
tcreech-intel authored Jun 24, 2024
1 parent e5a41f0 commit 6479604
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,21 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
OUTPUT ${output_file}
EXTRA_OPTS "${mcpu}" -fno-builtin -nostdlib
"${build_flags}" -I${PROJECT_SOURCE_DIR}/${file_dir}
DEPENDENCIES generate_convert.cl clspv-generate_convert.cl
)
list( APPEND bytecode_files ${output_file} )
endforeach()

set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
set( builtins_comp_lib_tgt builtins.comp.${arch_suffix} )
add_custom_target( ${builtins_comp_lib_tgt}
DEPENDS ${bytecode_files}
)

set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
link_bc(
TARGET ${builtins_link_lib_tgt}
INPUTS ${bytecode_files}
DEPENDENCIES ${builtins_comp_lib_tgt}
)

set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> )
Expand All @@ -391,7 +397,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
set( spv_suffix ${arch_suffix}.spv )
add_custom_command( OUTPUT ${spv_suffix}
COMMAND libclc::llvm-spirv ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
DEPENDS ${builtins_link_lib}
DEPENDS ${builtins_link_lib} ${builtins_link_lib_tgt}
)
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
Expand All @@ -403,7 +409,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
COMMAND libclc::opt ${opt_flags} -o ${builtins_opt_lib_tgt}.bc
${builtins_link_lib}
DEPENDS libclc::opt ${builtins_link_lib}
DEPENDS libclc::opt ${builtins_link_lib} ${builtins_link_lib_tgt}
)
add_custom_target( ${builtins_opt_lib_tgt}
ALL DEPENDS ${builtins_opt_lib_tgt}.bc
Expand All @@ -418,7 +424,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
set( obj_suffix ${arch_suffix}.bc )
add_custom_command( OUTPUT ${obj_suffix}
COMMAND prepare_builtins -o ${obj_suffix} ${builtins_opt_lib}
DEPENDS ${builtins_opt_lib} prepare_builtins )
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} prepare_builtins )
add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )

# nvptx-- targets don't include workitem builtins
Expand Down
6 changes: 4 additions & 2 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ endfunction()
# Custom target to create
# * INPUT <string> ...
# List of bytecode files to link together
# * DEPENDENCIES <string> ...
# List of extra dependencies to inject
function(link_bc)
cmake_parse_arguments(ARG
""
"TARGET"
"INPUTS"
"INPUTS;DEPENDENCIES"
${ARGN}
)

Expand All @@ -106,7 +108,7 @@ function(link_bc)
add_custom_command(
OUTPUT ${ARG_TARGET}.bc
COMMAND libclc::llvm-link -o ${ARG_TARGET}.bc ${LINK_INPUT_ARG}
DEPENDS libclc::llvm-link ${ARG_INPUTS} ${RSP_FILE}
DEPENDS libclc::llvm-link ${ARG_DEPENDENCIES} ${ARG_INPUTS} ${RSP_FILE}
)

add_custom_target( ${ARG_TARGET} ALL DEPENDS ${ARG_TARGET}.bc )
Expand Down

0 comments on commit 6479604

Please sign in to comment.