[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:27:05 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: miyan (miyanyan)
<details>
<summary>Changes</summary>
## 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.
---
Full diff: https://github.com/llvm/llvm-project/pull/225794.diff
1 Files Affected:
- (modified) mlir/cmake/modules/AddMLIRPython.cmake (+5-1)
``````````diff
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 1e483116569a0..e117f26472001 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/225794
More information about the Mlir-commits
mailing list