[libclc] [libclc] Use custom CMake handling to overhaul libclc compilation (PR #185247)

Wenju He via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 7 21:31:28 PST 2026


================
@@ -285,155 +262,176 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
     set( OS nvidiacl )
   endif()
 
-  # Append a variety of target- and triple-based directories to search,
-  # increasing in specificity.
-  list( APPEND opencl_dirs ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
-
-  # The 'generic' directory contains all of the generic implementations of the
-  # builtins. It is included first so it has the lowest search priority,
-  # allowing targets to override builtins based on file names found later in
-  # the list of search directories.
-  # CLC builds all builtins for all targets, so unconditionally prepend the
-  # 'generic' directory.
-  set( clc_dirs generic ${opencl_dirs} )
-  # Some OpenCL targets don't build all builtins, in which case they don't want
-  # the 'generic' directory. Otherwise, prepend the 'generic' directory.
-  if ( NOT ARCH STREQUAL spirv AND NOT ARCH STREQUAL spirv64 AND
-       NOT ARCH STREQUAL clspv AND NOT ARCH STREQUAL clspv64)
-    list( PREPEND opencl_dirs generic )
+  # Collect CLC sources, target specific sources will override the generic ones
+  # if present in the list.
+  set(_clc_overrides)
+  if(ARCH STREQUAL amdgcn)
+    list(APPEND _clc_overrides ${CLC_AMDGPU_SOURCES} ${CLC_AMDGCN_SOURCES})
+  elseif(DARCH STREQUAL ptx AND OS STREQUAL nvidiacl)
+    list(APPEND _clc_overrides ${CLC_PTX_NVIDIACL_SOURCES})
+  elseif(DARCH STREQUAL spirv)
+    list(APPEND _clc_overrides ${CLC_SPIRV_SOURCES})
+  elseif(DARCH STREQUAL clspv)
+    list(APPEND _clc_overrides ${CLC_CLSPV_SOURCES})
+  endif()
+  libclc_merge_sources(clc_sources ${CLC_GENERIC_SOURCES} ${_clc_overrides})
+
+  # Collect OpenCL sources. SPIR-V and Clspv targets use self-contained
+  # subsets while others merge with target-specific overrides.
+  if(ARCH STREQUAL spirv OR ARCH STREQUAL spirv64)
+    set(opencl_sources ${OPENCL_SPIRV_SOURCES})
+  elseif(ARCH STREQUAL clspv OR ARCH STREQUAL clspv64)
+    set(opencl_sources ${OPENCL_CLSPV_SOURCES})
+  else()
+    set(_opencl_overrides)
+    if(ARCH STREQUAL amdgcn)
+      list(APPEND _opencl_overrides ${OPENCL_AMDGCN_SOURCES})
+    elseif(DARCH STREQUAL ptx AND OS STREQUAL nvidiacl)
+      list(APPEND _opencl_overrides ${OPENCL_PTX_NVIDIACL_SOURCES})
+    endif()
+    libclc_merge_sources(opencl_sources
+      ${OPENCL_GENERIC_SOURCES} ${_opencl_overrides})
   endif()
 
-  set( clc_lib_files )
+  # Collect unique source directories for include paths. Each source file
+  # may include .inc files from its own directory.
+  set(_clc_inc_dirs)
+  foreach(f ${clc_sources})
+    get_filename_component(dir ${f} DIRECTORY)
+    list(APPEND _clc_inc_dirs ${dir})
+  endforeach()
+  list(REMOVE_DUPLICATES _clc_inc_dirs)
 
-  libclc_configure_lib_source(
-    clc_lib_files
-    LIB_ROOT_DIR clc
-    DIRS ${clc_dirs}
+  set(_opencl_inc_dirs)
+  foreach(f ${opencl_sources})
+    get_filename_component(dir ${f} DIRECTORY)
+    list(APPEND _opencl_inc_dirs ${dir})
+  endforeach()
+  list(REMOVE_DUPLICATES _opencl_inc_dirs)
+
+  # Common compile options shared by CLC and OpenCL libraries.
+  set(compile_flags
+    -flto
+    --target=${clang_triple}
+    -nostdlib
+    -nostdlibinc
+    -cl-no-stdinc
+    -cl-std=CL3.0
+    -include opencl-c-base.h
+    -Werror=undef
+    -Wall
+    -Wextra
+    -fdiscard-value-names
+    -ffp-contract=fast-honor-pragmas
+    -fdenormal-fp-math=dynamic
+    ${target_compile_flags}
   )
 
-  set( opencl_lib_files )
-
-  libclc_configure_lib_source(
-    opencl_lib_files
-    LIB_ROOT_DIR opencl
-    DIRS ${opencl_dirs}
+  # Build the CLC internal builtins library.
+  string(REPLACE "-" "_" lib_suffix ${t})
+  set(clc_lib clc_builtins_${lib_suffix})
+  add_library(${clc_lib} STATIC ${clc_sources})
+  target_compile_options(${clc_lib} PRIVATE ${compile_flags})
+  target_include_directories(${clc_lib} PRIVATE
+    ${CMAKE_CURRENT_SOURCE_DIR}/clc/include
+    ${_clc_inc_dirs}
+  )
+  target_compile_definitions(${clc_lib} PRIVATE
+    ${target_define}
+    ${target_extra_defines}
+    __CLC_PRIVATE_ADDRSPACE_VAL=${private_addrspace_val}
+    __CLC_GENERIC_ADDRSPACE_VAL=${generic_addrspace_val}
+  )
+  set_target_properties(${clc_lib} PROPERTIES
+    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+    FOLDER "libclc/Device IR/CLC"
   )
 
-  foreach( d ${${t}_devices} )
-    get_libclc_device_info(
-      TRIPLE ${t}
-      DEVICE ${d}
-      CPU cpu
-      ARCH_SUFFIX arch_suffix
-      CLANG_TRIPLE clang_triple
-    )
+  # Build the OpenCL builtins library.
----------------
wenju-he wrote:

same here, can we move OpenCL library creation into a cmake function like before?

https://github.com/llvm/llvm-project/pull/185247


More information about the cfe-commits mailing list