Mark de Wever | cbaa359 | 2023-05-24 16:12:32 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.20.0) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 2 | |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 3 | project( libclc VERSION 0.2.0 LANGUAGES CXX C) |
| 4 | |
Tom Stellard | 409f42b | 2023-01-27 18:50:01 | [diff] [blame] | 5 | set(CMAKE_CXX_STANDARD 17) |
| 6 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 7 | # Add path for custom modules |
| 8 | list( INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake/modules" ) |
| 9 | |
| 10 | set( LIBCLC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} ) |
| 11 | set( LIBCLC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} ) |
| 12 | set( LIBCLC_OBJFILE_DIR ${LIBCLC_BINARY_DIR}/obj.libclc.dir ) |
| 13 | |
| 14 | include( AddLibclc ) |
| 15 | |
Jan Vesely | e25db17 | 2019-01-07 20:20:37 | [diff] [blame] | 16 | include( GNUInstallDirs ) |
Jan Vesely | 814fb65 | 2020-02-15 03:29:04 | [diff] [blame] | 17 | set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS |
| 18 | amdgcn-amdhsa/lib/SOURCES; |
| 19 | amdgcn/lib/SOURCES; |
| 20 | amdgcn-mesa3d/lib/SOURCES; |
| 21 | amdgpu/lib/SOURCES; |
Alan Baker | 21427b8 | 2020-12-07 20:54:14 | [diff] [blame] | 22 | clspv/lib/SOURCES; |
Kévin Petit | ec0a880 | 2022-01-05 16:32:08 | [diff] [blame] | 23 | clspv64/lib/SOURCES; |
Jan Vesely | 814fb65 | 2020-02-15 03:29:04 | [diff] [blame] | 24 | generic/lib/SOURCES; |
| 25 | ptx/lib/SOURCES; |
| 26 | ptx-nvidiacl/lib/SOURCES; |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 27 | r600/lib/SOURCES; |
| 28 | spirv/lib/SOURCES; |
| 29 | spirv64/lib/SOURCES |
Jan Vesely | 814fb65 | 2020-02-15 03:29:04 | [diff] [blame] | 30 | ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 31 | |
Fraser Cormack | 338ecfb | 2024-04-04 16:54:54 | [diff] [blame] | 32 | set( LIBCLC_MIN_LLVM 3.9.0 ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 33 | |
| 34 | set( LIBCLC_TARGETS_TO_BUILD "all" |
Fraser Cormack | 93d5119 | 2024-04-16 15:55:53 | [diff] [blame] | 35 | CACHE STRING "Semicolon-separated list of libclc targets to build, or 'all'." ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 36 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 37 | option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 38 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 39 | if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) |
| 40 | # Out-of-tree configuration |
| 41 | set( LIBCLC_STANDALONE_BUILD TRUE ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 42 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 43 | find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}") |
| 44 | include(AddLLVM) |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 45 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 46 | message( STATUS "libclc LLVM version: ${LLVM_PACKAGE_VERSION}" ) |
| 47 | |
| 48 | if( LLVM_PACKAGE_VERSION VERSION_LESS LIBCLC_MIN_LLVM ) |
| 49 | message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" ) |
| 50 | endif() |
| 51 | |
| 52 | # Import required tools as targets |
Fraser Cormack | 0aeeff3 | 2024-04-16 15:46:37 | [diff] [blame] | 53 | if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} ) |
| 54 | foreach( tool IN ITEMS clang llvm-as llvm-link opt ) |
| 55 | find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH ) |
| 56 | add_executable( libclc::${tool} IMPORTED GLOBAL ) |
| 57 | set_target_properties( libclc::${tool} PROPERTIES IMPORTED_LOCATION ${LLVM_TOOL_${tool}} ) |
| 58 | endforeach() |
| 59 | endif() |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 60 | else() |
| 61 | # In-tree configuration |
| 62 | set( LIBCLC_STANDALONE_BUILD FALSE ) |
| 63 | |
| 64 | set( LLVM_PACKAGE_VERSION ${LLVM_VERSION} ) |
| 65 | |
| 66 | # Note that we check this later (for both build types) but we can provide a |
| 67 | # more useful error message when built in-tree. We assume that LLVM tools are |
| 68 | # always available so don't warn here. |
| 69 | if( NOT clang IN_LIST LLVM_ENABLE_PROJECTS ) |
| 70 | message(FATAL_ERROR "Clang is not enabled, but is required to build libclc in-tree") |
| 71 | endif() |
| 72 | |
Fraser Cormack | 0aeeff3 | 2024-04-16 15:46:37 | [diff] [blame] | 73 | if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} ) |
| 74 | foreach( tool IN ITEMS clang llvm-as llvm-link opt ) |
| 75 | add_executable(libclc::${tool} ALIAS ${tool}) |
| 76 | endforeach() |
| 77 | endif() |
| 78 | endif() |
| 79 | |
| 80 | if( EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} ) |
| 81 | message( WARNING "Using custom LLVM tools to build libclc: " |
| 82 | "${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR}, " |
| 83 | " ensure the tools are up to date." ) |
| 84 | # Note - use a differently named variable than LLVM_TOOL_${tool} as above, as |
| 85 | # the variable name is used to cache the result of find_program. If we used |
| 86 | # the same name, a user wouldn't be able to switch a build between default |
| 87 | # and custom tools. |
Fraser Cormack | 9d111286 | 2024-04-16 15:48:59 | [diff] [blame] | 88 | foreach( tool IN ITEMS clang llvm-as llvm-link opt ) |
Fraser Cormack | 0aeeff3 | 2024-04-16 15:46:37 | [diff] [blame] | 89 | find_program( LLVM_CUSTOM_TOOL_${tool} ${tool} |
| 90 | PATHS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH ) |
| 91 | add_executable( libclc::${tool} IMPORTED GLOBAL ) |
| 92 | set_target_properties( libclc::${tool} PROPERTIES |
| 93 | IMPORTED_LOCATION ${LLVM_CUSTOM_TOOL_${tool}} ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 94 | endforeach() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 95 | endif() |
| 96 | |
Fraser Cormack | dc74c69 | 2024-04-16 15:53:18 | [diff] [blame] | 97 | foreach( tool IN ITEMS clang opt llvm-as llvm-link ) |
| 98 | if( NOT TARGET libclc::${tool} ) |
| 99 | message( FATAL_ERROR "libclc toolchain incomplete - missing tool ${tool}!" ) |
| 100 | endif() |
| 101 | endforeach() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 102 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 103 | # llvm-spirv is an optional dependency, used to build spirv-* targets. |
| 104 | find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH ) |
| 105 | |
Fraser Cormack | 06f54e7 | 2024-04-16 14:30:20 | [diff] [blame] | 106 | if( LLVM_SPIRV ) |
| 107 | add_executable( libclc::llvm-spirv IMPORTED GLOBAL ) |
| 108 | set_target_properties( libclc::llvm-spirv PROPERTIES IMPORTED_LOCATION ${LLVM_SPIRV} ) |
| 109 | endif() |
| 110 | |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 111 | # List of all targets. Note that some are added dynamically below. |
| 112 | set( LIBCLC_TARGETS_ALL |
| 113 | amdgcn-- |
| 114 | amdgcn--amdhsa |
| 115 | clspv-- |
| 116 | clspv64-- |
| 117 | r600-- |
| 118 | nvptx-- |
| 119 | nvptx64-- |
| 120 | nvptx--nvidiacl |
| 121 | nvptx64--nvidiacl |
| 122 | ) |
| 123 | |
| 124 | # mesa3d environment is only available since LLVM 4.0 |
Fraser Cormack | 338ecfb | 2024-04-04 16:54:54 | [diff] [blame] | 125 | if( LLVM_PACKAGE_VERSION VERSION_GREATER_EQUAL 4.0.0 ) |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 126 | list( APPEND LIBCLC_TARGETS_ALL amdgcn-mesa-mesa3d ) |
| 127 | endif() |
| 128 | |
| 129 | # spirv-mesa3d and spirv64-mesa3d targets can only be built with the (optional) |
| 130 | # llvm-spirv external tool. |
Fraser Cormack | 06f54e7 | 2024-04-16 14:30:20 | [diff] [blame] | 131 | if( TARGET libclc::llvm-spirv ) |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 132 | list( APPEND LIBCLC_TARGETS_ALL spirv-mesa3d- spirv64-mesa3d- ) |
| 133 | endif() |
| 134 | |
| 135 | if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" ) |
| 136 | set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} ) |
| 137 | endif() |
| 138 | |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 139 | list( SORT LIBCLC_TARGETS_TO_BUILD ) |
| 140 | |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 141 | # Verify that the user hasn't requested mesa3d targets without an available |
| 142 | # llvm-spirv tool. |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 143 | if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD ) |
Fraser Cormack | 06f54e7 | 2024-04-16 14:30:20 | [diff] [blame] | 144 | if( NOT TARGET libclc::llvm-spirv ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 145 | message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" ) |
| 146 | endif() |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 147 | endif() |
| 148 | |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 149 | # Construct LLVM version define |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 150 | set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_VERSION_MAJOR}0${LLVM_VERSION_MINOR}" ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 151 | |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 152 | # This needs to be set before any target that needs it |
Tom Stellard | 8040e3a | 2023-01-27 19:15:54 | [diff] [blame] | 153 | # We need to use LLVM_INCLUDE_DIRS here, because if we are linking to an |
| 154 | # llvm build directory, this includes $src/llvm/include which is where all the |
| 155 | # headers are not $build/include/ which is what LLVM_INCLUDE_DIR is set to. |
| 156 | include_directories( ${LLVM_INCLUDE_DIRS} ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 157 | |
| 158 | # Setup prepare_builtins tools |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 159 | set(LLVM_LINK_COMPONENTS |
| 160 | BitReader |
| 161 | BitWriter |
| 162 | Core |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 163 | IRReader |
Thomas Debesse | 382b56d | 2023-05-22 10:20:59 | [diff] [blame] | 164 | Support |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 165 | ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 166 | if( LIBCLC_STANDALONE_BUILD ) |
Fraser Cormack | 8461d90 | 2024-04-08 10:01:58 | [diff] [blame] | 167 | add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp ) |
| 168 | else() |
| 169 | add_llvm_utility( prepare_builtins utils/prepare-builtins.cpp ) |
| 170 | endif() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 171 | target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} ) |
Tom Stellard | b264787 | 2022-11-23 06:56:55 | [diff] [blame] | 172 | # These were not properly reported in early LLVM and we don't need them |
| 173 | target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 174 | |
| 175 | # Setup arch devices |
| 176 | set( r600--_devices cedar cypress barts cayman ) |
| 177 | set( amdgcn--_devices tahiti ) |
| 178 | set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} ) |
| 179 | set( amdgcn--amdhsa_devices none ) |
Alan Baker | 21427b8 | 2020-12-07 20:54:14 | [diff] [blame] | 180 | set( clspv--_devices none ) |
Kévin Petit | ec0a880 | 2022-01-05 16:32:08 | [diff] [blame] | 181 | set( clspv64--_devices none ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 182 | set( nvptx--_devices none ) |
| 183 | set( nvptx64--_devices none ) |
| 184 | set( nvptx--nvidiacl_devices none ) |
| 185 | set( nvptx64--nvidiacl_devices none ) |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 186 | set( spirv-mesa3d-_devices none ) |
| 187 | set( spirv64-mesa3d-_devices none ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 188 | |
| 189 | # Setup aliases |
| 190 | set( cedar_aliases palm sumo sumo2 redwood juniper ) |
| 191 | set( cypress_aliases hemlock ) |
| 192 | set( barts_aliases turks caicos ) |
| 193 | set( cayman_aliases aruba ) |
| 194 | set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 195 | mullins tonga tongapro iceland carrizo fiji stoney polaris10 polaris11 |
| 196 | gfx602 gfx705 gfx805 |
| 197 | gfx900 gfx902 gfx904 gfx906 gfx908 gfx909 gfx90a gfx90c gfx940 gfx941 gfx942 |
| 198 | gfx1010 gfx1011 gfx1012 gfx1013 |
| 199 | gfx1030 gfx1031 gfx1032 gfx1033 gfx1034 gfx1035 gfx1036 |
| 200 | gfx1100 gfx1101 gfx1102 gfx1103 |
Shilei Tian | 1ca0055 | 2024-06-06 16:16:11 | [diff] [blame] | 201 | gfx1150 gfx1151 gfx1152 |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 202 | gfx1200 gfx1201 |
Zoltán Böszörményi | 262735b | 2024-01-22 08:32:13 | [diff] [blame] | 203 | ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 204 | |
| 205 | # pkg-config file |
| 206 | configure_file( libclc.pc.in libclc.pc @ONLY ) |
John Ericson | ddcc02d | 2021-12-11 01:54:42 | [diff] [blame] | 207 | install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" ) |
| 208 | install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 209 | |
| 210 | if( ENABLE_RUNTIME_SUBNORMAL ) |
Fraser Cormack | 9d111286 | 2024-04-16 15:48:59 | [diff] [blame] | 211 | foreach( file IN ITEMS subnormal_use_default subnormal_disable ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 212 | link_bc( |
| 213 | TARGET ${file} |
| 214 | INPUTS ${PROJECT_SOURCE_DIR}/generic/lib/${file}.ll |
| 215 | ) |
| 216 | install( FILES $<TARGET_PROPERTY:${file},TARGET_FILE> ARCHIVE |
| 217 | DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) |
| 218 | endforeach() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 219 | endif() |
| 220 | |
Aaron Puchert | 1c1a810 | 2020-10-01 20:31:30 | [diff] [blame] | 221 | find_package( Python3 REQUIRED COMPONENTS Interpreter ) |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 222 | file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/generic/lib/gen_convert.py script_loc ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 223 | add_custom_command( |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 224 | OUTPUT convert.cl |
| 225 | COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl |
| 226 | DEPENDS ${script_loc} ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 227 | add_custom_target( "generate_convert.cl" DEPENDS convert.cl ) |
| 228 | |
Romaric Jodin | b6193a2 | 2024-03-14 19:56:34 | [diff] [blame] | 229 | add_custom_command( |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 230 | OUTPUT clspv-convert.cl |
| 231 | COMMAND ${Python3_EXECUTABLE} ${script_loc} --clspv > clspv-convert.cl |
| 232 | DEPENDS ${script_loc} ) |
Romaric Jodin | b6193a2 | 2024-03-14 19:56:34 | [diff] [blame] | 233 | add_custom_target( "clspv-generate_convert.cl" DEPENDS clspv-convert.cl ) |
| 234 | |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 235 | enable_testing() |
| 236 | |
| 237 | foreach( t ${LIBCLC_TARGETS_TO_BUILD} ) |
Fraser Cormack | e251f56 | 2024-03-28 15:11:30 | [diff] [blame] | 238 | message( STATUS "libclc target '${t}' is enabled" ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 239 | string( REPLACE "-" ";" TRIPLE ${t} ) |
| 240 | list( GET TRIPLE 0 ARCH ) |
| 241 | list( GET TRIPLE 1 VENDOR ) |
| 242 | list( GET TRIPLE 2 OS ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 243 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 244 | set( dirs ) |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 245 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 246 | if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND |
| 247 | NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64) |
| 248 | LIST( APPEND dirs generic ) |
| 249 | endif() |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 250 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 251 | if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn ) |
| 252 | list( APPEND dirs amdgpu ) |
| 253 | endif() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 254 | |
Fraser Cormack | 338ecfb | 2024-04-04 16:54:54 | [diff] [blame] | 255 | # nvptx is special |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 256 | if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 ) |
| 257 | set( DARCH ptx ) |
| 258 | else() |
| 259 | set( DARCH ${ARCH} ) |
| 260 | endif() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 261 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 262 | # Enumerate SOURCES* files |
| 263 | set( source_list ) |
| 264 | foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} ) |
| 265 | foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" ) |
| 266 | file( TO_CMAKE_PATH ${l}/lib/${s} file_loc ) |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 267 | file( TO_CMAKE_PATH ${PROJECT_SOURCE_DIR}/${file_loc} loc ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 268 | # Prepend the location to give higher priority to |
| 269 | # specialized implementation |
| 270 | if( EXISTS ${loc} ) |
| 271 | set( source_list ${file_loc} ${source_list} ) |
| 272 | endif() |
| 273 | endforeach() |
| 274 | endforeach() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 275 | |
Fraser Cormack | 338ecfb | 2024-04-04 16:54:54 | [diff] [blame] | 276 | # Add the generated convert.cl here to prevent adding the one listed in |
| 277 | # SOURCES |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 278 | set( objects ) # A "set" of already-added input files |
| 279 | set( rel_files ) # Source directory input files, relative to the root dir |
| 280 | set( gen_files ) # Generated binary input files, relative to the binary dir |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 281 | if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" ) |
| 282 | if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND |
| 283 | NOT ${ARCH} STREQUAL "clspv64" ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 284 | list( APPEND gen_files convert.cl ) |
| 285 | list( APPEND objects convert.cl ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 286 | list( APPEND rel_files generic/lib/subnormal_use_default.ll ) |
| 287 | elseif(${ARCH} STREQUAL "clspv" OR ${ARCH} STREQUAL "clspv64") |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 288 | list( APPEND gen_files clspv-convert.cl ) |
| 289 | list( APPEND objects clspv-convert.cl ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 290 | endif() |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 291 | endif() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 292 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 293 | foreach( l ${source_list} ) |
| 294 | file( READ ${l} file_list ) |
| 295 | string( REPLACE "\n" ";" file_list ${file_list} ) |
| 296 | get_filename_component( dir ${l} DIRECTORY ) |
| 297 | foreach( f ${file_list} ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 298 | # Only add each file once, so that targets can 'specialize' builtins |
| 299 | if( NOT ${f} IN_LIST objects ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 300 | list( APPEND objects ${f} ) |
| 301 | list( APPEND rel_files ${dir}/${f} ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 302 | endif() |
| 303 | endforeach() |
| 304 | endforeach() |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 305 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 306 | foreach( d ${${t}_devices} ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 307 | get_libclc_device_info( |
| 308 | TRIPLE ${t} |
| 309 | DEVICE ${d} |
| 310 | CPU cpu |
| 311 | ARCH_SUFFIX arch_suffix |
| 312 | CLANG_TRIPLE clang_triple |
| 313 | ) |
| 314 | |
| 315 | set( mcpu ) |
| 316 | if( NOT "${cpu}" STREQUAL "" ) |
| 317 | set( mcpu "-mcpu=${cpu}" ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 318 | endif() |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 319 | |
Fraser Cormack | e251f56 | 2024-03-28 15:11:30 | [diff] [blame] | 320 | message( STATUS " device: ${d} ( ${${d}_aliases} )" ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 321 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 322 | if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 323 | set( build_flags -O0 -finline-hint-functions ) |
| 324 | set( opt_flags ) |
| 325 | set( spvflags --spirv-max-version=1.1 ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 326 | elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 327 | set( build_flags "-Wno-unknown-assumption") |
| 328 | set( opt_flags -O3 ) |
| 329 | else() |
| 330 | set( build_flags ) |
| 331 | set( opt_flags -O3 ) |
| 332 | endif() |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 333 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 334 | set( LIBCLC_ARCH_OBJFILE_DIR "${LIBCLC_OBJFILE_DIR}/${arch_suffix}" ) |
| 335 | file( MAKE_DIRECTORY ${LIBCLC_ARCH_OBJFILE_DIR} ) |
| 336 | |
| 337 | string( TOUPPER "CLC_${ARCH}" CLC_TARGET_DEFINE ) |
| 338 | |
| 339 | list( APPEND build_flags |
| 340 | -D__CLC_INTERNAL |
| 341 | -D${CLC_TARGET_DEFINE} |
| 342 | -I${PROJECT_SOURCE_DIR}/generic/include |
| 343 | # FIXME: Fix libclc to not require disabling this noisy warning |
| 344 | -Wno-bitwise-conditional-parentheses |
| 345 | ) |
| 346 | |
| 347 | set( bytecode_files "" ) |
| 348 | foreach( file IN LISTS gen_files rel_files ) |
| 349 | # We need to take each file and produce an absolute input file, as well |
| 350 | # as a unique architecture-specific output file. We deal with a mix of |
| 351 | # different input files, which makes this trickier. |
| 352 | if( ${file} IN_LIST gen_files ) |
| 353 | # Generated files are given just as file names, which we must make |
| 354 | # absolute to the binary directory. |
| 355 | set( input_file ${CMAKE_CURRENT_BINARY_DIR}/${file} ) |
Fraser Cormack | a0f8191 | 2024-04-16 12:12:52 | [diff] [blame] | 356 | set( output_file "${LIBCLC_ARCH_OBJFILE_DIR}/${file}.bc" ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 357 | else() |
| 358 | # Other files are originally relative to each SOURCE file, which are |
| 359 | # then make relative to the libclc root directory. We must normalize |
| 360 | # the path (e.g., ironing out any ".."), then make it relative to the |
| 361 | # root directory again, and use that relative path component for the |
| 362 | # binary path. |
| 363 | get_filename_component( abs_path ${file} ABSOLUTE BASE_DIR ${PROJECT_SOURCE_DIR} ) |
| 364 | file( RELATIVE_PATH root_rel_path ${PROJECT_SOURCE_DIR} ${abs_path} ) |
| 365 | set( input_file ${PROJECT_SOURCE_DIR}/${file} ) |
Fraser Cormack | a0f8191 | 2024-04-16 12:12:52 | [diff] [blame] | 366 | set( output_file "${LIBCLC_ARCH_OBJFILE_DIR}/${root_rel_path}.bc" ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 367 | endif() |
| 368 | |
| 369 | get_filename_component( file_dir ${file} DIRECTORY ) |
| 370 | |
| 371 | compile_to_bc( |
| 372 | TRIPLE ${clang_triple} |
| 373 | INPUT ${input_file} |
| 374 | OUTPUT ${output_file} |
| 375 | EXTRA_OPTS "${mcpu}" -fno-builtin -nostdlib |
| 376 | "${build_flags}" -I${PROJECT_SOURCE_DIR}/${file_dir} |
| 377 | ) |
| 378 | list( APPEND bytecode_files ${output_file} ) |
| 379 | endforeach() |
| 380 | |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 381 | set( builtins_link_lib_tgt builtins.link.${arch_suffix} ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 382 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 383 | link_bc( |
| 384 | TARGET ${builtins_link_lib_tgt} |
| 385 | INPUTS ${bytecode_files} |
| 386 | ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 387 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 388 | set( builtins_link_lib $<TARGET_PROPERTY:${builtins_link_lib_tgt},TARGET_FILE> ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 389 | |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 390 | if( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 391 | set( spv_suffix ${arch_suffix}.spv ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 392 | add_custom_command( OUTPUT ${spv_suffix} |
Fraser Cormack | 06f54e7 | 2024-04-16 14:30:20 | [diff] [blame] | 393 | COMMAND libclc::llvm-spirv ${spvflags} -o ${spv_suffix} ${builtins_link_lib} |
Fraser Cormack | 3d118f9 | 2024-04-16 12:59:59 | [diff] [blame] | 394 | DEPENDS ${builtins_link_lib} |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 395 | ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 396 | add_custom_target( "prepare-${spv_suffix}" ALL DEPENDS "${spv_suffix}" ) |
| 397 | install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix} |
| 398 | DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) |
| 399 | else() |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 400 | set( builtins_opt_lib_tgt builtins.opt.${arch_suffix} ) |
| 401 | |
| 402 | # Add opt target |
| 403 | add_custom_command( OUTPUT ${builtins_opt_lib_tgt}.bc |
| 404 | COMMAND libclc::opt ${opt_flags} -o ${builtins_opt_lib_tgt}.bc |
| 405 | ${builtins_link_lib} |
Fraser Cormack | 3d118f9 | 2024-04-16 12:59:59 | [diff] [blame] | 406 | DEPENDS libclc::opt ${builtins_link_lib} |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 407 | ) |
| 408 | add_custom_target( ${builtins_opt_lib_tgt} |
| 409 | ALL DEPENDS ${builtins_opt_lib_tgt}.bc |
| 410 | ) |
| 411 | set_target_properties( ${builtins_opt_lib_tgt} |
| 412 | PROPERTIES TARGET_FILE ${builtins_opt_lib_tgt}.bc |
| 413 | ) |
| 414 | |
Fraser Cormack | 3d118f9 | 2024-04-16 12:59:59 | [diff] [blame] | 415 | set( builtins_opt_lib $<TARGET_PROPERTY:${builtins_opt_lib_tgt},TARGET_FILE> ) |
| 416 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 417 | # Add prepare target |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 418 | set( obj_suffix ${arch_suffix}.bc ) |
| 419 | add_custom_command( OUTPUT ${obj_suffix} |
Fraser Cormack | 3d118f9 | 2024-04-16 12:59:59 | [diff] [blame] | 420 | COMMAND prepare_builtins -o ${obj_suffix} ${builtins_opt_lib} |
| 421 | DEPENDS ${builtins_opt_lib} prepare_builtins ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 422 | add_custom_target( prepare-${obj_suffix} ALL DEPENDS ${obj_suffix} ) |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 423 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 424 | # nvptx-- targets don't include workitem builtins |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 425 | if( NOT clang_triple MATCHES ".*ptx.*--$" ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 426 | add_test( NAME external-calls-${obj_suffix} |
| 427 | COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR} |
Fraser Cormack | 61efea7 | 2024-04-04 09:12:33 | [diff] [blame] | 428 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 429 | endif() |
Dave Airlie | c37145c | 2020-08-17 20:45:04 | [diff] [blame] | 430 | |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 431 | install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) |
| 432 | foreach( a ${${d}_aliases} ) |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 433 | set( alias_suffix "${a}-${clang_triple}.bc" ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 434 | add_custom_target( ${alias_suffix} ALL |
| 435 | COMMAND ${CMAKE_COMMAND} -E create_symlink ${obj_suffix} ${alias_suffix} |
Fraser Cormack | 72f9881 | 2024-04-11 16:09:07 | [diff] [blame] | 436 | DEPENDS prepare-${obj_suffix} ) |
Fraser Cormack | 9253950 | 2024-03-18 14:37:04 | [diff] [blame] | 437 | install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" ) |
| 438 | endforeach( a ) |
| 439 | endif() |
| 440 | endforeach( d ) |
Jan Vesely | 2ce1d09 | 2018-11-27 16:07:19 | [diff] [blame] | 441 | endforeach( t ) |