[Mlir-commits] [mlir] e2b19ef - [mlir] mark libraries in mlir/examples as such

Alex Zinenko llvmlistbot at llvm.org
Tue Jun 20 04:02:48 PDT 2023


Author: Alex Zinenko
Date: 2023-06-20T13:02:42+02:00
New Revision: e2b19ef6ac7e5635699730ff561e7f24775704c9

URL: https://github.com/llvm/llvm-project/commit/e2b19ef6ac7e5635699730ff561e7f24775704c9
DIFF: https://github.com/llvm/llvm-project/commit/e2b19ef6ac7e5635699730ff561e7f24775704c9.diff

LOG: [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

Added: 
    

Modified: 
    mlir/cmake/modules/AddMLIR.cmake
    mlir/examples/transform/Ch2/lib/CMakeLists.txt
    mlir/examples/transform/Ch3/lib/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index f6fb8a639fde9..6acb01c305ad0 100644
--- a/mlir/cmake/modules/AddMLIR.cmake
+++ b/mlir/cmake/modules/AddMLIR.cmake
@@ -201,25 +201,9 @@ function(add_mlir_doc doc_filename output_file output_directory command)
   add_dependencies(mlir-doc ${output_file}DocGen)
 endfunction()
 
-# Declare an mlir library which can be compiled in libMLIR.so
-# In addition to everything that llvm_add_library accepts, this
-# also has the following option:
-# EXCLUDE_FROM_LIBMLIR
-#   Don't include this library in libMLIR.so.  This option should be used
-#   for test libraries, executable-specific libraries, or rarely used libraries
-#   with large dependencies.
-# ENABLE_AGGREGATION
-#   Forces generation of an OBJECT library, exports additional metadata,
-#   and installs additional object files needed to include this as part of an
-#   aggregate shared library.
-#   TODO: Make this the default for all MLIR libraries once all libraries
-#   are compatible with building an object library.
-function(add_mlir_library name)
-  cmake_parse_arguments(ARG
-    "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
-    ""
-    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
-    ${ARGN})
+# Sets ${srcs} to contain the list of additional headers for the target. Extra
+# arguments are included into the list of additional headers.
+function(_set_mlir_additional_headers_as_srcs)
   set(srcs)
   if(MSVC_IDE OR XCODE)
     # Add public headers
@@ -245,13 +229,83 @@ function(add_mlir_library name)
       endif()
     endif()
   endif(MSVC_IDE OR XCODE)
-  if(srcs OR ARG_ADDITIONAL_HEADERS)
+  if(srcs OR ARGN)
     set(srcs
       ADDITIONAL_HEADERS
       ${srcs}
-      ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
+      ${ARGN} # It may contain unparsed unknown args.
+      PARENT_SCOPE
       )
   endif()
+endfunction()
+
+# Checks that the LLVM components are not listed in the extra arguments,
+# assumed to be coming from the LINK_LIBS variable.
+function(_check_llvm_components_usage name)
+  # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
+  # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
+  # Perhaps this should be in llvm_add_library instead?  However, it fails
+  # on libclang-cpp.so
+  get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
+  foreach(lib ${ARGN})
+    if(${lib} IN_LIST llvm_component_libs)
+      message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries.  Please use LINK_COMPONENTS instead.")
+    endif()
+  endforeach()
+endfunction()
+
+function(add_mlir_example_library name)
+  cmake_parse_arguments(ARG
+    "SHARED;DISABLE_INSTALL"
+    ""
+    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
+    ${ARGN})
+  _set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS})
+  if (ARG_SHARED)
+    set(LIBTYPE SHARED)
+  else()
+    if(BUILD_SHARED_LIBS)
+      set(LIBTYPE SHARED)
+    else()
+      set(LIBTYPE STATIC)
+    endif()
+  endif()
+
+  # MLIR libraries uniformly depend on LLVMSupport.  Just specify it once here.
+  list(APPEND ARG_LINK_COMPONENTS Support)
+  _check_llvm_components_usage(${name} ${ARG_LINK_LIBS})
+
+  list(APPEND ARG_DEPENDS mlir-generic-headers)
+
+  llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})
+  set_target_properties(${name} PROPERTIES FOLDER "Examples")
+  if (LLVM_BUILD_EXAMPLES AND NOT ${ARG_DISABLE_INSTALL})
+    add_mlir_library_install(${name})
+  else()
+    set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL ON)
+  endif()
+endfunction()
+
+# Declare an mlir library which can be compiled in libMLIR.so
+# In addition to everything that llvm_add_library accepts, this
+# also has the following option:
+# EXCLUDE_FROM_LIBMLIR
+#   Don't include this library in libMLIR.so.  This option should be used
+#   for test libraries, executable-specific libraries, or rarely used libraries
+#   with large dependencies.
+# ENABLE_AGGREGATION
+#   Forces generation of an OBJECT library, exports additional metadata,
+#   and installs additional object files needed to include this as part of an
+#   aggregate shared library.
+#   TODO: Make this the default for all MLIR libraries once all libraries
+#   are compatible with building an object library.
+function(add_mlir_library name)
+  cmake_parse_arguments(ARG
+    "SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
+    ""
+    "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
+    ${ARGN})
+  _set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS})
 
   # Is an object library needed.
   set(NEEDS_OBJECT_LIB OFF)
@@ -287,17 +341,7 @@ function(add_mlir_library name)
 
   # MLIR libraries uniformly depend on LLVMSupport.  Just specify it once here.
   list(APPEND ARG_LINK_COMPONENTS Support)
-
-  # LINK_COMPONENTS is necessary to allow libLLVM.so to be properly
-  # substituted for individual library dependencies if LLVM_LINK_LLVM_DYLIB
-  # Perhaps this should be in llvm_add_library instead?  However, it fails
-  # on libclang-cpp.so
-  get_property(llvm_component_libs GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
-  foreach(lib ${ARG_LINK_LIBS})
-    if(${lib} IN_LIST llvm_component_libs)
-      message(SEND_ERROR "${name} specifies LINK_LIBS ${lib}, but LINK_LIBS cannot be used for LLVM libraries.  Please use LINK_COMPONENTS instead.")
-    endif()
-  endforeach()
+  _check_llvm_components_usage(${name} ${ARG_LINK_LIBS})
 
   list(APPEND ARG_DEPENDS mlir-generic-headers)
   llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs} DEPENDS ${ARG_DEPENDS} LINK_COMPONENTS ${ARG_LINK_COMPONENTS} LINK_LIBS ${ARG_LINK_LIBS})

diff  --git a/mlir/examples/transform/Ch2/lib/CMakeLists.txt b/mlir/examples/transform/Ch2/lib/CMakeLists.txt
index c89cd97006eb5..9c2327284292e 100644
--- a/mlir/examples/transform/Ch2/lib/CMakeLists.txt
+++ b/mlir/examples/transform/Ch2/lib/CMakeLists.txt
@@ -1,14 +1,11 @@
-add_mlir_library(
+# Outside examples, this should be `add_mlir_library`.
+add_mlir_example_library(
   # Library called MyExtension.
   MyExtensionCh2
 
   # Built from the following source files.
   MyExtension.cpp
 
-  # Do not make this part of the main library distribution.
-  # Needed for the example only.
-  EXCLUDE_FROM_LIBMLIR
-
   # Make includes visible without top-level path.
   ADDITIONAL_HEADER_DIRS
   ${PROJECT_SOURCE_DIR}/examples/transform/Ch2/include
@@ -18,7 +15,7 @@ add_mlir_library(
   MyExtensionCh2IncGen
 
   # Link in the transform dialect, an all generated dialects.
-  LINK_LIBS PUBLIC
+  LINK_LIBS PRIVATE
   MLIRTransformDialect
   MLIRFuncDialect
   MLIRSCFDialect

diff  --git a/mlir/examples/transform/Ch3/lib/CMakeLists.txt b/mlir/examples/transform/Ch3/lib/CMakeLists.txt
index b43f79cc91a68..2c04a8bab85a2 100644
--- a/mlir/examples/transform/Ch3/lib/CMakeLists.txt
+++ b/mlir/examples/transform/Ch3/lib/CMakeLists.txt
@@ -1,14 +1,11 @@
-add_mlir_library(
+# Outside examples, this should be `add_mlir_library`.
+add_mlir_example_library(
   # Library called MyExtension.
   MyExtensionCh3
 
   # Built from the following source files.
   MyExtension.cpp
 
-  # Do not make this part of the main library distribution.
-  # Needed for the example only.
-  EXCLUDE_FROM_LIBMLIR
-
   # Make includes visible without top-level path.
   ADDITIONAL_HEADER_DIRS
   ${PROJECT_SOURCE_DIR}/examples/transform/Ch3/include
@@ -18,7 +15,7 @@ add_mlir_library(
   MyExtensionCh3IncGen
 
   # Link in the transform dialect, an all generated dialects.
-  LINK_LIBS PUBLIC
+  LINK_LIBS PRIVATE
   MLIRTransformDialect
   MLIRFuncDialect
   MLIRSCFDialect


        


More information about the Mlir-commits mailing list