Skip to content

Commit 0c21d6b

Browse files
authored
[libclc] Fix commands in compile_to_bc are executed sequentially (#130755)
In libclc, we observe that compiling OpenCL source files to bitcode is executed sequentially on Windows, which increases debug build time by about an hour. add_custom_command may introduce additional implicit dependencies, see https://gitlab.kitware.com/cmake/cmake/-/issues/17097 This PR adds a target for each command, enabling parallel builds of OpenCL source files. CMake 3.27 has fixed above issue with DEPENDS_EXPLICIT_ONLY. When LLVM upgrades cmake vertion to 3.7, we can switch to DEPENDS_EXPLICIT_ONLY.
1 parent 0078cf7 commit 0c21d6b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

libclc/cmake/modules/AddLibclc.cmake

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Compiles an OpenCL C - or assembles an LL file - to bytecode
22
#
33
# Arguments:
4+
# * TARGET <string>
5+
# Custom target to create
46
# * TRIPLE <string>
57
# Target triple for which to compile the bytecode file.
68
# * INPUT <string>
@@ -17,7 +19,7 @@
1719
function(compile_to_bc)
1820
cmake_parse_arguments(ARG
1921
""
20-
"TRIPLE;INPUT;OUTPUT"
22+
"TARGET;TRIPLE;INPUT;OUTPUT"
2123
"EXTRA_OPTS;DEPENDENCIES"
2224
${ARGN}
2325
)
@@ -63,6 +65,12 @@ function(compile_to_bc)
6365
${ARG_DEPENDENCIES}
6466
DEPFILE ${ARG_OUTPUT}.d
6567
)
68+
# FIXME: The target is added to ensure the parallel build of source files.
69+
# However, this may result in a large number of targets.
70+
# Starting with CMake 3.27, DEPENDS_EXPLICIT_ONLY can be used with
71+
# add_custom_command to enable parallel build.
72+
# Refer to https://gitlab.kitware.com/cmake/cmake/-/issues/17097 for details.
73+
add_custom_target( ${ARG_TARGET} DEPENDS ${ARG_OUTPUT}${TMP_SUFFIX} )
6674

6775
if( ${FILE_EXT} STREQUAL ".ll" )
6876
add_custom_command(
@@ -232,6 +240,7 @@ function(add_libclc_builtin_set)
232240

233241
set( bytecode_files )
234242
set( bytecode_ir_files )
243+
set( compile_tgts )
235244
foreach( file IN LISTS ARG_GEN_FILES ARG_LIB_FILES )
236245
# We need to take each file and produce an absolute input file, as well
237246
# as a unique architecture-specific output file. We deal with a mix of
@@ -261,20 +270,25 @@ function(add_libclc_builtin_set)
261270

262271
get_filename_component( file_dir ${file} DIRECTORY )
263272

273+
string( REPLACE "/" "-" replaced ${file} )
274+
set( tgt compile_tgt-${ARG_ARCH_SUFFIX}${replaced})
275+
264276
set( file_specific_compile_options )
265277
get_source_file_property( compile_opts ${file} COMPILE_OPTIONS)
266278
if( compile_opts )
267279
set( file_specific_compile_options "${compile_opts}" )
268280
endif()
269281

270282
compile_to_bc(
283+
TARGET ${tgt}
271284
TRIPLE ${ARG_TRIPLE}
272285
INPUT ${input_file}
273286
OUTPUT ${output_file}
274287
EXTRA_OPTS -fno-builtin -nostdlib "${file_specific_compile_options}"
275288
"${ARG_COMPILE_FLAGS}" -I${CMAKE_CURRENT_SOURCE_DIR}/${file_dir}
276289
DEPENDENCIES ${input_file_dep}
277290
)
291+
list( APPEND compile_tgts ${tgt} )
278292

279293
# Collect all files originating in LLVM IR separately
280294
get_filename_component( file_ext ${file} EXT )
@@ -294,7 +308,7 @@ function(add_libclc_builtin_set)
294308

295309
set( builtins_comp_lib_tgt builtins.comp.${ARG_ARCH_SUFFIX} )
296310
add_custom_target( ${builtins_comp_lib_tgt}
297-
DEPENDS ${bytecode_files}
311+
DEPENDS ${bytecode_files} ${compile_tgts}
298312
)
299313
set_target_properties( ${builtins_comp_lib_tgt} PROPERTIES FOLDER "libclc/Device IR/Comp" )
300314

0 commit comments

Comments
 (0)