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(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
31 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
32 add_subdirectory(${UNITTEST_DIR} third-party/unittest)
35 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
36 "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
37 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
39 set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
42 set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
43 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
44 mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
46 set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
47 set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
49 set(MLIR_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
50 set(MLIR_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
51 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
52 set(MLIR_TOOLS_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
54 # Make sure that our source directory is on the current cmake module path so
55 # that we can include cmake files from this directory.
56 list(INSERT CMAKE_MODULE_PATH 0
57 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
58 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
63 # -BSymbolic is incompatible with TypeID
64 if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
65 message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
66 " try `-Bsymbolic-functions` instead.")
69 # Forbid implicit function declaration: this may lead to subtle bugs and we
70 # don't have a reason to support this.
71 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
72 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
74 # Warn on undefined macros. This is often an indication that an include to
75 # `mlir-config.h` or similar is missing.
76 check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)
77 append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)
78 append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)
80 # Forbid mismatch between declaration and definition for class vs struct. This is
81 # harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
82 # where it creeps into the ABI.
83 check_c_compiler_flag("-Werror=mismatched-tags" C_SUPPORTS_WERROR_MISMATCHED_TAGS)
84 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_C_FLAGS)
85 append_if(C_SUPPORTS_WERROR_MISMATCHED_TAGS "-Werror=mismatched-tags" CMAKE_CXX_FLAGS)
87 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
88 # Silence a false positive GCC -Wunused-but-set-parameter warning in
89 # constexpr cases. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827
91 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "14.0")
92 check_cxx_compiler_flag("-Wno-unused-but-set-parameter" CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER)
93 append_if(CXX_SUPPORTS_WNO_UNUSED_BUT_SET_PARAMETER "-Wno-unused-but-set-parameter" CMAKE_CXX_FLAGS)
95 # Silence a false positive GCC -Wdeprecated-copy warning in cases where
96 # a copy operator is defined through "using" a base class copy operator.
97 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
98 check_cxx_compiler_flag("-Wno-deprecated-copy" CXX_SUPPORTS_WNO_DEPRECTAED_COPY)
99 append_if(CXX_SUPPORTS_WNO_DEPRECTAED_COPY "-Wno-deprecated-copy" CMAKE_CXX_FLAGS)
103 # Installing the headers and docs needs to depend on generating any public
104 # tablegen'd targets.
105 # mlir-generic-headers are dialect-independent.
106 add_custom_target(mlir-generic-headers)
107 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "MLIR/Resources")
108 # mlir-headers may be dialect-dependent.
109 add_custom_target(mlir-headers)
110 set_target_properties(mlir-headers PROPERTIES FOLDER "MLIR/Resources")
111 add_dependencies(mlir-headers mlir-generic-headers)
112 add_custom_target(mlir-doc)
113 set_target_properties(mlir-doc PROPERTIES FOLDER "MLIR/Docs")
115 # Only enable execution engine if the native target is available.
116 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
117 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
119 set(MLIR_ENABLE_EXECUTION_ENGINE 0)
122 # Build the ROCm conversions and run according tests if the AMDGPU backend
124 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
125 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
127 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
130 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the MLIR CUDA runner")
131 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the MLIR ROCm runner")
132 set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the MLIR SYCL runner")
133 set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the MLIR SPIR-V cpu runner")
134 set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the MLIR Vulkan runner")
135 set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
136 "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
137 for compiling PTX to cubin")
139 set(MLIR_ENABLE_PDL_IN_PATTERNMATCH 1 CACHE BOOL "Enable PDL in PatternMatch")
141 option(MLIR_INCLUDE_TESTS
142 "Generate build targets for the MLIR unit tests."
143 ${LLVM_INCLUDE_TESTS})
145 option(MLIR_INCLUDE_INTEGRATION_TESTS
146 "Generate build targets for the MLIR integration tests.")
148 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
149 "Installs object files needed for add_mlir_aggregate to work out of \
150 tree. Package maintainers can disable this to exclude these assets if \
151 not desired. Enabling this will result in object files being written \
152 under lib/objects-{CMAKE_BUILD_TYPE}.")
154 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
157 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
158 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
160 #-------------------------------------------------------------------------------
161 # Python Bindings Configuration
163 # The pybind11 library can be found (set with -DPYBIND_DIR=...)
164 # The python executable is correct (set with -DPython3_EXECUTABLE=...)
165 #-------------------------------------------------------------------------------
167 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
168 "Enables building of Python bindings.")
169 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
170 "Prime the python detection by searching for a full 'Development' \
171 component first (temporary while diagnosing environment specific Python \
173 if(MLIR_ENABLE_BINDINGS_PYTHON)
174 include(MLIRDetectPythonEnv)
175 mlir_configure_python_dev_packages()
178 set(CMAKE_INCLUDE_CURRENT_DIR ON)
180 include_directories( "include")
181 include_directories( ${MLIR_INCLUDE_DIR})
183 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
184 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
185 # from another directory like tools
186 add_subdirectory(tools/mlir-linalg-ods-gen)
187 add_subdirectory(tools/mlir-pdll)
188 add_subdirectory(tools/mlir-tblgen)
189 add_subdirectory(tools/mlir-src-sharder)
190 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
191 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
192 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
193 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
194 set(MLIR_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE}" CACHE INTERNAL "")
195 set(MLIR_SRC_SHARDER_TABLEGEN_TARGET "${MLIR_SRC_SHARDER_TABLEGEN_TARGET}" CACHE INTERNAL "")
197 add_subdirectory(include/mlir)
198 add_subdirectory(lib)
199 # C API needs all dialects for registration, but should be built before tests.
200 add_subdirectory(lib/CAPI)
202 if (MLIR_INCLUDE_TESTS)
203 add_definitions(-DMLIR_INCLUDE_TESTS)
204 add_custom_target(MLIRUnitTests)
205 set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")
206 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
207 add_subdirectory(unittests)
209 message(WARNING "gtest not found, unittests will not be available")
211 add_subdirectory(test)
213 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
214 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
215 add_subdirectory(tools)
217 if(MLIR_ENABLE_BINDINGS_PYTHON)
218 # Python sources: built extensions come in via lib/Bindings/Python
219 add_subdirectory(python)
222 if( LLVM_INCLUDE_EXAMPLES )
223 add_subdirectory(examples)
226 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
227 ${LLVM_INCLUDE_DOCS})
228 if (MLIR_INCLUDE_DOCS)
229 add_subdirectory(docs)
232 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
233 install(DIRECTORY include/mlir include/mlir-c
234 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
235 COMPONENT mlir-headers
241 PATTERN "LICENSE.TXT"
244 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
245 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
246 COMPONENT mlir-headers
253 PATTERN "CMakeFiles" EXCLUDE
254 PATTERN "config.h" EXCLUDE
257 if (NOT LLVM_ENABLE_IDE)
258 add_llvm_install_targets(install-mlir-headers
260 COMPONENT mlir-headers)
264 # Custom target to install all mlir libraries
265 add_custom_target(mlir-libraries)
266 set_target_properties(mlir-libraries PROPERTIES FOLDER "MLIR/Metatargets")
268 if (NOT LLVM_ENABLE_IDE)
269 add_llvm_install_targets(install-mlir-libraries
270 DEPENDS mlir-libraries
271 COMPONENT mlir-libraries)
274 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
276 list(REMOVE_DUPLICATES MLIR_LIBS)
277 foreach(lib ${MLIR_LIBS})
278 add_dependencies(mlir-libraries ${lib})
279 if(NOT LLVM_ENABLE_IDE)
280 add_dependencies(install-mlir-libraries install-${lib})
281 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
286 add_subdirectory(cmake/modules)
288 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
289 add_subdirectory(utils/mbr)
292 if(MLIR_STANDALONE_BUILD)
293 llvm_distribution_add_targets()