Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand Down Expand Up @@ -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.
Expand All @@ -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()

Expand Down
25 changes: 19 additions & 6 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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--" )
Expand Down Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should -c be added to clang command line? It avoids spirv-link step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I'm not sure, to be honest. I tried using -c and it generates a slightly larger file (6900 vs 6792). It seems to take a few fewer seconds to do so, which is an upside. I haven't looked into what exactly it's doing.

Copy link
Contributor

@wenju-he wenju-he Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps spirv-link does optimization, but obviously there is only one file to link. LGTM as the final size is smaller, so this is not meant to be a blocker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, not meant to be

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} )
Expand Down
Loading