Skip to content

Commit f14620a

Browse files
authored
Start to reduce libdevice dependencies on libclc (#19340)
The libdevice project included a CMake file from libclc to complete some work. This is an undesirable link for several reasons. For one, libclc is upstream and libdevice isn't. This means that it's easy to accidentally pull in changes from upstream that alter how libdevice is built. We can see this in the FOLDER property of the link_bc target, which was set to 'libclc/Device IR/Linking'. This would be confusing for any user building DPC++ in an IDE. For two, this sharing of code also necessitated making downstream changes to libclc, which make maintenance more difficult. This can be seen in the RSP_DIR argument to link_bc, which was never set in libclc to anything other than the upstream value. This commit therefore starts to sever this dependency by just copying over the two utility functions to libdevice's CMake. They aren't very big and it should make any existing dependencies clearer. For example, we implicitly are relying on libclc being built so that the prepare-bultins target and executable are set up. This isn't an ideal situation but is left for future work.
1 parent 0260118 commit f14620a

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,12 @@ endfunction()
9696
# Custom target to create
9797
# * INPUT <string> ...
9898
# List of bytecode files to link together
99-
# * RSP_DIR <string>
100-
# Directory where a response file should be placed
101-
# (Only needed for WIN32 or CYGWIN)
10299
# * DEPENDENCIES <string> ...
103100
# List of extra dependencies to inject
104101
function(link_bc)
105102
cmake_parse_arguments(ARG
106103
"INTERNALIZE"
107-
"TARGET;RSP_DIR"
104+
"TARGET"
108105
"INPUTS;DEPENDENCIES"
109106
${ARGN}
110107
)
@@ -113,7 +110,7 @@ function(link_bc)
113110
if( WIN32 OR CYGWIN )
114111
# Create a response file in case the number of inputs exceeds command-line
115112
# character limits on certain platforms.
116-
file( TO_CMAKE_PATH ${ARG_RSP_DIR}/${ARG_TARGET}.rsp RSP_FILE )
113+
file( TO_CMAKE_PATH ${LIBCLC_ARCH_OBJFILE_DIR}/${ARG_TARGET}.rsp RSP_FILE )
117114
# Turn it into a space-separate list of input files
118115
list( JOIN ARG_INPUTS " " RSP_INPUT )
119116
file( GENERATE OUTPUT ${RSP_FILE} CONTENT ${RSP_INPUT} )
@@ -414,7 +411,6 @@ function(add_libclc_builtin_set)
414411
link_bc(
415412
TARGET ${builtins_link_lib_tgt}
416413
INPUTS ${bytecode_files}
417-
RSP_DIR ${LIBCLC_ARCH_OBJFILE_DIR}
418414
DEPENDENCIES ${builtins_comp_lib_tgt}
419415
)
420416
else()
@@ -425,7 +421,6 @@ function(add_libclc_builtin_set)
425421
link_bc(
426422
TARGET ${builtins_link_lib_tmp_tgt}
427423
INPUTS ${bytecode_files}
428-
RSP_DIR ${LIBCLC_ARCH_OBJFILE_DIR}
429424
DEPENDENCIES ${builtins_comp_lib_tgt}
430425
)
431426
set( internal_link_depend_files )
@@ -437,7 +432,6 @@ function(add_libclc_builtin_set)
437432
TARGET ${builtins_link_lib_tgt}
438433
INPUTS $<TARGET_PROPERTY:${builtins_link_lib_tmp_tgt},TARGET_FILE>
439434
${internal_link_depend_files}
440-
RSP_DIR ${LIBCLC_ARCH_OBJFILE_DIR}
441435
DEPENDENCIES ${builtins_link_lib_tmp_tgt} ${ARG_INTERNAL_LINK_DEPENDENCIES}
442436
)
443437
endif()

libdevice/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Utility project providing various functionalities for SPIR-V devices
22
# without native support of these functionalities.
33

4-
include(${CMAKE_CURRENT_SOURCE_DIR}/../libclc/cmake/modules/AddLibclc.cmake)
5-
64
set(CMAKE_MODULE_PATH
75
${CMAKE_MODULE_PATH}
86
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,105 @@ function(compile_lib_ext filename)
161161
COMPONENT libsycldevice)
162162
endfunction()
163163

164+
# Links together one or more bytecode files
165+
#
166+
# Arguments:
167+
# * INTERNALIZE
168+
# Set if -internalize flag should be passed when linking
169+
# * TARGET <string>
170+
# Custom target to create
171+
# * INPUT <string> ...
172+
# List of bytecode files to link together
173+
# * RSP_DIR <string>
174+
# Directory where a response file should be placed
175+
# (Only needed for WIN32 or CYGWIN)
176+
# * DEPENDENCIES <string> ...
177+
# List of extra dependencies to inject
178+
function(link_bc)
179+
cmake_parse_arguments(ARG
180+
"INTERNALIZE"
181+
"TARGET;RSP_DIR"
182+
"INPUTS;DEPENDENCIES"
183+
${ARGN}
184+
)
185+
186+
set( LINK_INPUT_ARG ${ARG_INPUTS} )
187+
if( WIN32 OR CYGWIN )
188+
# Create a response file in case the number of inputs exceeds command-line
189+
# character limits on certain platforms.
190+
file( TO_CMAKE_PATH ${ARG_RSP_DIR}/${ARG_TARGET}.rsp RSP_FILE )
191+
# Turn it into a space-separate list of input files
192+
list( JOIN ARG_INPUTS " " RSP_INPUT )
193+
file( GENERATE OUTPUT ${RSP_FILE} CONTENT ${RSP_INPUT} )
194+
# Ensure that if this file is removed, we re-run CMake
195+
set_property( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
196+
${RSP_FILE}
197+
)
198+
set( LINK_INPUT_ARG "@${RSP_FILE}" )
199+
endif()
200+
201+
if( ARG_INTERNALIZE )
202+
set( link_flags --internalize --only-needed )
203+
endif()
204+
205+
add_custom_command(
206+
OUTPUT ${ARG_TARGET}.bc
207+
COMMAND ${llvm-link_exe} ${link_flags} -o ${ARG_TARGET}.bc ${LINK_INPUT_ARG}
208+
DEPENDS ${llvm-link_target} ${ARG_DEPENDENCIES} ${ARG_INPUTS} ${RSP_FILE}
209+
)
210+
211+
add_custom_target( ${ARG_TARGET} ALL DEPENDS ${ARG_TARGET}.bc )
212+
set_target_properties( ${ARG_TARGET} PROPERTIES
213+
TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/${ARG_TARGET}.bc
214+
)
215+
endfunction()
216+
217+
# Runs opt and prepare-builtins on a bitcode file specified by lib_tgt
218+
#
219+
# ARGUMENTS:
220+
# * LIB_TGT string
221+
# Target name that becomes dependent on the out file named LIB_TGT.bc
222+
# * IN_FILE string
223+
# Target name of the input bytecode file
224+
# * OUT_DIR string
225+
# Name of the directory where the output should be placed
226+
# * DEPENDENCIES <string> ...
227+
# List of extra dependencies to inject
228+
function(process_bc out_file)
229+
cmake_parse_arguments(ARG
230+
""
231+
"LIB_TGT;IN_FILE;OUT_DIR"
232+
"OPT_FLAGS;DEPENDENCIES"
233+
${ARGN})
234+
add_custom_command( OUTPUT ${ARG_LIB_TGT}.bc
235+
COMMAND ${opt_exe} ${ARG_OPT_FLAGS} -o ${ARG_LIB_TGT}.bc
236+
${ARG_IN_FILE}
237+
DEPENDS ${opt_target} ${ARG_IN_FILE} ${ARG_DEPENDENCIES}
238+
)
239+
add_custom_target( ${ARG_LIB_TGT}
240+
ALL DEPENDS ${ARG_LIB_TGT}.bc
241+
)
242+
set_target_properties( ${ARG_LIB_TGT}
243+
PROPERTIES TARGET_FILE ${ARG_LIB_TGT}.bc
244+
)
245+
246+
set( builtins_opt_lib $<TARGET_PROPERTY:${ARG_LIB_TGT},TARGET_FILE> )
247+
248+
# Add prepare target
249+
# FIXME: prepare_builtins_exe comes from having included libclc before this.
250+
# This is brittle.
251+
add_custom_command( OUTPUT ${ARG_OUT_DIR}/${out_file}
252+
COMMAND ${prepare_builtins_exe} -o ${ARG_OUT_DIR}/${out_file}
253+
${builtins_opt_lib}
254+
DEPENDS ${builtins_opt_lib} ${ARG_LIB_TGT} ${prepare_builtins_target} )
255+
add_custom_target( prepare-${out_file} ALL
256+
DEPENDS ${ARG_OUT_DIR}/${out_file}
257+
)
258+
set_target_properties( prepare-${out_file}
259+
PROPERTIES TARGET_FILE ${ARG_OUT_DIR}/${out_file}
260+
)
261+
endfunction()
262+
164263
# Appends a list to a global property.
165264
#
166265
# Arguments:

0 commit comments

Comments
 (0)