Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14bf8e7

Browse files
authored
[libclc] Base the build around add_sources instead of source list (#197034)
Summary: The current build uses a curated + deduplicated source list. This PR seeks to simplify this a little bit and canonicalize the behavior. Now we create the target up-front, `clc` and `opencl`. We add the directories which add sources to this target. We normalize the architecture to the variants. We always add target specific versions first. When we add sources we check if the file already exists and defer to the architecture specific one. This normalized the behavior, the directories are now laid out like this `clc/<arch>/<os>`. We normalize these to `amdgpu`, `nvptx`, and `spirv` respectively. We use the OS for the newly created vulkan target. We now control variants via checking if the directory for that exists, so it's nested more naturally. Hopefully this makes more sense, the goal is to exercise the fact that we have individual builds now. Previously this did not work because you could not add_subdirectory more than once.
1 parent 84812fd commit 14bf8e7

39 files changed

Lines changed: 171 additions & 201 deletions

libclc/CMakeLists.txt

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ option(
2424

2525
# List of all supported architectures.
2626
set( LIBCLC_ARCHS_ALL amdgpu amdgcn nvptx64 )
27-
set( LIBCLC_ARCHS_SPIRV spirv spirv32 spirv64)
28-
list( APPEND LIBCLC_ARCHS_ALL ${LIBCLC_ARCHS_SPIRV})
27+
set( LIBCLC_ARCHS_SPIRV spirv spirv32 spirv64 )
28+
list( APPEND LIBCLC_ARCHS_ALL ${LIBCLC_ARCHS_SPIRV} )
2929

3030
set(LIBCLC_TARGET ${LLVM_DEFAULT_TARGET_TRIPLE})
3131

3232
if(NOT LIBCLC_TARGET)
3333
message(FATAL_ERROR "libclc target is empty\n")
3434
endif()
3535

36-
string( REPLACE "-" ";" _target_components ${LIBCLC_TARGET} )
37-
list(GET _target_components 0 _target_arch)
38-
if(NOT "${_target_arch}" IN_LIST LIBCLC_ARCHS_ALL)
39-
message(FATAL_ERROR "Unknown libclc target architecture: ${_target_arch}\n"
36+
# Parse the target triple into arch and OS components.
37+
string(REPLACE "-" ";" triple_components ${LIBCLC_TARGET})
38+
list(GET triple_components 0 LIBCLC_TARGET_ARCH)
39+
list(GET triple_components 2 LIBCLC_TARGET_OS)
40+
41+
if(NOT "${LIBCLC_TARGET_ARCH}" IN_LIST LIBCLC_ARCHS_ALL)
42+
message(FATAL_ERROR "Unknown libclc target architecture: ${LIBCLC_TARGET_ARCH}\n"
4043
"Target was: ${LIBCLC_TARGET}\n"
4144
"Valid architectures are: ${LIBCLC_ARCHS_ALL}\n")
4245
endif()
@@ -98,12 +101,17 @@ endif()
98101

99102
message(STATUS "libclc target '${LIBCLC_TARGET}' is enabled")
100103

101-
string( REPLACE "-" ";" TRIPLE ${LIBCLC_TARGET} )
102-
list(GET TRIPLE 0 ARCH)
103-
list(GET TRIPLE 2 OS)
104+
# Map the LLVM target architecture to the standard directory name.
105+
if(LIBCLC_TARGET_ARCH STREQUAL amdgcn OR LIBCLC_TARGET_ARCH STREQUAL amdgpu)
106+
set(LIBCLC_ARCH_DIR amdgpu)
107+
elseif(LIBCLC_TARGET_ARCH STREQUAL nvptx64)
108+
set(LIBCLC_ARCH_DIR nvptx)
109+
elseif(LIBCLC_TARGET_ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
110+
set(LIBCLC_ARCH_DIR spirv)
111+
endif()
104112

105-
if(ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
106-
if(NOT OS STREQUAL vulkan AND NOT LIBCLC_USE_SPIRV_BACKEND AND NOT llvm-spirv_exe)
113+
if(LIBCLC_TARGET_ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
114+
if(NOT LIBCLC_TARGET_OS STREQUAL vulkan AND NOT LIBCLC_USE_SPIRV_BACKEND AND NOT llvm-spirv_exe)
107115
message(FATAL_ERROR "SPIR-V backend or llvm-spirv is required for libclc ${LIBCLC_TARGET}")
108116
endif()
109117
endif()
@@ -114,35 +122,12 @@ foreach( tool IN ITEMS opt llvm-link )
114122
endif()
115123
endforeach()
116124

117-
add_subdirectory(clc/lib/generic)
118-
add_subdirectory(opencl/lib/generic)
119-
120-
if(ARCH STREQUAL amdgcn)
121-
add_subdirectory(clc/lib/amdgpu)
122-
add_subdirectory(opencl/lib/amdgpu)
123-
elseif(ARCH STREQUAL nvptx64)
124-
add_subdirectory(clc/lib/ptx-nvidiacl)
125-
elseif(ARCH STREQUAL spirv OR ARCH STREQUAL spirv32 OR ARCH STREQUAL spirv64)
126-
if(OS STREQUAL vulkan)
127-
add_subdirectory(clc/lib/vulkan)
128-
add_subdirectory(opencl/lib/vulkan)
129-
else()
130-
add_subdirectory(clc/lib/spirv)
131-
add_subdirectory(opencl/lib/spirv)
132-
endif()
133-
endif()
134-
135-
add_custom_target( libclc ALL )
136-
137-
add_custom_target( libclc-opencl-builtins COMMENT "Build libclc OpenCL builtins" )
138-
add_dependencies( libclc libclc-opencl-builtins )
139-
140125
# Determine the clang target triple. Vulkan and SPIR-V backend targets use the
141126
# triple directly; other SPIR-V targets fall back to the legacy SPIR target.
142127
set(clang_triple ${LIBCLC_TARGET})
143-
if(ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
144-
if(NOT OS STREQUAL vulkan AND NOT LIBCLC_USE_SPIRV_BACKEND)
145-
if(ARCH STREQUAL spirv)
128+
if(LIBCLC_TARGET_ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
129+
if(NOT LIBCLC_TARGET_OS STREQUAL vulkan AND NOT LIBCLC_USE_SPIRV_BACKEND)
130+
if(LIBCLC_TARGET_ARCH STREQUAL spirv)
146131
set(clang_triple spir--)
147132
else()
148133
set(clang_triple spir64--)
@@ -153,10 +138,10 @@ endif()
153138
# Address space values.
154139
set(private_addrspace_val 0)
155140
set(generic_addrspace_val 0)
156-
if(ARCH STREQUAL amdgcn)
141+
if(LIBCLC_TARGET_ARCH STREQUAL amdgcn)
157142
set(private_addrspace_val 5)
158143
endif()
159-
if(ARCH IN_LIST LIBCLC_ARCHS_SPIRV AND NOT OS STREQUAL vulkan)
144+
if(LIBCLC_TARGET_ARCH IN_LIST LIBCLC_ARCHS_SPIRV AND NOT LIBCLC_TARGET_OS STREQUAL vulkan)
160145
set(generic_addrspace_val 4)
161146
endif()
162147

@@ -165,50 +150,18 @@ set(target_compile_flags)
165150
set(target_extra_defines)
166151
set(opt_flags -O3)
167152

168-
if(ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
169-
if(OS STREQUAL vulkan)
153+
if(LIBCLC_TARGET_ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
154+
if(LIBCLC_TARGET_OS STREQUAL vulkan)
170155
list(APPEND target_compile_flags -Wno-unknown-assumption -U__opencl_c_int64)
171156
else()
172157
list(APPEND target_compile_flags -O0 -finline-hint-functions)
173158
list(APPEND target_extra_defines CLC_SPIRV)
174159
set(opt_flags)
175160
endif()
176-
elseif(ARCH STREQUAL amdgcn)
161+
elseif(LIBCLC_TARGET_ARCH STREQUAL amdgcn)
177162
list(APPEND target_compile_flags "SHELL:-Xclang -mcode-object-version=none")
178163
endif()
179164

180-
# Collect CLC sources; target-specific sources override generic ones by basename.
181-
set(_clc_overrides)
182-
if(ARCH STREQUAL amdgcn)
183-
list(APPEND _clc_overrides ${CLC_AMDGPU_SOURCES})
184-
elseif(ARCH STREQUAL nvptx64 AND (OS STREQUAL nvidiacl OR OS STREQUAL cuda))
185-
list(APPEND _clc_overrides ${CLC_PTX_NVIDIACL_SOURCES})
186-
elseif(ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
187-
if(OS STREQUAL vulkan)
188-
list(APPEND _clc_overrides ${CLC_VULKAN_SOURCES})
189-
else()
190-
list(APPEND _clc_overrides ${CLC_SPIRV_SOURCES})
191-
endif()
192-
endif()
193-
libclc_merge_sources(clc_sources ${CLC_GENERIC_SOURCES} ${_clc_overrides})
194-
195-
# Collect OpenCL sources. SPIR-V and Vulkan targets use self-contained
196-
# subsets while others merge with target-specific overrides.
197-
if(ARCH IN_LIST LIBCLC_ARCHS_SPIRV)
198-
if(OS STREQUAL vulkan)
199-
set(opencl_sources ${OPENCL_VULKAN_SOURCES})
200-
else()
201-
set(opencl_sources ${OPENCL_SPIRV_SOURCES})
202-
endif()
203-
else()
204-
set(_opencl_overrides)
205-
if(ARCH STREQUAL amdgcn)
206-
list(APPEND _opencl_overrides ${OPENCL_AMDGCN_SOURCES})
207-
endif()
208-
libclc_merge_sources(opencl_sources
209-
${OPENCL_GENERIC_SOURCES} ${_opencl_overrides})
210-
endif()
211-
212165
# Common compile options shared by CLC and OpenCL libraries.
213166
set(compile_flags
214167
-flto
@@ -227,35 +180,45 @@ set(compile_flags
227180
${target_compile_flags}
228181
)
229182

230-
set(_common_defs
183+
set(common_defs
231184
${target_extra_defines}
232185
__CLC_PRIVATE_ADDRSPACE_VAL=${private_addrspace_val}
233186
__CLC_GENERIC_ADDRSPACE_VAL=${generic_addrspace_val}
234187
)
235188

236-
# Build the CLC internal builtins library.
237-
string(REPLACE "-" "_" lib_suffix ${LIBCLC_TARGET})
238-
set(clc_lib clc_builtins_${lib_suffix})
239-
add_libclc_builtin_library(${clc_lib}
240-
SOURCES ${clc_sources}
189+
add_custom_target(libclc ALL)
190+
add_custom_target(libclc-opencl-builtins COMMENT "Build libclc OpenCL builtins")
191+
add_dependencies(libclc libclc-opencl-builtins)
192+
193+
# Configure the CLC internal builtins library.
194+
set(LIBCLC_CLC_TARGET clc)
195+
libclc_add_builtin_library(${LIBCLC_CLC_TARGET}
241196
COMPILE_OPTIONS ${compile_flags}
242197
INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/clc/include
243-
COMPILE_DEFINITIONS ${_common_defs}
198+
COMPILE_DEFINITIONS ${common_defs}
244199
FOLDER "libclc/Device IR/CLC"
245200
)
246201

247-
# Build, link, and install the final OpenCL builtins library.
248-
add_libclc_library(libclc-${LIBCLC_TARGET}
249-
ARCH ${ARCH}
250-
TRIPLE ${clang_triple}
251-
TARGET_TRIPLE ${LIBCLC_TARGET}
252-
SOURCES ${opencl_sources}
202+
set(LIBCLC_OPENCL_TARGET opencl)
203+
libclc_add_builtin_library(${LIBCLC_OPENCL_TARGET}
253204
COMPILE_OPTIONS ${compile_flags} "SHELL:-Xclang -fdeclare-opencl-builtins"
254205
INCLUDE_DIRS
255206
${CMAKE_CURRENT_SOURCE_DIR}/clc/include
256207
${CMAKE_CURRENT_SOURCE_DIR}/opencl/include
257-
COMPILE_DEFINITIONS ${_common_defs}
258-
INTERNALIZE_LIBRARIES ${clc_lib}
208+
COMPILE_DEFINITIONS ${common_defs}
209+
FOLDER "libclc/Device IR/Intermediate"
210+
)
211+
212+
add_subdirectory(clc)
213+
add_subdirectory(opencl)
214+
215+
# Link and install the final OpenCL builtins library.
216+
libclc_add_library(libclc-${LIBCLC_TARGET}
217+
ARCH ${LIBCLC_TARGET_ARCH}
218+
TRIPLE ${clang_triple}
219+
TARGET_TRIPLE ${LIBCLC_TARGET}
220+
LIBRARIES ${LIBCLC_OPENCL_TARGET}
221+
INTERNALIZE_LIBRARIES ${LIBCLC_CLC_TARGET}
259222
OPT_FLAGS ${opt_flags}
260223
OUTPUT_FILENAME libclc
261224
PARENT_TARGET libclc-opencl-builtins

libclc/clc/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Add the target specific files first so they can override the generic fallback.
2+
if(LIBCLC_ARCH_DIR AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/lib/${LIBCLC_ARCH_DIR})
3+
add_subdirectory(lib/${LIBCLC_ARCH_DIR})
4+
endif()
5+
add_subdirectory(lib/generic)

libclc/clc/lib/amdgpu/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
libclc_configure_source_list(CLC_AMDGPU_SOURCES
2-
${CMAKE_CURRENT_SOURCE_DIR}
1+
libclc_add_sources(${LIBCLC_CLC_TARGET} FILES
32
address_space/clc_qualifier.cl
43
math/clc_cbrt.cl
54
math/clc_exp.cl
@@ -45,9 +44,10 @@ libclc_configure_source_list(CLC_AMDGPU_SOURCES
4544
workitem/clc_get_num_sub_groups.cl
4645
workitem/clc_get_sub_group_id.cl
4746
workitem/clc_get_sub_group_size.cl
48-
workitem/clc_get_work_dim.cl)
47+
workitem/clc_get_work_dim.cl
48+
)
4949

50-
libclc_configure_source_options(${CMAKE_CURRENT_SOURCE_DIR} -fapprox-func
50+
libclc_set_source_options(-fapprox-func
5151
math/clc_native_exp.cl
5252
math/clc_native_exp2.cl
5353
math/clc_native_log10.cl

libclc/clc/lib/generic/CMakeLists.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
libclc_configure_source_list(CLC_GENERIC_SOURCES
2-
${CMAKE_CURRENT_SOURCE_DIR}
1+
libclc_add_sources(${LIBCLC_CLC_TARGET} FILES
32
async/clc_prefetch.cl
43
atomic/clc_atomic_compare_exchange.cl
54
atomic/clc_atomic_dec.cl
@@ -209,7 +208,7 @@ libclc_configure_source_list(CLC_GENERIC_SOURCES
209208
workitem/clc_get_sub_group_size.cl
210209
)
211210

212-
libclc_configure_source_options(${CMAKE_CURRENT_SOURCE_DIR} -fapprox-func
211+
libclc_set_source_options(-fapprox-func
213212
math/clc_native_cos.cl
214213
math/clc_native_divide.cl
215214
math/clc_native_exp.cl
@@ -226,8 +225,10 @@ libclc_configure_source_options(${CMAKE_CURRENT_SOURCE_DIR} -fapprox-func
226225
math/clc_native_tan.cl
227226
math/clc_div_fast.cl
228227
math/clc_recip_fast.cl
229-
math/clc_sqrt_fast.cl)
228+
math/clc_sqrt_fast.cl
229+
)
230230

231-
libclc_configure_source_options(${CMAKE_CURRENT_SOURCE_DIR} -cl-fp32-correctly-rounded-divide-sqrt
231+
libclc_set_source_options(-cl-fp32-correctly-rounded-divide-sqrt
232232
math/clc_div_cr.cl
233-
math/clc_sqrt_cr.cl)
233+
math/clc_sqrt_cr.cl
234+
)

libclc/clc/lib/ptx-nvidiacl/CMakeLists.txt renamed to libclc/clc/lib/nvptx/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
libclc_configure_source_list(CLC_PTX_NVIDIACL_SOURCES
2-
${CMAKE_CURRENT_SOURCE_DIR}
1+
libclc_add_sources(${LIBCLC_CLC_TARGET} FILES
32
math/clc_log.cl
43
math/clc_rsqrt.cl
54
math/clc_sinpi.cl
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)