summaryrefslogtreecommitdiffstats
path: root/libclc/CMakeLists.txt
blob: 9236f09d366782fc25ee0e96c99765d6120019fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
cmake_minimum_required(VERSION 3.20.0)

project( libclc VERSION 0.2.0 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17)

include( GNUInstallDirs )
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
  amdgcn-amdhsa/lib/SOURCES;
  amdgcn/lib/SOURCES;
  amdgcn-mesa3d/lib/SOURCES;
  amdgpu/lib/SOURCES;
  clspv/lib/SOURCES;
  clspv64/lib/SOURCES;
  generic/lib/SOURCES;
  ptx/lib/SOURCES;
  ptx-nvidiacl/lib/SOURCES;
  r600/lib/SOURCES;
  spirv/lib/SOURCES;
  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"
    CACHE STRING "Semicolon-separated list of targets to build, or 'all'." )

option( ENABLE_RUNTIME_SUBNORMAL "Enable runtime linking of subnormal support." OFF )

find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
include(AddLLVM)

message( STATUS "libclc LLVM version: ${LLVM_PACKAGE_VERSION}" )

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 )
find_program( LLVM_OPT opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )

# Print toolchain
message( STATUS "libclc toolchain - clang: ${LLVM_CLANG}" )
message( STATUS "libclc toolchain - llvm-as: ${LLVM_AS}" )
message( STATUS "libclc toolchain - llvm-link: ${LLVM_LINK}" )
message( STATUS "libclc toolchain - opt: ${LLVM_OPT}" )
message( STATUS "libclc toolchain - llvm-spirv: ${LLVM_SPIRV}" )
if( NOT LLVM_CLANG OR NOT LLVM_OPT OR NOT LLVM_AS OR NOT LLVM_LINK )
  message( FATAL_ERROR "libclc toolchain incomplete!" )
endif()

list( SORT LIBCLC_TARGETS_TO_BUILD )

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_CLC_COMPILER ${LLVM_CLANG} )
set( CMAKE_CLC_ARCHIVE ${LLVM_LINK} )
set( CMAKE_LLAsm_PREPROCESSOR ${LLVM_CLANG} )
set( CMAKE_LLAsm_COMPILER ${LLVM_AS} )
set( CMAKE_LLAsm_ARCHIVE ${LLVM_LINK} )

# Construct LLVM version define
set( LLVM_VERSION_DEFINE "-DHAVE_LLVM=0x${LLVM_VERSION_MAJOR}0${LLVM_VERSION_MINOR}" )


# LLVM 13 enables standard includes by default
if( ${LLVM_PACKAGE_VERSION} VERSION_GREATER "12.99.99" )
  set( CMAKE_LLAsm_FLAGS "${CMAKE_LLAsm_FLAGS} -cl-no-stdinc" )
  set( CMAKE_CLC_FLAGS "${CMAKE_CLC_FLAGS} -cl-no-stdinc" )
endif()

enable_language( CLC LLAsm )
# This needs to be set before any target that needs it
# We need to use LLVM_INCLUDE_DIRS here, because if we are linking to an
# llvm build directory, this includes $src/llvm/include which is where all the
# headers are not $build/include/ which is what LLVM_INCLUDE_DIR is set to.
include_directories( ${LLVM_INCLUDE_DIRS} )

# Setup prepare_builtins tools
set(LLVM_LINK_COMPONENTS
  BitReader
  BitWriter
  Core
  Support
)
add_llvm_executable( 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 )

# Setup arch devices
set( r600--_devices cedar cypress barts cayman )
set( amdgcn--_devices tahiti )
set( amdgcn-mesa-mesa3d_devices ${amdgcn--_devices} )
set( amdgcn--amdhsa_devices none )
set( clspv--_devices none )
set( clspv64--_devices none )
set( nvptx--_devices none )
set( nvptx64--_devices none )
set( nvptx--nvidiacl_devices none )
set( nvptx64--nvidiacl_devices none )
set( spirv-mesa3d-_devices none )
set( spirv64-mesa3d-_devices none )

# Setup aliases
set( cedar_aliases palm sumo sumo2 redwood juniper )
set( cypress_aliases hemlock )
set( barts_aliases turks caicos )
set( cayman_aliases aruba )
set( tahiti_aliases pitcairn verde oland hainan bonaire kabini kaveri hawaii
  mullins tonga tongapro iceland carrizo fiji stoney polaris10 polaris11
  gfx602 gfx705 gfx805
  gfx900 gfx902 gfx904 gfx906 gfx908 gfx909 gfx90a gfx90c gfx940 gfx941 gfx942
  gfx1010 gfx1011 gfx1012 gfx1013
  gfx1030 gfx1031 gfx1032 gfx1033 gfx1034 gfx1035 gfx1036
  gfx1100 gfx1101 gfx1102 gfx1103
  gfx1150 gfx1151
  gfx1200 gfx1201
)

# pkg-config file
configure_file( libclc.pc.in libclc.pc @ONLY )
install( FILES ${CMAKE_CURRENT_BINARY_DIR}/libclc.pc DESTINATION "${CMAKE_INSTALL_DATADIR}/pkgconfig" )
install( DIRECTORY generic/include/clc DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" )

if( ENABLE_RUNTIME_SUBNORMAL )
  add_library( subnormal_use_default STATIC
    generic/lib/subnormal_use_default.ll )
  add_library( subnormal_disable STATIC
    generic/lib/subnormal_disable.ll )
  install( TARGETS subnormal_use_default subnormal_disable ARCHIVE
    DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
endif()

find_package( Python3 REQUIRED COMPONENTS Interpreter )
file( TO_CMAKE_PATH ${CMAKE_SOURCE_DIR}/generic/lib/gen_convert.py script_loc )
add_custom_command(
  OUTPUT convert.cl
  COMMAND ${Python3_EXECUTABLE} ${script_loc} > convert.cl
  DEPENDS ${script_loc} )
add_custom_target( "generate_convert.cl" DEPENDS convert.cl )

add_custom_command(
  OUTPUT clspv-convert.cl
  COMMAND ${Python3_EXECUTABLE} ${script_loc} --clspv > clspv-convert.cl
  DEPENDS ${script_loc} )
add_custom_target( "clspv-generate_convert.cl" DEPENDS clspv-convert.cl )

enable_testing()

foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
  message( STATUS "libclc target '${t}' is enabled" )
  string( REPLACE "-" ";" TRIPLE  ${t} )
  list( GET TRIPLE 0 ARCH )
  list( GET TRIPLE 1 VENDOR )
  list( GET TRIPLE 2 OS )

  set( dirs )

  if ( NOT ${ARCH} STREQUAL spirv AND NOT ${ARCH} STREQUAL spirv64 AND
       NOT ${ARCH} STREQUAL clspv AND NOT ${ARCH} STREQUAL clspv64)
    LIST( APPEND dirs generic )
  endif()

  if( ${ARCH} STREQUAL r600 OR ${ARCH} STREQUAL amdgcn )
    list( APPEND dirs amdgpu )
  endif()

  #nvptx is special
  if( ${ARCH} STREQUAL nvptx OR ${ARCH} STREQUAL nvptx64 )
    set( DARCH ptx )
  else()
    set( DARCH ${ARCH} )
  endif()

  # Enumerate SOURCES* files
  set( source_list )
  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 )
      # Prepend the location to give higher priority to
      # specialized implementation
      if( EXISTS ${loc} )
        set( source_list ${file_loc} ${source_list} )
      endif()
    endforeach()
  endforeach()

  # Add the generated convert.cl here to prevent adding
  # the one listed in SOURCES
  if( NOT ${ARCH} STREQUAL "spirv" AND NOT ${ARCH} STREQUAL "spirv64" )
    if( NOT ENABLE_RUNTIME_SUBNORMAL AND NOT ${ARCH} STREQUAL "clspv" AND
        NOT ${ARCH} STREQUAL "clspv64" )
      set( rel_files convert.cl )
      set( objects convert.cl )
      list( APPEND rel_files generic/lib/subnormal_use_default.ll )
    elseif(${ARCH} STREQUAL "clspv" OR ${ARCH} STREQUAL "clspv64")
      set( rel_files clspv-convert.cl )
      set( objects clspv-convert.cl )
    endif()
  else()
    set( rel_files )
    set( objects )
  endif()

  foreach( l ${source_list} )
    file( READ ${l} file_list )
    string( REPLACE "\n" ";" file_list ${file_list} )
    get_filename_component( dir ${l} DIRECTORY )
    foreach( f ${file_list} )
      list( FIND objects ${f} found )
      if( found EQUAL  -1 )
        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 )
        get_filename_component( fdir ${src_loc} DIRECTORY )

        set_source_files_properties( ${dir}/${f}
          PROPERTIES COMPILE_FLAGS "-I ${fdir}" )
      endif()
    endforeach()
  endforeach()

  foreach( d ${${t}_devices} )
    # Some targets don't have a specific GPU to target
    if( ${d} STREQUAL "none" OR ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
      set( mcpu )
      set( arch_suffix "${t}" )
    else()
      set( mcpu "-mcpu=${d}" )
      set( arch_suffix "${d}-${t}" )
    endif()
    message( STATUS "  device: ${d} ( ${${d}_aliases} )" )

    if ( ${ARCH} STREQUAL "spirv" OR ${ARCH} STREQUAL "spirv64" )
      if( ${ARCH} STREQUAL "spirv" )
        set( t "spir--" )
      else()
        set( t "spir64--" )
      endif()
      set( build_flags -O0 -finline-hint-functions )
      set( opt_flags )
      set( spvflags --spirv-max-version=1.1 )
    elseif( ${ARCH} STREQUAL "clspv" )
      set( t "spir--" )
      set( build_flags "-Wno-unknown-assumption")
      set( opt_flags -O3 )
    elseif( ${ARCH} STREQUAL "clspv64" )
      set( t "spir64--" )
      set( build_flags "-Wno-unknown-assumption")
      set( opt_flags -O3 )
    else()
      set( build_flags )
      set( opt_flags -O3 )
    endif()

    add_library( builtins.link.${arch_suffix} 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 )
    # CMake will turn this include into absolute path
    target_include_directories( builtins.link.${arch_suffix} PRIVATE
      "generic/include" )
    target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
      "__CLC_INTERNAL" )
    string( TOUPPER "-DCLC_${ARCH}" CLC_TARGET_DEFINE )
    target_compile_definitions( builtins.link.${arch_suffix} PRIVATE
      ${CLC_TARGET_DEFINE} )
    target_compile_options( builtins.link.${arch_suffix} PRIVATE  -target
      ${t} ${mcpu} -fno-builtin -nostdlib ${build_flags} )
    set_target_properties( builtins.link.${arch_suffix} PROPERTIES
      LINKER_LANGUAGE CLC )

    set( obj_suffix ${arch_suffix}.bc )

    # 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_target( "opt.${obj_suffix}" ALL
      DEPENDS "builtins.opt.${obj_suffix}" )

    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}" )
      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 )
      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} )
      endif()

      install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${obj_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
      foreach( a ${${d}_aliases} )
        set( alias_suffix "${a}-${t}.bc" )
        add_custom_target( ${alias_suffix} ALL
          COMMAND ${CMAKE_COMMAND} -E create_symlink ${obj_suffix} ${alias_suffix}
          DEPENDS "prepare-${obj_suffix}" )
        install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${alias_suffix} DESTINATION "${CMAKE_INSTALL_DATADIR}/clc" )
      endforeach( a )
    endif()
  endforeach( d )
endforeach( t )