[libclc] [libclc] Add custom CMake language handling for OpenCL files (PR #185243)
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 7 20:07:02 PST 2026
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/185243
>From 87f2adb492be26c97e60a03fe60bca57f049e737 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Sat, 7 Mar 2026 15:16:52 -0600
Subject: [PATCH] [libclc] Add custom CMake language handling for OpenCL files
Summary:
The current handling of `libclc` is to use custom clang jobs to compile
source files. This is the way we used to compile GPU libraries, but we
have largely moved away from this. The eventual goal is to simple be
able to use `clang` like a normal project. One barrier to this is the
lack of language support for OpenCL in CMake.
This PR uses CMake's custom language support to define a minimal OpenCL
language, largely just a wrapper around `clang -x cl`. This does
nothing, just enables the support. Future PRs will need to untangle the
mess of dependencies.
The 'link+opt' steps that we now do should be able to simply be done as
a custom `llvm-link` and `opt` job on the library, which isn't ideal but
it still better than the current state.
---
libclc/CMakeLists.txt | 2 ++
.../cmake/modules/CMakeCLCCompiler.cmake.in | 13 +++++++
libclc/cmake/modules/CMakeCLCCompilerTest.cl | 1 +
.../cmake/modules/CMakeCLCInformation.cmake | 26 ++++++++++++++
.../modules/CMakeDetermineCLCCompiler.cmake | 18 ++++++++++
.../cmake/modules/CMakeTestCLCCompiler.cmake | 34 +++++++++++++++++++
6 files changed, 94 insertions(+)
create mode 100644 libclc/cmake/modules/CMakeCLCCompiler.cmake.in
create mode 100644 libclc/cmake/modules/CMakeCLCCompilerTest.cl
create mode 100644 libclc/cmake/modules/CMakeCLCInformation.cmake
create mode 100644 libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
create mode 100644 libclc/cmake/modules/CMakeTestCLCCompiler.cmake
diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 1aa67b0745781..6448bb8b3df5b 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -10,6 +10,8 @@ set(CMAKE_CXX_STANDARD 17)
# Add path for custom modules
list( INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" )
+enable_language( CLC )
+
set( LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
set( LIBCLC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
set( LIBCLC_OBJFILE_DIR ${LIBCLC_BINARY_DIR}/obj.libclc.dir )
diff --git a/libclc/cmake/modules/CMakeCLCCompiler.cmake.in b/libclc/cmake/modules/CMakeCLCCompiler.cmake.in
new file mode 100644
index 0000000000000..ac7856ef76c62
--- /dev/null
+++ b/libclc/cmake/modules/CMakeCLCCompiler.cmake.in
@@ -0,0 +1,13 @@
+set(CMAKE_CLC_COMPILER "@CMAKE_CLC_COMPILER@")
+set(CMAKE_CLC_COMPILER_ID "@CMAKE_CLC_COMPILER_ID@")
+
+set(CMAKE_CLC_COMPILER_LOADED 1)
+set(CMAKE_CLC_COMPILER_WORKS @CMAKE_CLC_COMPILER_WORKS@)
+
+set(CMAKE_CLC_COMPILER_ENV_VAR "CLC")
+set(CMAKE_CLC_COMPILER_ID_RUN 1)
+
+set(CMAKE_CLC_SOURCE_FILE_EXTENSIONS cl)
+
+set(CMAKE_CLC_LINKER_PREFERENCE 0)
+set(CMAKE_CLC_LINKER_PREFERENCE_PROPAGATES 0)
diff --git a/libclc/cmake/modules/CMakeCLCCompilerTest.cl b/libclc/cmake/modules/CMakeCLCCompilerTest.cl
new file mode 100644
index 0000000000000..e84fd83ba7edb
--- /dev/null
+++ b/libclc/cmake/modules/CMakeCLCCompilerTest.cl
@@ -0,0 +1 @@
+kernel void __test_clc(global int *out) { *out = 0; }
diff --git a/libclc/cmake/modules/CMakeCLCInformation.cmake b/libclc/cmake/modules/CMakeCLCInformation.cmake
new file mode 100644
index 0000000000000..f92592221f034
--- /dev/null
+++ b/libclc/cmake/modules/CMakeCLCInformation.cmake
@@ -0,0 +1,26 @@
+set(CMAKE_CLC_OUTPUT_EXTENSION .o)
+set(CMAKE_INCLUDE_FLAG_CLC "-I")
+
+set(CMAKE_CLC_DEPFILE_FORMAT gcc)
+set(CMAKE_DEPFILE_FLAGS_CLC "-MD -MT <DEP_TARGET> -MF <DEP_FILE>")
+
+cmake_initialize_per_config_variable(CMAKE_CLC_FLAGS "Flags used by the CLC compiler")
+
+if(NOT CMAKE_CLC_COMPILE_OBJECT)
+ set(CMAKE_CLC_COMPILE_OBJECT
+ "<CMAKE_CLC_COMPILER> -x cl <DEFINES> <INCLUDES> <FLAGS> -c -o <OBJECT> <SOURCE>")
+endif()
+
+if(NOT DEFINED CMAKE_CLC_ARCHIVE_CREATE)
+ set(CMAKE_CLC_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
+endif()
+if(NOT DEFINED CMAKE_CLC_ARCHIVE_APPEND)
+ set(CMAKE_CLC_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
+endif()
+if(NOT DEFINED CMAKE_CLC_ARCHIVE_FINISH)
+ set(CMAKE_CLC_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
+endif()
+
+set(CMAKE_CLC_USE_LINKER_INFORMATION FALSE)
+
+set(CMAKE_CLC_INFORMATION_LOADED 1)
diff --git a/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake b/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
new file mode 100644
index 0000000000000..2138ad85d0059
--- /dev/null
+++ b/libclc/cmake/modules/CMakeDetermineCLCCompiler.cmake
@@ -0,0 +1,18 @@
+if(NOT CMAKE_CLC_COMPILER)
+ if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
+ message(FATAL_ERROR
+ "The CLC language requires the C compiler (CMAKE_C_COMPILER) to be "
+ "Clang, but CMAKE_C_COMPILER_ID is '${CMAKE_C_COMPILER_ID}'.")
+ endif()
+ set(CMAKE_CLC_COMPILER "${CMAKE_C_COMPILER}" CACHE FILEPATH "CLC compiler")
+endif()
+
+mark_as_advanced(CMAKE_CLC_COMPILER)
+
+set(CMAKE_CLC_COMPILER_ID "Clang")
+set(CMAKE_CLC_COMPILER_ID_RUN TRUE)
+
+configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeCLCCompiler.cmake.in
+ ${CMAKE_PLATFORM_INFO_DIR}/CMakeCLCCompiler.cmake @ONLY)
+
+set(CMAKE_CLC_COMPILER_ENV_VAR "CLC")
diff --git a/libclc/cmake/modules/CMakeTestCLCCompiler.cmake b/libclc/cmake/modules/CMakeTestCLCCompiler.cmake
new file mode 100644
index 0000000000000..d18a818f93bfb
--- /dev/null
+++ b/libclc/cmake/modules/CMakeTestCLCCompiler.cmake
@@ -0,0 +1,34 @@
+configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeCLCCompiler.cmake.in
+ ${CMAKE_PLATFORM_INFO_DIR}/CMakeCLCCompiler.cmake @ONLY)
+include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCLCCompiler.cmake)
+
+if(CMAKE_CLC_COMPILER_FORCED)
+ set(CMAKE_CLC_COMPILER_WORKS TRUE)
+ return()
+endif()
+
+set(_test_file "${CMAKE_CURRENT_LIST_DIR}/CMakeCLCCompilerTest.cl")
+set(_test_dir "${CMAKE_PLATFORM_INFO_DIR}/CMakeTmp")
+set(_test_out "${_test_dir}/test_clc.o")
+file(MAKE_DIRECTORY "${_test_dir}")
+
+message(STATUS "Check for working CLC compiler: ${CMAKE_CLC_COMPILER}")
+
+execute_process(
+ COMMAND "${CMAKE_CLC_COMPILER}" -x cl -c -flto
+ -o "${_test_out}" "${_test_file}"
+ RESULT_VARIABLE _clc_result
+ ERROR_VARIABLE _clc_error
+)
+
+if(_clc_result EQUAL 0)
+ set(CMAKE_CLC_COMPILER_WORKS TRUE)
+ message(STATUS "Check for working CLC compiler: ${CMAKE_CLC_COMPILER} - works")
+ file(REMOVE "${_test_out}")
+else()
+ message(FATAL_ERROR
+ "The CLC compiler\n"
+ " ${CMAKE_CLC_COMPILER}\n"
+ "is not able to compile a simple OpenCL test program.\n"
+ "Output:\n${_clc_error}")
+endif()
More information about the cfe-commits
mailing list