[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / mlir / CMakeLists.txt
blobc9b0d53bc3e94acddb91aa3e4cbd3ad553821b3a
1 # MLIR project.
2 cmake_minimum_required(VERSION 3.13.4)
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5   set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6 endif()
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
8   NO_POLICY_SCOPE)
10 # Check if MLIR is built as a standalone project.
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12   project(mlir)
13   set(MLIR_STANDALONE_BUILD TRUE)
14   if ("${CMAKE_VERSION}" VERSION_LESS "3.20.0")
15     message(WARNING
16       "Your CMake version is ${CMAKE_VERSION}. Starting with LLVM 17.0.0, the "
17       "minimum version of CMake required to build LLVM will become 3.20.0, and "
18       "using an older CMake will become an error. Please upgrade your CMake to "
19       "at least 3.20.0 now to avoid issues in the future!")
20   endif()
21 endif()
23 # Must go below project(..)
24 include(GNUInstallDirs)
25 set(CMAKE_CXX_STANDARD 17)
27 if(MLIR_STANDALONE_BUILD)
28   find_package(LLVM CONFIG REQUIRED)
29   set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
30   include(HandleLLVMOptions)
31   include(AddLLVM)
32   include(TableGen)
34   include_directories(${LLVM_INCLUDE_DIRS})
36   set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest)
37   if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
38     add_subdirectory(${UNITTEST_DIR} third-party/unittest)
39   endif()
41   set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
42     "${CMAKE_CURRENT_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}")
43   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
45   set(LLVM_LIT_ARGS "-sv" CACHE STRING "Default options for lit")
46 endif()
48 set(MLIR_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
49     "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
50 mark_as_advanced(MLIR_TOOLS_INSTALL_DIR)
52 set(MLIR_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  )
53 set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
55 set(MLIR_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
56 set(MLIR_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
57 set(MLIR_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
58 set(MLIR_TOOLS_DIR   ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
60 # Make sure that our source directory is on the current cmake module path so
61 # that we can include cmake files from this directory.
62 list(INSERT CMAKE_MODULE_PATH 0
63   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
64   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
65   )
67 include(AddMLIR)
69 # -BSymbolic is incompatible with TypeID
70 if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]")
71   message(FATAL_ERROR " MLIR does not support `-Bsymbolic` (see http://llvm.org/pr51420 ),"
72           " try `-Bsymbolic-functions` instead.")
73 endif()
75 # Forbid implicit function declaration: this may lead to subtle bugs and we
76 # don't have a reason to support this.
77 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
78 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_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 # Installing the headers and docs needs to depend on generating any public
88 # tablegen'd targets.
89 # mlir-generic-headers are dialect-independent.
90 add_custom_target(mlir-generic-headers)
91 set_target_properties(mlir-generic-headers PROPERTIES FOLDER "Misc")
92 # mlir-headers may be dialect-dependent.
93 add_custom_target(mlir-headers)
94 set_target_properties(mlir-headers PROPERTIES FOLDER "Misc")
95 add_dependencies(mlir-headers mlir-generic-headers)
96 add_custom_target(mlir-doc)
98 # Only enable execution engine if the native target is available.
99 if(${LLVM_NATIVE_ARCH} IN_LIST LLVM_TARGETS_TO_BUILD)
100   set(MLIR_ENABLE_EXECUTION_ENGINE 1)
101 else()
102   set(MLIR_ENABLE_EXECUTION_ENGINE 0)
103 endif()
105 # Build the CUDA conversions and run according tests if the NVPTX backend
106 # is available
107 if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD AND MLIR_ENABLE_EXECUTION_ENGINE)
108   set(MLIR_ENABLE_CUDA_CONVERSIONS 1)
109 else()
110   set(MLIR_ENABLE_CUDA_CONVERSIONS 0)
111 endif()
112 # TODO: we should use a config.h file like LLVM does
113 add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS})
115 # Build the ROCm conversions and run according tests if the AMDGPU backend
116 # is available.
117 if ("AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD AND MLIR_ENABLE_EXECUTION_ENGINE)
118   set(MLIR_ENABLE_ROCM_CONVERSIONS 1)
119 else()
120   set(MLIR_ENABLE_ROCM_CONVERSIONS 0)
121 endif()
122 add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS})
124 set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner")
125 set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm 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")
129 option(MLIR_INCLUDE_TESTS
130        "Generate build targets for the MLIR unit tests."
131        ${LLVM_INCLUDE_TESTS})
133 option(MLIR_INCLUDE_INTEGRATION_TESTS
134        "Generate build targets for the MLIR integration tests.")
136 set(MLIR_INSTALL_AGGREGATE_OBJECTS 1 CACHE BOOL
137     "Installs object files needed for add_mlir_aggregate to work out of \
138     tree. Package maintainers can disable this to exclude these assets if \
139     not desired. Enabling this will result in object files being written \
140     under lib/objects-{CMAKE_BUILD_TYPE}.")
142 set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.")
144 #-------------------------------------------------------------------------------
145 # Python Bindings Configuration
146 # Requires:
147 #   The pybind11 library can be found (set with -DPYBIND_DIR=...)
148 #   The python executable is correct (set with -DPython3_EXECUTABLE=...)
149 #-------------------------------------------------------------------------------
151 set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
152        "Enables building of Python bindings.")
153 set(MLIR_DETECT_PYTHON_ENV_PRIME_SEARCH 1 CACHE BOOL
154        "Prime the python detection by searching for a full 'Development' \
155        component first (temporary while diagnosing environment specific Python \
156        detection issues)")
157 if(MLIR_ENABLE_BINDINGS_PYTHON)
158   include(MLIRDetectPythonEnv)
159   mlir_configure_python_dev_packages()
160 endif()
162 set(CMAKE_INCLUDE_CURRENT_DIR ON)
164 include_directories( "include")
165 include_directories( ${MLIR_INCLUDE_DIR})
167 # Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
168 # MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
169 # from another directory like tools
170 add_subdirectory(tools/mlir-tblgen)
171 add_subdirectory(tools/mlir-linalg-ods-gen)
172 add_subdirectory(tools/mlir-pdll)
174 set(MLIR_TABLEGEN_EXE "${MLIR_TABLEGEN_EXE}" CACHE INTERNAL "")
175 set(MLIR_TABLEGEN_TARGET "${MLIR_TABLEGEN_TARGET}" CACHE INTERNAL "")
176 set(MLIR_PDLL_TABLEGEN_EXE "${MLIR_PDLL_TABLEGEN_EXE}" CACHE INTERNAL "")
177 set(MLIR_PDLL_TABLEGEN_TARGET "${MLIR_PDLL_TABLEGEN_TARGET}" CACHE INTERNAL "")
179 add_subdirectory(include/mlir)
180 add_subdirectory(lib)
181 # C API needs all dialects for registration, but should be built before tests.
182 add_subdirectory(lib/CAPI)
184 if (MLIR_INCLUDE_TESTS)
185   add_definitions(-DMLIR_INCLUDE_TESTS)
186   add_custom_target(MLIRUnitTests)
187   if (EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest/googletest/include/gtest/gtest.h)
188     add_subdirectory(unittests)
189   else()
190     message(WARNING "gtest not found, unittests will not be available")
191   endif()
192   add_subdirectory(test)
193 endif()
194 # Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
195 # Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
196 add_subdirectory(tools)
198 if(MLIR_ENABLE_BINDINGS_PYTHON)
199   # Python sources: built extensions come in via lib/Bindings/Python
200   add_subdirectory(python)
201 endif()
203 if( LLVM_INCLUDE_EXAMPLES )
204   add_subdirectory(examples)
205 endif()
207 option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
208   ${LLVM_INCLUDE_DOCS})
209 if (MLIR_INCLUDE_DOCS)
210   add_subdirectory(docs)
211 endif()
213 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
214   install(DIRECTORY include/mlir include/mlir-c
215     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
216     COMPONENT mlir-headers
217     FILES_MATCHING
218     PATTERN "*.def"
219     PATTERN "*.h"
220     PATTERN "*.inc"
221     PATTERN "*.td"
222     PATTERN "LICENSE.TXT"
223     )
225   install(DIRECTORY ${MLIR_INCLUDE_DIR}/mlir ${MLIR_INCLUDE_DIR}/mlir-c
226     DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
227     COMPONENT mlir-headers
228     FILES_MATCHING
229     PATTERN "*.def"
230     PATTERN "*.h"
231     PATTERN "*.gen"
232     PATTERN "*.inc"
233     PATTERN "*.td"
234     PATTERN "CMakeFiles" EXCLUDE
235     PATTERN "config.h" EXCLUDE
236     )
238   if (NOT LLVM_ENABLE_IDE)
239     add_llvm_install_targets(install-mlir-headers
240                              DEPENDS mlir-headers
241                              COMPONENT mlir-headers)
242   endif()
243 endif()
245 # Custom target to install all mlir libraries
246 add_custom_target(mlir-libraries)
247 set_target_properties(mlir-libraries PROPERTIES FOLDER "Misc")
249 if (NOT LLVM_ENABLE_IDE)
250   add_llvm_install_targets(install-mlir-libraries
251                            DEPENDS mlir-libraries
252                            COMPONENT mlir-libraries)
253 endif()
255 get_property(MLIR_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
256 if(MLIR_LIBS)
257   list(REMOVE_DUPLICATES MLIR_LIBS)
258   foreach(lib ${MLIR_LIBS})
259     add_dependencies(mlir-libraries ${lib})
260     if(NOT LLVM_ENABLE_IDE)
261       add_dependencies(install-mlir-libraries install-${lib})
262       add_dependencies(install-mlir-libraries-stripped install-${lib}-stripped)
263     endif()
264   endforeach()
265 endif()
267 add_subdirectory(cmake/modules)
269 if (MLIR_ENABLE_PYTHON_BENCHMARKS)
270   add_subdirectory(utils/mbr)
271 endif()
273 if(MLIR_STANDALONE_BUILD)
274   llvm_distribution_add_targets()
275 endif()