[Mlir-commits] [mlir] Added free-threading CPython mode support in MLIR Python bindings (PR #107103)

Scott Todd llvmlistbot at llvm.org
Wed Jan 22 11:10:14 PST 2025


================
@@ -668,12 +668,31 @@ function(add_mlir_python_extension libname extname)
   elseif(ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind")
     nanobind_add_module(${libname}
       NB_DOMAIN mlir
+      FREE_THREADED
       ${ARG_SOURCES}
     )
 
     if (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
       # Avoids warnings from upstream nanobind.
-      target_compile_options(nanobind-static
+      set(nanobind_target "nanobind-static")
+      if (NOT TARGET ${nanobind_target})
+        # Get correct nanobind target name: nanobind-static-ft or something else
+        # It is set by nanobind_add_module function according to the passed options
+        get_property(all_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS)
----------------
ScottTodd wrote:

I'm seeing issues with this code when building in the downstream https://github.com/iree-org/iree project, using Python 3.13t. In my project, the `add_mlir_python_extension` function here is running from a different CMake source directory that does not include _any_ `nanobind` targets, as they are defined in other source files:

Logs with some extra debugging messages:
```
  --   Building iree-dialects project at /home/nod/dev/projects/iree/llvm-external-projects/iree-dialects (into /home/nod/dev/projects/iree/compiler/build/b/llvm-external-projects/mlir-iree-dialects)
  --   libname IREEDialectsPythonModules.extension._mlirRegisterEverything.dso using 'nanobind'
  --   all_targets in /home/nod/dev/projects/iree/llvm-external-projects/iree-dialects/python: IREEDialectsPythonSources;IREEDialectsPythonExtensions;IREEDialectsPythonSources.Dialects;IREEDialectsPythonSources.Dialects.iree_input;IREEDialectsPythonSources.Dialects.iree_input.tablegen;IREEDialectsPythonSources.Dialects.iree_input.ops_gen;IREEDialectsPythonSources.Dialects.iree_structured_transform;IREEDialectsPythonSources.Dialects.iree_structured_transform.tablegen;IREEDialectsPythonSources.Dialects.iree_structured_transform.ops_gen;IREEDialectsPythonExtensions.Main;IREEDialectsAggregateCAPI;IREEDialectsAggregateCAPI.sources;IREEDialectsPythonModules;IREEDialectsPythonModules.extension._mlirRegisterEverything.dso
  CMake Error at compiler/build/b/lib/cmake/mlir/AddMLIRPython.cmake:696 (message):
    Could not find nanobind target to set compile options to
  Call Stack (most recent call first):
    compiler/build/b/lib/cmake/mlir/AddMLIRPython.cmake:235 (add_mlir_python_extension)
    compiler/build/b/lib/cmake/mlir/AddMLIRPython.cmake:256 (_process_target)
    llvm-external-projects/iree-dialects/python/CMakeLists.txt:82 (add_mlir_python_modules)
```

full logs from CI: https://github.com/iree-org/iree/actions/runs/12901589475/job/35973909192#step:12:53035

The target name with 3.13t is `nanobind-static-ft`, but this search misses it because of the different CMake source.

---

I can think of a few solutions, if this target lookup and options setting is truly required:

1. Search through an explicit list of target names using `if (TARGET)`
2. Iterate over _all_ targets in the project, not just the targets in the current directory, using code like https://stackoverflow.com/a/62311397
3. Iterate over targets in the directory known to MLIR (`llvm-project/mlir/python`)

https://github.com/llvm/llvm-project/pull/107103


More information about the Mlir-commits mailing list