[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:11 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.
----------------
wenju-he wrote:

can we move CLC library creation into a cmake function like before? This would simplify the main CMakeLists.txt file

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


More information about the cfe-commits mailing list