blob: 3ea0be3e6b05c2436dabd09be51a12dd5db515ab [file] [log] [blame]
Jacques Pienaar1273af22019-03-30 05:10:121# MLIR project.
Michał Górny2aa1af92021-02-02 19:09:452
3# Check if MLIR is built as a standalone project.
4if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
5 project(mlir)
John Ericsondf31ff12022-01-19 06:45:076 set(MLIR_STANDALONE_BUILD TRUE)
7endif()
8
9# Must go below project(..)
10include(GNUInstallDirs)
11
12if(MLIR_STANDALONE_BUILD)
Michał Górny2aa1af92021-02-02 19:09:4513 cmake_minimum_required(VERSION 3.13.4)
14
15 find_package(LLVM CONFIG REQUIRED)
16 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
17 include(HandleLLVMOptions)
18 include(AddLLVM)
19 include(TableGen)
20
21 include_directories(${LLVM_INCLUDE_DIRS})
22
23 set(LLVM_MAIN_SRC_DIR ${CMAKE_SOURCE_DIR}/../llvm CACHE PATH
24 "Path to LLVM source tree")
25 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
26 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
27 add_subdirectory(${UNITTEST_DIR} utils/unittest)
28 endif()
29
30 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
31 "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
32 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
33endif()
34
John Ericson07b74982022-06-11 06:11:5935set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
36 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
37mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
38
Ehud Katz3e8de2e2020-04-12 06:29:0739set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
40set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
Jacques Pienaar1273af22019-03-30 05:10:1241
Ehud Katz3e8de2e2020-04-12 06:29:0742set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
43set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
44set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
Michał Górny2aa1af92021-02-02 19:09:4545set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
Jacques Pienaar1273af22019-03-30 05:10:1246
John Ericson44e33652022-01-03 02:25:0647if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
48 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
49endif()
50
51# Make sure that our source directory is on the current cmake module path so
52# that we can include cmake files from this directory.
53list(INSERT CMAKE_MODULE_PATH 0
54 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
55 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
56 )
Jacques Pienaar1273af22019-03-30 05:10:1257
Mehdi Amini5114d552020-01-22 00:44:1758include(AddMLIR)
59
Mehdi Aminic3aed0d2021-09-25 17:24:5560# -BSymbolic is incompatible with TypeID
61if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
62 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
63 " try `-Bsymbolic-functions` instead.")
64endif()
65
Mehdi Amini065047a2020-11-04 00:06:2866# Forbid implicit function declaration: this may lead to subtle bugs and we
67# don't have a reason to support this.
Fangrui Songbf146852020-11-05 02:33:5168check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
69append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
Mehdi Amini065047a2020-11-04 00:06:2870
Mehdi Amini3bed2a72021-12-08 05:14:0171# Forbid mismatch between declaration and definition for class vs struct. This is
72# harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
73# where it creeps into the ABI.
74check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
75append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
76append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
77
Stella Laurenzo7d82d202020-01-01 00:32:4178# Installing the headers and docs needs to depend on generating any public
79# tablegen'd targets.
Stephen Neuendorffer7add6b62020-04-24 23:42:0780# mlir-generic-headers are dialect-independent.
81add_custom_target(mlir-generic-headers)
82set_target_properties(mlir-generic-headers PROPERTIES FOLDER "Misc")
83# mlir-headers may be dialect-dependent.
Stella Laurenzo7d82d202020-01-01 00:32:4184add_custom_target(mlir-headers)
85set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
Stephen Neuendorffer7add6b62020-04-24 23:42:0786add_dependencies(mlir-headers mlir-generic-headers)
Mehdi Amini5e679502019-12-02 17:17:5187add_custom_target(mlir-doc)
88
Stephan Herhutc72c6c32019-06-26 12:16:1189# Build the CUDA conversions and run according tests if the NVPTX backend
90# is available
91if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
Uday Bondhugula9c21ddb2021-05-22 14:25:3892 set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
Stephan Herhutc72c6c32019-06-26 12:16:1193else()
Uday Bondhugula9c21ddb2021-05-22 14:25:3894 set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
Stephan Herhutc72c6c32019-06-26 12:16:1195endif()
Mehdi Amini850cb132020-02-14 09:12:4196# TODO: we should use a config.h file like LLVM does
Uday Bondhugula9c21ddb2021-05-22 14:25:3897add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS})
Stephan Herhutc72c6c32019-06-26 12:16:1198
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:0099# Build the ROCm conversions and run according tests if the AMDGPU backend
100# is available
101if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
Uday Bondhugula9c21ddb2021-05-22 14:25:38102 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00103else()
Uday Bondhugula9c21ddb2021-05-22 14:25:38104 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00105endif()
Uday Bondhugula9c21ddb2021-05-22 14:25:38106add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS})
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00107
Uday Bondhugula587408c2021-05-24 03:20:17108set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
Uday Bondhugula9c21ddb2021-05-22 14:25:38109set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")
110set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the mlir SPIR-V cpu runner")
111set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the mlir Vulkan runner")
Stephan Herhute8b21a72019-07-04 14:49:52112
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34113option(MLIR_INCLUDE_TESTS
114 "Generate build targets for the MLIR unit tests."
115 ${LLVM_INCLUDE_TESTS})
116
117option(MLIR_INCLUDE_INTEGRATION_TESTS
118 "Generate build targets for the MLIR integration tests.")
119
Stella Laurenzoa8975902021-10-19 19:22:56120set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
121 "Installs object files needed for add_mlir_aggregate to work out of \
122 tree. Package maintainers can disable this to exclude these assets if \
123 not desired. Enabling this will result in object files being written \
124 under lib/objects-{CMAKE_BUILD_TYPE}.")
125
Stella Laurenzoc2651702021-11-12 05:18:16126set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
127
Stella Laurenzo722475a2020-07-07 06:05:46128#-------------------------------------------------------------------------------
129# Python Bindings Configuration
130# Requires:
131# The pybind11 library can be found (set with -DPYBIND_DIR=...)
Stella Laurenzo15481bb2020-11-24 17:50:18132# The python executable is correct (set with -DPython3_EXECUTABLE=...)
Stella Laurenzo722475a2020-07-07 06:05:46133#-------------------------------------------------------------------------------
134
Uday Bondhugula587408c2021-05-24 03:20:17135set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
Stella Laurenzo722475a2020-07-07 06:05:46136 "Enables building of Python bindings.")
rkayaithd75ac582022-03-01 22:51:25137set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
138 "Prime the python detection by searching for a full 'Development' \
139 component first (temporary while diagnosing environment specific Python \
140 detection issues)")
Uday Bondhugula9c21ddb2021-05-22 14:25:38141if(MLIR_ENABLE_BINDINGS_PYTHON)
Stella Laurenzof4f8a672020-11-21 01:57:46142 include(MLIRDetectPythonEnv)
Stella Laurenzo3d927222021-10-13 02:32:48143 mlir_configure_python_dev_packages()
Stella Laurenzo722475a2020-07-07 06:05:46144endif()
145
Will Dietz02db3cf2022-03-19 21:53:59146set(CMAKE_INCLUDE_CURRENT_DIR ON)
147
Jacques Pienaar1273af22019-03-30 05:10:12148include_directories( "include")
149include_directories( ${MLIR_INCLUDE_DIR})
150
Isuru Fernando103678d2020-03-14 18:41:12151# Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
152# MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
153# from another directory like tools
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34154add_subdirectory(tools/mlir-tblgen)
Vladislav Vinogradovaca240b2021-01-18 10:54:06155add_subdirectory(tools/mlir-linalg-ods-gen)
River Riddle597fde52022-04-20 05:46:34156add_subdirectory(tools/mlir-pdll)
Isuru Fernando103678d2020-03-14 18:41:12157
Jacques Pienaar1273af22019-03-30 05:10:12158add_subdirectory(include/mlir)
159add_subdirectory(lib)
Alex Zinenko75f239e2020-08-05 12:36:16160# C API needs all dialects for registration, but should be built before tests.
161add_subdirectory(lib/CAPI)
Alex Zinenko14c92072021-10-14 15:18:28162
Alexandre Rames23dc9482020-05-18 16:44:26163if (MLIR_INCLUDE_TESTS)
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34164 add_definitions(-DMLIR_INCLUDE_TESTS)
Mehdi Amini09cfec62021-02-11 01:17:24165 add_custom_target(MLIRUnitTests)
Isuru Fernandoc95c0db2021-02-04 01:59:08166 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h)
Michał Górny2aa1af92021-02-02 19:09:45167 add_subdirectory(unittests)
168 else()
169 message(WARNING "gtest not found, unittests will not be available")
170 endif()
Alexandre Rames23dc9482020-05-18 16:44:26171 add_subdirectory(test)
172endif()
Valentin Churavy7c64f6b2020-02-09 03:27:54173# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
174# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34175add_subdirectory(tools)
Mehdi Amini38b71d62019-04-02 17:02:07176
Uday Bondhugula9c21ddb2021-05-22 14:25:38177if(MLIR_ENABLE_BINDINGS_PYTHON)
Stella Laurenzo9f3f6d72021-04-28 20:04:17178 # Python sources: built extensions come in via lib/Bindings/Python
179 add_subdirectory(python)
180endif()
181
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34182if( LLVM_INCLUDE_EXAMPLES )
Mehdi Amini38b71d62019-04-02 17:02:07183 add_subdirectory(examples)
184endif()
Eric Schweitz88368a12019-11-20 05:04:45185
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34186option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
187 ${LLVM_INCLUDE_DOCS})
Jacques Pienaare298e212020-01-25 17:17:31188if (MLIR_INCLUDE_DOCS)
189 add_subdirectory(docs)
190endif()
191
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34192if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Eric Schweitz88368a12019-11-20 05:04:45193 install(DIRECTORY include/mlir include/mlir-c
John Ericson5ad96992022-01-18 07:03:10194 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Eric Schweitz88368a12019-11-20 05:04:45195 COMPONENT mlir-headers
196 FILES_MATCHING
Kern Handaaab72f82020-01-06 09:01:59197 PATTERN "*.def"
Eric Schweitz88368a12019-11-20 05:04:45198 PATTERN "*.h"
199 PATTERN "*.inc"
Kern Handacde071c42019-12-29 17:04:09200 PATTERN "*.td"
Eric Schweitz88368a12019-11-20 05:04:45201 PATTERN "LICENSE.TXT"
202 )
203
204 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
John Ericson5ad96992022-01-18 07:03:10205 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Eric Schweitz88368a12019-11-20 05:04:45206 COMPONENT mlir-headers
207 FILES_MATCHING
Mehdi Amini5114d552020-01-22 00:44:17208 PATTERN "*.def"
Eric Schweitz88368a12019-11-20 05:04:45209 PATTERN "*.h"
210 PATTERN "*.gen"
211 PATTERN "*.inc"
Mehdi Amini5114d552020-01-22 00:44:17212 PATTERN "*.td"
Eric Schweitz88368a12019-11-20 05:04:45213 PATTERN "CMakeFiles" EXCLUDE
214 PATTERN "config.h" EXCLUDE
215 )
216
217 if (NOT LLVM_ENABLE_IDE)
218 add_llvm_install_targets(install-mlir-headers
219 DEPENDS mlir-headers
220 COMPONENT mlir-headers)
221 endif()
222endif()
Mehdi Amini5114d552020-01-22 00:44:17223
Nathan Lanzaf46ce032022-06-08 02:55:05224# Custom target to install all mlir libraries
225add_custom_target(mlir-libraries)
226set_target_properties(mlir-libraries PROPERTIES FOLDER "Misc")
227
228if (NOT LLVM_ENABLE_IDE)
229 add_llvm_install_targets(install-mlir-libraries
230 DEPENDS mlir-libraries
231 COMPONENT mlir-libraries)
232endif()
233
234get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
235if(MLIR_LIBS)
236 list(REMOVE_DUPLICATES MLIR_LIBS)
237 foreach(lib ${MLIR_LIBS})
238 add_dependencies(mlir-libraries ${lib})
239 if(NOT LLVM_ENABLE_IDE)
240 add_dependencies(install-mlir-libraries install-${lib})
241 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
242 endif()
243 endforeach()
244endif()
245
Mehdi Amini5114d552020-01-22 00:44:17246add_subdirectory(cmake/modules)
Saurabh Jhafa90c9d2022-01-27 21:35:34247
248if (MLIR_ENABLE_PYTHON_BENCHMARKS)
249 add_subdirectory(utils/mbr)
250endif()