[libclc] b0313ad - [libclc] Add an option to build SPIR-V targets with the LLVM backend (#151347)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 1 01:48:43 PDT 2025
Author: Fraser Cormack
Date: 2025-08-01T09:48:40+01:00
New Revision: b0313adefa2dd7b40ad3cf48c926045806af5a5f
URL: https://github.com/llvm/llvm-project/commit/b0313adefa2dd7b40ad3cf48c926045806af5a5f
DIFF: https://github.com/llvm/llvm-project/commit/b0313adefa2dd7b40ad3cf48c926045806af5a5f.diff
LOG: [libclc] Add an option to build SPIR-V targets with the LLVM backend (#151347)
This removes the dependency on an external tool to build the SPIR-V
files. It may be of interest to projects such as Mesa.
Note that the option is off by default as using the SPIR-V backend, at
least on my machine, uses a *lot* of memory and the process is often
killed in a parallelized build. It does complete, however.
Fixes #135327.
Added:
Modified:
libclc/CMakeLists.txt
libclc/cmake/modules/AddLibclc.cmake
Removed:
################################################################################
diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 328dfcf81631a..5b95edc75bcdf 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -42,6 +42,10 @@ set( LIBCLC_TARGETS_TO_BUILD "all"
option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF )
+option(
+ LIBCLC_USE_SPIRV_BACKEND "Build SPIR-V targets with the SPIR-V backend." OFF
+)
+
# Top level target used to build all Libclc libraries.
add_custom_target( libclc ALL )
@@ -115,14 +119,17 @@ foreach( tool IN ITEMS clang opt llvm-as llvm-link )
endif()
endforeach()
-# llvm-spirv is an optional dependency, used to build spirv-* targets.
-# It may be provided in-tree or externally.
-if( TARGET llvm-spirv )
- get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
-else()
- find_program( LLVM_SPIRV llvm-spirv HINTS ${LLVM_TOOLS_BINARY_DIR} )
- set( llvm-spirv_exe "${LLVM_SPIRV}" )
- set( llvm-spirv_target )
+if( NOT LIBCLC_USE_SPIRV_BACKEND )
+ # llvm-spirv is an optional dependency, used to build spirv-* targets when
+ # the SPIR-V backend hasn't been requested. It may be provided in-tree or
+ # externally.
+ if( TARGET llvm-spirv )
+ get_host_tool_path( llvm-spirv LLVM_SPIRV llvm-spirv_exe llvm-spirv_target )
+ else()
+ find_program( LLVM_SPIRV llvm-spirv HINTS ${LLVM_TOOLS_BINARY_DIR} )
+ set( llvm-spirv_exe "${LLVM_SPIRV}" )
+ set( llvm-spirv_target )
+ endif()
endif()
# List of all targets. Note that some are added dynamically below.
@@ -138,22 +145,24 @@ set( LIBCLC_TARGETS_ALL
nvptx64--nvidiacl
)
-# mesa3d environment is only available since LLVM 4.0
+# The mesa3d environment is only available since LLVM 4.0
if( LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL 4.0.0 )
list( APPEND LIBCLC_TARGETS_ALL amdgcn-mesa-mesa3d )
endif()
-# spirv-mesa3d and spirv64-mesa3d targets can only be built with the (optional)
-# llvm-spirv external tool.
-if( llvm-spirv_exe )
- list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
+# The spirv-mesa3d and spirv64-mesa3d targets are optional and can be built
+# with either the LLVM SPIR-V backend or the external llvm-spirv tool.
+if( LIBCLC_USE_SPIRV_BACKEND OR llvm-spirv_exe )
+ list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
endif()
# Verify that the user hasn't requested mesa3d targets without an available
# llvm-spirv tool.
-if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
- if( NOT llvm-spirv_exe )
- message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
+if( spirv-mesa3d- IN_LIST LIBCLC_TARGETS_TO_BUILD
+ OR spirv64-mesa3d- IN_LIST LIBCLC_TARGETS_TO_BUILD )
+ if( NOT LIBCLC_USE_SPIRV_BACKEND AND NOT llvm-spirv_exe )
+ message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not "
+ "installed and the SPIR-V backend has not been requested." )
endif()
endif()
diff --git a/libclc/cmake/modules/AddLibclc.cmake b/libclc/cmake/modules/AddLibclc.cmake
index 9b0e5d99b5826..47185586d4094 100644
--- a/libclc/cmake/modules/AddLibclc.cmake
+++ b/libclc/cmake/modules/AddLibclc.cmake
@@ -164,7 +164,9 @@ function(get_libclc_device_info)
list( GET TRIPLE 0 ARCH )
# Some targets don't have a specific device architecture to target
- if( ARG_DEVICE STREQUAL none OR ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
+ if( ARG_DEVICE STREQUAL none
+ OR ((ARCH STREQUAL spirv OR ARCH STREQUAL spirv64)
+ AND NOT LIBCLC_USE_SPIRV_BACKEND) )
set( cpu )
set( arch_suffix "${ARG_TRIPLE}" )
else()
@@ -182,7 +184,11 @@ function(get_libclc_device_info)
# Some libclc targets are not real clang triples: return their canonical
# triples.
- if( ARCH STREQUAL spirv OR ARCH STREQUAL clspv )
+ if( ARCH STREQUAL spirv AND LIBCLC_USE_SPIRV_BACKEND )
+ set( ARG_TRIPLE "spirv32--" )
+ elseif( ARCH STREQUAL spirv64 AND LIBCLC_USE_SPIRV_BACKEND )
+ set( ARG_TRIPLE "spirv64--" )
+ elseif( ARCH STREQUAL spirv OR ARCH STREQUAL clspv )
set( ARG_TRIPLE "spir--" )
elseif( ARCH STREQUAL spirv64 OR ARCH STREQUAL clspv64 )
set( ARG_TRIPLE "spir64--" )
@@ -363,10 +369,17 @@ function(add_libclc_builtin_set)
if( ARG_ARCH STREQUAL spirv OR ARG_ARCH STREQUAL spirv64 )
set( obj_suffix ${ARG_ARCH_SUFFIX}.spv )
set( libclc_builtins_lib ${LIBCLC_OUTPUT_LIBRARY_DIR}/${obj_suffix} )
- add_custom_command( OUTPUT ${libclc_builtins_lib}
- COMMAND ${llvm-spirv_exe} ${spvflags} -o ${libclc_builtins_lib} ${builtins_link_lib}
- DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
- )
+ if ( LIBCLC_USE_SPIRV_BACKEND )
+ add_custom_command( OUTPUT ${libclc_builtins_lib}
+ COMMAND ${clang_exe} --target=${ARG_TRIPLE} -x ir -o ${libclc_builtins_lib} ${builtins_link_lib}
+ DEPENDS ${clang_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
+ )
+ else()
+ add_custom_command( OUTPUT ${libclc_builtins_lib}
+ COMMAND ${llvm-spirv_exe} ${spvflags} -o ${libclc_builtins_lib} ${builtins_link_lib}
+ DEPENDS ${llvm-spirv_target} ${builtins_link_lib} ${builtins_link_lib_tgt}
+ )
+ endif()
else()
# Non-SPIR-V targets add an extra step to optimize the bytecode
set( builtins_opt_lib_tgt builtins.opt.${ARG_ARCH_SUFFIX} )
More information about the cfe-commits
mailing list