Skip to content

Conversation

@makslevental
Copy link
Contributor

@makslevental makslevental commented Dec 17, 2025

This PR removes pybind which has been deprecated for over a year (#117922).

@makslevental
Copy link
Contributor Author

makslevental commented Dec 17, 2025

Note, unlike the previous attempt, no FetchContent_Declare is used (we still expect nanobind to be in the environment somewhere).

@makslevental makslevental marked this pull request as ready for review December 17, 2025 00:50
@llvmbot llvmbot added infrastructure Bugs about LLVM infrastructure mlir:core MLIR Core Infrastructure mlir:linalg mlir:python MLIR Python bindings mlir bazel "Peripheral" support tier build system: utils/bazel labels Dec 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir-linalg

Author: Maksim Levental (makslevental)

Changes

This PR removes pybind which has been deprecated for almost a year (#117922).


Patch is 67.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/172581.diff

17 Files Affected:

  • (modified) .ci/all_requirements.txt (-4)
  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+40-57)
  • (modified) mlir/cmake/modules/MLIRDetectPythonEnv.cmake (+33-76)
  • (modified) mlir/docs/Dialects/Linalg/OpDSL.md (+2-2)
  • (modified) mlir/examples/standalone/python/CMakeLists.txt (-18)
  • (removed) mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp (-38)
  • (removed) mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py (-6)
  • (modified) mlir/examples/standalone/test/python/smoketest.py (+1-10)
  • (removed) mlir/include/mlir/Bindings/Python/PybindAdaptors.h (-616)
  • (modified) mlir/python/CMakeLists.txt (+16-47)
  • (modified) mlir/python/mlir/dialects/python_test.py (+3-8)
  • (modified) mlir/python/requirements.txt (-1)
  • (modified) mlir/test/python/dialects/python_test.py (+9-22)
  • (modified) mlir/test/python/lib/CMakeLists.txt (-1)
  • (removed) mlir/test/python/lib/PythonTestModulePybind11.cpp (-118)
  • (modified) mlir/tools/mlir-linalg-ods-gen/update_core_linalg_named_ops.sh.in (+1-1)
  • (modified) utils/bazel/llvm-project-overlay/mlir/BUILD.bazel (+19-35)
diff --git a/.ci/all_requirements.txt b/.ci/all_requirements.txt
index 4918d7519291f..32c1f43b7f9a1 100644
--- a/.ci/all_requirements.txt
+++ b/.ci/all_requirements.txt
@@ -443,10 +443,6 @@ pyasn1-modules==0.4.2 \
     --hash=sha256:29253a9207ce32b64c3ac6600edc75368f98473906e8fd1043bd6b5b1de2c14a \
     --hash=sha256:677091de870a80aae844b1ca6134f54652fa2c8c5a52aa396440ac3106e941e6
     # via google-auth
-pybind11==2.13.6 \
-    --hash=sha256:237c41e29157b962835d356b370ededd57594a26d5894a795960f0047cb5caf5 \
-    --hash=sha256:ba6af10348c12b24e92fa086b39cfba0eff619b61ac77c406167d813b096d39a
-    # via -r mlir/python/requirements.txt
 pycparser==2.23 \
     --hash=sha256:78816d4f24add8f10a06d6f05b4d424ad9e96cfebf68a4ddc99c65c0720d00c2 \
     --hash=sha256:e5c6e8d3fbad53479cab09ac03729e0a9faf2bee3db8208a550daf5af81a5934
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 8196e2a2a3321..ca90151e76268 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -123,12 +123,12 @@ function(mlir_generate_type_stubs)
     "IMPORT_PATHS;DEPENDS_TARGETS;OUTPUTS;DEPENDS_TARGET_SRC_DEPS"
     ${ARGN})
 
-  # for people doing find_package(nanobind)
+  # for people installing a distro (e.g., pip install) of nanobind
   if(EXISTS ${nanobind_DIR}/../src/stubgen.py)
     set(NB_STUBGEN "${nanobind_DIR}/../src/stubgen.py")
   elseif(EXISTS ${nanobind_DIR}/../stubgen.py)
     set(NB_STUBGEN "${nanobind_DIR}/../stubgen.py")
-  # for people using FetchContent_Declare and FetchContent_MakeAvailable
+  # for people using nanobind git source tree (e.g., FetchContent_Declare and FetchContent_MakeAvailable)
   elseif(EXISTS ${nanobind_SOURCE_DIR}/src/stubgen.py)
     set(NB_STUBGEN "${nanobind_SOURCE_DIR}/src/stubgen.py")
   elseif(EXISTS ${nanobind_SOURCE_DIR}/stubgen.py)
@@ -226,11 +226,10 @@ endfunction()
 #   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.
-#   PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
 function(declare_mlir_python_extension name)
   cmake_parse_arguments(ARG
     ""
-    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY"
+    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
     "SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
     ${ARGN})
 
@@ -239,20 +238,15 @@ function(declare_mlir_python_extension name)
   endif()
   set(_install_destination "src/python/${name}")
 
-  if(NOT ARG_PYTHON_BINDINGS_LIBRARY)
-    set(ARG_PYTHON_BINDINGS_LIBRARY "pybind11")
-  endif()
-
   add_library(${name} INTERFACE)
   set_target_properties(${name} PROPERTIES
     # Yes: Leading-lowercase property names are load bearing and the recommended
     # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
-    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY"
+    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
     mlir_python_SOURCES_TYPE extension
     mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
     mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
     mlir_python_DEPENDS ""
-    mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY}"
   )
 
   # Set the interface source and link_libs properties of the target
@@ -341,14 +335,12 @@ function(add_mlir_python_modules name)
     elseif(_source_type STREQUAL "extension")
       # Native CPP extension.
       get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
-      get_target_property(_bindings_library ${sources_target} mlir_python_BINDINGS_LIBRARY)
       # Transform relative source to based on root dir.
       set(_extension_target "${modules_target}.extension.${_module_name}.dso")
       add_mlir_python_extension(${_extension_target} "${_module_name}"
         INSTALL_COMPONENT ${modules_target}
         INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
         OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
-        PYTHON_BINDINGS_LIBRARY ${_bindings_library}
         LINK_LIBS PRIVATE
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
@@ -753,7 +745,7 @@ endfunction()
 function(add_mlir_python_extension libname extname)
   cmake_parse_arguments(ARG
   ""
-  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY;PYTHON_BINDINGS_LIBRARY"
+  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
   "SOURCES;LINK_LIBS"
   ${ARGN})
   if(ARG_UNPARSED_ARGUMENTS)
@@ -761,7 +753,7 @@ function(add_mlir_python_extension libname extname)
   endif()
 
   # The extension itself must be compiled with RTTI and exceptions enabled.
-  # Also, some warning classes triggered by pybind11 are disabled.
+  # Also, some warning classes triggered by nanobind are disabled.
   set(eh_rtti_enable)
   if (MSVC)
     set(eh_rtti_enable /EHsc /GR)
@@ -769,29 +761,21 @@ function(add_mlir_python_extension libname extname)
     set(eh_rtti_enable -frtti -fexceptions)
   endif ()
 
-  # The actual extension library produces a shared-object or DLL and has
-  # sources that must be compiled in accordance with pybind11 needs (RTTI and
-  # exceptions).
-  if(NOT DEFINED ARG_PYTHON_BINDINGS_LIBRARY OR ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11")
-    pybind11_add_module(${libname}
-      ${ARG_SOURCES}
-    )
-  elseif(ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind")
-    nanobind_add_module(${libname}
-      NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
-      FREE_THREADED
-      ${ARG_SOURCES}
-    )
+  nanobind_add_module(${libname}
+    NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
+    FREE_THREADED
+    ${ARG_SOURCES}
+  )
 
-    if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
-        AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
-      # Avoid some warnings from upstream nanobind.
-      # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
-      # the super project handle compile options as it wishes.
-      get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
-      target_compile_options(${NB_LIBRARY_TARGET_NAME}
-        PRIVATE
-          -Wno-c++98-compat-extra-semi
+  if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
+      AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
+    # Avoid some warnings from upstream nanobind.
+    # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
+    # the super project handle compile options as it wishes.
+    get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
+    target_compile_options(${NB_LIBRARY_TARGET_NAME}
+      PRIVATE
+        -Wno-c++98-compat-extra-semi
           -Wno-cast-qual
           -Wno-covered-switch-default
           -Wno-deprecated-literal-operator
@@ -799,11 +783,11 @@ function(add_mlir_python_extension libname extname)
           -Wno-unused-parameter
           -Wno-zero-length-array
           -Wno-missing-field-initializers
-          ${eh_rtti_enable})
+        ${eh_rtti_enable})
 
-      target_compile_options(${libname}
-        PRIVATE
-          -Wno-c++98-compat-extra-semi
+    target_compile_options(${libname}
+      PRIVATE
+        -Wno-c++98-compat-extra-semi
           -Wno-cast-qual
           -Wno-covered-switch-default
           -Wno-deprecated-literal-operator
@@ -811,20 +795,19 @@ function(add_mlir_python_extension libname extname)
           -Wno-unused-parameter
           -Wno-zero-length-array
           -Wno-missing-field-initializers
-          ${eh_rtti_enable})
-    endif()
+        ${eh_rtti_enable})
+  endif()
 
-    if(APPLE)
-      # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
-      # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
-      # for downstream users that do not do something like `-undefined dynamic_lookup`.
-      # Same for the rest.
-      target_link_options(${libname} PUBLIC
-        "LINKER:-U,_PyClassMethod_New"
-        "LINKER:-U,_PyCode_Addr2Location"
-        "LINKER:-U,_PyFrame_GetLasti"
-      )
-    endif()
+  if(APPLE)
+    # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
+    # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
+    # for downstream users that do not do something like `-undefined dynamic_lookup`.
+    # Same for the rest.
+    target_link_options(${libname} PUBLIC
+      "LINKER:-U,_PyClassMethod_New"
+      "LINKER:-U,_PyCode_Addr2Location"
+      "LINKER:-U,_PyFrame_GetLasti"
+    )
   endif()
 
   target_compile_options(${libname} PRIVATE ${eh_rtti_enable})
@@ -862,11 +845,11 @@ function(add_mlir_python_extension libname extname)
   if(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.
-    # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
-    # is not passed in as a compile definition, pybind11 undefs _DEBUG when including
+    # nanobind has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
+    # is not passed in as a compile definition, nanobind undefs _DEBUG when including
     # python.h, so that the release python library would be used).
-    # Since mlir uses pybind11, we can leverage their workaround by never directly
-    # pyconfig.h or python.h and instead relying on the pybind11 headers to include the
+    # Since mlir uses nanobind, we can leverage their workaround by never directly
+    # pyconfig.h or python.h and instead relying on the nanobind headers to include the
     # necessary python headers. This results in mlir always linking against the
     # release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE.
     target_link_libraries(${libname} PRIVATE ${Python3_LIBRARY_RELEASE})
diff --git a/mlir/cmake/modules/MLIRDetectPythonEnv.cmake b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
index d18f8c06158b2..81573f98a439e 100644
--- a/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
+++ b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
@@ -46,81 +46,38 @@ macro(mlir_configure_python_dev_packages)
     message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
     message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
     message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
-    mlir_detect_pybind11_install()
-    find_package(pybind11 2.10 CONFIG REQUIRED)
-    message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
-    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
-                  "suffix = '${PYTHON_MODULE_SUFFIX}', "
-                  "extension = '${PYTHON_MODULE_EXTENSION}")
-
-    mlir_detect_nanobind_install()
-    find_package(nanobind 2.9 CONFIG REQUIRED)
-    message(STATUS "Found nanobind v${nanobind_VERSION}: ${nanobind_INCLUDE_DIR}")
-    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
-                  "suffix = '${PYTHON_MODULE_SUFFIX}', "
-                  "extension = '${PYTHON_MODULE_EXTENSION}")
-  endif()
-endmacro()
-
-# Detects a pybind11 package installed in the current python environment
-# and sets variables to allow it to be found. This allows pybind11 to be
-# installed via pip, which typically yields a much more recent version than
-# the OS install, which will be available otherwise.
-function(mlir_detect_pybind11_install)
-  if(pybind11_DIR)
-    message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
-  else()
-    message(STATUS "Checking for pybind11 in python path...")
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
-      return()
-    endif()
-    message(STATUS "found (${PACKAGE_DIR})")
-    set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
-  endif()
-endfunction()
-
-
-# Detects a nanobind package installed in the current python environment
-# and sets variables to allow it to be found. This allows nanobind to be
-# installed via pip, which typically yields a much more recent version than
-# the OS install, which will be available otherwise.
-function(mlir_detect_nanobind_install)
-  if(nanobind_DIR)
-    message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
-  else()
-    message(STATUS "Checking for nanobind in python path...")
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import nanobind;print(nanobind.cmake_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
-      return()
+    message(STATUS "Python extension suffix for modules: '${Python3_SOABI}'")
+    if(nanobind_DIR)
+      message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
+      find_package(nanobind 2.9 CONFIG REQUIRED)
+    else()
+      message(STATUS "Checking for nanobind in python path...")
+      execute_process(
+        COMMAND "${Python3_EXECUTABLE}"
+        -c "import nanobind;print(nanobind.cmake_dir(), end='')"
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+        RESULT_VARIABLE STATUS
+        OUTPUT_VARIABLE PACKAGE_DIR
+        ERROR_QUIET)
+      if(NOT STATUS EQUAL "0")
+        message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
+        return()
+      endif()
+      message(STATUS "found (${PACKAGE_DIR})")
+      set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
+      execute_process(
+        COMMAND "${Python3_EXECUTABLE}"
+        -c "import nanobind;print(nanobind.include_dir(), end='')"
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+        RESULT_VARIABLE STATUS
+        OUTPUT_VARIABLE PACKAGE_DIR
+        ERROR_QUIET)
+      if(NOT STATUS EQUAL "0")
+        message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
+        return()
+      endif()
+      set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
     endif()
-    message(STATUS "found (${PACKAGE_DIR})")
-    set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import nanobind;print(nanobind.include_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
-      return()
-    endif()
-    set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
+    message(STATUS "Found nanobind: ${NB_DIR}")
   endif()
-endfunction()
+endmacro()
diff --git a/mlir/docs/Dialects/Linalg/OpDSL.md b/mlir/docs/Dialects/Linalg/OpDSL.md
index 37604fc17dd9b..5b766a31295cc 100644
--- a/mlir/docs/Dialects/Linalg/OpDSL.md
+++ b/mlir/docs/Dialects/Linalg/OpDSL.md
@@ -16,7 +16,7 @@ corresponding `linalg.generic` IR for the composition.
 ## Basic usage
 
 The tool is bundled with the MLIR Python bindings. To use from the CMake build
-tree, MLIR must be build with Python bindings enabled
+tree, MLIR must be built with Python bindings enabled
 (`-DMLIR_ENABLE_BINDINGS_PYTHON=ON`). Then add the `python` directory in the
 build tree to your `PYTHONPATH` environment variable (i.e. `export
 PYTHONPATH=$PWD/build/tools/mlir/python_packages/mlir_core`). Optionally, use an
@@ -24,7 +24,7 @@ installed MLIR package, if available, to avoid building.
 
 ```shell
 # Dump the `core_named_ops.py` module as YAML.
-python -m mlir.dialects.linalg.opdsl.dump_oplib .ops.core_named_ops
+python -m mlir.dialects.linalg.opdsl.dump_oplib.ops.core_named_ops
 ```
 
 Alternatively, run the `$PWD/build/bin/update_core_linalg_named_ops.sh` script,
diff --git a/mlir/examples/standalone/python/CMakeLists.txt b/mlir/examples/standalone/python/CMakeLists.txt
index 8469bff0eda27..edaedf18cc843 100644
--- a/mlir/examples/standalone/python/CMakeLists.txt
+++ b/mlir/examples/standalone/python/CMakeLists.txt
@@ -16,27 +16,10 @@ declare_mlir_dialect_python_bindings(
   ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir_standalone"
   TD_FILE dialects/StandaloneOps.td
   SOURCES
-    dialects/standalone_pybind11.py
     dialects/standalone_nanobind.py
     _mlir_libs/_standaloneDialectsNanobind/py.typed
   DIALECT_NAME standalone)
 
-
-declare_mlir_python_extension(StandalonePythonSources.Pybind11Extension
-  MODULE_NAME _standaloneDialectsPybind11
-  ADD_TO_PARENT StandalonePythonSources
-  SOURCES
-    StandaloneExtensionPybind11.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
-  EMBED_CAPI_LINK_LIBS
-    MLIRCAPIIR
-    MLIRCAPIArith
-    MLIRCAPITransforms
-    StandaloneCAPI
-  PYTHON_BINDINGS_LIBRARY pybind11
-)
-
 declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
   MODULE_NAME _standaloneDialectsNanobind
   ADD_TO_PARENT StandalonePythonSources
@@ -49,7 +32,6 @@ declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
     MLIRCAPIArith
     MLIRCAPITransforms
     StandaloneCAPI
-  PYTHON_BINDINGS_LIBRARY nanobind
 )
 
 
diff --git a/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp b/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp
deleted file mode 100644
index da8c2167dc36b..0000000000000
--- a/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-//===- StandaloneExtensionPybind11.cpp - Extension module -----------------===//
-//
-// This is the pybind11 version of the example module. There is also a nanobind
-// example in StandaloneExtensionNanobind.cpp.
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "Standalone-c/Dialects.h"
-#include "mlir-c/Dialect/Arith.h"
-#include "mlir/Bindings/Python/PybindAdaptors.h"
-
-using namespace mlir::python::adaptors;
-
-PYBIND11_MODULE(_standaloneDialectsPybind11, m) {
-  //===--------------------------------------------------------------------===//
-  // standalone dialect
-  //===--------------------------------------------------------------------===//
-  auto standaloneM = m.def_submodule("standalone");
-
-  standaloneM.def(
-      "register_dialects",
-      [](MlirContext context, bool load) {
-        MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
-        MlirDialectHandle standaloneHandle =
-            mlirGetDialectHandle__standalone__();
-        mlirDialectHandleRegisterDialect(arithHandle, context);
-        mlirDialectHandleRegisterDialect(standaloneHandle, context);
-        if (load) {
-          mlirDialectHandleLoadDialect(arithHandle, context);
-          mlirDialectHandleRegisterDialect(standaloneHandle, context);
-        }
-      },
-      py::arg("context") = py::none(), py::arg("load") = true);
-}
diff --git a/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py b/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py
deleted file mode 100644
index bfb98e404e13f..0000000000000
--- a/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py
+++ /dev/null
@@ -1,6 +0,0 @@
-#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-#  See https://llvm.org/LICENSE.txt for license information.
-#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-from ._standalone_ops_gen import *
-from .._mlir_libs._standaloneDialectsPybind11.standalone import *
diff --git a/mlir/examples/standalone/test/python/smoketest.py ...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-infrastructure

Author: Maksim Levental (makslevental)

Changes

This PR removes pybind which has been deprecated for almost a year (#117922).


Patch is 67.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/172581.diff

17 Files Affected:

  • (modified) .ci/all_requirements.txt (-4)
  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+40-57)
  • (modified) mlir/cmake/modules/MLIRDetectPythonEnv.cmake (+33-76)
  • (modified) mlir/docs/Dialects/Linalg/OpDSL.md (+2-2)
  • (modified) mlir/examples/standalone/python/CMakeLists.txt (-18)
  • (removed) mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp (-38)
  • (removed) mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py (-6)
  • (modified) mlir/examples/standalone/test/python/smoketest.py (+1-10)
  • (removed) mlir/include/mlir/Bindings/Python/PybindAdaptors.h (-616)
  • (modified) mlir/python/CMakeLists.txt (+16-47)
  • (modified) mlir/python/mlir/dialects/python_test.py (+3-8)
  • (modified) mlir/python/requirements.txt (-1)
  • (modified) mlir/test/python/dialects/python_test.py (+9-22)
  • (modified) mlir/test/python/lib/CMakeLists.txt (-1)
  • (removed) mlir/test/python/lib/PythonTestModulePybind11.cpp (-118)
  • (modified) mlir/tools/mlir-linalg-ods-gen/update_core_linalg_named_ops.sh.in (+1-1)
  • (modified) utils/bazel/llvm-project-overlay/mlir/BUILD.bazel (+19-35)
diff --git a/.ci/all_requirements.txt b/.ci/all_requirements.txt
index 4918d7519291f..32c1f43b7f9a1 100644
--- a/.ci/all_requirements.txt
+++ b/.ci/all_requirements.txt
@@ -443,10 +443,6 @@ pyasn1-modules==0.4.2 \
     --hash=sha256:29253a9207ce32b64c3ac6600edc75368f98473906e8fd1043bd6b5b1de2c14a \
     --hash=sha256:677091de870a80aae844b1ca6134f54652fa2c8c5a52aa396440ac3106e941e6
     # via google-auth
-pybind11==2.13.6 \
-    --hash=sha256:237c41e29157b962835d356b370ededd57594a26d5894a795960f0047cb5caf5 \
-    --hash=sha256:ba6af10348c12b24e92fa086b39cfba0eff619b61ac77c406167d813b096d39a
-    # via -r mlir/python/requirements.txt
 pycparser==2.23 \
     --hash=sha256:78816d4f24add8f10a06d6f05b4d424ad9e96cfebf68a4ddc99c65c0720d00c2 \
     --hash=sha256:e5c6e8d3fbad53479cab09ac03729e0a9faf2bee3db8208a550daf5af81a5934
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 8196e2a2a3321..ca90151e76268 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -123,12 +123,12 @@ function(mlir_generate_type_stubs)
     "IMPORT_PATHS;DEPENDS_TARGETS;OUTPUTS;DEPENDS_TARGET_SRC_DEPS"
     ${ARGN})
 
-  # for people doing find_package(nanobind)
+  # for people installing a distro (e.g., pip install) of nanobind
   if(EXISTS ${nanobind_DIR}/../src/stubgen.py)
     set(NB_STUBGEN "${nanobind_DIR}/../src/stubgen.py")
   elseif(EXISTS ${nanobind_DIR}/../stubgen.py)
     set(NB_STUBGEN "${nanobind_DIR}/../stubgen.py")
-  # for people using FetchContent_Declare and FetchContent_MakeAvailable
+  # for people using nanobind git source tree (e.g., FetchContent_Declare and FetchContent_MakeAvailable)
   elseif(EXISTS ${nanobind_SOURCE_DIR}/src/stubgen.py)
     set(NB_STUBGEN "${nanobind_SOURCE_DIR}/src/stubgen.py")
   elseif(EXISTS ${nanobind_SOURCE_DIR}/stubgen.py)
@@ -226,11 +226,10 @@ endfunction()
 #   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.
-#   PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
 function(declare_mlir_python_extension name)
   cmake_parse_arguments(ARG
     ""
-    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY"
+    "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
     "SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
     ${ARGN})
 
@@ -239,20 +238,15 @@ function(declare_mlir_python_extension name)
   endif()
   set(_install_destination "src/python/${name}")
 
-  if(NOT ARG_PYTHON_BINDINGS_LIBRARY)
-    set(ARG_PYTHON_BINDINGS_LIBRARY "pybind11")
-  endif()
-
   add_library(${name} INTERFACE)
   set_target_properties(${name} PROPERTIES
     # Yes: Leading-lowercase property names are load bearing and the recommended
     # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
-    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY"
+    EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
     mlir_python_SOURCES_TYPE extension
     mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
     mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
     mlir_python_DEPENDS ""
-    mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY}"
   )
 
   # Set the interface source and link_libs properties of the target
@@ -341,14 +335,12 @@ function(add_mlir_python_modules name)
     elseif(_source_type STREQUAL "extension")
       # Native CPP extension.
       get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
-      get_target_property(_bindings_library ${sources_target} mlir_python_BINDINGS_LIBRARY)
       # Transform relative source to based on root dir.
       set(_extension_target "${modules_target}.extension.${_module_name}.dso")
       add_mlir_python_extension(${_extension_target} "${_module_name}"
         INSTALL_COMPONENT ${modules_target}
         INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
         OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
-        PYTHON_BINDINGS_LIBRARY ${_bindings_library}
         LINK_LIBS PRIVATE
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
@@ -753,7 +745,7 @@ endfunction()
 function(add_mlir_python_extension libname extname)
   cmake_parse_arguments(ARG
   ""
-  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY;PYTHON_BINDINGS_LIBRARY"
+  "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
   "SOURCES;LINK_LIBS"
   ${ARGN})
   if(ARG_UNPARSED_ARGUMENTS)
@@ -761,7 +753,7 @@ function(add_mlir_python_extension libname extname)
   endif()
 
   # The extension itself must be compiled with RTTI and exceptions enabled.
-  # Also, some warning classes triggered by pybind11 are disabled.
+  # Also, some warning classes triggered by nanobind are disabled.
   set(eh_rtti_enable)
   if (MSVC)
     set(eh_rtti_enable /EHsc /GR)
@@ -769,29 +761,21 @@ function(add_mlir_python_extension libname extname)
     set(eh_rtti_enable -frtti -fexceptions)
   endif ()
 
-  # The actual extension library produces a shared-object or DLL and has
-  # sources that must be compiled in accordance with pybind11 needs (RTTI and
-  # exceptions).
-  if(NOT DEFINED ARG_PYTHON_BINDINGS_LIBRARY OR ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11")
-    pybind11_add_module(${libname}
-      ${ARG_SOURCES}
-    )
-  elseif(ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind")
-    nanobind_add_module(${libname}
-      NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
-      FREE_THREADED
-      ${ARG_SOURCES}
-    )
+  nanobind_add_module(${libname}
+    NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
+    FREE_THREADED
+    ${ARG_SOURCES}
+  )
 
-    if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
-        AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
-      # Avoid some warnings from upstream nanobind.
-      # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
-      # the super project handle compile options as it wishes.
-      get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
-      target_compile_options(${NB_LIBRARY_TARGET_NAME}
-        PRIVATE
-          -Wno-c++98-compat-extra-semi
+  if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
+      AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
+    # Avoid some warnings from upstream nanobind.
+    # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
+    # the super project handle compile options as it wishes.
+    get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
+    target_compile_options(${NB_LIBRARY_TARGET_NAME}
+      PRIVATE
+        -Wno-c++98-compat-extra-semi
           -Wno-cast-qual
           -Wno-covered-switch-default
           -Wno-deprecated-literal-operator
@@ -799,11 +783,11 @@ function(add_mlir_python_extension libname extname)
           -Wno-unused-parameter
           -Wno-zero-length-array
           -Wno-missing-field-initializers
-          ${eh_rtti_enable})
+        ${eh_rtti_enable})
 
-      target_compile_options(${libname}
-        PRIVATE
-          -Wno-c++98-compat-extra-semi
+    target_compile_options(${libname}
+      PRIVATE
+        -Wno-c++98-compat-extra-semi
           -Wno-cast-qual
           -Wno-covered-switch-default
           -Wno-deprecated-literal-operator
@@ -811,20 +795,19 @@ function(add_mlir_python_extension libname extname)
           -Wno-unused-parameter
           -Wno-zero-length-array
           -Wno-missing-field-initializers
-          ${eh_rtti_enable})
-    endif()
+        ${eh_rtti_enable})
+  endif()
 
-    if(APPLE)
-      # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
-      # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
-      # for downstream users that do not do something like `-undefined dynamic_lookup`.
-      # Same for the rest.
-      target_link_options(${libname} PUBLIC
-        "LINKER:-U,_PyClassMethod_New"
-        "LINKER:-U,_PyCode_Addr2Location"
-        "LINKER:-U,_PyFrame_GetLasti"
-      )
-    endif()
+  if(APPLE)
+    # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
+    # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
+    # for downstream users that do not do something like `-undefined dynamic_lookup`.
+    # Same for the rest.
+    target_link_options(${libname} PUBLIC
+      "LINKER:-U,_PyClassMethod_New"
+      "LINKER:-U,_PyCode_Addr2Location"
+      "LINKER:-U,_PyFrame_GetLasti"
+    )
   endif()
 
   target_compile_options(${libname} PRIVATE ${eh_rtti_enable})
@@ -862,11 +845,11 @@ function(add_mlir_python_extension libname extname)
   if(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.
-    # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
-    # is not passed in as a compile definition, pybind11 undefs _DEBUG when including
+    # nanobind has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
+    # is not passed in as a compile definition, nanobind undefs _DEBUG when including
     # python.h, so that the release python library would be used).
-    # Since mlir uses pybind11, we can leverage their workaround by never directly
-    # pyconfig.h or python.h and instead relying on the pybind11 headers to include the
+    # Since mlir uses nanobind, we can leverage their workaround by never directly
+    # pyconfig.h or python.h and instead relying on the nanobind headers to include the
     # necessary python headers. This results in mlir always linking against the
     # release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE.
     target_link_libraries(${libname} PRIVATE ${Python3_LIBRARY_RELEASE})
diff --git a/mlir/cmake/modules/MLIRDetectPythonEnv.cmake b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
index d18f8c06158b2..81573f98a439e 100644
--- a/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
+++ b/mlir/cmake/modules/MLIRDetectPythonEnv.cmake
@@ -46,81 +46,38 @@ macro(mlir_configure_python_dev_packages)
     message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
     message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
     message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")
-    mlir_detect_pybind11_install()
-    find_package(pybind11 2.10 CONFIG REQUIRED)
-    message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIR}")
-    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
-                  "suffix = '${PYTHON_MODULE_SUFFIX}', "
-                  "extension = '${PYTHON_MODULE_EXTENSION}")
-
-    mlir_detect_nanobind_install()
-    find_package(nanobind 2.9 CONFIG REQUIRED)
-    message(STATUS "Found nanobind v${nanobind_VERSION}: ${nanobind_INCLUDE_DIR}")
-    message(STATUS "Python prefix = '${PYTHON_MODULE_PREFIX}', "
-                  "suffix = '${PYTHON_MODULE_SUFFIX}', "
-                  "extension = '${PYTHON_MODULE_EXTENSION}")
-  endif()
-endmacro()
-
-# Detects a pybind11 package installed in the current python environment
-# and sets variables to allow it to be found. This allows pybind11 to be
-# installed via pip, which typically yields a much more recent version than
-# the OS install, which will be available otherwise.
-function(mlir_detect_pybind11_install)
-  if(pybind11_DIR)
-    message(STATUS "Using explicit pybind11 cmake directory: ${pybind11_DIR} (-Dpybind11_DIR to change)")
-  else()
-    message(STATUS "Checking for pybind11 in python path...")
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import pybind11;print(pybind11.get_cmake_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install pybind11' or set pybind11_DIR)")
-      return()
-    endif()
-    message(STATUS "found (${PACKAGE_DIR})")
-    set(pybind11_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
-  endif()
-endfunction()
-
-
-# Detects a nanobind package installed in the current python environment
-# and sets variables to allow it to be found. This allows nanobind to be
-# installed via pip, which typically yields a much more recent version than
-# the OS install, which will be available otherwise.
-function(mlir_detect_nanobind_install)
-  if(nanobind_DIR)
-    message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
-  else()
-    message(STATUS "Checking for nanobind in python path...")
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import nanobind;print(nanobind.cmake_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
-      return()
+    message(STATUS "Python extension suffix for modules: '${Python3_SOABI}'")
+    if(nanobind_DIR)
+      message(STATUS "Using explicit nanobind cmake directory: ${nanobind_DIR} (-Dnanobind_DIR to change)")
+      find_package(nanobind 2.9 CONFIG REQUIRED)
+    else()
+      message(STATUS "Checking for nanobind in python path...")
+      execute_process(
+        COMMAND "${Python3_EXECUTABLE}"
+        -c "import nanobind;print(nanobind.cmake_dir(), end='')"
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+        RESULT_VARIABLE STATUS
+        OUTPUT_VARIABLE PACKAGE_DIR
+        ERROR_QUIET)
+      if(NOT STATUS EQUAL "0")
+        message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
+        return()
+      endif()
+      message(STATUS "found (${PACKAGE_DIR})")
+      set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
+      execute_process(
+        COMMAND "${Python3_EXECUTABLE}"
+        -c "import nanobind;print(nanobind.include_dir(), end='')"
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+        RESULT_VARIABLE STATUS
+        OUTPUT_VARIABLE PACKAGE_DIR
+        ERROR_QUIET)
+      if(NOT STATUS EQUAL "0")
+        message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
+        return()
+      endif()
+      set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
     endif()
-    message(STATUS "found (${PACKAGE_DIR})")
-    set(nanobind_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
-    execute_process(
-      COMMAND "${Python3_EXECUTABLE}"
-      -c "import nanobind;print(nanobind.include_dir(), end='')"
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      RESULT_VARIABLE STATUS
-      OUTPUT_VARIABLE PACKAGE_DIR
-      ERROR_QUIET)
-    if(NOT STATUS EQUAL "0")
-      message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
-      return()
-    endif()
-    set(nanobind_INCLUDE_DIR "${PACKAGE_DIR}" PARENT_SCOPE)
+    message(STATUS "Found nanobind: ${NB_DIR}")
   endif()
-endfunction()
+endmacro()
diff --git a/mlir/docs/Dialects/Linalg/OpDSL.md b/mlir/docs/Dialects/Linalg/OpDSL.md
index 37604fc17dd9b..5b766a31295cc 100644
--- a/mlir/docs/Dialects/Linalg/OpDSL.md
+++ b/mlir/docs/Dialects/Linalg/OpDSL.md
@@ -16,7 +16,7 @@ corresponding `linalg.generic` IR for the composition.
 ## Basic usage
 
 The tool is bundled with the MLIR Python bindings. To use from the CMake build
-tree, MLIR must be build with Python bindings enabled
+tree, MLIR must be built with Python bindings enabled
 (`-DMLIR_ENABLE_BINDINGS_PYTHON=ON`). Then add the `python` directory in the
 build tree to your `PYTHONPATH` environment variable (i.e. `export
 PYTHONPATH=$PWD/build/tools/mlir/python_packages/mlir_core`). Optionally, use an
@@ -24,7 +24,7 @@ installed MLIR package, if available, to avoid building.
 
 ```shell
 # Dump the `core_named_ops.py` module as YAML.
-python -m mlir.dialects.linalg.opdsl.dump_oplib .ops.core_named_ops
+python -m mlir.dialects.linalg.opdsl.dump_oplib.ops.core_named_ops
 ```
 
 Alternatively, run the `$PWD/build/bin/update_core_linalg_named_ops.sh` script,
diff --git a/mlir/examples/standalone/python/CMakeLists.txt b/mlir/examples/standalone/python/CMakeLists.txt
index 8469bff0eda27..edaedf18cc843 100644
--- a/mlir/examples/standalone/python/CMakeLists.txt
+++ b/mlir/examples/standalone/python/CMakeLists.txt
@@ -16,27 +16,10 @@ declare_mlir_dialect_python_bindings(
   ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir_standalone"
   TD_FILE dialects/StandaloneOps.td
   SOURCES
-    dialects/standalone_pybind11.py
     dialects/standalone_nanobind.py
     _mlir_libs/_standaloneDialectsNanobind/py.typed
   DIALECT_NAME standalone)
 
-
-declare_mlir_python_extension(StandalonePythonSources.Pybind11Extension
-  MODULE_NAME _standaloneDialectsPybind11
-  ADD_TO_PARENT StandalonePythonSources
-  SOURCES
-    StandaloneExtensionPybind11.cpp
-  PRIVATE_LINK_LIBS
-    LLVMSupport
-  EMBED_CAPI_LINK_LIBS
-    MLIRCAPIIR
-    MLIRCAPIArith
-    MLIRCAPITransforms
-    StandaloneCAPI
-  PYTHON_BINDINGS_LIBRARY pybind11
-)
-
 declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
   MODULE_NAME _standaloneDialectsNanobind
   ADD_TO_PARENT StandalonePythonSources
@@ -49,7 +32,6 @@ declare_mlir_python_extension(StandalonePythonSources.NanobindExtension
     MLIRCAPIArith
     MLIRCAPITransforms
     StandaloneCAPI
-  PYTHON_BINDINGS_LIBRARY nanobind
 )
 
 
diff --git a/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp b/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp
deleted file mode 100644
index da8c2167dc36b..0000000000000
--- a/mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-//===- StandaloneExtensionPybind11.cpp - Extension module -----------------===//
-//
-// This is the pybind11 version of the example module. There is also a nanobind
-// example in StandaloneExtensionNanobind.cpp.
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "Standalone-c/Dialects.h"
-#include "mlir-c/Dialect/Arith.h"
-#include "mlir/Bindings/Python/PybindAdaptors.h"
-
-using namespace mlir::python::adaptors;
-
-PYBIND11_MODULE(_standaloneDialectsPybind11, m) {
-  //===--------------------------------------------------------------------===//
-  // standalone dialect
-  //===--------------------------------------------------------------------===//
-  auto standaloneM = m.def_submodule("standalone");
-
-  standaloneM.def(
-      "register_dialects",
-      [](MlirContext context, bool load) {
-        MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
-        MlirDialectHandle standaloneHandle =
-            mlirGetDialectHandle__standalone__();
-        mlirDialectHandleRegisterDialect(arithHandle, context);
-        mlirDialectHandleRegisterDialect(standaloneHandle, context);
-        if (load) {
-          mlirDialectHandleLoadDialect(arithHandle, context);
-          mlirDialectHandleRegisterDialect(standaloneHandle, context);
-        }
-      },
-      py::arg("context") = py::none(), py::arg("load") = true);
-}
diff --git a/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py b/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py
deleted file mode 100644
index bfb98e404e13f..0000000000000
--- a/mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py
+++ /dev/null
@@ -1,6 +0,0 @@
-#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-#  See https://llvm.org/LICENSE.txt for license information.
-#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-from ._standalone_ops_gen import *
-from .._mlir_libs._standaloneDialectsPybind11.standalone import *
diff --git a/mlir/examples/standalone/test/python/smoketest.py ...
[truncated]

@makslevental makslevental force-pushed the users/makslevental/removepybind branch 3 times, most recently from ee8e531 to f5a6777 Compare December 17, 2025 00:55
@llvm llvm deleted a comment from github-actions bot Dec 17, 2025
@llvm llvm deleted a comment from github-actions bot Dec 17, 2025
@makslevental
Copy link
Contributor Author

@rupprecht I hope y'all are ready for this now 😅 but no rush - we can wait some more if you're still not ready.

@makslevental makslevental changed the title [MLIR][Python] remove pybind [MLIR][Python] remove pybind11 support Dec 17, 2025
@makslevental makslevental force-pushed the users/makslevental/removepybind branch from f5a6777 to 4dfd0f6 Compare December 17, 2025 01:10
@rupprecht
Copy link
Collaborator

@rupprecht I hope y'all are ready for this now 😅 but no rush - we can wait some more if you're still not ready.

LGTM, thanks for checking!

Copy link
Member

@jpienaar jpienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks

@makslevental makslevental merged commit 3d7018c into main Dec 19, 2025
11 checks passed
@makslevental makslevental deleted the users/makslevental/removepybind branch December 19, 2025 17:51
valadaptive pushed a commit to valadaptive/llvm-project that referenced this pull request Dec 24, 2025
This PR removes pybind which has been deprecated for over a year
(llvm#117922).
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jan 6, 2026
This PR removes pybind which has been deprecated for over a year
(llvm#117922).
ERROR_QUIET)
if(NOT STATUS EQUAL "0")
message(STATUS "not found (install via 'pip install nanobind' or set nanobind_DIR)")
return()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this return here is a bit problematic because mlir_configure_python_dev_packages is macro, not function. In circt when I forget to enable pyenv that has nanobind, it unexpectedly exits from top-level CMakeList.txt and all circt targets (e.g. check-circt) were not included at all. Can it cause FATAL_ERROR instead of STATUS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that's my mistake (this used to be part of a function). Can you send a PR or do you want me to do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can handle this. #174649 is a PR

ingomueller-net added a commit to substrait-io/substrait-mlir-contrib that referenced this pull request Jan 8, 2026
This become unnecessary and breaks the build with
llvm/llvm-project#172581.

Signed-off-by: Ingo Müller <[email protected]>
ingomueller-net added a commit to substrait-io/substrait-mlir-contrib that referenced this pull request Jan 8, 2026
This become unnecessary and breaks the build with
llvm/llvm-project#172581.

Signed-off-by: Ingo Müller <[email protected]>
ingomueller-net added a commit to substrait-io/substrait-mlir-contrib that referenced this pull request Jan 9, 2026
This become unnecessary and breaks the build with
llvm/llvm-project#172581.

Signed-off-by: Ingo Müller <[email protected]>
ingomueller-net added a commit to substrait-io/substrait-mlir-contrib that referenced this pull request Jan 9, 2026
#182)

Bumps [third_party/llvm-project](https://github.com/llvm/llvm-project) from `a33e505` to `8e90208`.
- [Release notes](https://github.com/llvm/llvm-project/releases)
- [Commits](llvm/llvm-project@a33e505...8e90208)

---
updated-dependencies:
- dependency-name: third_party/llvm-project
  dependency-version: 8e9020803787b5f4593dd7e2a24a966689889a35
  dependency-type: direct:production
...

This commit also includes the following fixes:

* ci(deps): update nanobind to v2.9.2 as required by LLVM.
* fix(build): remove setting of PYTHON_BINDINGS_LIBRARY.
  This become unnecessary and breaks the build with
  llvm/llvm-project#172581.
* fix(build): create main MLIR Python stubs explicitly.
  This is necessary with llvm/llvm-project#157930.
* ci(pyright): suppress some type checking errors.
  This commits silences two kinds of type checking errors found by
  pyright. Both are related to the method of extending tablegen'd ops in
  Python by inheriting from the generated class with the same name. The
  first one complains during import because the imported (generated)
  class clashes with the class defined in the Python file, which is the
  whole purpose of that class. The second one complains about the
  manually defined classes overriding the `body` property of the
  generated base class with a different return type but, again, the
  purpose of the overriding is to provide access to the one `Block`
  instead of the `Region`.
* build(bazel): migrate to bazel module and bazel version 8.4.
  This commit replaces the now deprecated `WORKSPACE.bazel` with the new
  approach using `MODULE.bazel` and upgrades bazel to version 8.4. The
  immediate reason for this change was the fact that the current LLVM
  repository does not build with the current set-up anymore. I tried a
  bit to fix the old version but didn't make it work. Migrating to
  bazelmod also took some time but was ultimately successful. Also, the
  old bazel version was about to be end-of-life, plus the new approach
  with modules *is* more elegant, so migrating is a good idea anyways.

Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Ingo Müller <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ingo Müller <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bazel "Peripheral" support tier build system: utils/bazel infrastructure Bugs about LLVM infrastructure mlir:core MLIR Core Infrastructure mlir:linalg mlir:python MLIR Python bindings mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants