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

4 5
# Utility functions
include(cmake/util/Util.cmake)
6 7
include(cmake/util/FindCUDA.cmake)
include(cmake/util/FindVulkan.cmake)
8
include(cmake/util/FindLLVM.cmake)
9
include(cmake/util/FindROCM.cmake)
10
include(cmake/util/FindANTLR.cmake)
11

12 13 14 15 16 17
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()
18 19
endif()

20 21 22 23 24
# 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)
25
tvm_option(USE_OPENCL "Build with OpenCL" OFF)
26
tvm_option(USE_VULKAN "Build with Vulkan" OFF)
27
tvm_option(USE_OPENGL "Build with OpenGL" OFF)
28
tvm_option(USE_METAL "Build with Metal" OFF)
29 30
tvm_option(USE_ROCM "Build with ROCM" OFF)
tvm_option(ROCM_PATH "The path to rocm" /opt/rocm)
31
tvm_option(USE_RPC "Build with RPC" ON)
32
tvm_option(USE_THREADS "Build with thread support" ON)
33
tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" OFF)
34
tvm_option(USE_STACKVM_RUNTIME "Include stackvm into the runtime" OFF)
35
tvm_option(USE_GRAPH_RUNTIME "Build with tiny graph runtime" ON)
36
tvm_option(USE_GRAPH_RUNTIME_DEBUG "Build with tiny graph runtime debug mode" OFF)
37
tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF)
38
tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF)
39
tvm_option(USE_RTTI "Build with RTTI" ON)
40
tvm_option(USE_MSVC_MT "Build with MT" OFF)
41
tvm_option(USE_MICRO "Build with Micro" OFF)
42
tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF)
43
tvm_option(HIDE_PRIVATE_SYMBOLS "Compile with -fvisibility=hidden." OFF)
44

45 46 47 48 49
# 3rdparty libraries
tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include")
tvm_option(DMLC_PATH "Path to DMLC" "3rdparty/dmlc-core/include")
tvm_option(RANG_PATH "Path to RANG" "3rdparty/rang/include")
tvm_option(COMPILER_RT_PATH "Path to COMPILER-RT" "3rdparty/compiler-rt")
50
tvm_option(PICOJSON_PATH "Path to PicoJSON" "3rdparty/picojson")
51

52 53
# Contrib library options
tvm_option(USE_BLAS "The blas library to be linked" none)
54
tvm_option(USE_MKL_PATH "MKL root path when use MKL blas" none)
55
tvm_option(USE_MKLDNN "Build with MKLDNN" OFF)
56
tvm_option(USE_CUDNN "Build with cuDNN" OFF)
57
tvm_option(USE_CUBLAS "Build with cuBLAS" OFF)
58
tvm_option(USE_THRUST "Build with Thrust" OFF)
59 60 61 62 63
tvm_option(USE_MIOPEN "Build with ROCM:MIOpen" OFF)
tvm_option(USE_ROCBLAS "Build with ROCM:RoCBLAS" OFF)
tvm_option(USE_SORT "Build with sort support" OFF)
tvm_option(USE_NNPACK "Build with nnpack support" OFF)
tvm_option(USE_RANDOM "Build with random support" OFF)
64
tvm_option(USE_MICRO_STANDALONE_RUNTIME "Build with micro.standalone_runtime support" OFF)
65
tvm_option(USE_ANTLR "Build with ANTLR for Relay parsing" OFF)
66 67
tvm_option(USE_TFLITE "Build with tflite support" OFF)
tvm_option(USE_TENSORFLOW_PATH "TensorFlow root path when use TFLite" none)
68 69

# include directories
70
include_directories(${CMAKE_INCLUDE_PATH})
71
include_directories("include")
72 73 74 75
include_directories(${DLPACK_PATH})
include_directories(${DMLC_PATH})
include_directories(${RANG_PATH})
include_directories(${COMPILER_RT_PATH})
76
include_directories(${PICOJSON_PATH})
77

78
# initial variables
79
set(TVM_LINKER_LIBS "")
80
set(TVM_RUNTIME_LINKER_LIBS ${CMAKE_DL_LIBS})
81

82
# Generic compilation options
83 84 85 86
if(MSVC)
  add_definitions(-DWIN32_LEAN_AND_MEAN)
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  add_definitions(-D_SCL_SECURE_NO_WARNINGS)
87
  add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE)
88 89
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
90 91 92 93 94 95 96 97 98 99
  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()
100
else(MSVC)
101
  if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
102
    message("Build in Debug mode")
103 104
    set(CMAKE_C_FLAGS "-O0 -g -Wall -fPIC ${CMAKE_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "-O0 -g -Wall -fPIC ${CMAKE_CXX_FLAGS}")
105
    set(CMAKE_CUDA_FLAGS "-O0 -g -Xcompiler=-Wall -Xcompiler=-fPIC ${CMAKE_CUDA_FLAGS}")
106
  else()
107
    set(CMAKE_C_FLAGS "-O2 -Wall -fPIC ${CMAKE_C_FLAGS}")
Yizhi Liu committed
108
    set(CMAKE_CXX_FLAGS "-O2 -Wall -fPIC ${CMAKE_CXX_FLAGS}")
109
    set(CMAKE_CUDA_FLAGS "-O2 -Xcompiler=-Wall -Xcompiler=-fPIC ${CMAKE_CUDA_FLAGS}")
110
    if (HIDE_PRIVATE_SYMBOLS)
111
      message(STATUS "Hide private symbols...")
112 113 114
      set(CMAKE_C_FLAGS "-fvisibility=hidden ${CMAKE_C_FLAGS}")
      set(CMAKE_CXX_FLAGS "-fvisibility=hidden ${CMAKE_CXX_FLAGS}")
    endif(HIDE_PRIVATE_SYMBOLS)
115
  endif ()
116 117 118 119
  if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND
      CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
    set(CMAKE_CXX_FLAGS "-faligned-new ${CMAKE_CXX_FLAGS}")
  endif()
120 121
endif(MSVC)

122
# add source group
123 124
FILE(GLOB_RECURSE GROUP_SOURCE "src/*.cc")
FILE(GLOB_RECURSE GROUP_INCLUDE "src/*.h" "include/*.h")
125
assign_source_group("Source" ${GROUP_SOURCE})
126
assign_source_group("Include" ${GROUP_INCLUDE})
127

128
# Source file lists
129
file(GLOB_RECURSE COMPILER_SRCS
130 131
    src/node/*.cc
    src/ir/*.cc
132
    src/arith/*.cc
133
    src/te/*.cc
134
    src/autotvm/*.cc
135
    src/tir/*.cc
136
    src/driver/*.cc
137
    src/printer/*.cc
138
    src/support/*.cc
abergeron committed
139 140
    )

141
file(GLOB CODEGEN_SRCS
142 143
  src/target/*.cc
  src/target/source/*.cc
144 145 146 147
    )

list(APPEND COMPILER_SRCS ${CODEGEN_SRCS})

148 149
file(GLOB_RECURSE RELAY_OP_SRCS
    src/relay/op/*.cc
150
    )
151
file(GLOB_RECURSE RELAY_PASS_SRCS
152 153 154
    src/relay/analysis/*.cc
    src/relay/transforms/*.cc
    src/relay/quantize/*.cc
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    )
file(GLOB RELAY_BACKEND_SRCS
    src/relay/backend/*.cc
    src/relay/backend/vm/*.cc
    )
file(GLOB_RECURSE RELAY_IR_SRCS
    src/relay/ir/*.cc
    )
file(GLOB_RECURSE RELAY_QNN_SRCS
    src/relay/qnn/*.cc
)
list(APPEND COMPILER_SRCS ${RELAY_OP_SRCS})
list(APPEND COMPILER_SRCS ${RELAY_PASS_SRCS})
list(APPEND COMPILER_SRCS ${RELAY_BACKEND_SRCS})
list(APPEND COMPILER_SRCS ${RELAY_IR_SRCS})
list(APPEND COMPILER_SRCS ${RELAY_QNN_SRCS})

172

173 174 175 176 177
if(USE_VM_PROFILER)
  message(STATUS "Build compiler with Relay VM profiler support...")
  file(GLOB BACKEND_VM_PROFILER_SRCS src/relay/backend/vm/profiler/*.cc)
  list(APPEND COMPILER_SRCS ${BACKEND_VM_PROFILER_SRCS})
endif(USE_VM_PROFILER)
178

179
file(GLOB DATATYPE_SRCS src/target/datatype/*.cc)
180
list(APPEND COMPILER_SRCS ${DATATYPE_SRCS})
181

abergeron committed
182

183 184 185
file(GLOB TOPI_SRCS
    topi/src/*.cc
)
186

187 188 189 190
file(GLOB RUNTIME_SRCS
  src/runtime/*.cc
  src/runtime/vm/*.cc
)
191

192 193 194 195
# Package runtime rules
if(NOT USE_RTTI)
  add_definitions(-DDMLC_ENABLE_RTTI=0)
endif()
196

197 198
list(APPEND RUNTIME_SRCS 3rdparty/bfloat16/bfloat16.cc)

199 200
if(USE_RPC)
  message(STATUS "Build with RPC support...")
201
  file(GLOB RUNTIME_RPC_SRCS src/runtime/rpc/*.cc)
202 203 204
  list(APPEND RUNTIME_SRCS ${RUNTIME_RPC_SRCS})
endif(USE_RPC)

205
file(GLOB STACKVM_RUNTIME_SRCS src/runtime/stackvm/*.cc)
206
file(GLOB STACKVM_CODEGEN_SRCS src/target/stackvm/*.cc)
207 208 209 210 211 212 213 214
list(APPEND COMPILER_SRCS ${STACKVM_CODEGEN_SRCS})
if(USE_STACKVM_RUNTIME)
  message(STATUS "Build with stackvm support in runtime...")
  list(APPEND RUNTIME_SRCS ${STACKVM_RUNTIME_SRCS})
else()
  list(APPEND COMPILER_SRCS ${STACKVM_RUNTIME_SRCS})
endif(USE_STACKVM_RUNTIME)

215 216
if(USE_GRAPH_RUNTIME)
  message(STATUS "Build with Graph runtime support...")
217
  file(GLOB RUNTIME_GRAPH_SRCS src/runtime/graph/*.cc)
218
  list(APPEND RUNTIME_SRCS ${RUNTIME_GRAPH_SRCS})
219

220
  if(USE_GRAPH_RUNTIME_DEBUG)
221 222 223
    message(STATUS "Build with Graph runtime debug support...")
    file(GLOB RUNTIME_GRAPH_DEBUG_SRCS src/runtime/graph/debug/*.cc)
    list(APPEND RUNTIME_SRCS ${RUNTIME_GRAPH_DEBUG_SRCS})
224 225 226 227
    set_source_files_properties(${RUNTIME_GRAPH_SRCS}
      PROPERTIES COMPILE_DEFINITIONS "TVM_GRAPH_RUNTIME_DEBUG")
  endif(USE_GRAPH_RUNTIME_DEBUG)
endif(USE_GRAPH_RUNTIME)
228

229 230 231 232 233 234
if(USE_VM_PROFILER)
  message(STATUS "Build with Relay VM profiler support...")
  file(GLOB RUNTIME_VM_PROFILER_SRCS src/runtime/vm/profiler/*.cc)
  list(APPEND RUNTIME_SRCS ${RUNTIME_VM_PROFILER_SRCS})
endif(USE_VM_PROFILER)

235 236 237 238 239 240
if(USE_EXAMPLE_EXT_RUNTIME)
  message(STATUS "Build with example external runtime...")
  file(GLOB RUNTIME_EXAMPLE_EXTERNAL_SRCS src/runtime/contrib/example_ext_runtime/*.cc)
  list(APPEND RUNTIME_SRCS ${RUNTIME_EXAMPLE_EXTERNAL_SRCS})
endif(USE_EXAMPLE_EXT_RUNTIME)

241
# Module rules
242
include(cmake/modules/VTA.cmake)
243 244 245
include(cmake/modules/CUDA.cmake)
include(cmake/modules/OpenCL.cmake)
include(cmake/modules/OpenGL.cmake)
246
include(cmake/modules/OpenMP.cmake)
247 248 249 250
include(cmake/modules/Vulkan.cmake)
include(cmake/modules/Metal.cmake)
include(cmake/modules/ROCM.cmake)
include(cmake/modules/LLVM.cmake)
251
include(cmake/modules/Micro.cmake)
252
include(cmake/modules/ANTLR.cmake)
253
include(cmake/modules/contrib/BLAS.cmake)
Zhi committed
254 255
include(cmake/modules/contrib/CODEGENC.cmake)
include(cmake/modules/contrib/DNNL.cmake)
256
include(cmake/modules/contrib/Random.cmake)
257
include(cmake/modules/contrib/MicroStandaloneRuntime.cmake)
258 259
include(cmake/modules/contrib/Sort.cmake)
include(cmake/modules/contrib/NNPack.cmake)
260
include(cmake/modules/contrib/HybridDump.cmake)
261
include(cmake/modules/contrib/TFLite.cmake)
262

Yizhi Liu committed
263 264
if(NOT MSVC)
  include(CheckCXXCompilerFlag)
265 266 267
  check_cxx_compiler_flag("-std=c++14" SUPPORT_CXX14)
  message(STATUS "Build with c++14")
  set(CMAKE_CXX_FLAGS "-std=c++14 ${CMAKE_CXX_FLAGS}")
268
  set(CMAKE_CUDA_STANDARD 14)
Yizhi Liu committed
269 270
endif()

271
add_library(tvm SHARED ${COMPILER_SRCS} ${RUNTIME_SRCS})
272
add_library(tvm_topi SHARED ${TOPI_SRCS})
273
add_library(tvm_runtime SHARED ${RUNTIME_SRCS})
274

275

276 277 278
if(USE_RELAY_DEBUG)
  message(STATUS "Building Relay in debug mode...")
  set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "USE_RELAY_DEBUG")
279
  set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "DMLC_LOG_DEBUG")
280
else()
281 282 283
  set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "NDEBUG")
endif(USE_RELAY_DEBUG)

284 285 286 287 288 289 290 291 292 293
if(USE_THREADS)
  message(STATUS "Build with thread support...")
  set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  find_package(Threads REQUIRED)
  target_link_libraries(tvm Threads::Threads)
  target_link_libraries(tvm_topi Threads::Threads)
  target_link_libraries(tvm_runtime Threads::Threads)
endif(USE_THREADS)

294
target_link_libraries(tvm ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS})
295
target_link_libraries(tvm_topi tvm ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS})
abergeron committed
296 297
target_link_libraries(tvm_runtime ${TVM_RUNTIME_LINKER_LIBS})

298 299 300 301 302 303 304 305 306 307
if (HIDE_PRIVATE_SYMBOLS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  set(HIDE_SYMBOLS_LINKER_FLAGS "-Wl,--exclude-libs,ALL")
  # Note: 'target_link_options' with 'PRIVATE' keyword would be cleaner
  # but it's not available until CMake 3.13. Switch to 'target_link_options'
  # once minimum CMake version is bumped up to 3.13 or above.
  target_link_libraries(tvm ${HIDE_SYMBOLS_LINKER_FLAGS})
  target_link_libraries(tvm_topi ${HIDE_SYMBOLS_LINKER_FLAGS})
  target_link_libraries(tvm_runtime ${HIDE_SYMBOLS_LINKER_FLAGS})
endif()

308 309 310 311 312 313 314
# Related headers
target_include_directories(
  tvm
  PUBLIC "topi/include")
target_include_directories(
  tvm_topi
  PUBLIC "topi/include")
315

316

317 318 319
# Tests
set(TEST_EXECS "")
file(GLOB TEST_SRCS tests/cpp/*.cc)
320
find_path(GTEST_INCLUDE_DIR gtest/gtest.h)
321
find_library(GTEST_LIB gtest "$ENV{GTEST_LIB}")
322

323 324 325
# Create the `cpptest` target if we can find GTest.  If not, we create dummy
# targets that give the user an informative error message.
if(GTEST_INCLUDE_DIR AND GTEST_LIB)
326 327 328 329 330
  foreach(__srcpath ${TEST_SRCS})
    get_filename_component(__srcname ${__srcpath} NAME)
    string(REPLACE ".cc" "" __execname ${__srcname})
    add_executable(${__execname} ${__srcpath})
    list(APPEND TEST_EXECS ${__execname})
331 332
    target_include_directories(${__execname} PUBLIC ${GTEST_INCLUDE_DIR})
    target_link_libraries(${__execname} tvm ${GTEST_LIB} pthread dl)
333 334 335 336
    set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_ALL 1)
    set_target_properties(${__execname} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
  endforeach()
  add_custom_target(cpptest DEPENDS ${TEST_EXECS})
337 338 339 340 341 342 343 344
elseif(NOT GTEST_INCLUDE_DIR)
  add_custom_target(cpptest
      COMMAND echo "Missing Google Test headers in include path"
      COMMAND exit 1)
elseif(NOT GTEST_LIB)
  add_custom_target(cpptest
      COMMAND echo "Missing Google Test library"
      COMMAND exit 1)
345
endif()
abergeron committed
346

347 348 349
# Custom targets
add_custom_target(runtime DEPENDS tvm_runtime)

350 351 352
# Installation rules
install(TARGETS tvm DESTINATION lib${LIB_SUFFIX})
install(TARGETS tvm_topi DESTINATION lib${LIB_SUFFIX})
353
install(TARGETS tvm_runtime DESTINATION lib${LIB_SUFFIX})
abergeron committed
354

355 356 357 358 359 360
if (INSTALL_DEV)
  install(
    DIRECTORY "include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
  )
361 362 363 364 365 366
  install(
    DIRECTORY "topi/include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
  )
  install(
367
    DIRECTORY "3rdparty/dlpack/include/." DESTINATION "include"
368 369
    FILES_MATCHING
    PATTERN "*.h"
abergeron committed
370 371
    )
  install(
372 373 374 375
    DIRECTORY "3rdparty/dmlc-core/include/." DESTINATION "include"
    FILES_MATCHING
    PATTERN "*.h"
    )
376 377 378 379 380
else(INSTALL_DEV)
  install(
    DIRECTORY "include/tvm/runtime/." DESTINATION "include/tvm/runtime"
    FILES_MATCHING
    PATTERN "*.h"
abergeron committed
381
    )
382
endif(INSTALL_DEV)
383

384
# More target definitions
385 386 387
if(MSVC)
  target_compile_definitions(tvm PRIVATE -DTVM_EXPORTS)
  target_compile_definitions(tvm_runtime PRIVATE -DTVM_EXPORTS)
388
endif()