Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libclc] Fix a couple of issues preventing in-tree builds #87505

Merged
merged 1 commit into from
Apr 4, 2024
Merged
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
102 changes: 56 additions & 46 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
spirv64/lib/SOURCES
)

# List of all targets
set( LIBCLC_TARGETS_ALL
amdgcn--
amdgcn--amdhsa
clspv--
clspv64--
r600--
nvptx--
nvptx64--
nvptx--nvidiacl
nvptx64--nvidiacl
spirv-mesa3d-
spirv64-mesa3d-
)

set( LIBCLC_MIN_LLVM "3.9.0" )

set( LIBCLC_TARGETS_TO_BUILD "all"
Expand All @@ -51,15 +36,6 @@ if( ${LLVM_PACKAGE_VERSION} VERSION_LESS ${LIBCLC_MIN_LLVM} )
message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
endif()

# mesa3d environment is only available since LLVM 4.0
if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.0" )
set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
endif()

if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
endif()

find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
Expand All @@ -76,15 +52,45 @@ if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
message( FATAL_ERROR "libclc toolchain incomplete!" )
endif()

# List of all targets. Note that some are added dynamically below.
set( LIBCLC_TARGETS_ALL
amdgcn--
amdgcn--amdhsa
clspv--
clspv64--
r600--
nvptx--
nvptx64--
nvptx--nvidiacl
nvptx64--nvidiacl
)

# mesa3d environment is only available since LLVM 4.0
if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.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 )
list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- )
endif()

if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
endif()

list( SORT LIBCLC_TARGETS_TO_BUILD )

# 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 )
message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
endif()
endif()

set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
set( CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake )
set( CMAKE_CLC_COMPILER ${LLVM_CLANG} )
set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
Expand Down Expand Up @@ -113,9 +119,10 @@ set(LLVM_LINK_COMPONENTS
BitReader
BitWriter
Core
IRReader
Support
)
add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp )
target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
# These were not properly reported in early LLVM and we don't need them
target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
Expand Down Expand Up @@ -165,7 +172,7 @@ if( ENABLE_RUNTIME_SUBNORMAL )
endif()

find_package( Python3 REQUIRED COMPONENTS Interpreter )
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
add_custom_command(
OUTPUT convert.cl
COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
Expand Down Expand Up @@ -210,7 +217,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${file_loc} loc )
# Prepend the location to give higher priority to
# specialized implementation
if( EXISTS ${loc} )
Expand Down Expand Up @@ -246,7 +253,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
list( APPEND objects ${f} )
list( APPEND rel_files ${dir}/${f} )
# FIXME: This should really go away
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${dir}/${f} src_loc )
get_filename_component( fdir ${src_loc} DIRECTORY )

set_source_files_properties( ${dir}/${f}
Expand Down Expand Up @@ -288,53 +295,56 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
set( opt_flags -O3 )
endif()

add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
set( builtins_link_lib_tgt builtins.link.${arch_suffix} )
add_library( ${builtins_link_lib_tgt} STATIC ${rel_files} )
# Make sure we depend on the pseudo target to prevent
# multiple invocations
add_dependencies( builtins.link.${arch_suffix} generate_convert.cl )
add_dependencies( builtins.link.${arch_suffix} clspv-generate_convert.cl )
add_dependencies( ${builtins_link_lib_tgt} generate_convert.cl )
add_dependencies( ${builtins_link_lib_tgt} clspv-generate_convert.cl )
# CMake will turn this include into absolute path
target_include_directories( builtins.link.${arch_suffix} PRIVATE
target_include_directories( ${builtins_link_lib_tgt} PRIVATE
"generic/include" )
target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
target_compile_definitions( ${builtins_link_lib_tgt} PRIVATE
"__CLC_INTERNAL" )
string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
target_compile_definitions( ${builtins_link_lib_tgt} PRIVATE
${CLC_TARGET_DEFINE} )
target_compile_options( builtins.link.${arch_suffix} PRIVATE -target
target_compile_options( ${builtins_link_lib_tgt} PRIVATE -target
${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
set_target_properties( builtins.link.${arch_suffix} PROPERTIES
set_target_properties( ${builtins_link_lib_tgt} PROPERTIES
LINKER_LANGUAGE CLC )

set( obj_suffix ${arch_suffix}.bc )
set( builtins_opt_lib_tgt builtins.opt.${obj_suffix} )

# Add opt target
add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
COMMAND ${LLVM_OPT} ${opt_flags} -o "builtins.opt.${obj_suffix}" "builtins.link.${obj_suffix}"
DEPENDS "builtins.link.${arch_suffix}" )
add_custom_command( OUTPUT ${builtins_opt_lib_tgt}
COMMAND ${LLVM_OPT} ${opt_flags} -o ${builtins_opt_lib_tgt}
$<TARGET_FILE:${builtins_link_lib_tgt}>
DEPENDS ${builtins_link_lib_tgt} )
add_custom_target( "opt.${obj_suffix}" ALL
DEPENDS "builtins.opt.${obj_suffix}" )
DEPENDS ${builtins_opt_lib_tgt} )

if( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
set( spv_suffix ${arch_suffix}.spv )
add_custom_command( OUTPUT "${spv_suffix}"
COMMAND ${LLVM_SPIRV} ${spvflags} -o "${spv_suffix}" "builtins.link.${obj_suffix}"
DEPENDS "builtins.link.${arch_suffix}" )
COMMAND ${LLVM_SPIRV} ${spvflags} -o "${spv_suffix}" ${builtins_opt_lib_tgt}
DEPENDS ${builtins_link_lib_tgt} )
add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" )
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
else()
# Add prepare target
add_custom_command( OUTPUT "${obj_suffix}"
COMMAND prepare_builtins -o "${obj_suffix}" "builtins.opt.${obj_suffix}"
DEPENDS "opt.${obj_suffix}" "builtins.opt.${obj_suffix}" prepare_builtins )
COMMAND prepare_builtins -o "${obj_suffix}" ${builtins_opt_lib_tgt}
DEPENDS "opt.${obj_suffix}" ${builtins_opt_lib_tgt} prepare_builtins )
add_custom_target( "prepare-${obj_suffix}" ALL DEPENDS "${obj_suffix}" )

# nvptx-- targets don't include workitem builtins
if( NOT ${t} MATCHES ".*ptx.*--$" )
add_test( NAME external-calls-${obj_suffix}
COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} )
endif()

install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
Expand Down
Loading