blob: 5c4301af040b47e2dfb51c4067e78679b1295847 [file] [log] [blame]
Jacques Pienaar1273af22019-03-30 05:10:121# MLIR project.
Mark de Wevercbaa3592023-05-24 16:12:322cmake_minimum_required(VERSION 3.20.0)
Michał Górny9dd01a52022-10-24 04:31:373
4if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6endif()
7include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
8 NO_POLICY_SCOPE)
Michał Górny2aa1af92021-02-02 19:09:459
10# Check if MLIR is built as a standalone project.
11if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12 project(mlir)
John Ericsondf31ff12022-01-19 06:45:0713 set(MLIR_STANDALONE_BUILD TRUE)
14endif()
15
16# Must go below project(..)
17include(GNUInstallDirs)
Mehdi Amini0096d172023-02-07 23:54:2718set(CMAKE_CXX_STANDARD 17)
John Ericsondf31ff12022-01-19 06:45:0719
20if(MLIR_STANDALONE_BUILD)
Michał Górny2aa1af92021-02-02 19:09:4521 find_package(LLVM CONFIG REQUIRED)
22 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
23 include(HandleLLVMOptions)
24 include(AddLLVM)
25 include(TableGen)
26
27 include_directories(${LLVM_INCLUDE_DIRS})
28
Tom Stellarda11cd0d2022-11-09 16:51:3429 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
Michał Górny2aa1af92021-02-02 19:09:4530 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
Tom Stellarda11cd0d2022-11-09 16:51:3431 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
Michał Górny2aa1af92021-02-02 19:09:4532 endif()
33
34 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
John Ericsone941b032022-08-19 02:44:4635 "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
Michał Górny2aa1af92021-02-02 19:09:4536 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
Michał Górnyf3713a92022-10-23 17:08:4737
38 set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
Michał Górny2aa1af92021-02-02 19:09:4539endif()
40
John Ericson07b74982022-06-11 06:11:5941set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
42 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
43mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
44
Ehud Katz3e8de2e2020-04-12 06:29:0745set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
46set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
Jacques Pienaar1273af22019-03-30 05:10:1247
Ehud Katz3e8de2e2020-04-12 06:29:0748set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
49set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
50set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
Michał Górny2aa1af92021-02-02 19:09:4551set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
Jacques Pienaar1273af22019-03-30 05:10:1252
John Ericson44e33652022-01-03 02:25:0653# Make sure that our source directory is on the current cmake module path so
54# that we can include cmake files from this directory.
55list(INSERT CMAKE_MODULE_PATH 0
56 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
57 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
58 )
Jacques Pienaar1273af22019-03-30 05:10:1259
Mehdi Amini5114d552020-01-22 00:44:1760include(AddMLIR)
61
Mehdi Aminic3aed0d2021-09-25 17:24:5562# -BSymbolic is incompatible with TypeID
63if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
64 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
65 " try `-Bsymbolic-functions` instead.")
66endif()
67
Mehdi Amini065047a2020-11-04 00:06:2868# Forbid implicit function declaration: this may lead to subtle bugs and we
69# don't have a reason to support this.
Fangrui Songbf146852020-11-05 02:33:5170check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
71append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
Mehdi Amini065047a2020-11-04 00:06:2872
Ingo Müller8406f802024-03-06 13:15:1773# Warn on undefined macros. This is often an indication that an include to
74# `mlir-config.h` or similar is missing.
75check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)
76append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)
77append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)
78
Mehdi Amini3bed2a72021-12-08 05:14:0179# Forbid mismatch between declaration and definition for class vs struct. This is
80# harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
81# where it creeps into the ABI.
82check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
83append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
84append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
85
long.chen80815df2023-10-10 05:20:1886# Silence a false positive GCC -Wunused-but-set-parameter warning in constexpr
87# cases, by marking SelectedCase as used. See
88# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827 for details. The issue is
89# fixed in GCC 10.
90if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.0")
91 check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)
92 append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" CMAKE_CXX_FLAGS)
93endif()
94
Stella Laurenzo7d82d202020-01-01 00:32:4195# Installing the headers and docs needs to depend on generating any public
96# tablegen'd targets.
Stephen Neuendorffer7add6b62020-04-24 23:42:0797# mlir-generic-headers are dialect-independent.
98add_custom_target(mlir-generic-headers)
99set_target_properties(mlir-generic-headers PROPERTIES FOLDER "Misc")
100# mlir-headers may be dialect-dependent.
Stella Laurenzo7d82d202020-01-01 00:32:41101add_custom_target(mlir-headers)
102set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
Stephen Neuendorffer7add6b62020-04-24 23:42:07103add_dependencies(mlir-headers mlir-generic-headers)
Mehdi Amini5e679502019-12-02 17:17:51104add_custom_target(mlir-doc)
105
Nikita Popov57a9bcc2022-08-03 13:28:49106# Only enable execution engine if the native target is available.
107if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
108 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
109else()
110 set(MLIR_ENABLE_EXECUTION_ENGINE 0)
111endif()
112
Stephan Herhutc72c6c32019-06-26 12:16:11113# Build the CUDA conversions and run according tests if the NVPTX backend
114# is available
Fabian Mora211c9752023-08-08 19:15:22115if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
Uday Bondhugula9c21ddb2021-05-22 14:25:38116 set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
Stephan Herhutc72c6c32019-06-26 12:16:11117else()
Uday Bondhugula9c21ddb2021-05-22 14:25:38118 set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
Stephan Herhutc72c6c32019-06-26 12:16:11119endif()
Stephan Herhutc72c6c32019-06-26 12:16:11120
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00121# Build the ROCm conversions and run according tests if the AMDGPU backend
Nikita Popov57a9bcc2022-08-03 13:28:49122# is available.
Krzysztof Drewniak6dc2ab92023-07-13 18:56:35123if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
Uday Bondhugula9c21ddb2021-05-22 14:25:38124 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00125else()
Uday Bondhugula9c21ddb2021-05-22 14:25:38126 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00127endif()
Wen-Heng (Jack) Chung061fb8e2020-05-22 21:25:00128
Uday Bondhugula587408c2021-05-24 03:20:17129set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
Uday Bondhugula9c21ddb2021-05-22 14:25:38130set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")
Nishant Patel7fa19e62023-10-26 17:41:09131set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the mlir Sycl runner")
Uday Bondhugula9c21ddb2021-05-22 14:25:38132set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the mlir SPIR-V cpu runner")
133set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the mlir Vulkan runner")
Fabian Mora211c9752023-08-08 19:15:22134set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
135 "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
136 for compiling PTX to cubin")
Stephan Herhute8b21a72019-07-04 14:49:52137
Jacques Pienaar6ae7f662024-01-04 04:37:19138set(MLIR_ENABLE_PDL_IN_PATTERNMATCH 1 CACHE BOOL "Enable PDL in PatternMatch")
139
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34140option(MLIR_INCLUDE_TESTS
141 "Generate build targets for the MLIR unit tests."
142 ${LLVM_INCLUDE_TESTS})
143
144option(MLIR_INCLUDE_INTEGRATION_TESTS
145 "Generate build targets for the MLIR integration tests.")
146
Stella Laurenzoa8975902021-10-19 19:22:56147set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
148 "Installs object files needed for add_mlir_aggregate to work out of \
149 tree. Package maintainers can disable this to exclude these assets if \
150 not desired. Enabling this will result in object files being written \
151 under lib/objects-{CMAKE_BUILD_TYPE}.")
152
Stella Laurenzoc2651702021-11-12 05:18:16153set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
154
Matthias Springere6d90a02023-05-24 14:14:47155configure_file(
156 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
157 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
158
Stella Laurenzo722475a2020-07-07 06:05:46159#-------------------------------------------------------------------------------
160# Python Bindings Configuration
161# Requires:
162# The pybind11 library can be found (set with -DPYBIND_DIR=...)
Stella Laurenzo15481bb2020-11-24 17:50:18163# The python executable is correct (set with -DPython3_EXECUTABLE=...)
Stella Laurenzo722475a2020-07-07 06:05:46164#-------------------------------------------------------------------------------
165
Uday Bondhugula587408c2021-05-24 03:20:17166set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
Stella Laurenzo722475a2020-07-07 06:05:46167 "Enables building of Python bindings.")
rkayaithd75ac582022-03-01 22:51:25168set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
169 "Prime the python detection by searching for a full 'Development' \
170 component first (temporary while diagnosing environment specific Python \
171 detection issues)")
Uday Bondhugula9c21ddb2021-05-22 14:25:38172if(MLIR_ENABLE_BINDINGS_PYTHON)
Stella Laurenzof4f8a672020-11-21 01:57:46173 include(MLIRDetectPythonEnv)
Stella Laurenzo3d927222021-10-13 02:32:48174 mlir_configure_python_dev_packages()
Stella Laurenzo722475a2020-07-07 06:05:46175endif()
176
Will Dietz02db3cf2022-03-19 21:53:59177set(CMAKE_INCLUDE_CURRENT_DIR ON)
178
Jacques Pienaar1273af22019-03-30 05:10:12179include_directories( "include")
180include_directories( ${MLIR_INCLUDE_DIR})
181
Isuru Fernando103678d2020-03-14 18:41:12182# Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
183# MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
184# from another directory like tools
Vladislav Vinogradovaca240b2021-01-18 10:54:06185add_subdirectory(tools/mlir-linalg-ods-gen)
River Riddle597fde52022-04-20 05:46:34186add_subdirectory(tools/mlir-pdll)
Jacques Pienaar6ae7f662024-01-04 04:37:19187add_subdirectory(tools/mlir-tblgen)
Martin Storsjö112499f2022-07-15 21:11:00188set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
189set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
Marius Brehler91b6f762022-08-10 17:19:23190set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
191set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
Martin Storsjö112499f2022-07-15 21:11:00192
Jacques Pienaar1273af22019-03-30 05:10:12193add_subdirectory(include/mlir)
194add_subdirectory(lib)
Alex Zinenko75f239e2020-08-05 12:36:16195# C API needs all dialects for registration, but should be built before tests.
196add_subdirectory(lib/CAPI)
Alex Zinenko14c92072021-10-14 15:18:28197
Alexandre Rames23dc9482020-05-18 16:44:26198if (MLIR_INCLUDE_TESTS)
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34199 add_definitions(-DMLIR_INCLUDE_TESTS)
Mehdi Amini09cfec62021-02-11 01:17:24200 add_custom_target(MLIRUnitTests)
Tom Stellarda11cd0d2022-11-09 16:51:34201 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
Michał Górny2aa1af92021-02-02 19:09:45202 add_subdirectory(unittests)
203 else()
204 message(WARNING "gtest not found, unittests will not be available")
205 endif()
Alexandre Rames23dc9482020-05-18 16:44:26206 add_subdirectory(test)
207endif()
Valentin Churavy7c64f6b2020-02-09 03:27:54208# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
209# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34210add_subdirectory(tools)
Mehdi Amini38b71d62019-04-02 17:02:07211
Uday Bondhugula9c21ddb2021-05-22 14:25:38212if(MLIR_ENABLE_BINDINGS_PYTHON)
Stella Laurenzo9f3f6d72021-04-28 20:04:17213 # Python sources: built extensions come in via lib/Bindings/Python
214 add_subdirectory(python)
215endif()
216
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34217if( LLVM_INCLUDE_EXAMPLES )
Mehdi Amini38b71d62019-04-02 17:02:07218 add_subdirectory(examples)
219endif()
Eric Schweitz88368a12019-11-20 05:04:45220
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34221option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
222 ${LLVM_INCLUDE_DOCS})
Jacques Pienaare298e212020-01-25 17:17:31223if (MLIR_INCLUDE_DOCS)
224 add_subdirectory(docs)
225endif()
226
Stephen Neuendorfferb0dce6b2020-10-04 22:17:34227if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Eric Schweitz88368a12019-11-20 05:04:45228 install(DIRECTORY include/mlir include/mlir-c
John Ericson5ad96992022-01-18 07:03:10229 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Eric Schweitz88368a12019-11-20 05:04:45230 COMPONENT mlir-headers
231 FILES_MATCHING
Kern Handaaab72f82020-01-06 09:01:59232 PATTERN "*.def"
Eric Schweitz88368a12019-11-20 05:04:45233 PATTERN "*.h"
234 PATTERN "*.inc"
Kern Handacde071c42019-12-29 17:04:09235 PATTERN "*.td"
Eric Schweitz88368a12019-11-20 05:04:45236 PATTERN "LICENSE.TXT"
237 )
238
239 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
John Ericson5ad96992022-01-18 07:03:10240 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Eric Schweitz88368a12019-11-20 05:04:45241 COMPONENT mlir-headers
242 FILES_MATCHING
Mehdi Amini5114d552020-01-22 00:44:17243 PATTERN "*.def"
Eric Schweitz88368a12019-11-20 05:04:45244 PATTERN "*.h"
245 PATTERN "*.gen"
246 PATTERN "*.inc"
Mehdi Amini5114d552020-01-22 00:44:17247 PATTERN "*.td"
Eric Schweitz88368a12019-11-20 05:04:45248 PATTERN "CMakeFiles" EXCLUDE
249 PATTERN "config.h" EXCLUDE
250 )
251
252 if (NOT LLVM_ENABLE_IDE)
253 add_llvm_install_targets(install-mlir-headers
254 DEPENDS mlir-headers
255 COMPONENT mlir-headers)
256 endif()
257endif()
Mehdi Amini5114d552020-01-22 00:44:17258
Nathan Lanzaf46ce032022-06-08 02:55:05259# Custom target to install all mlir libraries
260add_custom_target(mlir-libraries)
261set_target_properties(mlir-libraries PROPERTIES FOLDER "Misc")
262
263if (NOT LLVM_ENABLE_IDE)
264 add_llvm_install_targets(install-mlir-libraries
265 DEPENDS mlir-libraries
266 COMPONENT mlir-libraries)
267endif()
268
269get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
270if(MLIR_LIBS)
271 list(REMOVE_DUPLICATES MLIR_LIBS)
272 foreach(lib ${MLIR_LIBS})
273 add_dependencies(mlir-libraries ${lib})
274 if(NOT LLVM_ENABLE_IDE)
275 add_dependencies(install-mlir-libraries install-${lib})
276 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
277 endif()
278 endforeach()
279endif()
280
Mehdi Amini5114d552020-01-22 00:44:17281add_subdirectory(cmake/modules)
Saurabh Jhafa90c9d2022-01-27 21:35:34282
283if (MLIR_ENABLE_PYTHON_BENCHMARKS)
284 add_subdirectory(utils/mbr)
285endif()
Michał Górny14d9ef22022-10-19 17:14:59286
287if(MLIR_STANDALONE_BUILD)
288 llvm_distribution_add_targets()
289endif()