[Mlir-commits] [mlir] [mlir][CMake] This patch modifies the behavior of `add_mlir_aggregate ` and adds `mlir_c_target_link_libraries`. (PR #141435)

Fabian Mora llvmlistbot at llvm.org
Sun May 25 17:02:23 PDT 2025


https://github.com/fabianmcg created https://github.com/llvm/llvm-project/pull/141435

This patch modifies `add_mlir_aggregate` to link against `libMLIR.so` if `MLIR_LINK_MLIR_DYLIB` is set. This means that aggregates that are present in `libMLIR.so` are not added to the final aggregate library.

This allows using `libMLIR.so` as a single source of truth, making MLIR easier to be distributed.

This patch also adds `mlir_c_target_link_libraries`, the behavior is identical to `mlir_target_link_libraries` except it targets `libMLIR-C.so`.

It also makes the python CAPI depend on `libMLIR-C.so` if `MLIR_BUILD_MLIR_C_DYLIB=1`.

Finally, it fixes some ill-configured library dependencies.

>From ce5ac91a3b0fc3cdf6fe9559c817d49df99e4263 Mon Sep 17 00:00:00 2001
From: Fabian Mora <6982088+fabianmcg at users.noreply.github.com>
Date: Sun, 25 May 2025 22:09:15 +0000
Subject: [PATCH] [mlir][CMake] This patch modifies the behavior of
 `add_mlir_aggregate` and adds `mlir_c_target_link_libraries`.

This patch modifies `add_mlir_aggregate` to link against `libMLIR.so` if
`MLIR_LINK_MLIR_DYLIB` is set. This means that aggregates that are present in
`libMLIR.so` are not added to the final aggregate library.

This allows using `libMLIR.so` as a single source of truth, making MLIR
easier to be distributed.

This patch also adds `mlir_c_target_link_libraries`, the behavior is identical
to `mlir_target_link_libraries` except it targets `libMLIR-C.so`.

It also makes the python CAPI depend on `libMLIR-C.so` if
`MLIR_BUILD_MLIR_C_DYLIB=1`.

Finally, it fixes some ill-configured library dependencies.
---
 mlir/cmake/modules/AddMLIR.cmake             | 57 +++++++++++++++++++-
 mlir/cmake/modules/AddMLIRPython.cmake       |  6 +++
 mlir/lib/CAPI/CMakeLists.txt                 |  1 +
 mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt |  2 -
 mlir/python/CMakeLists.txt                   |  9 +++-
 mlir/test/python/lib/CMakeLists.txt          |  7 ++-
 6 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index 353e64b3d013e..150694d2e85fb 100644
--- a/mlir/cmake/modules/AddMLIR.cmake
+++ b/mlir/cmake/modules/AddMLIR.cmake
@@ -475,12 +475,14 @@ endfunction(get_mlir_filtered_link_libraries)
 #   EMBED_LIBS: list of dependent libraries that should be embedded directly
 #     into this library. Each of these must be an add_mlir_library() library
 #     without DISABLE_AGGREGATE.
+#   KEEP_CAPI: If enabled and `MLIR_BUILD_MLIR_C_DYLIB` and
+#   `MLIR_LINK_MLIR_DYLIB`, CAPI aggregates are not removed from the library.
 #
 # Note: This is a work in progress and is presently only sufficient for certain
 # non nested cases involving the C-API.
 function(add_mlir_aggregate name)
   cmake_parse_arguments(ARG
-    "SHARED;STATIC"
+    "SHARED;STATIC;KEEP_CAPI"
     ""
     "PUBLIC_LIBS;EMBED_LIBS"
     ${ARGN})
@@ -493,6 +495,19 @@ function(add_mlir_aggregate name)
   endif()
   set(_debugmsg)
 
+  # If `MLIR_LINK_MLIR_DYLIB == 1` remove all the libraries in `libMLIR.so` from
+  # `EMBED_LIBS`.
+  get_property(mlir_libs GLOBAL PROPERTY MLIR_STATIC_LIBS)
+  if (MLIR_LINK_MLIR_DYLIB)
+    # If `libMLIR-C.so` is available and not `KEEP_CAPI`, remove MLIR CAPI
+    # libraries from `EMBED_LIBS`.
+    if (MLIR_BUILD_MLIR_C_DYLIB AND NOT ARG_KEEP_CAPI)
+      get_property(mlir_c_libs GLOBAL PROPERTY MLIR_CAPI_LIBS)
+      list(APPEND mlir_libs ${mlir_c_libs})
+    endif()
+    list(REMOVE_ITEM ARG_EMBED_LIBS ${mlir_libs})
+  endif()
+
   set(_embed_libs)
   set(_objects)
   set(_deps)
@@ -565,6 +580,26 @@ function(add_mlir_aggregate name)
       "    OBJECTS = ${_local_objects}\n"
       "    DEPS = ${_local_deps}\n\n")
   endforeach()
+  
+  if (MLIR_LINK_MLIR_DYLIB)
+    # This builds a regex with all the libraries contained in `libMLIR.so`.
+    # These libraries will be filtered out from the dependencies added by the
+    # EMBED_LIBS.
+    list(GET mlir_libs 0 _libs_regex)
+    list(REMOVE_AT mlir_libs 0)
+    set(_libs_regex "(${_libs_regex}")
+    foreach (lib ${mlir_libs})
+      string(APPEND _libs_regex "|${lib}")
+    endforeach()
+    foreach (lib ${ARG_EMBED_LIBS})
+      string(APPEND _libs_regex "|${lib}")
+    endforeach()
+    string(APPEND _libs_regex ")")
+    list(APPEND _deps "MLIR")
+    # TODO: On CMake >= 3.27 the `$<LIST:REMOVE_ITEM,list,value,...>` expression
+    # is available, migrate to it once LLVM bumps the CMake version.
+    set(_deps $<REMOVE_DUPLICATES:$<FILTER:${_deps},EXCLUDE,${_libs_regex}>>)
+  endif()
 
   add_mlir_library(${name}
     ${_libtype}
@@ -745,3 +780,23 @@ function(mlir_target_link_libraries target type)
     target_link_libraries(${target} ${type} ${ARGN})
   endif()
 endfunction()
+
+# Link target against a list of MLIR CAPI libraries. If MLIR_BUILD_MLIR_C_DYLIB
+# is enabled, this will link against the MLIR CAPI dylib instead of the static
+# libraries.
+#
+# This function should be used instead of target_link_libraries() when linking
+# MLIR libraries that are part of the MLIR CAPI dylib. For libraries that are
+# not part of the dylib (like test libraries), target_link_libraries() should be
+# used.
+function(mlir_c_target_link_libraries target type)
+  if (TARGET obj.${target})
+    target_link_libraries(obj.${target} ${ARGN})
+  endif()
+
+  if (MLIR_BUILD_MLIR_C_DYLIB)
+    target_link_libraries(${target} ${type} MLIR-C)
+  else()
+    target_link_libraries(${target} ${type} ${ARGN})
+  endif()
+endfunction()
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index c2616ea57acef..491a12addb396 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -494,11 +494,17 @@ function(add_mlir_python_common_capi_library name)
   endforeach()
   list(REMOVE_DUPLICATES _embed_libs)
 
+  # If `libMLIR-C.so` is present link against it.
+  if (MLIR_BUILD_MLIR_C_DYLIB)
+    list(APPEND _dylibs "MLIR-C")
+  endif()
+
   # Generate the aggregate .so that everything depends on.
   add_mlir_aggregate(${name}
     SHARED
     DISABLE_INSTALL
     EMBED_LIBS ${_embed_libs}
+    PUBLIC_LIBS ${_dylibs}
   )
 
   # Process any headers associated with the library
diff --git a/mlir/lib/CAPI/CMakeLists.txt b/mlir/lib/CAPI/CMakeLists.txt
index 6c438508425b7..5ab3f73353aa0 100644
--- a/mlir/lib/CAPI/CMakeLists.txt
+++ b/mlir/lib/CAPI/CMakeLists.txt
@@ -26,6 +26,7 @@ if(MLIR_BUILD_MLIR_C_DYLIB)
   get_property(_capi_libraries GLOBAL PROPERTY MLIR_CAPI_LIBS)
   add_mlir_aggregate(MLIR-C
     SHARED
+    KEEP_CAPI
     EMBED_LIBS
       ${_capi_libraries}
   )
diff --git a/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt b/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt
index bf7dff897ab6d..8e0ab3188763d 100644
--- a/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt
+++ b/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt
@@ -10,7 +10,5 @@ add_mlir_upstream_c_api_library(MLIRCAPIExecutionEngine
   ExecutionEngine.cpp
 
   LINK_LIBS PUBLIC
-  MLIRBuiltinToLLVMIRTranslation
   MLIRExecutionEngine
-  MLIRLLVMToLLVMIRTranslation
 )
diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index e3934fc9f3511..c1a188b7de1f1 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -819,6 +819,12 @@ endif()
 # This must come last.
 ################################################################################
 
+set(MLIRPythonCAPI_LIBS "MLIRPythonCAPI")
+
+if(MLIR_BUILD_MLIR_C_DYLIB)
+  list(APPEND MLIRPythonCAPI_LIBS "MLIR-C")
+endif()
+
 add_mlir_python_modules(MLIRPythonModules
   ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir"
   INSTALL_PREFIX "python_packages/mlir_core/mlir"
@@ -827,6 +833,5 @@ add_mlir_python_modules(MLIRPythonModules
     MLIRPythonExtension.RegisterEverything
     ${_ADDL_TEST_SOURCES}
   COMMON_CAPI_LINK_LIBS
-    MLIRPythonCAPI
+    ${MLIRPythonCAPI_LIBS}
 )
-
diff --git a/mlir/test/python/lib/CMakeLists.txt b/mlir/test/python/lib/CMakeLists.txt
index 9a813dace2f54..59c061a82b6c0 100644
--- a/mlir/test/python/lib/CMakeLists.txt
+++ b/mlir/test/python/lib/CMakeLists.txt
@@ -26,8 +26,11 @@ add_mlir_public_c_api_library(MLIRCAPIPythonTestDialect
   MLIRPythonTestIncGen
 
   LINK_LIBS PUBLIC
-  MLIRCAPIInterfaces
-  MLIRCAPIIR
   MLIRPythonTestDialect
 )
 
+mlir_c_target_link_libraries(MLIRCAPIPythonTestDialect
+  PUBLIC
+  MLIRCAPIInterfaces
+  MLIRCAPIIR
+)



More information about the Mlir-commits mailing list