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

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 8 18:08:22 PDT 2026


================
@@ -285,155 +262,147 @@ 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 )
-
-  libclc_configure_lib_source(
-    clc_lib_files
-    LIB_ROOT_DIR clc
-    DIRS ${clc_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 )
+  set(_common_defs
+    ${target_define}
+    ${target_extra_defines}
+    __CLC_PRIVATE_ADDRSPACE_VAL=${private_addrspace_val}
+    __CLC_GENERIC_ADDRSPACE_VAL=${generic_addrspace_val}
+  )
 
-  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_libclc_builtin_library(${clc_lib}
+    SOURCES ${clc_sources}
+    COMPILE_OPTIONS ${compile_flags}
+    INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/clc/include
+    COMPILE_DEFINITIONS ${_common_defs}
+    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.
+  set(opencl_lib opencl_builtins_${lib_suffix})
+  add_libclc_builtin_library(${opencl_lib}
+    SOURCES ${opencl_sources}
+    COMPILE_OPTIONS ${compile_flags} "SHELL:-Xclang -fdeclare-opencl-builtins"
+    INCLUDE_DIRS
+      ${CMAKE_CURRENT_SOURCE_DIR}/clc/include
+      ${CMAKE_CURRENT_SOURCE_DIR}/opencl/include
+    COMPILE_DEFINITIONS ${_common_defs}
+    FOLDER "libclc/Device IR/OpenCL"
+  )
 
-    message( STATUS "  device: ${d} ( ${${d}_aliases} )" )
-
-    set( MACRO_ARCH ${ARCH} )
-    if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
-      set( build_flags -O0 -finline-hint-functions -DCLC_SPIRV )
-      set( opt_flags )
-      set( spvflags --spirv-max-version=1.1 )
-      set( MACRO_ARCH SPIRV32 )
-      if( ARCH STREQUAL spirv64 )
-        set( MACRO_ARCH SPIRV64 )
-      endif()
-    elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
-      set( build_flags "-Wno-unknown-assumption" -DCLC_CLSPV )
-      set( opt_flags -O3 )
-      set( MACRO_ARCH CLSPV32 )
-      if( ARCH STREQUAL clspv64 )
-        set( MACRO_ARCH CLSPV64 )
-      endif()
-    elseif( ARCH STREQUAL amdgcn )
-      set( build_flags "-Xclang" "-mcode-object-version=none" )
-      set( opt_flags -O3 )
-    else()
-      set( build_flags )
-      set( opt_flags -O3 )
-    endif()
+  # Link the final library.
----------------
jhuber6 wrote:

Alright, moved the install and dependency handling in there as well. Is this sufficient? That should just be a new compile and link job for each of your auxiliary libraries which seems reasonable.

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


More information about the cfe-commits mailing list