Skip to content

Commit 7ed894a

Browse files
authored
[SYCL][LIBCLC] Allow custom tools location when building libclc (#12034)
Use `LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR` to specify the location of custom toolchain to be used for creation of libclc. This helps with debug build times: * debug build of sycl-toolchain with libclc built with debug tools: ```sh $ for f in $(ls lib/clc/*.bc); touch $f; time ninja sycl-toolchain [0/2] Re-checking globbed directories... [6/6] Generating ../../lib/clc/remangled-l64-signed_char.libspirv-amdgcn-amd-amdhsa.bc ninja sycl-toolchain 682.55s user 1.33s system 112% cpu 10:07.81 total ``` * debug build of sycl-toolchain with libclc built with release tools: ```sh $ for f in $(ls lib/clc/*.bc); touch $f; time ninja sycl-toolchain [0/2] Re-checking globbed directories... [6/6] Generating ../../lib/clc/remangled-l64-signed_char.libspirv-amdgcn-amd-amdhsa.bc ninja sycl-toolchain 158.51s user 1.15s system 189% cpu 1:24.31 total ``` Fixes: #6925
1 parent aa4e878 commit 7ed894a

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

libclc/CMakeLists.txt

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,63 @@ execute_process( COMMAND ${LLVM_CONFIG} "--bindir"
116116
# These were not properly reported in early LLVM and we don't need them
117117
list( APPEND LLVM_CXX_FLAGS -fno-rtti -fno-exceptions )
118118

119+
# List containing all the toolchain variables.
120+
list( APPEND BINARY_VARIABLES LLVM_CLANG LLVM_AS LLVM_LINK LLVM_OPT LLVM_SPIRV
121+
LIBCLC_REMANGLER )
122+
# List containing all the names of toolchain binaries.
123+
# NOTE: both lists (BINARY_VARIABLES and BINARY_NAMES) must be in sync.
124+
list( APPEND BINARY_NAMES clang llvm-as llvm-link opt llvm-spirv
125+
libclc-remangler )
126+
127+
# find_program needs the variable to be cleared in order to perform a search.
128+
# Make sure that the cached entries are cleared as well.
129+
function( ClearVariables BINARY_VARIABLES_LIST )
130+
foreach( V ${BINARY_VARIABLES_LIST} )
131+
unset( ${V} CACHE )
132+
unset( ${V} PARENT_SCOPE )
133+
endforeach( V )
134+
endfunction()
135+
136+
# Use find_program to locate toolchain binaries.
137+
function( FindToolBinary BINARY_VARIABLES_LIST BINARY_NAMES_LIST PATH_NAME )
138+
list( LENGTH BINARY_NAMES_LIST COUNT )
139+
math( EXPR COUNT "${COUNT}-1" )
140+
foreach( I RANGE ${COUNT} )
141+
list( GET BINARY_VARIABLES_LIST ${I} BV )
142+
list( GET BINARY_NAMES_LIST ${I} BN )
143+
find_program( ${BV} ${BN} PATHS ${PATH_NAME} NO_DEFAULT_PATH )
144+
endforeach( I )
145+
endfunction()
146+
147+
# Use custom toolchain to build libclc, this can be useful when dealing with
148+
# debug builds, that do not need libclc bitcode to be built using debug tools.
149+
if ( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
150+
message( WARNING "Using custom LLVM tools to build libclc: "
151+
"${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}, "
152+
" make sure that the tools are up to date." )
153+
154+
# First clear the variables,
155+
ClearVariables( "${BINARY_VARIABLES}" )
156+
# then set.
157+
FindToolBinary( "${BINARY_VARIABLES}" "${BINARY_NAMES}"
158+
${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
159+
160+
if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK
161+
OR NOT LIBCLC_REMANGLER )
162+
message( FATAL_ERROR "Custom toolchain incomplete!" )
163+
endif()
164+
endif()
165+
119166
# Print LLVM variables
120167
message( "LLVM libdir: ${LLVM_LIBRARY_DIR}" )
121168
message( "LLVM bindir: ${LLVM_TOOLS_BINARY_DIR}" )
122169
message( "LLVM cxx flags: ${LLVM_CXX_FLAGS}" )
123170
message( "" )
124171

125-
find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
126-
find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
127-
find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
128-
find_program( LLVM_OPT opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
129-
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
130-
find_program( LIBCLC_REMANGLER libclc-remangler PATHS ${LLVM_TOOLS_BINARY_DIR}
131-
NO_DEFAULT_PATH )
172+
# It's OK to call find program again, if the variables have been set in the
173+
# custom location clause, find_program returns immediately.
174+
FindToolBinary( "${BINARY_VARIABLES}" "${BINARY_NAMES}"
175+
${LLVM_TOOLS_BINARY_DIR} )
132176

133177
# Print toolchain
134178
message( "clang: ${LLVM_CLANG}" )

sycl/doc/GetStartedGuide.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ control which revision of Unified Runtime should be used when building DPC++:
377377
* `SYCL_PI_UR_SOURCE_DIR` is a variable used to specify the path to the Unified
378378
Runtime repository when `SYCL_PI_UR_USE_FETCH_CONTENT` is set of `OFF`.
379379

380+
### Build DPC++ libclc with a custom toolchain
381+
382+
libclc is an implementation of the OpenCL required libraries, as described in
383+
the [OpenCL C specification](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html),
384+
additionally providing definitions of SPIR-V builtins. It is built to
385+
target-specific bitcode, that is linked against SYCL binaries. By default, the
386+
built system uses the SYCL toolchain currently being built to create libclc
387+
bitcode. This can be suboptimal in case of debug builds, in which case debug
388+
tools are used to build non-debug libclc bitcode (the notion of debug builds
389+
doesn't really apply to libclc), resulting in very long compilation time. In
390+
order to specify a directory containing custom toolchain users can set:
391+
`LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR` variable. Care is required, as the
392+
changes to the local SYCL tree might not be reflected in the custom location
393+
during the build time.
394+
380395
### Deployment
381396

382397
TODO: add instructions how to deploy built DPC++ toolchain.

0 commit comments

Comments
 (0)