Skip to content

[MLIR] Fix nanobind linker args on macOS #125733

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 2 commits into from
Feb 5, 2025
Merged

Conversation

nikalra
Copy link
Contributor

@nikalra nikalra commented Feb 4, 2025

Set the "-U,_PyClassMethod_New" linker flag on the Python library itself isntead of on the CMAKE_MODULE_LINKER_FLAGS property.

Set the "-U,_PyClassMethod_New" linker flag on the Python library itself isntead of on the CMAKE_MODULE_LINKER_FLAGS property.
@nikalra nikalra requested a review from makslevental February 4, 2025 18:08
@llvmbot llvmbot added the mlir label Feb 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2025

@llvm/pr-subscribers-mlir

Author: Nikhil Kalra (nikalra)

Changes

Set the "-U,_PyClassMethod_New" linker flag on the Python library itself isntead of on the CMAKE_MODULE_LINKER_FLAGS property.


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

1 Files Affected:

  • (modified) mlir/cmake/modules/AddMLIRPython.cmake (+1-1)
diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index a23de004eb014ff..a21688213339b05 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -709,7 +709,7 @@ function(add_mlir_python_extension libname extname)
       # 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`.
-      set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-U -Wl,_PyClassMethod_New")
+      target_link_options(${libname} PRIVATE "LINKER:-U,_PyClassMethod_New")
     endif()
   endif()
 

@makslevental
Copy link
Contributor

makslevental commented Feb 4, 2025

But NanobindAdaptors.h is a header and so this flag does need to be set on all users of the header not just those building/using the library. Possibly actually this should be an extern in the header itself?

Just curious what issue do you have with the current solution?

@nikalra
Copy link
Contributor Author

nikalra commented Feb 4, 2025

But NanobindAdaptors.h is a header and so this flag does need to be set on all users of the header not just those building/using the library. Possibly actually this should be an extern in the header itself?

Just curious what issue do you have with the current solution?

Would it be better if I made it a PUBLIC link property so that downstream link dependencies also inherited the symbol? They could include the header in isolation, but presumably they'd be linking against the core MLIR Python bindings.

I gave extern a shot, but that doesn't work because the symbol is still missing at link time (and we can't link against a Python dylib for the linker to recognize the symbol exists).

I'm not sure what exactly is different between our build setup and the upstream one, but CMAKE_MODULE_LINKER_FLAGS were not getting propagated to the target so we'd fail to link when creating the Python .so. Our Python library is declared with declare_mlir_python_extension and executes through the exact flow where the property was getting applied.

@makslevental
Copy link
Contributor

Would it be better if I made it a PUBLIC link property so that downstream link dependencies also inherited the symbol?

Ya that's a reasonable in-between.

Btw cc @marbre this change wouldn't affect IREE right because we just duplicated this same patch locally (in IREE CMake)?

@marbre
Copy link
Member

marbre commented Feb 5, 2025

Would it be better if I made it a PUBLIC link property so that downstream link dependencies also inherited the symbol?

Ya that's a reasonable in-between.

Btw cc @marbre this change wouldn't affect IREE right because we just duplicated this same patch locally (in IREE CMake)?

What we have in IREE atm to work around the issue is

    set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-U -Wl,_PyClassMethod_New")

at top-level (https://github.com/iree-org/iree/blob/56bb652fd6b03b7473994426aa4a698f87a1418a/CMakeLists.txt#L655) if building for macOS. IREE also uses declare_mlir_python_extension for its compiler Python bindings and what is there upstream didn't kicked in.

Happy to give this patch a try before merging upstream if you want me to (?).

@makslevental
Copy link
Contributor

Happy to give this patch a try before merging upstream if you want me to (?).

Ya can you please.

@marbre
Copy link
Member

marbre commented Feb 5, 2025

Happy to give this patch a try before merging upstream if you want me to (?).

Ya can you please.

I did in the meantime, and the patch upstream allows us to drop the downstream one, see https://github.com/iree-org/iree/actions/runs/13155988107.

marbre added a commit to marbre/iree that referenced this pull request Feb 5, 2025
This drops the workaround to set linker flags to work around the issue
when building the compiler Python bindings for macOS as this is fixed
upstream as soon as llvm/llvm-project#125733 lands and was integrated
into IREE's llvm fork.

Closes iree-org#19591
@makslevental
Copy link
Contributor

image

@makslevental
Copy link
Contributor

@nikalra let me know if you need me to merge this (not sure if you have commit access)

@nikalra nikalra merged commit 113534d into llvm:main Feb 5, 2025
8 checks passed
@nikalra nikalra deleted the fix-linker-py branch February 5, 2025 19:49
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Set the "-U,_PyClassMethod_New" linker flag on the Python library itself
isntead of on the CMAKE_MODULE_LINKER_FLAGS property.
marbre added a commit to iree-org/iree that referenced this pull request Feb 13, 2025
This drops the workaround to set linker flags to work around the issue
when building the compiler Python bindings for macOS as this is fixed
upstream as soon as llvm/llvm-project#125733 lands and was integrated
into IREE's llvm fork.

Closes #19591
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.

4 participants