1 include(LLVMDistributionSupport)
3 function(mlir_tablegen ofn)
5 set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
7 include_directories(${CMAKE_CURRENT_BINARY_DIR})
10 # Declare a dialect in the include directory
11 function(add_mlir_dialect dialect dialect_namespace)
12 set(LLVM_TARGET_DEFINITIONS ${dialect}.td)
13 mlir_tablegen(${dialect}.h.inc -gen-op-decls)
14 mlir_tablegen(${dialect}.cpp.inc -gen-op-defs)
15 mlir_tablegen(${dialect}Types.h.inc -gen-typedef-decls)
16 mlir_tablegen(${dialect}Types.cpp.inc -gen-typedef-defs)
17 mlir_tablegen(${dialect}Dialect.h.inc -gen-dialect-decls -dialect=${dialect_namespace})
18 mlir_tablegen(${dialect}Dialect.cpp.inc -gen-dialect-defs -dialect=${dialect_namespace})
19 add_public_tablegen_target(MLIR${dialect}IncGen)
20 add_dependencies(mlir-headers MLIR${dialect}IncGen)
23 # Declare a dialect in the include directory
24 function(add_mlir_interface interface)
25 set(LLVM_TARGET_DEFINITIONS ${interface}.td)
26 mlir_tablegen(${interface}.h.inc -gen-op-interface-decls)
27 mlir_tablegen(${interface}.cpp.inc -gen-op-interface-defs)
28 add_public_tablegen_target(MLIR${interface}IncGen)
29 add_dependencies(mlir-generic-headers MLIR${interface}IncGen)
33 # Generate Documentation
34 function(add_mlir_doc doc_filename output_file output_directory command)
35 set(LLVM_TARGET_DEFINITIONS ${doc_filename}.td)
36 tablegen(MLIR ${output_file}.md ${command} ${ARGN})
37 set(GEN_DOC_FILE ${MLIR_BINARY_DIR}/docs/${output_directory}${output_file}.md)
39 OUTPUT ${GEN_DOC_FILE}
40 COMMAND ${CMAKE_COMMAND} -E copy
41 ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md
43 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output_file}.md)
44 add_custom_target(${output_file}DocGen DEPENDS ${GEN_DOC_FILE})
45 add_dependencies(mlir-doc ${output_file}DocGen)
48 # Declare an mlir library which can be compiled in libMLIR.so
49 # In addition to everything that llvm_add_librar accepts, this
50 # also has the following option:
51 # EXCLUDE_FROM_LIBMLIR
52 # Don't include this library in libMLIR.so. This option should be used
53 # for test libraries, executable-specific libraries, or rarely used libraries
54 # with large dependencies.
56 # Forces generation of an OBJECT library, exports additional metadata,
57 # and installs additional object files needed to include this as part of an
58 # aggregate shared library.
59 # TODO: Make this the default for all MLIR libraries once all libraries
60 # are compatible with building an object library.
61 function(add_mlir_library name)
62 cmake_parse_arguments(ARG
63 "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
65 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
70 file(RELATIVE_PATH lib_path
71 ${MLIR_SOURCE_DIR}/lib/
72 ${CMAKE_CURRENT_SOURCE_DIR}
74 if(NOT lib_path MATCHES "^[.][.]")
75 file( GLOB_RECURSE headers
76 ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.h
77 ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.def
79 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
81 file( GLOB_RECURSE tds
82 ${MLIR_SOURCE_DIR}/include/mlir/${lib_path}/*.td
84 source_group("TableGen descriptions" FILES ${tds})
85 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
88 set(srcs ${headers} ${tds})
91 endif(MSVC_IDE OR XCODE)
92 if(srcs OR ARG_ADDITIONAL_HEADERS)
96 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
100 # Is an object library needed.
101 set(NEEDS_OBJECT_LIB OFF)
102 if(ARG_ENABLE_AGGREGATION)
103 set(NEEDS_OBJECT_LIB ON)
106 # Determine type of library.
110 # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
111 # so we need to handle it here.
112 if(BUILD_SHARED_LIBS)
117 # Test libraries and such shouldn't be include in libMLIR.so
118 if(NOT ARG_EXCLUDE_FROM_LIBMLIR)
119 set(NEEDS_OBJECT_LIB ON)
120 set_property(GLOBAL APPEND PROPERTY MLIR_STATIC_LIBS ${name})
121 set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
122 set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
126 if(NEEDS_OBJECT_LIB AND NOT XCODE)
127 # The Xcode generator doesn't handle object libraries correctly.
128 # We special case xcode when building aggregates.
129 list(APPEND LIBTYPE OBJECT)
132 # MLIR libraries uniformly depend on LLVMSupport. Just specify it once here.
133 list(APPEND ARG_LINK_COMPONENTS Support)
135 # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
136 # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
137 # Perhaps this should be in llvm_add_library instead? However, it fails
139 get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
140 foreach(lib ${ARG_LINK_LIBS})
141 if(${lib} IN_LIST llvm_component_libs)
142 message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries. Please use LINK_COMPONENTS instead.")
146 list(APPEND ARG_DEPENDS mlir-generic-headers)
147 llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})
150 target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
151 if(NOT ARG_DISABLE_INSTALL)
152 add_mlir_library_install(${name})
155 # Add empty "phony" target
156 add_custom_target(${name})
158 set_target_properties(${name} PROPERTIES FOLDER "MLIR libraries")
161 if(ARG_ENABLE_AGGREGATION)
162 # Compute and store the properties needed to build aggregates.
163 set(AGGREGATE_OBJECTS)
164 set(AGGREGATE_OBJECT_LIB)
167 # XCode has limited support for object libraries. Instead, add dep flags
168 # that force the entire library to be embedded.
169 list(APPEND AGGREGATE_DEPS "-force_load" "${name}")
171 list(APPEND AGGREGATE_OBJECTS "$<TARGET_OBJECTS:obj.${name}>")
172 list(APPEND AGGREGATE_OBJECT_LIB "obj.${name}")
175 # For each declared dependency, transform it into a generator expression
176 # which excludes it if the ultimate link target is excluding the library.
177 set(NEW_LINK_LIBRARIES)
178 get_target_property(CURRENT_LINK_LIBRARIES ${name} LINK_LIBRARIES)
179 get_mlir_filtered_link_libraries(NEW_LINK_LIBRARIES ${CURRENT_LINK_LIBRARIES})
180 set_target_properties(${name} PROPERTIES LINK_LIBRARIES "${NEW_LINK_LIBRARIES}")
181 list(APPEND AGGREGATE_DEPS ${NEW_LINK_LIBRARIES})
182 set_target_properties(${name} PROPERTIES
183 EXPORT_PROPERTIES "MLIR_AGGREGATE_OBJECT_LIB_IMPORTED;MLIR_AGGREGATE_DEP_LIBS_IMPORTED"
184 MLIR_AGGREGATE_OBJECTS "${AGGREGATE_OBJECTS}"
185 MLIR_AGGREGATE_DEPS "${AGGREGATE_DEPS}"
186 MLIR_AGGREGATE_OBJECT_LIB_IMPORTED "${AGGREGATE_OBJECT_LIB}"
187 MLIR_AGGREGATE_DEP_LIBS_IMPORTED "${CURRENT_LINK_LIBRARIES}"
190 # In order for out-of-tree projects to build aggregates of this library,
191 # we need to install the OBJECT library.
192 if(NOT ARG_DISABLE_INSTALL)
193 add_mlir_library_install(obj.${name})
196 endfunction(add_mlir_library)
198 # Sets a variable with a transformed list of link libraries such individual
199 # libraries will be dynamically excluded when evaluated on a final library
200 # which defines an MLIR_AGGREGATE_EXCLUDE_LIBS which contains any of the
201 # libraries. Each link library can be a generator expression but must not
202 # resolve to an arity > 1 (i.e. it can be optional).
203 function(get_mlir_filtered_link_libraries output)
205 foreach(linklib ${ARGN})
206 # In English, what this expression does:
207 # For each link library, resolve the property MLIR_AGGREGATE_EXCLUDE_LIBS
208 # on the context target (i.e. the executable or shared library being linked)
209 # and, if it is not in that list, emit the library name. Otherwise, empty.
211 "$<$<NOT:$<IN_LIST:${linklib},$<GENEX_EVAL:$<TARGET_PROPERTY:MLIR_AGGREGATE_EXCLUDE_LIBS>>>>:${linklib}>"
214 set(${output} "${_results}" PARENT_SCOPE)
215 endfunction(get_mlir_filtered_link_libraries)
217 # Declares an aggregate library. Such a library is a combination of arbitrary
218 # regular add_mlir_library() libraries with the special feature that they can
219 # be configured to statically embed some subset of their dependencies, as is
220 # typical when creating a .so/.dylib/.dll or a mondo static library.
222 # It is always safe to depend on the aggregate directly in order to compile/link
223 # against the superset of embedded entities and transitive deps.
226 # PUBLIC_LIBS: list of dependent libraries to add to the
227 # INTERFACE_LINK_LIBRARIES property, exporting them to users. This list
228 # will be transitively filtered to exclude any EMBED_LIBS.
229 # EMBED_LIBS: list of dependent libraries that should be embedded directly
230 # into this library. Each of these must be an add_mlir_library() library
231 # without DISABLE_AGGREGATE.
233 # Note: This is a work in progress and is presently only sufficient for certain
234 # non nested cases involving the C-API.
235 function(add_mlir_aggregate name)
236 cmake_parse_arguments(ARG
239 "PUBLIC_LIBS;EMBED_LIBS"
243 list(APPEND _libtype STATIC)
246 list(APPEND _libtype SHARED)
253 foreach(lib ${ARG_EMBED_LIBS})
254 # We have to handle imported vs in-tree differently:
255 # in-tree: To support arbitrary ordering, the generator expressions get
256 # set on the dependent target when it is constructed and then just
257 # eval'd here. This means we can build an aggregate from targets that
258 # may not yet be defined, which is typical for in-tree.
259 # imported: Exported properties do not support generator expressions, so
260 # we imperatively query and manage the expansion here. This is fine
261 # because imported targets will always be found/configured first and
262 # do not need to support arbitrary ordering. If CMake every supports
263 # exporting generator expressions, then this can be simplified.
264 set(_is_imported OFF)
266 get_target_property(_is_imported ${lib} IMPORTED)
270 # Evaluate the in-tree generator expressions directly (this allows target
271 # order independence, since these aren't evaluated until the generate
273 # What these expressions do:
274 # In the context of this aggregate, resolve the list of OBJECTS and DEPS
275 # that each library advertises and patch it into the whole.
276 set(_local_objects $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${lib},MLIR_AGGREGATE_OBJECTS>>)
277 set(_local_deps $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${lib},MLIR_AGGREGATE_DEPS>>)
279 # It is an imported target, which can only have flat strings populated
280 # (no generator expressions).
281 # Rebuild the generator expressions from the imported flat string lists.
282 get_property(_has_object_lib_prop TARGET ${lib} PROPERTY MLIR_AGGREGATE_OBJECT_LIB_IMPORTED SET)
283 get_property(_has_dep_libs_prop TARGET ${lib} PROPERTY MLIR_AGGREGATE_DEP_LIBS_IMPORTED SET)
284 if(NOT _has_object_lib_prop OR NOT _has_dep_libs_prop)
285 message(SEND_ERROR "Cannot create an aggregate out of imported ${lib}: It is missing properties indicating that it was built for aggregation")
287 get_target_property(_imp_local_object_lib ${lib} MLIR_AGGREGATE_OBJECT_LIB_IMPORTED)
288 get_target_property(_imp_dep_libs ${lib} MLIR_AGGREGATE_DEP_LIBS_IMPORTED)
290 if(_imp_local_object_lib)
291 set(_local_objects "$<TARGET_OBJECTS:${_imp_local_object_lib}>")
293 # We should just be able to do this:
294 # get_mlir_filtered_link_libraries(_local_deps ${_imp_dep_libs})
295 # However, CMake complains about the unqualified use of the one-arg
296 # $<TARGET_PROPERTY> expression. So we do the same thing but use the
297 # two-arg form which takes an explicit target.
298 foreach(_imp_dep_lib ${_imp_dep_libs})
299 # In English, what this expression does:
300 # For each link library, resolve the property MLIR_AGGREGATE_EXCLUDE_LIBS
301 # on the context target (i.e. the executable or shared library being linked)
302 # and, if it is not in that list, emit the library name. Otherwise, empty.
303 list(APPEND _local_deps
304 "$<$<NOT:$<IN_LIST:${_imp_dep_lib},$<GENEX_EVAL:$<TARGET_PROPERTY:${name},MLIR_AGGREGATE_EXCLUDE_LIBS>>>>:${_imp_dep_lib}>"
309 list(APPEND _embed_libs ${lib})
310 list(APPEND _objects ${_local_objects})
311 list(APPEND _deps ${_local_deps})
313 string(APPEND _debugmsg
314 ": EMBED_LIB ${lib}:\n"
315 " OBJECTS = ${_local_objects}\n"
316 " DEPS = ${_local_deps}\n\n")
319 # Unfortunately need to compile at least one source file, which is hard
320 # to guarantee, so just always generate one. We generate one vs using the
321 # LLVM common dummy.cpp because it works better out of tree.
322 set(_empty_src "${CMAKE_CURRENT_BINARY_DIR}/${name}__empty.cpp")
323 file(WRITE "${_empty_src}" "typedef int dummy;")
325 add_mlir_library(${name}
327 ${ARG_UNPARSED_ARGUMENTS}
328 PARTIAL_SOURCES_INTENDED
335 target_sources(${name} PRIVATE ${_objects})
336 # TODO: Should be transitive.
337 set_target_properties(${name} PROPERTIES
338 MLIR_AGGREGATE_EXCLUDE_LIBS "${_embed_libs}")
340 set_property(TARGET ${name} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
342 string(APPEND _debugmsg
344 " OBJECTS = ${_objects}\n"
345 " SOURCES = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},SOURCES>>\n"
347 " LINK_LIBRARIES = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},LINK_LIBRARIES>>\n"
348 " MLIR_AGGREGATE_EXCLUDE_LIBS = $<TARGET_GENEX_EVAL:${name},$<TARGET_PROPERTY:${name},MLIR_AGGREGATE_EXCLUDE_LIBS>>\n"
351 "${CMAKE_CURRENT_BINARY_DIR}/${name}.aggregate_debug.txt"
352 CONTENT "${_debugmsg}"
354 endfunction(add_mlir_aggregate)
356 # Adds an MLIR library target for installation.
357 # This is usually done as part of add_mlir_library but is broken out for cases
358 # where non-standard library builds can be installed.
359 function(add_mlir_library_install name)
360 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
361 get_target_export_arg(${name} MLIR export_to_mlirtargets UMBRELLA mlir-libraries)
362 install(TARGETS ${name}
364 ${export_to_mlirtargets}
365 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
366 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
367 RUNTIME DESTINATION bin
368 # Note that CMake will create a directory like:
369 # objects-${CMAKE_BUILD_TYPE}/obj.LibName
370 # and put object files there.
371 OBJECTS DESTINATION lib${LLVM_LIBDIR_SUFFIX}
374 if (NOT LLVM_ENABLE_IDE)
375 add_llvm_install_targets(install-${name}
379 set_property(GLOBAL APPEND PROPERTY MLIR_ALL_LIBS ${name})
381 set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name})
384 # Declare an mlir library which is part of the public C-API.
385 function(add_mlir_public_c_api_library name)
386 add_mlir_library(${name}
390 ADDITIONAL_HEADER_DIRS
391 ${MLIR_MAIN_INCLUDE_DIR}/mlir-c
393 # API libraries compile with hidden visibility and macros that enable
394 # exporting from the DLL. Only apply to the obj lib, which only affects
395 # the exports via a shared library.
396 set_target_properties(obj.${name}
398 CXX_VISIBILITY_PRESET hidden
400 target_compile_definitions(obj.${name}
402 -DMLIR_CAPI_BUILDING_LIBRARY=1
406 # Declare the library associated with a dialect.
407 function(add_mlir_dialect_library name)
408 set_property(GLOBAL APPEND PROPERTY MLIR_DIALECT_LIBS ${name})
409 add_mlir_library(${ARGV} DEPENDS mlir-headers)
410 endfunction(add_mlir_dialect_library)
412 # Declare the library associated with a conversion.
413 function(add_mlir_conversion_library name)
414 set_property(GLOBAL APPEND PROPERTY MLIR_CONVERSION_LIBS ${name})
415 add_mlir_library(${ARGV} DEPENDS mlir-headers)
416 endfunction(add_mlir_conversion_library)
418 # Declare the library associated with a translation.
419 function(add_mlir_translation_library name)
420 set_property(GLOBAL APPEND PROPERTY MLIR_TRANSLATION_LIBS ${name})
421 add_mlir_library(${ARGV} DEPENDS mlir-headers)
422 endfunction(add_mlir_translation_library)
424 # Verification tools to aid debugging.
425 function(mlir_check_link_libraries name)
427 get_target_property(type ${name} TYPE)
428 if (${type} STREQUAL "INTERFACE_LIBRARY")
429 get_target_property(libs ${name} INTERFACE_LINK_LIBRARIES)
431 get_target_property(libs ${name} LINK_LIBRARIES)
433 # message("${name} libs are: ${libs}")
437 if(${lib} MATCHES "^LLVM$")
440 if((${lib} MATCHES "^LLVM.+") AND ${linking_llvm})
441 # This will almost always cause execution problems, since the
442 # same symbol might be loaded from 2 separate libraries. This
443 # often comes from referring to an LLVM library target
444 # explicitly in target_link_libraries()
445 message("WARNING: ${name} links LLVM and ${lib}!")
450 endfunction(mlir_check_link_libraries)
452 function(mlir_check_all_link_libraries name)
453 mlir_check_link_libraries(${name})
455 get_target_property(libs ${name} LINK_LIBRARIES)
456 # message("${name} libs are: ${libs}")
458 mlir_check_link_libraries(${lib})
461 endfunction(mlir_check_all_link_libraries)