[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / libclc / CMakeLists.txt
blob9daef8265c16f263d779db81934018a27fc93732
1 cmake_minimum_required(VERSION 3.20.0)
3 project( libclc VERSION 0.2.0 LANGUAGES CXX C)
5 set(CMAKE_CXX_STANDARD 17)
7 include( GNUInstallDirs )
8 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
9   amdgcn-amdhsa/lib/SOURCES;
10   amdgcn/lib/SOURCES;
11   amdgcn-mesa3d/lib/SOURCES;
12   amdgpu/lib/SOURCES;
13   clspv/lib/SOURCES;
14   clspv64/lib/SOURCES;
15   generic/lib/SOURCES;
16   ptx/lib/SOURCES;
17   ptx-nvidiacl/lib/SOURCES;
18   r600/lib/SOURCES;
19   spirv/lib/SOURCES;
20   spirv64/lib/SOURCES
23 # List of all targets
24 set( LIBCLC_TARGETS_ALL
25   amdgcn--
26   amdgcn--amdhsa
27   clspv--
28   clspv64--
29   r600--
30   nvptx--
31   nvptx64--
32   nvptx--nvidiacl
33   nvptx64--nvidiacl
34   spirv-mesa3d-
35   spirv64-mesa3d-
38 set( LIBCLC_MIN_LLVM "3.9.0" )
40 set( LIBCLC_TARGETS_TO_BUILD "all"
41     CACHE STRING "Semicolon-separated list of targets to build, or 'all'." )
43 option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support."
44 OFF )
46 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
47 include(AddLLVM)
49 message( "LLVM version: ${LLVM_PACKAGE_VERSION}" )
51 if( ${LLVM_PACKAGE_VERSION} VERSION_LESS ${LIBCLC_MIN_LLVM} )
52         message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
53 endif()
55 # mesa3d environment is only available since LLVM 4.0
56 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "3.9.0" )
57         set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
58 endif()
60 if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
61         set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
62 endif()
64 find_program( LLVM_CLANG clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
65 find_program( LLVM_AS llvm-as PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
66 find_program( LLVM_LINK llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
67 find_program( LLVM_OPT opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
68 find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
70 # Print toolchain
71 message( "clang: ${LLVM_CLANG}" )
72 message( "llvm-as: ${LLVM_AS}" )
73 message( "llvm-link: ${LLVM_LINK}" )
74 message( "opt: ${LLVM_OPT}" )
75 message( "llvm-spirv: ${LLVM_SPIRV}" )
76 message( "" )
77 if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
78         message( FATAL_ERROR "toolchain incomplete!" )
79 endif()
81 list( SORT LIBCLC_TARGETS_TO_BUILD )
83 if( "spirv-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD OR "spirv64-mesa3d-" IN_LIST LIBCLC_TARGETS_TO_BUILD )
84         if( NOT LLVM_SPIRV )
85                 message( FATAL_ERROR "SPIR-V targets requested, but spirv-tools is not installed" )
86         endif()
87 endif()
89 set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
90 set( CMAKE_CLC_COMPILER ${LLVM_CLANG} )
91 set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
92 set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
93 set( CMAKE_LLAsm_COMPILER ${LLVM_AS} )
94 set( CMAKE_LLAsm_ARCHIVE ${LLVM_LINK} )
96 # Construct LLVM version define
97 set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_VERSION_MAJOR}0${LLVM_VERSION_MINOR}" )
100 # LLVM 13 enables standard includes by default
101 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "12.99.99" )
102                                 set( CMAKE_LLAsm_FLAGS "${CMAKE_LLAsm_FLAGS} -cl-no-stdinc")
103                                 set( CMAKE_CLC_FLAGS "${CMAKE_CLC_FLAGS} -cl-no-stdinc")
104 endif()
106 enable_language( CLC LLAsm )
107 # This needs to be set before any target that needs it
108 # We need to use LLVM_INCLUDE_DIRS here, because if we are linking to an
109 # llvm build directory, this includes $src/llvm/include which is where all the
110 # headers are not $build/include/ which is what LLVM_INCLUDE_DIR is set to.
111 include_directories( ${LLVM_INCLUDE_DIRS} )
113 # Setup prepare_builtins tools
114 set(LLVM_LINK_COMPONENTS
115   BitReader
116   BitWriter
117   Core
118   Support
120 add_llvm_executable( prepare_builtins utils/prepare-builtins.cpp )
121 target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
122 # These were not properly reported in early LLVM and we don't need them
123 target_compile_options( prepare_builtins PRIVATE -fno-rtti -fno-exceptions )
125 # Setup arch devices
126 set( r600--_devices cedar cypress barts cayman )
127 set( amdgcn--_devices tahiti )
128 set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
129 set( amdgcn--amdhsa_devices none )
130 set( clspv--_devices none )
131 set( clspv64--_devices none )
132 set( nvptx--_devices none )
133 set( nvptx64--_devices none )
134 set( nvptx--nvidiacl_devices none )
135 set( nvptx64--nvidiacl_devices none )
136 set( spirv-mesa3d-_devices none )
137 set( spirv64-mesa3d-_devices none )
139 # Setup aliases
140 set( cedar_aliases palm sumo sumo2 redwood juniper )
141 set( cypress_aliases hemlock )
142 set( barts_aliases turks caicos )
143 set( cayman_aliases aruba )
144 set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
145         mullins tonga iceland carrizo fiji stoney polaris10 polaris11 )
147 # Support for gfx9 was added in LLVM 5.0 (r295554)
148 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "4.99.99" )
149         set( tahiti_aliases ${tahiti_aliases} gfx900 gfx902 )
150 endif()
152 # Support for Vega12 and Vega20 was added in LLVM 7 (r331215)
153 if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "6.99.99" )
154         set( tahiti_aliases ${tahiti_aliases} gfx904 gfx906 )
155 endif()
157 # pkg-config file
158 configure_file( libclc.pc.in libclc.pc @ONLY )
159 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
160 install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" )
162 if( ENABLE_RUNTIME_SUBNORMAL )
163         add_library( subnormal_use_default STATIC
164                 generic/lib/subnormal_use_default.ll )
165         add_library( subnormal_disable STATIC
166                 generic/lib/subnormal_disable.ll )
167         install( TARGETS subnormal_use_default subnormal_disable ARCHIVE
168                 DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
169 endif()
171 find_package( Python3 REQUIRED COMPONENTS Interpreter )
172 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
173 add_custom_command(
174         OUTPUT convert.cl
175         COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
176         DEPENDS ${script_loc} )
177 add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
179 enable_testing()
181 foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
182         message( "BUILDING ${t}" )
183         string( REPLACE "-" ";" TRIPLE  ${t} )
184         list( GET TRIPLE 0 ARCH )
185         list( GET TRIPLE 1 VENDOR )
186         list( GET TRIPLE 2 OS )
188         set( dirs )
190         if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND
191                          NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64)
192                 LIST( APPEND dirs generic )
193         endif()
195         if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
196                 list( APPEND dirs amdgpu )
197         endif()
199         #nvptx is special
200         if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
201                 set( DARCH ptx )
202         else()
203                 set( DARCH ${ARCH} )
204         endif()
206         # Enumerate SOURCES* files
207         set( source_list )
208         foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
209                 foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
210                         file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
211                         file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
212                         # Prepend the location to give higher priority to
213                         # specialized implementation
214                         if( EXISTS ${loc} )
215                                 set( source_list ${file_loc} ${source_list} )
216                         endif()
217                 endforeach()
218         endforeach()
220         # Add the generated convert.cl here to prevent adding
221         # the one listed in SOURCES
222         if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" )
223                 set( rel_files convert.cl )
224                 set( objects convert.cl )
225                 if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND
226                     NOT ${ARCH} STREQUAL "clspv64" )
227                         list( APPEND rel_files generic/lib/subnormal_use_default.ll )
228                 endif()
229         else()
230                 set( rel_files )
231                 set( objects )
232         endif()
234         foreach( l ${source_list} )
235                 file( READ ${l} file_list )
236                 string( REPLACE "\n" ";" file_list ${file_list} )
237                 get_filename_component( dir ${l} DIRECTORY )
238                 foreach( f ${file_list} )
239                         list( FIND objects ${f} found )
240                         if( found EQUAL  -1 )
241                                 list( APPEND objects ${f} )
242                                 list( APPEND rel_files ${dir}/${f} )
243                                 # FIXME: This should really go away
244                                 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
245                                 get_filename_component( fdir ${src_loc} DIRECTORY )
247                                 set_source_files_properties( ${dir}/${f}
248                                         PROPERTIES COMPILE_FLAGS "-I ${fdir}" )
249                         endif()
250                 endforeach()
251         endforeach()
253         foreach( d ${${t}_devices} )
254                 # Some targets don't have a specific GPU to target
255                 if( ${d} STREQUAL "none" OR ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
256                         set( mcpu )
257                         set( arch_suffix "${t}" )
258                 else()
259                         set( mcpu "-mcpu=${d}" )
260                         set( arch_suffix "${d}-${t}" )
261                 endif()
262                 message( "      DEVICE: ${d} ( ${${d}_aliases} )" )
264                 if ( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
265                         if( ${ARCH} STREQUAL "spirv" )
266                                 set( t "spir--" )
267                         else()
268                                 set( t "spir64--" )
269                         endif()
270                         set( build_flags -O0 -finline-hint-functions )
271                         set( opt_flags )
272                         set( spvflags --spirv-max-version=1.1 )
273                 elseif( ${ARCH} STREQUAL "clspv" )
274                         set( t "spir--" )
275                         set( build_flags "-Wno-unknown-assumption")
276                         set( opt_flags -O3 )
277                 elseif( ${ARCH} STREQUAL "clspv64" )
278                         set( t "spir64--" )
279                         set( build_flags "-Wno-unknown-assumption")
280                         set( opt_flags -O3 )
281                 else()
282                         set( build_flags )
283                         set( opt_flags -O3 )
284                 endif()
286                 add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
287                 # Make sure we depend on the pseudo target to prevent
288                 # multiple invocations
289                 add_dependencies( builtins.link.${arch_suffix}
290                         generate_convert.cl )
291                 # CMake will turn this include into absolute path
292                 target_include_directories( builtins.link.${arch_suffix} PRIVATE
293                         "generic/include" )
294                 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
295                         "__CLC_INTERNAL" )
296                 string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
297                 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
298                         ${CLC_TARGET_DEFINE} )
299                 target_compile_options( builtins.link.${arch_suffix} PRIVATE  -target
300                         ${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
301                 set_target_properties( builtins.link.${arch_suffix} PROPERTIES
302                         LINKER_LANGUAGE CLC )
304                 set( obj_suffix ${arch_suffix}.bc )
306                 # Add opt target
307                 add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
308                                     COMMAND ${LLVM_OPT} ${opt_flags} -o
309                                     "builtins.opt.${obj_suffix}"
310                                     "builtins.link.${obj_suffix}"
311                                     DEPENDS "builtins.link.${arch_suffix}" )
312                 add_custom_target( "opt.${obj_suffix}" ALL
313                                    DEPENDS "builtins.opt.${obj_suffix}" )
315                 if( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
316                         set( spv_suffix ${arch_suffix}.spv )
317                         add_custom_command( OUTPUT "${spv_suffix}"
318                                             COMMAND ${LLVM_SPIRV} ${spvflags}
319                                             -o "${spv_suffix}"
320                                             "builtins.link.${obj_suffix}"
321                                             DEPENDS "builtins.link.${arch_suffix}" )
322                         add_custom_target( "prepare-${spv_suffix}" ALL
323                                            DEPENDS "${spv_suffix}" )
324                         install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${spv_suffix}
325                                  DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
326                 else()
328                         # Add prepare target
329                         add_custom_command( OUTPUT "${obj_suffix}"
330                                             COMMAND prepare_builtins -o
331                                             "${obj_suffix}"
332                                             "builtins.opt.${obj_suffix}"
333                                             DEPENDS "opt.${obj_suffix}"
334                                                     "builtins.opt.${obj_suffix}"
335                                                     prepare_builtins )
336                         add_custom_target( "prepare-${obj_suffix}" ALL
337                                            DEPENDS "${obj_suffix}" )
339                         # nvptx-- targets don't include workitem builtins
340                         if( NOT ${t} MATCHES ".*ptx.*--$" )
341                                 add_test( NAME external-calls-${obj_suffix}
342                                           COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} ${LLVM_TOOLS_BINARY_DIR}
343                                           WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
344                         endif()
346                         install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
347                         foreach( a ${${d}_aliases} )
348                                 set( alias_suffix "${a}-${t}.bc" )
349                                 add_custom_target( ${alias_suffix} ALL
350                                                    COMMAND ${CMAKE_COMMAND} -E
351                                                    create_symlink ${obj_suffix}
352                                                    ${alias_suffix}
353                                                    DEPENDS "prepare-${obj_suffix}" )
354                                 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
355                         endforeach( a )
356                 endif()
357         endforeach( d )
358 endforeach( t )