[Mlir-commits] [mlir] [mlir][python] Link Windows stable ABI extensions against Python::SABIModule (PR #225794)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Sep 23 07:26:02 PDT 2026


https://github.com/miyanyan created https://github.com/llvm/llvm-project/pull/225794

## Summary

When `MLIR_ENABLE_PYTHON_STABLE_ABI` is enabled on Windows, `add_mlir_python_extension` still links every extension and shared support library against `Python3_LIBRARY_RELEASE`. This adds a dependency on a versioned Python DLL and prevents the resulting abi3 modules from loading with another Python version.

Link against `Python::SABIModule` for stable ABI builds instead. Keep the existing release-library behavior for non-stable and free-threaded builds.

## Testing

Configured current upstream `main` (9120c137) with Python 3.12.11, nanobind 2.9.2, clang-cl, Ninja, `MLIR_ENABLE_BINDINGS_PYTHON=ON`, and `MLIR_ENABLE_PYTHON_STABLE_ABI=ON`, then built `_mlir.pyd`.

Before the change, `llvm-readobj --coff-imports` showed `python312.dll` in both `_mlir.pyd` and `MLIRPythonSupport-mlir.dll`. Python 3.12 could import and parse `module {}`, while Python 3.14 failed to load `_mlir`.

After the change, both binaries import only `python3.dll`. The same build imports and parses `module {}` with Python 3.12 and 3.14. `git diff --check` passes.


>From 6ef6d4b3af098b6d2afb4a85aee831a8dbd769ba Mon Sep 17 00:00:00 2001
From: miyanyan <1138989048 at qq.com>
Date: Wed, 23 Sep 2026 22:25:06 +0800
Subject: [PATCH] [mlir][python] Link stable ABI extensions with python3.dll on
 Windows

---
 mlir/cmake/modules/AddMLIRPython.cmake | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 1e483116569a06..e117f26472001b 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -1129,7 +1129,11 @@ function(add_mlir_python_extension libname extname nb_library_target_name)
       $<$<PLATFORM_ID:Linux>:LINKER:--exclude-libs,ALL>
   )
 
-  if(WIN32)
+  if(WIN32 AND MLIR_ENABLE_PYTHON_STABLE_ABI AND NOT (NB_ABI MATCHES "[0-9]t"))
+    # Linking the versioned Python library would prevent loading the stable
+    # ABI extension with a different Python version.
+    target_link_libraries(${libname} PRIVATE Python::SABIModule)
+  elseif(WIN32)
     # On Windows, pyconfig.h (and by extension python.h) hardcode the version of the
     # python library which will be used for linkage depending on the flavor of the build.
     # nanobind has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG



More information about the Mlir-commits mailing list