llvm-project.cmake 4.0 KB
Newer Older
1 2
# * Find the llvm/mlir libraries This module finds if llvm/mlir is installed, or build
#   llvm/mlir from source. This module sets the following variables.
M
Megvii Engine Team 已提交
3
#
4 5
# MLIR_LLVM_INCLUDE_DIR     - path to the LLVM/MLIR include files MLIR_LLVM_LIBS - path
# to the LLVM/MLIR libraries
M
Megvii Engine Team 已提交
6 7 8
#
# This module define the following functions.
#
9 10
# external_tablegen_library  - created interface library which depends on tablegen
# outputs
M
Megvii Engine Team 已提交
11 12 13 14

include(CMakeParseArguments)

function(external_tablegen_library)
15
  cmake_parse_arguments(_RULE "TESTONLY" "NAME;TBLGEN" "SRCS;INCLUDES;OUTS" ${ARGN})
M
Megvii Engine Team 已提交
16

17 18 19
  if(_RULE_TESTONLY AND NOT MGE_WITH_TEST)
    return()
  endif()
M
Megvii Engine Team 已提交
20

21
  set(_NAME ${_RULE_NAME})
M
Megvii Engine Team 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35
  set(LLVM_TARGET_DEFINITIONS ${_RULE_SRCS})
  set(_INCLUDE_DIRS ${_RULE_INCLUDES})
  list(TRANSFORM _INCLUDE_DIRS PREPEND "-I")
  set(_OUTPUTS)
  while(_RULE_OUTS)
    list(GET _RULE_OUTS 0 _COMMAND)
    list(REMOVE_AT _RULE_OUTS 0)
    list(GET _RULE_OUTS 0 _FILE)
    list(REMOVE_AT _RULE_OUTS 0)
    tablegen(${_RULE_TBLGEN} ${_FILE} ${_COMMAND} ${_INCLUDE_DIRS})
    list(APPEND _OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/${_FILE})
  endwhile()
  add_custom_target(${_NAME}_target DEPENDS ${_OUTPUTS})
M
Megvii Engine Team 已提交
36

37 38
  add_library(${_NAME} INTERFACE)
  add_dependencies(${_NAME} ${_NAME}_target)
M
Megvii Engine Team 已提交
39

40
  target_include_directories(${_NAME} INTERFACE "$<BUILD_INTERFACE:${_RULE_INCLUDES}>")
M
Megvii Engine Team 已提交
41

42
  install(TARGETS ${_NAME} EXPORT ${MGE_EXPORT_TARGETS})
M
Megvii Engine Team 已提交
43 44
endfunction()

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
set(LLVM_LIBS
    LLVMCore
    LLVMSupport
    LLVMX86CodeGen
    LLVMOrcJIT
    LLVMNVPTXCodeGen
    LLVMNVPTXDesc
    LLVMNVPTXInfo)
set(MLIR_CORE_LIBS
    MLIRAnalysis
    MLIRExecutionEngine
    MLIRIR
    MLIRParser
    MLIRPass
    MLIRSideEffectInterfaces
    MLIRTransforms)
set(MLIR_DIALECT_LIBS
    MLIRAsync
    MLIRAVX512
    MLIRGPU
    MLIRLLVMAVX512
    MLIRNVVMIR
    MLIROpenACC
    MLIRPDL
    MLIRPDLInterp
    MLIRQuant
    MLIRROCDLIR
    MLIRSDBM
    MLIRShape
    MLIRSPIRV
    MLIRStandardOpsTransforms
    MLIRTosa)
set(MLIR_CONVERSION_LIBS
    MLIRAffineToStandard MLIRAVX512ToLLVM MLIRGPUToGPURuntimeTransforms
    MLIRGPUToNVVMTransforms MLIRSCFToStandard)
80
set(MLIR_TRANSLATION_LIBS MLIRTargetLLVMIR MLIRTargetNVVMIR)
81 82
set(MLIR_LIBS ${MLIR_CORE_LIBS} ${MLIR_DIALECT_LIBS} ${MLIR_CONVERSION_LIBS}
              ${MLIR_TRANSLATION_LIBS})
83 84
set(MLIR_LLVM_LIBS ${LLVM_LIBS} ${MLIR_LIBS})

M
Megvii Engine Team 已提交
85
function(add_mge_mlir_src_dep llvm_monorepo_path)
86 87 88 89 90 91 92 93 94 95
  set(_CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
  string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
  if(NOT uppercase_CMAKE_BUILD_TYPE MATCHES
     "^(DEBUG|RELEASE|RELWITHDEBINFO|MINSIZEREL)$")
    set(CMAKE_BUILD_TYPE "Debug")
  endif()
  set(_CMAKE_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
  set(BUILD_SHARED_LIBS
      OFF
      CACHE BOOL "" FORCE)
M
Megvii Engine Team 已提交
96

97
  add_subdirectory("${llvm_monorepo_path}/llvm" ${LLVM_BUILD_DIR} EXCLUDE_FROM_ALL)
M
Megvii Engine Team 已提交
98

99 100 101 102 103 104 105 106
  # Reset CMAKE_BUILD_TYPE to its previous setting
  set(CMAKE_BUILD_TYPE
      "${_CMAKE_BUILD_TYPE}"
      CACHE STRING "Build type" FORCE)
  # Reset BUILD_SHARED_LIBS to its previous setting
  set(BUILD_SHARED_LIBS
      ${_CMAKE_BUILD_SHARED_LIBS}
      CACHE BOOL "Build shared libraries" FORCE)
M
Megvii Engine Team 已提交
107 108
endfunction()

109
# llvm build options
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
set(LLVM_INCLUDE_EXAMPLES
    OFF
    CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_TESTS
    OFF
    CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_DOCS
    OFF
    CACHE BOOL "" FORCE)
set(LLVM_ENABLE_BINDINGS
    OFF
    CACHE BOOL "" FORCE)
set(LLVM_INCLUDE_BENCHMARKS
    OFF
    CACHE BOOL "" FORCE)
set(LLVM_ENABLE_RTTI
    ${MGE_ENABLE_RTTI}
    CACHE BOOL "" FORCE)
set(LLVM_TARGETS_TO_BUILD
    "X86;NVPTX;AArch64;ARM"
    CACHE STRING "" FORCE)
set(LLVM_ENABLE_PROJECTS
    "mlir"
    CACHE STRING "" FORCE)
M
Megvii Engine Team 已提交
134 135 136 137 138 139 140 141
set(LLVM_BUILD_DIR ${PROJECT_BINARY_DIR}/third_party/llvm-project/llvm)

add_mge_mlir_src_dep("third_party/llvm-project")

set(MLIR_LLVM_INCLUDE_DIR
    ${PROJECT_SOURCE_DIR}/third_party/llvm-project/llvm/include
    ${PROJECT_BINARY_DIR}/third_party/llvm-project/llvm/include
    ${PROJECT_SOURCE_DIR}/third_party/llvm-project/mlir/include
142
    ${PROJECT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include)
M
Megvii Engine Team 已提交
143
set(MLIR_TABLEGEN_EXE mlir-tblgen)