CMakeLists.txt 9.81 KB
Newer Older
1
cmake_minimum_required(VERSION 3.7)
2
project(tvm C CXX)
3

4 5 6
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake)
  include(${CMAKE_CURRENT_SOURCE_DIR}/build/private/local_config.cmake)
endif()
7

8 9 10 11 12 13 14 15
include(cmake/Util.cmake)

if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/config.cmake)
  include(${CMAKE_CURRENT_BINARY_DIR}/config.cmake)
else()
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config.cmake)
    include(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake)
  endif()
16 17
endif()

18 19 20 21 22 23
# NOTE: do not modify this file to change option values.
# You can create a config.cmake at build folder
# and add set(OPTION VALUE) to override these build options.
# Alernatively, use cmake -DOPTION=VALUE through command-line.

tvm_option(USE_CUDA "Build with CUDA" OFF)
24
tvm_option(USE_OPENCL "Build with OpenCL" OFF)
25
tvm_option(USE_VULKAN "Build with Vulkan" OFF)
26
tvm_option(USE_OPENGL "Build with OpenGL" OFF)
27
tvm_option(USE_METAL "Build with Metal" OFF)
28 29
tvm_option(USE_RPC "Build with RPC" ON)
tvm_option(USE_GRAPH_RUNTIME "Build with tiny graph runtime" ON)
30
tvm_option(USE_LLVM "Build with LLVM" OFF)
31
tvm_option(USE_RTTI "Build with RTTI" ON)
32
tvm_option(USE_MSVC_MT "Build with MT" OFF)
33
tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF)
34

35 36
tvm_option(USE_CUDNN "Build with cuDNN" OFF)

37 38
include_directories("include")
include_directories("HalideIR/src")
39 40 41
include_directories("dlpack/include")


42 43 44 45 46 47 48 49
set(TVM_LINKER_LIBS "")
set(TVM_RUNTIME_LINKER_LIBS "")

# compile
if(MSVC)
  add_definitions(-DWIN32_LEAN_AND_MEAN)
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  add_definitions(-D_SCL_SECURE_NO_WARNINGS)
50
  add_definitions(-DHalide_SHARED)
51 52
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
53 54 55 56 57 58 59 60 61 62
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj")
  if(USE_MSVC_MT)
    foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
      if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
      endif(${flag_var} MATCHES "/MD")
    endforeach(flag_var)
  endif()
63 64 65
else(MSVC)
  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag("-std=c++11"    SUPPORT_CXX11)
66 67
  set(CMAKE_C_FLAGS "-O3 -Wall -fPIC")
  set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
68 69
endif(MSVC)

70 71 72 73 74
# add source group
FILE(GLOB_RECURSE GROUP_SOURCE "src/*.cc" "HalideIR/src/*.cpp")
FILE(GLOB_RECURSE GROUP_Include "src/*.h" "include/*.h" "HalideIR/src/*.h")
assign_source_group("Source" ${GROUP_SOURCE})
assign_source_group("Include" ${GROUP_Include})
75 76 77 78 79

file(GLOB COMPILER_SRCS
    src/api/*.cc
    src/arithmetic/*.cc
    src/codegen/*.cc
80
    src/codegen/stack_vm/*.cc
81 82
    src/lang/*.cc
    src/pass/*.cc
83
    src/op/*.cc
84 85
    src/schedule/*.cc
)
86 87 88 89
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/topi/include)
file(GLOB TOPI_SRCS
    topi/src/*.cc
)
90 91 92 93
file(GLOB_RECURSE HALIDEIR_SRCS HalideIR/src/*.cpp)
list(APPEND COMPILER_SRCS ${HALIDEIR_SRCS})
file(GLOB RUNTIME_SRCS src/runtime/*.cc)
file(GLOB COMPILER_LLVM_SRCS src/codegen/llvm/*.cc)
94
file(GLOB COMPILER_VULKAN_SRCS src/codegen/spirv/*.cc)
95 96
file(GLOB RUNTIME_CUDA_SRCS src/runtime/cuda/*.cc)
file(GLOB RUNTIME_OPENCL_SRCS src/runtime/opencl/*.cc)
97
file(GLOB RUNTIME_OPENGL_SRCS src/runtime/opengl/*.cc)
98
file(GLOB RUNTIME_VULKAN_SRCS src/runtime/vulkan/*.cc)
99
file(GLOB RUNTIME_METAL_SRCS src/runtime/metal/*.mm)
100
file(GLOB RUNTIME_RPC_SRCS src/runtime/rpc/*.cc)
101
file(GLOB RUNTIME_GRAPH_SRCS src/runtime/graph/*.cc)
102 103

if(USE_CUDA)
104
find_package(CUDA)
105 106 107 108 109 110 111 112 113
# Find CUDA doesn't find all the libraries we need, add the extra ones
find_library(CUDA_CUDA_LIBRARIES cuda
  PATHS ${CUDA_TOOLKIT_ROOT_DIR}
  PATH_SUFFIXES lib lib64 targets/x86_64-linux/lib targets/x86_64-linux/lib/stubs)
find_library(CUDA_NVRTC_LIBRARIES nvrtc
  PATHS ${CUDA_TOOLKIT_ROOT_DIR}
  PATH_SUFFIXES lib lib64 targets/x86_64-linux/lib targets/x86_64-linux/lib/stubs)
  set(CUDA_CUDA_LIBRARY ${CUDA_CUDA_LIBRARIES})

114
  find_package(CUDA QUIET REQUIRED)
115
  message(STATUS "Build with CUDA support")
116 117 118
	include_directories(${CUDA_INCLUDE_DIRS})
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDART_LIBRARY})
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDA_LIBRARY})
119
  list(APPEND RUNTIME_SRCS ${RUNTIME_CUDA_SRCS})
120 121 122 123 124
  if(MSVC)
    find_library(CUDA_NVRTC_LIB nvrtc
      ${CUDA_TOOLKIT_ROOT_DIR}/lib/x64
      ${CUDA_TOOLKIT_ROOT_DIR}/lib/win32)
    list(APPEND TVM_LINKER_LIBS ${CUDA_NVRTC_LIB})
125 126
  else(MSVC)
    find_library(CUDA_NVRTC_LIB nvrtc
127 128
      ${CUDA_TOOLKIT_ROOT_DIR}/lib64
      ${CUDA_TOOLKIT_ROOT_DIR}/lib)
129 130
    list(APPEND TVM_LINKER_LIBS ${CUDA_NVRTC_LIB})
  endif(MSVC)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  if(USE_CUDNN)
    message(STATUS "Build with cuDNN support")
    file(GLOB CONTRIB_CUDNN_SRCS src/contrib/cudnn/*.cc)
    list(APPEND RUNTIME_SRCS ${CONTRIB_CUDNN_SRCS})
    if(MSVC)
      find_library(CUDA_CUDNN_LIB cudnn
        ${CUDA_TOOLKIT_ROOT_DIR}/lib/x64
        ${CUDA_TOOLKIT_ROOT_DIR}/lib/win32)
      list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDNN_LIB})
    else(MSVC)
      find_library(CUDA_CUDNN_LIB cudnn
        ${CUDA_TOOLKIT_ROOT_DIR}/lib64
        ${CUDA_TOOLKIT_ROOT_DIR}/lib)
      list(APPEND TVM_RUNTIME_LINKER_LIBS ${CUDA_CUDNN_LIB})
    endif(MSVC)
  endif(USE_CUDNN)

149
  add_definitions(-DTVM_CUDA_RUNTIME=1)
150 151 152 153 154
else(USE_CUDA)
  add_definitions(-DTVM_CUDA_RUNTIME=0)
endif(USE_CUDA)

if(USE_OPENCL)
155
  find_package(OpenCL QUIET REQUIRED)
156
  message(STATUS "Build with OpenCL support")
157
  include_directories(${OpenCL_INCLUDE_DIRS})
158
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${OpenCL_LIBRARIES})
159
  list(APPEND RUNTIME_SRCS ${RUNTIME_OPENCL_SRCS})
160
  add_definitions(-DTVM_OPENCL_RUNTIME=1)
161 162 163 164
else(USE_OPENCL)
  add_definitions(-DTVM_OPENCL_RUNTIME=0)
endif(USE_OPENCL)

165 166 167 168 169 170 171 172 173 174 175 176
if(USE_OPENGL)
  find_package(OpenGL QUIET REQUIRED)
  find_package(glfw3 QUIET REQUIRED)
  message(STATUS "Build with OpenGL support")
  include_directories(${OPENGL_INCLUDE_DIRS})
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${OpenGL_LIBRARIES} glfw)
  list(APPEND RUNTIME_SRCS ${RUNTIME_OPENGL_SRCS})
  add_definitions(-DTVM_OPENGL_RUNTIME=1)
else(USE_OPENGL)
  add_definitions(-DTVM_OPENGL_RUNTIME=0)
endif(USE_OPENGL)

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
if(USE_VULKAN)
  find_package(Vulkan REQUIRED)
  message(STATUS "Build with VULKAN support")
  include_directories(${Vulkan_INCLUDE_DIRS})
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${Vulkan_LIBRARIES})
  list(APPEND RUNTIME_SRCS ${RUNTIME_VULKAN_SRCS})
  list(APPEND COMPILER_SRCS ${COMPILER_VULKAN_SRCS})
  get_filename_component(VULKAN_LIB_PATH ${Vulkan_LIBRARY} DIRECTORY)
  find_library(SPIRV_TOOLS_LIB SPIRV-Tools
               ${VULKAN_LIB_PATH}/spirv-tools)
  list(APPEND TVM_LINKER_LIBS ${SPIRV_TOOLS_LIB})
  add_definitions(-DTVM_VULKAN_RUNTIME=1)
else(USE_VULKAN)
  add_definitions(-DTVM_VULKAN_RUNTIME=0)
endif(USE_VULKAN)

193
if(USE_METAL)
194
  find_package(OpenCL QUIET REQUIRED)
195 196 197 198 199 200 201 202 203 204
  message(STATUS "Build with Metal support")
  FIND_LIBRARY(METAL_LIB Metal)
  FIND_LIBRARY(FOUNDATION_LIB Foundation)
  list(APPEND TVM_RUNTIME_LINKER_LIBS ${METAL_LIB} ${FOUNDATION_LIB})
  list(APPEND RUNTIME_SRCS ${RUNTIME_METAL_SRCS})
  add_definitions(-DTVM_METAL_RUNTIME=1)
else(USE_METAL)
  add_definitions(-DTVM_METAL_RUNTIME=0)
endif(USE_METAL)

205 206 207 208 209
if(USE_RPC)
  message(STATUS "Build with RPC support...")
  list(APPEND RUNTIME_SRCS ${RUNTIME_RPC_SRCS})
endif(USE_RPC)

210 211 212 213 214
if(USE_GRAPH_RUNTIME)
  message(STATUS "Build with Graph runtime support...")
  list(APPEND RUNTIME_SRCS ${RUNTIME_GRAPH_SRCS})
endif(USE_GRAPH_RUNTIME)

215
if(USE_LLVM)
216
  find_package(LLVM CONFIG REQUIRED)
217 218
  include_directories(${LLVM_INCLUDE_DIRS})
  add_definitions(${LLVM_DEFINITIONS})
219 220 221
  set(TVM_LLVM_VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR})
  message(STATUS "Build with LLVM " ${LLVM_PACKAGE_VERSION})
  message(STATUS "Set TVM_LLVM_VERSION=" ${TVM_LLVM_VERSION})
222
  add_definitions(-DTVM_LLVM_VERSION=${TVM_LLVM_VERSION})
223
  add_definitions(-DDMLC_USE_FOPEN64=0)
224 225 226
  llvm_map_components_to_libnames(LLVM_LIBS all)
  list(REMOVE_ITEM LLVM_LIBS LTO)
  list(APPEND TVM_LINKER_LIBS ${LLVM_LIBS})
227
  list(APPEND COMPILER_SRCS ${COMPILER_LLVM_SRCS})
228 229 230 231
  if(NOT MSVC)
    set_property(SOURCE ${COMPILER_LLVM_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS
      "-fno-rtti -DDMLC_ENABLE_RTTI=0")
  endif()
232 233 234 235 236 237 238 239
endif(USE_LLVM)

if(NOT USE_RTTI)
  add_definitions(-DDMLC_ENABLE_RTTI=0)
endif()

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dmlc-core/CMakeLists.txt)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/dmlc-core/include)
240 241 242 243 244 245 246
  if (INSTALL_DEV)
    install(
      DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/dmlc-core/include/." DESTINATION "include"
      FILES_MATCHING
      PATTERN "*.h"
    )
  endif()
247 248 249 250
elseif(DMLC_CORE_PATH)
  include_directories(${DMLC_CORE_PATH}/include)
endif()

251
list(APPEND RUNTIME_SRCS ${GROUP_Include})
252
add_library(tvm SHARED ${COMPILER_SRCS} ${RUNTIME_SRCS})
253
add_library(tvm_topi SHARED ${TOPI_SRCS})
254 255
add_library(tvm_runtime SHARED ${RUNTIME_SRCS})
target_link_libraries(tvm ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS})
256
target_link_libraries(tvm_topi tvm ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS})
257
target_link_libraries(tvm_runtime  ${TVM_RUNTIME_LINKER_LIBS})
258
install(TARGETS tvm_runtime DESTINATION lib${LIB_SUFFIX})
259
if (INSTALL_DEV)
260
  install(TARGETS tvm DESTINATION lib${LIB_SUFFIX})
261 262 263 264 265
  install(
    DIRECTORY "include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
  )
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  install(
    DIRECTORY "topi/include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
  )
  install(
    DIRECTORY "HalideIR/src/." DESTINATION "include/HalideIR"
    FILES_MATCHING
    PATTERN "*.h"
  )
  install(
    DIRECTORY "dlpack/include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
  )
281 282 283 284 285 286 287
else(INSTALL_DEV)
  install(
    DIRECTORY "include/tvm/runtime/." DESTINATION "include/tvm/runtime"
    FILES_MATCHING
    PATTERN "*.h"
  )
endif(INSTALL_DEV)
288 289 290 291 292 293

if(MSVC)
  target_compile_definitions(tvm PRIVATE -DHalide_EXPORTS)
  target_compile_definitions(tvm_runtime PRIVATE -DHalide_EXPORTS)
  target_compile_definitions(tvm PRIVATE -DTVM_EXPORTS)
  target_compile_definitions(tvm_runtime PRIVATE -DTVM_EXPORTS)
294
endif()