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

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 7 20:33:11 PST 2026


================
@@ -283,155 +213,231 @@ 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 )
+  # Determine the clang target triple.
+  set(clang_triple ${t})
+  if(ARCH STREQUAL spirv AND LIBCLC_USE_SPIRV_BACKEND)
+    set(clang_triple spirv32--)
+  elseif(ARCH STREQUAL spirv64 AND LIBCLC_USE_SPIRV_BACKEND)
+    set(clang_triple spirv64--)
+  elseif(ARCH STREQUAL spirv OR ARCH STREQUAL clspv)
+    set(clang_triple spir--)
+  elseif(ARCH STREQUAL spirv64 OR ARCH STREQUAL clspv64)
+    set(clang_triple spir64--)
+  endif()
+
+  # Determine the preprocessor define for this target
+  set(MACRO_ARCH ${ARCH})
+  if(ARCH STREQUAL spirv)
+    set(MACRO_ARCH SPIRV32)
+  elseif(ARCH STREQUAL spirv64)
+    set(MACRO_ARCH SPIRV64)
+  elseif(ARCH STREQUAL clspv)
+    set(MACRO_ARCH CLSPV32)
+  elseif(ARCH STREQUAL clspv64)
+    set(MACRO_ARCH CLSPV64)
   endif()
+  string(TOUPPER "CLC_${MACRO_ARCH}" target_define)
 
-  set( clc_lib_files )
+  # Address space values
+  set(private_addrspace_val 0)
+  set(generic_addrspace_val 0)
+  if(ARCH STREQUAL amdgcn)
+    set(private_addrspace_val 5)
+  endif()
+  if(ARCH STREQUAL spirv OR ARCH STREQUAL spirv64)
+    set(generic_addrspace_val 4)
+  endif()
 
-  libclc_configure_lib_source(
-    clc_lib_files
-    LIB_ROOT_DIR clc
-    DIRS ${clc_dirs}
-  )
+  # Target-specific compile flags and defines
+  set(target_compile_flags)
+  set(target_extra_defines)
+  set(opt_flags -O3)
+
+  if(ARCH STREQUAL spirv OR ARCH STREQUAL spirv64)
+    list(APPEND target_compile_flags -O0 -finline-hint-functions)
+    list(APPEND target_extra_defines CLC_SPIRV)
+    set(opt_flags)
+  elseif(ARCH STREQUAL clspv OR ARCH STREQUAL clspv64)
+    list(APPEND target_compile_flags -Wno-unknown-assumption)
+    list(APPEND target_extra_defines CLC_CLSPV)
+  elseif(ARCH STREQUAL amdgcn)
+    list(APPEND target_compile_flags "SHELL:-Xclang -mcode-object-version=none")
+  endif()
+
+  # 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})
+      if(OS STREQUAL amdhsa)
+        list(APPEND _opencl_overrides ${OPENCL_AMDGCN_AMDHSA_SOURCES})
+      endif()
+    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( opencl_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(
-    opencl_lib_files
-    LIB_ROOT_DIR opencl
-    DIRS ${opencl_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}
   )
 
-  foreach( d ${${t}_devices} )
-    get_libclc_device_info(
-      TRIPLE ${t}
-      DEVICE ${d}
-      CPU cpu
-      ARCH_SUFFIX arch_suffix
-      CLANG_TRIPLE clang_triple
-    )
 
-    message( STATUS "  device: ${d} ( ${${d}_aliases} )" )
+  # 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})
----------------
jhuber6 wrote:

Handling in https://github.com/llvm/llvm-project/pull/185243

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


More information about the cfe-commits mailing list