Skip to content

[mlir][python][cmake] Allow skipping nanobind compile options changes. #123997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

ScottTodd
Copy link
Member

Context: #107103 (comment)

This code is brittle, especially when called from a superproject that adds the nanobind-* target in a different source directory:

get_property(all_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS)

The changes here do help with my downstream build, but I'm not sure if using the MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES option introduced in #117934 is the right fix given that the option is currently scoped directly to one location with a matching name:

macro(mlir_configure_python_dev_packages)
if(NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES)

Some other solutions to consider:

  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)
  4. Move this target_compile_options setup into mlir_configure_python_dev_packages (I started on this, but that runs into similar issues where the target is defined in a different directory)

@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-mlir

Author: Scott Todd (ScottTodd)

Changes

Context: #107103 (comment)

This code is brittle, especially when called from a superproject that adds the nanobind-* target in a different source directory:

get_property(all_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS)

The changes here do help with my downstream build, but I'm not sure if using the MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES option introduced in #117934 is the right fix given that the option is currently scoped directly to one location with a matching name:

macro(mlir_configure_python_dev_packages)
if(NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES)

Some other solutions to consider:

  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)
  4. Move this target_compile_options setup into mlir_configure_python_dev_packages (I started on this, but that runs into similar issues where the target is defined in a different directory)

Full diff: https://github.com/llvm/llvm-project/pull/123997.diff

1 Files Affected:

  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+6-3)
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 815f65b106d945..018a81f1ad5500 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -672,8 +672,11 @@ function(add_mlir_python_extension libname extname)
       ${ARG_SOURCES}
     )
 
-    if (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
-      # Avoids warnings from upstream nanobind.
+    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.
       set(nanobind_target "nanobind-static")
       if (NOT TARGET ${nanobind_target})
         # Get correct nanobind target name: nanobind-static-ft or something else
@@ -702,7 +705,7 @@ function(add_mlir_python_extension libname extname)
           ${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

@ScottTodd
Copy link
Member Author

cc @vfdev-5 @hawkinsp (feel free to add reviews too)

I can also try some other solutions and iterate a bit

Copy link
Member

@marbre marbre left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me, thanks!

@ScottTodd
Copy link
Member Author

Tested a bit more downstream and I'm confident that this solves our issues. The code here could still be reworked, ideally by fixing the nanobind warnings upstream and not needing to fiddle with compile options at all.

@ScottTodd ScottTodd merged commit 1a8f49f into llvm:main Jan 23, 2025
10 checks passed
@marbre
Copy link
Member

marbre commented Jan 23, 2025

Tested a bit more downstream and I'm confident that this solves our issues. The code here could still be reworked, ideally by fixing the nanobind warnings upstream and not needing to fiddle with compile options at all.

I can confirm that this does resolve the downstream issue. I agree that this code still deserves to be reworked.

marbre pushed a commit to iree-org/llvm-project that referenced this pull request Jan 23, 2025
Context:
llvm#107103 (comment)

This code is brittle, especially when called from a superproject that
adds the `nanobind-*` target in a different source directory:
```cmake
get_property(all_targets DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS)
```

The changes here do help with my downstream build, but I'm not sure if
using the `MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES` option introduced
in llvm#117934 is the right fix
given that the option is currently scoped directly to one location with
a matching name:
https://github.com/llvm/llvm-project/blob/7ad8a3da4771ce8abbd146611124104d42a4e63e/mlir/cmake/modules/MLIRDetectPythonEnv.cmake#L4-L5

Some other solutions to consider:

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`)
4. Move this `target_compile_options` setup into
`mlir_configure_python_dev_packages` (I started on this, but that runs
into similar issues where the target is defined in a different
directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants