[Mlir-commits] [mlir] [MLIR][Python] Drop the LLVMSupport link dependency from the python extensions (PR #213509)

Maksim Levental llvmlistbot at llvm.org
Sat Aug 1 23:54:54 PDT 2026


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/213509

>From 0ff552244ade386b2e1b900bf19754937ef58e9c Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Sat, 1 Aug 2026 23:45:56 -0700
Subject: [PATCH 1/2] [MLIR][Python] Drop the LLVMSupport link dependency from
 the python extensions

#180986 switched the last python bindings off the C++ LLVM APIs and dropped
LLVMSupport from the support library in AddMLIRPython.cmake, but the
per-extension `PRIVATE_LINK_LIBS LLVMSupport` in mlir/python/CMakeLists.txt
was missed. `declare_mlir_python_extension`'s PRIVATE_LINK_LIBS becomes
`target_link_libraries(... INTERFACE ...)`, so every extension module still
links libLLVMSupport.a. None of the extension sources reference LLVM any more:
the only remaining matches for `llvm::` are two comments and a namespace that
happens to be called `llvm`.

Statically linking LLVMSupport into every extension gives each one its own copy
of the CommandLine.cpp globals. Where the loader resolves symbols across modules
rather than keeping them module-local, whichever copy initializes second aborts
the process:

  CommandLine Error: Option 'print-inst-addrs' registered more than once!

That is what happens with the emscripten build of the bindings, where the
extensions are side modules sharing one global symbol table. The two extensions
that a downstream project declares itself, without PRIVATE_LINK_LIBS, are ~5KB
and load fine; the 19 declared here are ~1.9MB each and abort on load.

Also note the constraint on PRIVATE_LINK_LIBS in the docs for
declare_mlir_python_extension, mirroring the comment #180986 left on the
support library.
---
 mlir/cmake/modules/AddMLIRPython.cmake        |  4 +-
 .../examples/standalone/python/CMakeLists.txt |  2 -
 mlir/python/CMakeLists.txt                    | 39 -------------------
 3 files changed, 3 insertions(+), 42 deletions(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 7d47fd0bca34f..6cfc64bcb307b 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -249,7 +249,9 @@ endfunction()
 #   SOURCES: C++ sources making up the module.
 #   PRIVATE_LINK_LIBS: List of libraries to link in privately to the module
 #     regardless of how it is included in the project (generally should be
-#     static libraries that can be included with hidden visibility).
+#     static libraries that can be included with hidden visibility). Do not
+#     list C++ LLVM/MLIR libraries here; use the C++ standard library instead,
+#     or wrap the functionality in the C API first.
 #   EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
 #     on. These will be collected for all extensions and put into an
 #     aggregate dylib that is linked against.
diff --git a/mlir/examples/standalone/python/CMakeLists.txt b/mlir/examples/standalone/python/CMakeLists.txt
index edaedf18cc843..6a0d0db6dbf6c 100644
--- a/mlir/examples/standalone/python/CMakeLists.txt
+++ b/mlir/examples/standalone/python/CMakeLists.txt
@@ -25,8 +25,6 @@ declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
   ADD_TO_PARENT StandalonePythonSources
   SOURCES
     StandaloneExtensionNanobind.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIArith
diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index 54d915fac3d48..aa0adea66d2fe 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -568,8 +568,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Core
     # Headers must be included explicitly so they are installed.
     Pass.h
     Rewrite.h
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIDebug
     MLIRCAPIIR
@@ -591,8 +589,6 @@ declare_mlir_python_extension(MLIRPythonExtension.RegisterEverything
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     RegisterEverything.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIConversion
     MLIRCAPITransforms
@@ -605,8 +601,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.Linalg.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectLinalg.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPILinalg
@@ -618,8 +612,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.GPU.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectGPU.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIGPU
@@ -631,8 +623,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.LLVM.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectLLVM.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPILLVM
@@ -646,8 +636,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectQuant.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIQuant
@@ -659,8 +647,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.NVGPU.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectNVGPU.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPINVGPU
@@ -672,8 +658,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectPDL.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIPDL
@@ -685,8 +669,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.SparseTensor.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectSparseTensor.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPISparseTensor
@@ -700,7 +682,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.Transform.Nanobind
     DialectTransform.cpp
     Rewrite.h
   PRIVATE_LINK_LIBS
-    LLVMSupport
     MLIRPythonExtension.Core
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
@@ -713,8 +694,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.IRDL.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectIRDL.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIIRDL
@@ -726,8 +705,6 @@ declare_mlir_python_extension(MLIRPythonExtension.AsyncDialectPasses
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     AsyncPasses.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIAsync
 )
@@ -739,8 +716,6 @@ if(MLIR_ENABLE_EXECUTION_ENGINE)
     ROOT_DIR "${PYTHON_SOURCE_DIR}"
     SOURCES
       ExecutionEngineModule.cpp
-    PRIVATE_LINK_LIBS
-      LLVMSupport
     EMBED_CAPI_LINK_LIBS
       MLIRCAPIExecutionEngine
   )
@@ -752,8 +727,6 @@ declare_mlir_python_extension(MLIRPythonExtension.GPUDialectPasses
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     GPUPasses.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIGPU
 )
@@ -764,8 +737,6 @@ declare_mlir_python_extension(MLIRPythonExtension.LinalgPasses
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     LinalgPasses.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPILinalg
 )
@@ -776,8 +747,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.SMT.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectSMT.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPISMT
@@ -790,8 +759,6 @@ declare_mlir_python_extension(MLIRPythonExtension.SparseTensorDialectPasses
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     SparseTensorPasses.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPISparseTensor
 )
@@ -802,8 +769,6 @@ declare_mlir_python_extension(MLIRPythonExtension.TransformInterpreter
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     TransformInterpreter.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPITransformDialectTransforms
 )
@@ -814,8 +779,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.AMDGPU.Nanobind
   ROOT_DIR "${PYTHON_SOURCE_DIR}"
   SOURCES
     DialectAMDGPU.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
   EMBED_CAPI_LINK_LIBS
     MLIRCAPIIR
     MLIRCAPIAMDGPU
@@ -861,8 +824,6 @@ if(MLIR_INCLUDE_TESTS)
     ROOT_DIR "${MLIR_SOURCE_DIR}/test/python/lib"
     SOURCES
       PythonTestModuleNanobind.cpp
-    PRIVATE_LINK_LIBS
-      LLVMSupport
     EMBED_CAPI_LINK_LIBS
       MLIRCAPIPythonTestDialect
   )

>From 3a09d9f625ce20313994805585aeebac080458b0 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Sat, 1 Aug 2026 23:54:46 -0700
Subject: [PATCH 2/2] Update AddMLIRPython.cmake

restore doc string
---
 mlir/cmake/modules/AddMLIRPython.cmake | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 6cfc64bcb307b..7d47fd0bca34f 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -249,9 +249,7 @@ endfunction()
 #   SOURCES: C++ sources making up the module.
 #   PRIVATE_LINK_LIBS: List of libraries to link in privately to the module
 #     regardless of how it is included in the project (generally should be
-#     static libraries that can be included with hidden visibility). Do not
-#     list C++ LLVM/MLIR libraries here; use the C++ standard library instead,
-#     or wrap the functionality in the C API first.
+#     static libraries that can be included with hidden visibility).
 #   EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
 #     on. These will be collected for all extensions and put into an
 #     aggregate dylib that is linked against.



More information about the Mlir-commits mailing list