Skip to content

Commit 6479604

Browse files
[CMake][libclc] Improve dependencies to avoid build errors (#95018)
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
1 parent e5a41f0 commit 6479604

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

libclc/CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,21 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
374374
OUTPUT ${output_file}
375375
EXTRA_OPTS "${mcpu}" -fno-builtin -nostdlib
376376
"${build_flags}" -I${PROJECT_SOURCE_DIR}/${file_dir}
377+
DEPENDENCIES generate_convert.cl clspv-generate_convert.cl
377378
)
378379
list( APPEND bytecode_files ${output_file} )
379380
endforeach()
380381

381-
set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
382+
set( builtins_comp_lib_tgt builtins.comp.${arch_suffix} )
383+
add_custom_target( ${builtins_comp_lib_tgt}
384+
DEPENDS ${bytecode_files}
385+
)
382386

387+
set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
383388
link_bc(
384389
TARGET ${builtins_link_lib_tgt}
385390
INPUTS ${bytecode_files}
391+
DEPENDENCIES ${builtins_comp_lib_tgt}
386392
)
387393

388394
set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> )
@@ -391,7 +397,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
391397
set( spv_suffix ${arch_suffix}.spv )
392398
add_custom_command( OUTPUT ${spv_suffix}
393399
COMMAND libclc::llvm-spirv ${spvflags} -o ${spv_suffix} ${builtins_link_lib}
394-
DEPENDS ${builtins_link_lib}
400+
DEPENDS ${builtins_link_lib} ${builtins_link_lib_tgt}
395401
)
396402
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
397403
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
@@ -403,7 +409,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
403409
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc
404410
COMMAND libclc::opt ${opt_flags} -o ${builtins_opt_lib_tgt}.bc
405411
${builtins_link_lib}
406-
DEPENDS libclc::opt ${builtins_link_lib}
412+
DEPENDS libclc::opt ${builtins_link_lib} ${builtins_link_lib_tgt}
407413
)
408414
add_custom_target( ${builtins_opt_lib_tgt}
409415
ALL DEPENDS ${builtins_opt_lib_tgt}.bc
@@ -418,7 +424,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
418424
set( obj_suffix ${arch_suffix}.bc )
419425
add_custom_command( OUTPUT ${obj_suffix}
420426
COMMAND prepare_builtins -o ${obj_suffix} ${builtins_opt_lib}
421-
DEPENDS ${builtins_opt_lib} prepare_builtins )
427+
DEPENDS ${builtins_opt_lib} ${builtins_opt_lib_tgt} prepare_builtins )
422428
add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} )
423429

424430
# nvptx-- targets don't include workitem builtins

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ endfunction()
8080
# Custom target to create
8181
# * INPUT <string> ...
8282
# List of bytecode files to link together
83+
# * DEPENDENCIES <string> ...
84+
# List of extra dependencies to inject
8385
function(link_bc)
8486
cmake_parse_arguments(ARG
8587
""
8688
"TARGET"
87-
"INPUTS"
89+
"INPUTS;DEPENDENCIES"
8890
${ARGN}
8991
)
9092

@@ -106,7 +108,7 @@ function(link_bc)
106108
add_custom_command(
107109
OUTPUT ${ARG_TARGET}.bc
108110
COMMAND libclc::llvm-link -o ${ARG_TARGET}.bc ${LINK_INPUT_ARG}
109-
DEPENDS libclc::llvm-link ${ARG_INPUTS} ${RSP_FILE}
111+
DEPENDS libclc::llvm-link ${ARG_DEPENDENCIES} ${ARG_INPUTS} ${RSP_FILE}
110112
)
111113

112114
add_custom_target( ${ARG_TARGET} ALL DEPENDS ${ARG_TARGET}.bc )

0 commit comments

Comments
 (0)