2 cmake_minimum_required(VERSION 3.20.0)
3 set(LLVM_SUBPROJECT_TITLE "MLIR")
5 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
6 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
8 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
11 # Check if MLIR is built as a standalone project.
12 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
14 set(MLIR_STANDALONE_BUILD TRUE)
17 # Must go below project(..)
18 include(GNUInstallDirs)
19 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
21 if(MLIR_STANDALONE_BUILD)
22 find_package(LLVM CONFIG REQUIRED)
23 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
24 include(HandleLLVMOptions)
28 include_directories(${LLVM_INCLUDE_DIRS})
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")
34 # These definitions are needed to fill SHLIBDIR in tests.
35 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
36 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
38 # DLL platform -- put DLLs into bin.
39 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
41 set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
43 set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
46 set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
47 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
48 mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
50 set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
51 set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
53 set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
54 set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
55 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
56 set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
58 # Make sure that our source directory is on the current cmake module path so
59 # that we can include cmake files from this directory.
60 list(INSERT CMAKE_MODULE_PATH 0
61 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
62 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
67 # -BSymbolic is incompatible with TypeID
68 if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
69 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
70 " try `-Bsymbolic-functions` instead.")
73 # Forbid implicit function declaration: this may lead to subtle bugs and we
74 # don't have a reason to support this.
75 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
76 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
78 # Warn on undefined macros. This is often an indication that an include to
79 # `mlir-config.h` or similar is missing.
80 check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)
81 append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)
82 append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)
84 # Forbid mismatch between declaration and definition for class vs struct. This is
85 # harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
86 # where it creeps into the ABI.
87 check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
88 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
89 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
91 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
92 # Silence a false positive GCC -Wunused-but-set-parameter warning in
93 # constexpr cases. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827
95 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0")
96 check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)
97 append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" CMAKE_CXX_FLAGS)
99 # Silence a false positive GCC -Wdeprecated-copy warning in cases where
100 # a copy operator is defined through "using" a base class copy operator.
101 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
102 check_cxx_compiler_flag("-Wno-deprecated-copy" CXX_SUPPORTS_WNO_DEPRECTAED_COPY)
103 append_if(CXX_SUPPORTS_WNO_DEPRECTAED_COPY "-Wno-deprecated-copy" CMAKE_CXX_FLAGS)
107 # Installing the headers and docs needs to depend on generating any public
108 # tablegen'd targets.
109 # mlir-generic-headers are dialect-independent.
110 add_custom_target(mlir-generic-headers)
111 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "MLIR/Resources")
112 # mlir-headers may be dialect-dependent.
113 add_custom_target(mlir-headers)
114 set_target_properties(mlir-headers PROPERTIES FOLDER "MLIR/Resources")
115 add_dependencies(mlir-headers mlir-generic-headers)
116 add_custom_target(mlir-doc)
117 set_target_properties(mlir-doc PROPERTIES FOLDER "MLIR/Docs")
119 # Only enable execution engine if the native target is available.
120 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
121 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
123 set(MLIR_ENABLE_EXECUTION_ENGINE 0)
126 # Build the ROCm conversions and run according tests if the AMDGPU backend
128 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
129 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
131 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
134 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the MLIR CUDA runner")
135 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the MLIR ROCm runner")
136 set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the MLIR SYCL runner")
137 set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the MLIR SPIR-V cpu runner")
138 set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the MLIR Vulkan runner")
139 set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
140 "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
141 for compiling PTX to cubin")
143 set(MLIR_ENABLE_PDL_IN_PATTERNMATCH 1 CACHE BOOL "Enable PDL in PatternMatch")
145 option(MLIR_INCLUDE_TESTS
146 "Generate build targets for the MLIR unit tests."
147 ${LLVM_INCLUDE_TESTS})
149 option(MLIR_INCLUDE_INTEGRATION_TESTS
150 "Generate build targets for the MLIR integration tests.")
152 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
153 "Installs object files needed for add_mlir_aggregate to work out of \
154 tree. Package maintainers can disable this to exclude these assets if \
155 not desired. Enabling this will result in object files being written \
156 under lib/objects-{CMAKE_BUILD_TYPE}.")
158 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
160 set(MLIR_LINK_MLIR_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL
161 "Link tools against libMLIR.so")
164 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
165 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
167 #-------------------------------------------------------------------------------
168 # Python Bindings Configuration
170 # The pybind11 library can be found (set with -DPYBIND_DIR=...)
171 # The python executable is correct (set with -DPython3_EXECUTABLE=...)
172 # By default, find_package and probing for installed pybind11 is performed.
173 # Super projects can set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES=ON to
174 # disable all package setup and control it themselves.
175 #-------------------------------------------------------------------------------
177 set(MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir"
178 CACHE STRING "nanobind domain for MLIR python bindings.")
179 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
180 "Enables building of Python bindings.")
181 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
182 "Prime the python detection by searching for a full 'Development' \
183 component first (temporary while diagnosing environment specific Python \
185 set(MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES 0 CACHE BOOL
186 "Performs python dev package configuration sufficient to use all MLIR \
187 python features. Super-projects that wish to control their own setup \
188 must perform an appropriate find_package of Python3 with \
189 'Development.Module' and ensure that find_package(pybind11) is \
190 satisfied (and keep up to date as requirements evolve).")
192 if(MLIR_ENABLE_BINDINGS_PYTHON)
193 include(MLIRDetectPythonEnv)
194 # Note that both upstream and downstreams often call this macro. It gates
195 # internally on the MLIR_CONFIGURE_PYTHON_DEV_PACKAGES option.
196 mlir_configure_python_dev_packages()
199 set(CMAKE_INCLUDE_CURRENT_DIR ON)
201 include_directories(BEFORE
206 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
207 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
208 # from another directory like tools
209 add_subdirectory(tools/mlir-linalg-ods-gen)
210 add_subdirectory(tools/mlir-pdll)
211 add_subdirectory(tools/mlir-tblgen)
212 add_subdirectory(tools/mlir-src-sharder)
213 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
214 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
215 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
216 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
217 set(MLIR_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE}" CACHE INTERNAL "")
218 set(MLIR_SRC_SHARDER_TABLEGEN_TARGET "${MLIR_SRC_SHARDER_TABLEGEN_TARGET}" CACHE INTERNAL "")
220 add_subdirectory(include/mlir)
221 add_subdirectory(lib)
222 # C API needs all dialects for registration, but should be built before tests.
223 add_subdirectory(lib/CAPI)
225 if (MLIR_INCLUDE_TESTS)
226 add_definitions(-DMLIR_INCLUDE_TESTS)
227 add_custom_target(MLIRUnitTests)
228 set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")
229 if (TARGET llvm_gtest)
230 add_subdirectory(unittests)
232 message(WARNING "gtest not found, unittests will not be available")
234 add_subdirectory(test)
236 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
237 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
238 add_subdirectory(tools)
240 if(MLIR_ENABLE_BINDINGS_PYTHON)
241 # Python sources: built extensions come in via lib/Bindings/Python
242 add_subdirectory(python)
245 if( LLVM_INCLUDE_EXAMPLES )
246 add_subdirectory(examples)
249 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
250 ${LLVM_INCLUDE_DOCS})
251 if (MLIR_INCLUDE_DOCS)
252 add_subdirectory(docs)
255 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
256 install(DIRECTORY include/mlir include/mlir-c
257 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
258 COMPONENT mlir-headers
264 PATTERN "LICENSE.TXT"
267 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
268 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
269 COMPONENT mlir-headers
276 PATTERN "CMakeFiles" EXCLUDE
277 PATTERN "config.h" EXCLUDE
280 if (NOT LLVM_ENABLE_IDE)
281 add_llvm_install_targets(install-mlir-headers
283 COMPONENT mlir-headers)
287 # Custom target to install all mlir libraries
288 add_custom_target(mlir-libraries)
289 set_target_properties(mlir-libraries PROPERTIES FOLDER "MLIR/Metatargets")
291 if (NOT LLVM_ENABLE_IDE)
292 add_llvm_install_targets(install-mlir-libraries
293 DEPENDS mlir-libraries
294 COMPONENT mlir-libraries)
297 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
299 list(REMOVE_DUPLICATES MLIR_LIBS)
300 foreach(lib ${MLIR_LIBS})
301 add_dependencies(mlir-libraries ${lib})
302 if(NOT LLVM_ENABLE_IDE)
303 add_dependencies(install-mlir-libraries install-${lib})
304 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
309 add_subdirectory(cmake/modules)
311 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
312 add_subdirectory(utils/mbr)
315 if(MLIR_STANDALONE_BUILD)
316 llvm_distribution_add_targets()