@@ -24,19 +24,22 @@ option(
2424
2525# List of all supported architectures.
2626set ( 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
3030set (LIBCLC_TARGET ${LLVM_DEFAULT_TARGET_TRIPLE} )
3131
3232if (NOT LIBCLC_TARGET)
3333 message (FATAL_ERROR "libclc target is empty\n " )
3434endif ()
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 " )
4245endif ()
@@ -98,12 +101,17 @@ endif()
98101
99102message (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 ()
109117endif ()
@@ -114,35 +122,12 @@ foreach( tool IN ITEMS opt llvm-link )
114122 endif ()
115123endforeach ()
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.
142127set (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.
154139set (private_addrspace_val 0)
155140set (generic_addrspace_val 0)
156- if (ARCH STREQUAL amdgcn)
141+ if (LIBCLC_TARGET_ARCH STREQUAL amdgcn)
157142 set (private_addrspace_val 5)
158143endif ()
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)
161146endif ()
162147
@@ -165,50 +150,18 @@ set(target_compile_flags)
165150set (target_extra_defines)
166151set (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" )
178163endif ()
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.
213166set (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
0 commit comments