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)
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 # Silence a false positive GCC -Wunused-but-set-parameter warning in constexpr
88 # cases, by marking SelectedCase as used. See
89 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827 for details. The issue is
91 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.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)
96 # Installing the headers and docs needs to depend on generating any public
98 # mlir-generic-headers are dialect-independent.
99 add_custom_target(mlir-generic-headers)
100 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "MLIR/Resources")
101 # mlir-headers may be dialect-dependent.
102 add_custom_target(mlir-headers)
103 set_target_properties(mlir-headers PROPERTIES FOLDER "MLIR/Resources")
104 add_dependencies(mlir-headers mlir-generic-headers)
105 add_custom_target(mlir-doc)
106 set_target_properties(mlir-doc PROPERTIES FOLDER "MLIR/Docs")
108 # Only enable execution engine if the native target is available.
109 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
110 set(MLIR_ENABLE_EXECUTION_ENGINE 1)
112 set(MLIR_ENABLE_EXECUTION_ENGINE 0)
115 # Build the ROCm conversions and run according tests if the AMDGPU backend
117 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
118 set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
120 set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
123 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
124 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner")
125 set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the mlir Sycl runner")
126 set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the mlir SPIR-V cpu runner")
127 set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the mlir Vulkan runner")
128 set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
129 "Statically link the nvptxlibrary instead of calling ptxas as a subprocess \
130 for compiling PTX to cubin")
132 set(MLIR_ENABLE_PDL_IN_PATTERNMATCH 1 CACHE BOOL "Enable PDL in PatternMatch")
134 option(MLIR_INCLUDE_TESTS
135 "Generate build targets for the MLIR unit tests."
136 ${LLVM_INCLUDE_TESTS})
138 option(MLIR_INCLUDE_INTEGRATION_TESTS
139 "Generate build targets for the MLIR integration tests.")
141 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
142 "Installs object files needed for add_mlir_aggregate to work out of \
143 tree. Package maintainers can disable this to exclude these assets if \
144 not desired. Enabling this will result in object files being written \
145 under lib/objects-{CMAKE_BUILD_TYPE}.")
147 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
150 ${MLIR_MAIN_INCLUDE_DIR}/mlir/Config/mlir-config.h.cmake
151 ${MLIR_INCLUDE_DIR}/mlir/Config/mlir-config.h)
153 #-------------------------------------------------------------------------------
154 # Python Bindings Configuration
156 # The pybind11 library can be found (set with -DPYBIND_DIR=...)
157 # The python executable is correct (set with -DPython3_EXECUTABLE=...)
158 #-------------------------------------------------------------------------------
160 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
161 "Enables building of Python bindings.")
162 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
163 "Prime the python detection by searching for a full 'Development' \
164 component first (temporary while diagnosing environment specific Python \
166 if(MLIR_ENABLE_BINDINGS_PYTHON)
167 include(MLIRDetectPythonEnv)
168 mlir_configure_python_dev_packages()
171 set(CMAKE_INCLUDE_CURRENT_DIR ON)
173 include_directories( "include")
174 include_directories( ${MLIR_INCLUDE_DIR})
176 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
177 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
178 # from another directory like tools
179 add_subdirectory(tools/mlir-linalg-ods-gen)
180 add_subdirectory(tools/mlir-pdll)
181 add_subdirectory(tools/mlir-tblgen)
182 add_subdirectory(tools/mlir-src-sharder)
183 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
184 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
185 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
186 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
187 set(MLIR_SRC_SHARDER_TABLEGEN_EXE "${MLIR_SRC_SHARDER_TABLEGEN_EXE}" CACHE INTERNAL "")
188 set(MLIR_SRC_SHARDER_TABLEGEN_TARGET "${MLIR_SRC_SHARDER_TABLEGEN_TARGET}" CACHE INTERNAL "")
190 add_subdirectory(include/mlir)
191 add_subdirectory(lib)
192 # C API needs all dialects for registration, but should be built before tests.
193 add_subdirectory(lib/CAPI)
195 if (MLIR_INCLUDE_TESTS)
196 add_definitions(-DMLIR_INCLUDE_TESTS)
197 add_custom_target(MLIRUnitTests)
198 set_target_properties(MLIRUnitTests PROPERTIES FOLDER "MLIR/Tests")
199 if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
200 add_subdirectory(unittests)
202 message(WARNING "gtest not found, unittests will not be available")
204 add_subdirectory(test)
206 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
207 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
208 add_subdirectory(tools)
210 if(MLIR_ENABLE_BINDINGS_PYTHON)
211 # Python sources: built extensions come in via lib/Bindings/Python
212 add_subdirectory(python)
215 if( LLVM_INCLUDE_EXAMPLES )
216 add_subdirectory(examples)
219 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
220 ${LLVM_INCLUDE_DOCS})
221 if (MLIR_INCLUDE_DOCS)
222 add_subdirectory(docs)
225 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
226 install(DIRECTORY include/mlir include/mlir-c
227 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
228 COMPONENT mlir-headers
234 PATTERN "LICENSE.TXT"
237 install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
238 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
239 COMPONENT mlir-headers
246 PATTERN "CMakeFiles" EXCLUDE
247 PATTERN "config.h" EXCLUDE
250 if (NOT LLVM_ENABLE_IDE)
251 add_llvm_install_targets(install-mlir-headers
253 COMPONENT mlir-headers)
257 # Custom target to install all mlir libraries
258 add_custom_target(mlir-libraries)
259 set_target_properties(mlir-libraries PROPERTIES FOLDER "MLIR/Metatargets")
261 if (NOT LLVM_ENABLE_IDE)
262 add_llvm_install_targets(install-mlir-libraries
263 DEPENDS mlir-libraries
264 COMPONENT mlir-libraries)
267 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
269 list(REMOVE_DUPLICATES MLIR_LIBS)
270 foreach(lib ${MLIR_LIBS})
271 add_dependencies(mlir-libraries ${lib})
272 if(NOT LLVM_ENABLE_IDE)
273 add_dependencies(install-mlir-libraries install-${lib})
274 add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
279 add_subdirectory(cmake/modules)
281 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
282 add_subdirectory(utils/mbr)
285 if(MLIR_STANDALONE_BUILD)
286 llvm_distribution_add_targets()