Skip to content

Commit e2b19ef

Browse files
committed
[mlir] mark libraries in mlir/examples as such
LLVM build system separates between `add_llvm_example_library` and `add_llvm_library`, which is presumably used to package examples separately from the regular library. Introduce a similar approach to building example libraries in MLIR and use it for the transform dialect tutorial. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D153265
1 parent ef9159c commit e2b19ef

File tree

3 files changed

+82
-44
lines changed

3 files changed

+82
-44
lines changed

mlir/cmake/modules/AddMLIR.cmake

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -201,25 +201,9 @@ function(add_mlir_doc doc_filename output_file output_directory command)
201201
add_dependencies(mlir-doc ${output_file}DocGen)
202202
endfunction()
203203

204-
# Declare an mlir library which can be compiled in libMLIR.so
205-
# In addition to everything that llvm_add_library accepts, this
206-
# also has the following option:
207-
# EXCLUDE_FROM_LIBMLIR
208-
# Don't include this library in libMLIR.so. This option should be used
209-
# for test libraries, executable-specific libraries, or rarely used libraries
210-
# with large dependencies.
211-
# ENABLE_AGGREGATION
212-
# Forces generation of an OBJECT library, exports additional metadata,
213-
# and installs additional object files needed to include this as part of an
214-
# aggregate shared library.
215-
# TODO: Make this the default for all MLIR libraries once all libraries
216-
# are compatible with building an object library.
217-
function(add_mlir_library name)
218-
cmake_parse_arguments(ARG
219-
"SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
220-
""
221-
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
222-
${ARGN})
204+
# Sets ${srcs} to contain the list of additional headers for the target. Extra
205+
# arguments are included into the list of additional headers.
206+
function(_set_mlir_additional_headers_as_srcs)
223207
set(srcs)
224208
if(MSVC_IDE OR XCODE)
225209
# Add public headers
@@ -245,13 +229,83 @@ function(add_mlir_library name)
245229
endif()
246230
endif()
247231
endif(MSVC_IDE OR XCODE)
248-
if(srcs OR ARG_ADDITIONAL_HEADERS)
232+
if(srcs OR ARGN)
249233
set(srcs
250234
ADDITIONAL_HEADERS
251235
${srcs}
252-
${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
236+
${ARGN} # It may contain unparsed unknown args.
237+
PARENT_SCOPE
253238
)
254239
endif()
240+
endfunction()
241+
242+
# Checks that the LLVM components are not listed in the extra arguments,
243+
# assumed to be coming from the LINK_LIBS variable.
244+
function(_check_llvm_components_usage name)
245+
# LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
246+
# substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
247+
# Perhaps this should be in llvm_add_library instead? However, it fails
248+
# on libclang-cpp.so
249+
get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
250+
foreach(lib ${ARGN})
251+
if(${lib} IN_LIST llvm_component_libs)
252+
message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries. Please use LINK_COMPONENTS instead.")
253+
endif()
254+
endforeach()
255+
endfunction()
256+
257+
function(add_mlir_example_library name)
258+
cmake_parse_arguments(ARG
259+
"SHARED;DISABLE_INSTALL"
260+
""
261+
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
262+
${ARGN})
263+
_set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS})
264+
if (ARG_SHARED)
265+
set(LIBTYPE SHARED)
266+
else()
267+
if(BUILD_SHARED_LIBS)
268+
set(LIBTYPE SHARED)
269+
else()
270+
set(LIBTYPE STATIC)
271+
endif()
272+
endif()
273+
274+
# MLIR libraries uniformly depend on LLVMSupport. Just specify it once here.
275+
list(APPEND ARG_LINK_COMPONENTS Support)
276+
_check_llvm_components_usage(${name} ${ARG_LINK_LIBS})
277+
278+
list(APPEND ARG_DEPENDS mlir-generic-headers)
279+
280+
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})
281+
set_target_properties(${name} PROPERTIES FOLDER "Examples")
282+
if (LLVM_BUILD_EXAMPLES AND NOT ${ARG_DISABLE_INSTALL})
283+
add_mlir_library_install(${name})
284+
else()
285+
set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL ON)
286+
endif()
287+
endfunction()
288+
289+
# Declare an mlir library which can be compiled in libMLIR.so
290+
# In addition to everything that llvm_add_library accepts, this
291+
# also has the following option:
292+
# EXCLUDE_FROM_LIBMLIR
293+
# Don't include this library in libMLIR.so. This option should be used
294+
# for test libraries, executable-specific libraries, or rarely used libraries
295+
# with large dependencies.
296+
# ENABLE_AGGREGATION
297+
# Forces generation of an OBJECT library, exports additional metadata,
298+
# and installs additional object files needed to include this as part of an
299+
# aggregate shared library.
300+
# TODO: Make this the default for all MLIR libraries once all libraries
301+
# are compatible with building an object library.
302+
function(add_mlir_library name)
303+
cmake_parse_arguments(ARG
304+
"SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
305+
""
306+
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
307+
${ARGN})
308+
_set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS})
255309

256310
# Is an object library needed.
257311
set(NEEDS_OBJECT_LIB OFF)
@@ -287,17 +341,7 @@ function(add_mlir_library name)
287341

288342
# MLIR libraries uniformly depend on LLVMSupport. Just specify it once here.
289343
list(APPEND ARG_LINK_COMPONENTS Support)
290-
291-
# LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
292-
# substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
293-
# Perhaps this should be in llvm_add_library instead? However, it fails
294-
# on libclang-cpp.so
295-
get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
296-
foreach(lib ${ARG_LINK_LIBS})
297-
if(${lib} IN_LIST llvm_component_libs)
298-
message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries. Please use LINK_COMPONENTS instead.")
299-
endif()
300-
endforeach()
344+
_check_llvm_components_usage(${name} ${ARG_LINK_LIBS})
301345

302346
list(APPEND ARG_DEPENDS mlir-generic-headers)
303347
llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})

mlir/examples/transform/Ch2/lib/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
add_mlir_library(
1+
# Outside examples, this should be `add_mlir_library`.
2+
add_mlir_example_library(
23
# Library called MyExtension.
34
MyExtensionCh2
45

56
# Built from the following source files.
67
MyExtension.cpp
78

8-
# Do not make this part of the main library distribution.
9-
# Needed for the example only.
10-
EXCLUDE_FROM_LIBMLIR
11-
129
# Make includes visible without top-level path.
1310
ADDITIONAL_HEADER_DIRS
1411
${PROJECT_SOURCE_DIR}/examples/transform/Ch2/include
@@ -18,7 +15,7 @@ add_mlir_library(
1815
MyExtensionCh2IncGen
1916

2017
# Link in the transform dialect, an all generated dialects.
21-
LINK_LIBS PUBLIC
18+
LINK_LIBS PRIVATE
2219
MLIRTransformDialect
2320
MLIRFuncDialect
2421
MLIRSCFDialect

mlir/examples/transform/Ch3/lib/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
add_mlir_library(
1+
# Outside examples, this should be `add_mlir_library`.
2+
add_mlir_example_library(
23
# Library called MyExtension.
34
MyExtensionCh3
45

56
# Built from the following source files.
67
MyExtension.cpp
78

8-
# Do not make this part of the main library distribution.
9-
# Needed for the example only.
10-
EXCLUDE_FROM_LIBMLIR
11-
129
# Make includes visible without top-level path.
1310
ADDITIONAL_HEADER_DIRS
1411
${PROJECT_SOURCE_DIR}/examples/transform/Ch3/include
@@ -18,7 +15,7 @@ add_mlir_library(
1815
MyExtensionCh3IncGen
1916

2017
# Link in the transform dialect, an all generated dialects.
21-
LINK_LIBS PUBLIC
18+
LINK_LIBS PRIVATE
2219
MLIRTransformDialect
2320
MLIRFuncDialect
2421
MLIRSCFDialect

0 commit comments

Comments
 (0)