353 lines · plain
1# MLIR project.2cmake_minimum_required(VERSION 3.20.0)3set(LLVM_SUBPROJECT_TITLE "MLIR")4 5if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)6 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)7endif()8include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake9 NO_POLICY_SCOPE)10 11# Check if MLIR is built as a standalone project.12if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)13 project(mlir)14 set(MLIR_STANDALONE_BUILD TRUE)15endif()16 17# Must go below project(..)18include(GNUInstallDirs)19set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")20 21if(MLIR_STANDALONE_BUILD)22 find_package(LLVM CONFIG REQUIRED)23 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})24 25 separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})26 add_definitions(${LLVM_DEFINITIONS_LIST})27 list(APPEND CMAKE_REQUIRED_DEFINITIONS ${LLVM_DEFINITIONS_LIST})28 29 include(HandleLLVMOptions)30 include(AddLLVM)31 include(TableGen)32 33 include_directories(${LLVM_INCLUDE_DIRS})34 35 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY36 "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")37 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")38 39 # These definitions are needed to fill SHLIBDIR in tests.40 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)41 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})42 if(WIN32 OR CYGWIN)43 # DLL platform -- put DLLs into bin.44 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})45 else()46 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})47 endif()48 set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")49endif()50 51set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH52 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")53mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)54 55set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )56set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )57 58set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})59set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})60set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)61set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})62 63# Make sure that our source directory is on the current cmake module path so64# that we can include cmake files from this directory.65list(INSERT CMAKE_MODULE_PATH 066 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"67 "${LLVM_COMMON_CMAKE_UTILS}/Modules"68 )69 70include(AddMLIR)71include(IRDLToCpp)72 73# -BSymbolic is incompatible with TypeID74if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")75 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"76 " try `-Bsymbolic-functions` instead.")77endif()78 79# Forbid implicit function declaration: this may lead to subtle bugs and we80# don't have a reason to support this.81check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)82append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)83 84# Warn on undefined macros. This is often an indication that an include to85# `mlir-config.h` or similar is missing.86check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)87append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)88append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)89 90# Forbid mismatch between declaration and definition for class vs struct. This is91# harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems92# where it creeps into the ABI.93check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)94append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)95append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)96 97if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")98 # Silence a false positive GCC -Wunused-but-set-parameter warning in99 # constexpr cases. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827100 # for details101 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0")102 check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)103 append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" CMAKE_CXX_FLAGS)104 endif()105 # Silence a false positive GCC -Wdeprecated-copy warning in cases where106 # a copy operator is defined through "using" a base class copy operator.107 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")108 check_cxx_compiler_flag("-Wno-deprecated-copy" CXX_SUPPORTS_WNO_DEPRECTAED_COPY)109 append_if(CXX_SUPPORTS_WNO_DEPRECTAED_COPY "-Wno-deprecated-copy" CMAKE_CXX_FLAGS)110 endif()111endif()112 113# Installing the headers and docs needs to depend on generating any public114# tablegen'd targets.115# mlir-generic-headers are dialect-independent.116add_custom_target(mlir-generic-headers)117set_target_properties(mlir-generic-headers PROPERTIES FOLDER "MLIR/Resources")118# mlir-headers may be dialect-dependent.119add_custom_target(mlir-headers)120set_target_properties(mlir-headers PROPERTIES FOLDER "MLIR/Resources")121add_dependencies(mlir-headers mlir-generic-headers)122add_custom_target(mlir-doc)123set_target_properties(mlir-doc PROPERTIES FOLDER "MLIR/Docs")124 125# Only enable execution engine if the native target is available.126if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)127 set(MLIR_ENABLE_EXECUTION_ENGINE 1)128else()129 set(MLIR_ENABLE_EXECUTION_ENGINE 0)130endif()131 132# Build the ROCm conversions and run according tests if the AMDGPU backend133# is available.134if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)135 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)136else()137 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)138endif()139 140# Build the XeVM conversions and run according tests if the SPIRV backend141# is available.142if ("SPIRV" IN_LIST LLVM_TARGETS_TO_BUILD)143 set(MLIR_ENABLE_XEVM_CONVERSIONS 1)144else()145 set(MLIR_ENABLE_XEVM_CONVERSIONS 0)146endif()147 148set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the MLIR CUDA runner")149set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the MLIR ROCm runner")150set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the MLIR SYCL runner")151set(MLIR_ENABLE_LEVELZERO_RUNNER 0 CACHE BOOL "Enable building the MLIR LevelZero runner")152set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the MLIR SPIR-V cpu runner")153set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the MLIR Vulkan runner")154set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL155 "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \156 for compiling PTX to cubin")157 158set(MLIR_ENABLE_PDL_IN_PATTERNMATCH 1 CACHE BOOL "Enable PDL in PatternMatch")159 160option(MLIR_INCLUDE_TESTS161 "Generate build targets for the MLIR unit tests."162 ${LLVM_INCLUDE_TESTS})163 164option(MLIR_INCLUDE_INTEGRATION_TESTS165 "Generate build targets for the MLIR integration tests.")166 167set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL168 "Installs object files needed for add_mlir_aggregate to work out of \169 tree. Package maintainers can disable this to exclude these assets if \170 not desired. Enabling this will result in object files being written \171 under lib/objects-{CMAKE_BUILD_TYPE}.")172 173set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")174 175set(MLIR_LINK_MLIR_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL176 "Link tools against libMLIR.so")177 178configure_file(179 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake180 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)181 182#-------------------------------------------------------------------------------183# Python Bindings Configuration184# Requires:185# The pybind11 library can be found (set with -DPYBIND_DIR=...)186# The python executable is correct (set with -DPython3_EXECUTABLE=...)187# By default, find_package and probing for installed pybind11 is performed.188# Super projects can set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES=ON to189# disable all package setup and control it themselves.190#-------------------------------------------------------------------------------191 192set(MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir"193 CACHE STRING "nanobind domain for MLIR python bindings.")194set(MLIR_PYTHON_PACKAGE_PREFIX "mlir"195 CACHE STRING "Specifies that all MLIR packages are co-located under the196 `MLIR_PYTHON_PACKAGE_PREFIX` top level package (the API has been197 embedded in a relocatable way).")198set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL199 "Enables building of Python bindings.")200set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/mlir_core/mlir" CACHE STRING201 "Prefix under install directory to place python bindings")202set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL203 "Prime the python detection by searching for a full 'Development' \204 component first (temporary while diagnosing environment specific Python \205 detection issues)")206set(MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES 0 CACHE BOOL207 "Performs python dev package configuration sufficient to use all MLIR \208 python features. Super-projects that wish to control their own setup \209 must perform an appropriate find_package of Python3 with \210 'Development.Module' and ensure that find_package(pybind11) is \211 satisfied (and keep up to date as requirements evolve).")212 213set(_mlir_python_stubgen_enabled ON)214# Stubgen doesn't work when cross-compiling (stubgen will run in the host interpreter and then fail215# to find the extension module for the host arch).216# Note: Stubgen requires some extra handling to work properly when sanitizers are enabled,217# so we skip running it in that case now.218if(CMAKE_CROSSCOMPILING OR (NOT LLVM_USE_SANITIZER STREQUAL ""))219 set(_mlir_python_stubgen_enabled OFF)220endif()221 222option(MLIR_PYTHON_STUBGEN_ENABLED223 "Generate Python type stubs for the MLIR Python bindings."224 ${_mlir_python_stubgen_enabled})225 226if(MLIR_ENABLE_BINDINGS_PYTHON)227 include(MLIRDetectPythonEnv)228 # Note that both upstream and downstreams often call this macro. It gates229 # internally on the MLIR_CONFIGURE_PYTHON_DEV_PACKAGES option.230 mlir_configure_python_dev_packages()231endif()232 233set(CMAKE_INCLUDE_CURRENT_DIR ON)234 235include_directories(BEFORE236 "include"237 ${MLIR_INCLUDE_DIR}238 )239 240# Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like241# MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included242# from another directory like tools243add_subdirectory(tools/mlir-irdl-to-cpp)244add_subdirectory(tools/mlir-linalg-ods-gen)245add_subdirectory(tools/mlir-pdll)246add_subdirectory(tools/mlir-tblgen)247add_subdirectory(tools/mlir-src-sharder)248set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")249set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")250set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")251set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")252set(MLIR_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE}" CACHE INTERNAL "")253set(MLIR_SRC_SHARDER_TABLEGEN_TARGET "${MLIR_SRC_SHARDER_TABLEGEN_TARGET}" CACHE INTERNAL "")254 255add_subdirectory(include/mlir)256add_subdirectory(lib)257# C API needs all dialects for registration, but should be built before tests.258add_subdirectory(lib/CAPI)259 260if (MLIR_INCLUDE_TESTS)261 add_definitions(-DMLIR_INCLUDE_TESTS)262 add_custom_target(MLIRUnitTests)263 set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")264 if (TARGET llvm_gtest)265 add_subdirectory(unittests)266 else()267 message(WARNING "gtest not found, unittests will not be available")268 endif()269 add_subdirectory(test)270endif()271# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.272# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.273add_subdirectory(tools)274 275if(MLIR_ENABLE_BINDINGS_PYTHON)276 # Python sources: built extensions come in via lib/Bindings/Python277 add_subdirectory(python)278endif()279 280if( LLVM_INCLUDE_EXAMPLES )281 add_subdirectory(examples)282endif()283 284option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."285 ${LLVM_INCLUDE_DOCS})286if (MLIR_INCLUDE_DOCS)287 add_subdirectory(docs)288endif()289 290if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)291 install(DIRECTORY include/mlir include/mlir-c292 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"293 COMPONENT mlir-headers294 FILES_MATCHING295 PATTERN "*.def"296 PATTERN "*.h"297 PATTERN "*.inc"298 PATTERN "*.td"299 PATTERN "LICENSE.TXT"300 )301 302 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c303 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"304 COMPONENT mlir-headers305 FILES_MATCHING306 PATTERN "*.def"307 PATTERN "*.h"308 PATTERN "*.gen"309 PATTERN "*.inc"310 PATTERN "*.td"311 PATTERN "CMakeFiles" EXCLUDE312 PATTERN "config.h" EXCLUDE313 )314 315 if (NOT LLVM_ENABLE_IDE)316 add_llvm_install_targets(install-mlir-headers317 DEPENDS mlir-headers318 COMPONENT mlir-headers)319 endif()320endif()321 322# Custom target to install all mlir libraries323add_custom_target(mlir-libraries)324set_target_properties(mlir-libraries PROPERTIES FOLDER "MLIR/Metatargets")325 326if (NOT LLVM_ENABLE_IDE)327 add_llvm_install_targets(install-mlir-libraries328 DEPENDS mlir-libraries329 COMPONENT mlir-libraries)330endif()331 332get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)333if(MLIR_LIBS)334 list(REMOVE_DUPLICATES MLIR_LIBS)335 foreach(lib ${MLIR_LIBS})336 add_dependencies(mlir-libraries ${lib})337 if(NOT LLVM_ENABLE_IDE)338 add_dependencies(install-mlir-libraries install-${lib})339 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)340 endif()341 endforeach()342endif()343 344add_subdirectory(cmake/modules)345 346if (MLIR_ENABLE_PYTHON_BENCHMARKS)347 add_subdirectory(utils/mbr)348endif()349 350if(MLIR_STANDALONE_BUILD)351 llvm_distribution_add_targets()352endif()353