[Mlir-commits] [mlir] effa813 - [mlir][Python] don't build libnanobind if module only has "pure" sources (#175090)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jan 12 09:09:20 PST 2026


Author: Maksim Levental
Date: 2026-01-12T17:09:16Z
New Revision: effa813844a6a52613cc6e59c19cb35eeb08b6b6

URL: https://github.com/llvm/llvm-project/commit/effa813844a6a52613cc6e59c19cb35eeb08b6b6
DIFF: https://github.com/llvm/llvm-project/commit/effa813844a6a52613cc6e59c19cb35eeb08b6b6.diff

LOG: [mlir][Python] don't build libnanobind if module only has "pure" sources (#175090)

Like IREE's
[`IREECompilerBuildPythonModules`](https://github.com/iree-org/iree/blob/47fe908217caef04be21608cfcf45d44410f8dad/compiler/bindings/python/CMakeLists.txt#L300-L305).
Note, setting `MLIR_BINDINGS_PYTHON_NB_DOMAIN` on
`add_mlir_python_modules` explicitly would also workaround issues
related to this (and is generally recommended for projects building
multiple bindings packages).

---------

Co-authored-by: Rahul Kayaith <rkayaith at amd.com>

Added: 
    

Modified: 
    mlir/cmake/modules/AddMLIRPython.cmake

Removed: 
    


################################################################################
diff  --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index f4d078dfe7118..97873d0c07ab8 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -393,13 +393,28 @@ function(add_mlir_python_modules name)
     set(ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir")
   endif()
 
+  # Collect up all the transitive extension targets.
+  _flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES})
+
+  # Modules with only pure python sources shouldn't build libnanobind.
+  set(_needs_build_nanobind_lib OFF)
+  foreach(sources_target ${_flat_targets})
+    get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
+    if((_source_type STREQUAL "support") OR (_source_type STREQUAL "extension"))
+      set(_needs_build_nanobind_lib ON)
+      break()
+    endif()
+  endforeach()
+
   # This call sets NB_LIBRARY_TARGET_NAME.
-  build_nanobind_lib(
-    INSTALL_COMPONENT ${name}
-    INSTALL_DESTINATION "${ARG_INSTALL_PREFIX}/_mlir_libs"
-    OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
-    MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
-  )
+  if(_needs_build_nanobind_lib)
+    build_nanobind_lib(
+      INSTALL_COMPONENT ${name}
+      INSTALL_DESTINATION "${ARG_INSTALL_PREFIX}/_mlir_libs"
+      OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
+      MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
+    )
+  endif()
 
   # Helper to process an individual target.
   function(_process_target modules_target sources_target support_libs)
@@ -442,7 +457,6 @@ function(add_mlir_python_modules name)
 
   # Build the modules target.
   add_custom_target(${name} ALL)
-  _flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES})
 
   # Build all support libs first.
   set(_mlir_python_support_libs)


        


More information about the Mlir-commits mailing list