Skip to content

Commit 55e76c7

Browse files
committed
[mlir] Limit Python dependency to Development.Module when possible.
After CMake 3.18, we are able to limit the scope of the search to just Development.Module. Searching for Development will fail in situations where the Python libraries are not available. When possible, limit to just Development.Module. See: https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode Reviewed By: stellaraccident Differential Revision: https://reviews.llvm.org/D111585
1 parent a76e698 commit 55e76c7

File tree

3 files changed

+13
-72
lines changed

3 files changed

+13
-72
lines changed

mlir/CMakeLists.txt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,26 @@ option(MLIR_INCLUDE_INTEGRATION_TESTS
9595
# Requires:
9696
# The pybind11 library can be found (set with -DPYBIND_DIR=...)
9797
# The python executable is correct (set with -DPython3_EXECUTABLE=...)
98-
#
99-
# Version locking
100-
# ---------------
101-
# By default, python extensions are version locked to specific Python libraries.
102-
# This linking mode is somewhat more consistent across platforms and surfaces
103-
# undefined symbols at link time (vs runtime). It is suitable for development
104-
# workflows but can be disabled for more flexible deployment by
105-
# setting -DMLIR_BINDINGS_PYTHON_LOCK_VERSION=OFF
10698
#-------------------------------------------------------------------------------
10799

108100
set(MLIR_ENABLE_BINDINGS_PYTHON 0 CACHE BOOL
109101
"Enables building of Python bindings.")
110-
set(MLIR_BINDINGS_PYTHON_LOCK_VERSION 1 CACHE BOOL
111-
"Links to specific python libraries, resolving all symbols.")
112102

113103
if(MLIR_ENABLE_BINDINGS_PYTHON)
114104
include(MLIRDetectPythonEnv)
105+
# After CMake 3.18, we are able to limit the scope of the search to just
106+
# Development.Module. Searching for Development will fail in situations where
107+
# the Python libraries are not available. When possible, limit to just
108+
# Development.Module.
109+
# See https://pybind11.readthedocs.io/en/stable/compiling.html#findpython-mode
110+
if(CMAKE_VERSION VERSION_LESS "3.18.0")
111+
set(_python_development_component Development)
112+
else()
113+
set(_python_development_component Development.Module)
114+
endif()
115115
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION}
116-
COMPONENTS Interpreter Development NumPy REQUIRED)
116+
COMPONENTS Interpreter ${_python_development_component} NumPy REQUIRED)
117+
unset(_python_development_component)
117118
message(STATUS "Found python include dirs: ${Python3_INCLUDE_DIRS}")
118119
message(STATUS "Found python libraries: ${Python3_LIBRARIES}")
119120
message(STATUS "Found numpy v${Python3_NumPy_VERSION}: ${Python3_NumPy_INCLUDE_DIRS}")

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -419,62 +419,19 @@ function(add_mlir_python_extension libname extname)
419419
message(FATAL_ERROR " Missing SOURCES argument to add_mlir_python_extension(${libname}, ...")
420420
endif()
421421

422-
# Build-time RPath layouts require to be a directory one up from the
423-
# binary root.
424-
# TODO: Don't reference the LLVM_BINARY_DIR here: the invariant is that
425-
# the output directory must be at the same level of the lib directory
426-
# where libMLIR.so is installed. This is presently not optimal from a
427-
# project separation perspective and a discussion on how to better
428-
# segment MLIR libraries needs to happen.
429-
# TODO: Remove this when downstreams are moved off of it.
430-
if(NOT ARG_OUTPUT_DIRECTORY)
431-
set(ARG_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/python)
432-
endif()
433-
434-
# Normally on unix-like platforms, extensions are built as "MODULE" libraries
435-
# and do not explicitly link to the python shared object. This allows for
436-
# some greater deployment flexibility since the extension will bind to
437-
# symbols in the python interpreter on load. However, it also keeps the
438-
# linker from erroring on undefined symbols, leaving this to (usually obtuse)
439-
# runtime errors. Building in "SHARED" mode with an explicit link to the
440-
# python libraries allows us to build with the expectation of no undefined
441-
# symbols, which is better for development. Note that not all python
442-
# configurations provide build-time libraries to link against, in which
443-
# case, we fall back to MODULE linking.
444-
if(Python3_LIBRARIES STREQUAL "" OR NOT MLIR_BINDINGS_PYTHON_LOCK_VERSION)
445-
set(PYEXT_LINK_MODE MODULE)
446-
set(PYEXT_LIBADD)
447-
else()
448-
set(PYEXT_LINK_MODE SHARED)
449-
set(PYEXT_LIBADD ${Python3_LIBRARIES})
450-
endif()
451-
452422
# The actual extension library produces a shared-object or DLL and has
453423
# sources that must be compiled in accordance with pybind11 needs (RTTI and
454424
# exceptions).
455-
add_library(${libname}
456-
${PYEXT_LINK_MODE}
425+
pybind11_add_module(${libname}
457426
${ARG_SOURCES}
458427
)
459428

460-
target_include_directories(${libname} PRIVATE
461-
"${Python3_INCLUDE_DIRS}"
462-
"${pybind11_INCLUDE_DIR}"
463-
)
464-
465-
target_link_directories(${libname} PRIVATE
466-
"${Python3_LIBRARY_DIRS}"
467-
)
468-
469429
# The extension itself must be compiled with RTTI and exceptions enabled.
470430
# Also, some warning classes triggered by pybind11 are disabled.
471431
target_compile_options(${libname} PRIVATE
472432
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
473433
# Enable RTTI and exceptions.
474434
-frtti -fexceptions
475-
# Noisy pybind warnings
476-
-Wno-unused-value
477-
-Wno-covered-switch-default
478435
>
479436
$<$<CXX_COMPILER_ID:MSVC>:
480437
# Enable RTTI and exceptions.
@@ -486,8 +443,6 @@ function(add_mlir_python_extension libname extname)
486443
${libname} PROPERTIES
487444
LIBRARY_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
488445
OUTPUT_NAME "${extname}"
489-
PREFIX "${PYTHON_MODULE_PREFIX}"
490-
SUFFIX "${PYTHON_MODULE_SUFFIX}${PYTHON_MODULE_EXTENSION}"
491446
NO_SONAME ON
492447
)
493448

@@ -501,13 +456,6 @@ function(add_mlir_python_extension libname extname)
501456
)
502457
endif()
503458

504-
# pybind11 requires binding code to be compiled with -fvisibility=hidden
505-
# For static linkage, better code can be generated if the entire project
506-
# compiles that way, but that is not enforced here. Instead, include a linker
507-
# script that explicitly hides anything but the PyInit_* symbols, allowing gc
508-
# to take place.
509-
set_target_properties(${libname} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
510-
511459
# Python extensions depends *only* on the public API and LLVMSupport unless
512460
# if further dependencies are added explicitly.
513461
target_link_libraries(${libname}

mlir/docs/Bindings/Python.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@
2525
multiple Python implementations, setting this explicitly to the preferred
2626
`python3` executable is strongly recommended.
2727

28-
* **`MLIR_BINDINGS_PYTHON_LOCK_VERSION`**`:BOOL`
29-
30-
Links the native extension against the Python runtime library, which is
31-
optional on some platforms. While setting this to `OFF` can yield some greater
32-
deployment flexibility, linking in this way allows the linker to report
33-
compile time errors for unresolved symbols on all platforms, which makes for a
34-
smoother development workflow. Defaults to `ON`.
35-
3628
### Recommended development practices
3729

3830
It is recommended to use a python virtual environment. Many ways exist for this,

0 commit comments

Comments
 (0)