Extends the expansion of the LWZtoc pseduo op for AIX.
[llvm-project.git] / libclc / CMakeLists.txt
blob440eab0765095736ecf286e82570aefcf69e0e94
1 cmake_minimum_required( VERSION 3.9.2 )
3 project( libclc VERSION 0.2.0 LANGUAGES CXX )
4 include( GNUInstallDirs )
6 # List of all targets
7 set( LIBCLC_TARGETS_ALL
8   amdgcn--
9   amdgcn--amdhsa
10   r600--
11   nvptx--
12   nvptx64--
13   nvptx--nvidiacl
14   nvptx64--nvidiacl
17 set( LIBCLC_MIN_LLVM "3.9.0" )
19 set( LIBCLC_TARGETS_TO_BUILD "all"
20     CACHE STRING "Semicolon-separated list of targets to build, or 'all'." )
22 option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support."
23 OFF )
25 if( NOT LLVM_CONFIG )
26         find_program( LLVM_CONFIG llvm-config )
27 endif()
28 execute_process( COMMAND ${LLVM_CONFIG} "--version"
29                  OUTPUT_VARIABLE LLVM_VERSION
30                  OUTPUT_STRIP_TRAILING_WHITESPACE )
31 message( "LLVM version: ${LLVM_VERSION}" )
33 if( ${LLVM_VERSION} VERSION_LESS ${LIBCLC_MIN_LLVM} )
34         message( FATAL_ERROR "libclc needs at least LLVM ${LIBCLC_MIN_LLVM}" )
35 endif()
37 # mesa3d environment is only available since LLVM 4.0
38 if( ${LLVM_VERSION} VERSION_GREATER "3.9.0" )
39         set( LIBCLC_TARGETS_ALL ${LIBCLC_TARGETS_ALL} amdgcn-mesa-mesa3d )
40 endif()
42 if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
43         set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
44 endif()
46 list( SORT LIBCLC_TARGETS_TO_BUILD )
48 execute_process( COMMAND ${LLVM_CONFIG} "--system-libs"
49         OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
50         OUTPUT_STRIP_TRAILING_WHITESPACE )
51 execute_process( COMMAND ${LLVM_CONFIG} "--libs" "core" "bitreader" "bitwriter"
52         OUTPUT_VARIABLE LLVM_LIBS
53         OUTPUT_STRIP_TRAILING_WHITESPACE )
54 execute_process( COMMAND ${LLVM_CONFIG} "--libdir"
55         OUTPUT_VARIABLE LLVM_LIBDIR
56         OUTPUT_STRIP_TRAILING_WHITESPACE )
57 execute_process( COMMAND ${LLVM_CONFIG} "--ldflags"
58         OUTPUT_VARIABLE LLVM_LD_FLAGS
59         OUTPUT_STRIP_TRAILING_WHITESPACE )
60 execute_process( COMMAND ${LLVM_CONFIG} "--cxxflags"
61         OUTPUT_VARIABLE LLVM_CXX_FLAGS
62         OUTPUT_STRIP_TRAILING_WHITESPACE )
63 separate_arguments( LLVM_CXX_FLAGS )
64 execute_process( COMMAND ${LLVM_CONFIG} "--bindir"
65         OUTPUT_VARIABLE LLVM_BINDIR
66         OUTPUT_STRIP_TRAILING_WHITESPACE )
68 # These were not properly reported in early LLVM and we don't need them
69 set( LLVM_CXX_FLAGS ${LLVM_CXX_FLAGS} -fno-rtti -fno-exceptions )
71 # Print LLVM variables
72 message( "LLVM system libs: ${LLVM_SYSTEM_LIBS}" )
73 message( "LLVM libs: ${LLVM_LIBS}" )
74 message( "LLVM libdir: ${LLVM_LIBDIR}" )
75 message( "LLVM bindir: ${LLVM_BINDIR}" )
76 message( "LLVM ld flags: ${LLVM_LD_FLAGS}" )
77 message( "LLVM cxx flags: ${LLVM_CXX_FLAGS}" )
78 message( "" )
80 find_program( LLVM_CLANG clang PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
81 find_program( LLVM_AS llvm-as PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
82 find_program( LLVM_LINK llvm-link PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
83 find_program( LLVM_OPT opt PATHS ${LLVM_BINDIR} NO_DEFAULT_PATH )
85 # Print toolchain
86 message( "clang: ${LLVM_CLANG}" )
87 message( "llvm-as: ${LLVM_AS}" )
88 message( "llvm-link: ${LLVM_LINK}" )
89 message( "opt: ${LLVM_OPT}" )
90 message( "" )
91 if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
92         message( FATAL_ERROR "toolchain incomplete!" )
93 endif()
95 set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake )
96 set( CMAKE_CLC_COMPILER ${LLVM_CLANG} )
97 set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
98 set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
99 set( CMAKE_LLAsm_COMPILER ${LLVM_AS} )
100 set( CMAKE_LLAsm_ARCHIVE ${LLVM_LINK} )
101 enable_language( CLC LLAsm )
103 # Construct LLVM version define
104 string( REPLACE "." ";" LLVM_VERSION_LIST ${LLVM_VERSION} )
105 list( GET LLVM_VERSION_LIST 0 LLVM_MAJOR )
106 list( GET LLVM_VERSION_LIST 1 LLVM_MINOR )
107 set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_MAJOR}0${LLVM_MINOR}" )
109 # This needs to be set before any target that needs it
110 link_directories( ${LLVM_LIBDIR} )
112 # Setup prepare_builtins tools
113 add_executable( prepare_builtins utils/prepare-builtins.cpp )
114 target_compile_options( prepare_builtins PRIVATE ${LLVM_CXX_FLAGS} )
115 target_compile_definitions( prepare_builtins PRIVATE ${LLVM_VERSION_DEFINE} )
116 target_link_libraries( prepare_builtins PRIVATE ${LLVM_SYSTEM_LIBS} )
117 target_link_libraries( prepare_builtins PRIVATE ${LLVM_LIBS} )
119 # Setup arch devices
120 set( r600--_devices cedar cypress barts cayman )
121 set( amdgcn--_devices tahiti )
122 set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
123 set( amdgcn--amdhsa_devices none )
124 set( nvptx--_devices none )
125 set( nvptx64--_devices none )
126 set( nvptx--nvidiacl_devices none )
127 set( nvptx64--nvidiacl_devices none )
129 # Setup aliases
130 set( cedar_aliases palm sumo sumo2 redwood juniper )
131 set( cypress_aliases hemlock )
132 set( barts_aliases turks caicos )
133 set( cayman_aliases aruba )
134 set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
135         mullins tonga iceland carrizo fiji stoney polaris10 polaris11 )
137 # Support for gfx9 was added in LLVM 5.0 (r295554)
138 if( ${LLVM_VERSION} VERSION_GREATER "4.99.99" )
139         set( tahiti_aliases ${tahiti_aliases} gfx900 gfx902 )
140 endif()
142 # Support for Vega12 and Vega20 was added in LLVM 7 (r331215)
143 if( ${LLVM_VERSION} VERSION_GREATER "6.99.99" )
144         set( tahiti_aliases ${tahiti_aliases} gfx904 gfx906 )
145 endif()
147 # pkg-config file
148 configure_file( libclc.pc.in libclc.pc @ONLY )
149 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig )
150 install( DIRECTORY generic/include/clc DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
152 if( ENABLE_RUNTIME_SUBNORMAL )
153         add_library( subnormal_use_default STATIC
154                 generic/lib/subnormal_use_default.ll )
155         add_library( subnormal_disable STATIC
156                 generic/lib/subnormal_disable.ll )
157         install( TARGETS subnormal_use_default subnormal_disable ARCHIVE
158                 DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )
159 endif()
161 find_program( PYTHON python )
162 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
163 add_custom_command(
164         OUTPUT convert.cl
165         COMMAND ${PYTHON} ${script_loc} > convert.cl
166         DEPENDS ${script_loc} )
167 add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
169 enable_testing()
171 foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
172         message( "BUILDING ${t}" )
173         string( REPLACE "-" ";" TRIPLE  ${t} )
174         list( GET TRIPLE 0 ARCH )
175         list( GET TRIPLE 1 VENDOR )
176         list( GET TRIPLE 2 OS )
178         set( dirs generic )
179         if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
180                 set( dirs ${dirs} amdgpu )
181         endif()
183         #nvptx is special
184         if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
185                 set( DARCH ptx )
186         else()
187                 set( DARCH ${ARCH} )
188         endif()
190         # Enumerate SOURCES* files
191         set( source_list )
192         foreach( l ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS} )
193                 foreach( s "SOURCES" "SOURCES_${LLVM_MAJOR}.${LLVM_MINOR}" )
194                         file( TO_CMAKE_PATH ${l}/lib/${s} file_loc )
195                         file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${file_loc} loc )
196                         # Prepend the location to give higher priority to
197                         # specialized implementation
198                         if( EXISTS ${loc} )
199                                 set( source_list ${file_loc} ${source_list} )
200                         endif()
201                 endforeach()
202         endforeach()
204         # Add the generated convert.cl here to prevent adding
205         # the one listed in SOURCES
206         set( rel_files convert.cl )
207         set( objects convert.cl )
208         if( NOT ENABLE_RUNTIME_SUBNORMAL )
209                 list( APPEND rel_files generic/lib/subnormal_use_default.ll )
210         endif()
212         foreach( l ${source_list} )
213                 file( READ ${l} file_list )
214                 string( REPLACE "\n" ";" file_list ${file_list} )
215                 get_filename_component( dir ${l} DIRECTORY )
216                 foreach( f ${file_list} )
217                         list( FIND objects ${f} found )
218                         if( found EQUAL  -1 )
219                                 list( APPEND objects ${f} )
220                                 list( APPEND rel_files ${dir}/${f} )
221                                 # FIXME: This should really go away
222                                 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/${dir}/${f} src_loc )
223                                 get_filename_component( fdir ${src_loc} DIRECTORY )
225                                 set_source_files_properties( ${dir}/${f}
226                                         PROPERTIES COMPILE_FLAGS "-I ${fdir}" )
227                         endif()
228                 endforeach()
229         endforeach()
231         foreach( d ${${t}_devices} )
232                 # Some targets don't have a specific GPU to target
233                 if( ${d} STREQUAL "none" )
234                         set( mcpu )
235                         set( arch_suffix "${t}" )
236                 else()
237                         set( mcpu "-mcpu=${d}" )
238                         set( arch_suffix "${d}-${t}" )
239                 endif()
240                 message( "      DEVICE: ${d} ( ${${d}_aliases} )" )
242                 add_library( builtins.link.${arch_suffix} STATIC ${rel_files} )
243                 # Make sure we depend on the pseudo target to prevent
244                 # multiple invocations
245                 add_dependencies( builtins.link.${arch_suffix}
246                         generate_convert.cl )
247                 # CMake will turn this include into absolute path
248                 target_include_directories( builtins.link.${arch_suffix} PRIVATE
249                         "generic/include" )
250                 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
251                         "__CLC_INTERNAL" )
252                 target_compile_options( builtins.link.${arch_suffix} PRIVATE  -target
253                         ${t} ${mcpu} -fno-builtin )
254                 set_target_properties( builtins.link.${arch_suffix} PROPERTIES
255                         LINKER_LANGUAGE CLC )
257                 set( obj_suffix ${arch_suffix}.bc )
259                 # Add opt target
260                 add_custom_command( OUTPUT "builtins.opt.${obj_suffix}"
261                                     COMMAND ${LLVM_OPT} -O3 -o
262                                     "builtins.opt.${obj_suffix}"
263                                     "builtins.link.${obj_suffix}"
264                                     DEPENDS "builtins.link.${arch_suffix}" )
265                 add_custom_target( "opt.${obj_suffix}" ALL
266                                    DEPENDS "builtins.opt.${obj_suffix}" )
268                 # Add prepare target
269                 add_custom_command( OUTPUT "${obj_suffix}"
270                                     COMMAND prepare_builtins -o
271                                     "${obj_suffix}"
272                                     "builtins.opt.${obj_suffix}"
273                                     DEPENDS "opt.${obj_suffix}"
274                                             "builtins.opt.${obj_suffix}"
275                                             prepare_builtins )
276                 add_custom_target( "prepare-${obj_suffix}" ALL
277                                    DEPENDS "${obj_suffix}" )
278                 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )
279                 # nvptx-- targets don't include workitem builtins
280                 if( NOT ${t} MATCHES ".*ptx.*--$" )
281                         add_test( NAME external-calls-${obj_suffix}
282                                   COMMAND ./check_external_calls.sh ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix}
283                                   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
284                         set_tests_properties( external-calls-${obj_suffix}
285                                 PROPERTIES ENVIRONMENT "LLVM_CONFIG=${LLVM_CONFIG}" )
286                 endif()
289                 foreach( a ${${d}_aliases} )
290                         set( alias_suffix "${a}-${t}.bc" )
291                         add_custom_target( ${alias_suffix} ALL
292                                            COMMAND ${CMAKE_COMMAND} -E
293                                            create_symlink ${obj_suffix}
294                                            ${alias_suffix}
295                                            DEPENDS "prepare-${obj_suffix}" )
296                         install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )
297                 endforeach( a )
298         endforeach( d )
299 endforeach( t )