1 cmake_minimum_required( VERSION 3.9.2 )
3 project( libclc VERSION 0.2.0 LANGUAGES CXX )
4 include( GNUInstallDirs )
7 set( LIBCLC_TARGETS_ALL
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."
26 find_program( LLVM_CONFIG llvm-config )
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}" )
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 )
42 if( LIBCLC_TARGETS_TO_BUILD STREQUAL "all" )
43 set( LIBCLC_TARGETS_TO_BUILD ${LIBCLC_TARGETS_ALL} )
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}" )
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 )
86 message( "clang: ${LLVM_CLANG}" )
87 message( "llvm-as: ${LLVM_AS}" )
88 message( "llvm-link: ${LLVM_LINK}" )
89 message( "opt: ${LLVM_OPT}" )
91 if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
92 message( FATAL_ERROR "toolchain incomplete!" )
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} )
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 )
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 )
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 )
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 )
161 find_program( PYTHON python )
162 file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
165 COMMAND ${PYTHON} ${script_loc} > convert.cl
166 DEPENDS ${script_loc} )
167 add_custom_target( "generate_convert.cl" DEPENDS convert.cl )
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 )
179 if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
180 set( dirs ${dirs} amdgpu )
184 if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
190 # Enumerate SOURCES* files
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
199 set( source_list ${file_loc} ${source_list} )
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 )
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 )
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}" )
231 foreach( d ${${t}_devices} )
232 # Some targets don't have a specific GPU to target
233 if( ${d} STREQUAL "none" )
235 set( arch_suffix "${t}" )
237 set( mcpu "-mcpu=${d}" )
238 set( arch_suffix "${d}-${t}" )
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
250 target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
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 )
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}" )
269 add_custom_command( OUTPUT "${obj_suffix}"
270 COMMAND prepare_builtins -o
272 "builtins.opt.${obj_suffix}"
273 DEPENDS "opt.${obj_suffix}"
274 "builtins.opt.${obj_suffix}"
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}" )
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}
295 DEPENDS "prepare-${obj_suffix}" )
296 install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION ${CMAKE_INSTALL_DATADIR}/clc )