2020-11-05 10:29:40 +08:00
|
|
|
if(NOT LLVM_BUILD_LLVM_DYLIB)
|
|
|
|
message(FATAL_ERROR "Building the MLIR Python bindings require -DLLVM_BUILD_LLVM_DYLIB=ON")
|
|
|
|
endif()
|
|
|
|
|
2020-10-10 06:50:07 +08:00
|
|
|
################################################################################
|
|
|
|
# Copy python source tree.
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
set(PY_SRC_FILES
|
|
|
|
mlir/__init__.py
|
2020-10-22 14:34:01 +08:00
|
|
|
mlir/ir.py
|
|
|
|
mlir/dialects/__init__.py
|
|
|
|
mlir/dialects/std.py
|
2020-10-10 06:50:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
add_custom_target(MLIRBindingsPythonSources ALL
|
|
|
|
DEPENDS ${PY_SRC_FILES}
|
|
|
|
)
|
|
|
|
|
|
|
|
foreach(PY_SRC_FILE ${PY_SRC_FILES})
|
|
|
|
set(PY_DEST_FILE "${PROJECT_BINARY_DIR}/python/${PY_SRC_FILE}")
|
|
|
|
add_custom_command(
|
|
|
|
TARGET MLIRBindingsPythonSources PRE_BUILD
|
|
|
|
COMMENT "Copying python source ${PY_SRC_FILE} -> ${PY_DEST_FILE}"
|
|
|
|
DEPENDS "${PY_SRC_FILE}"
|
|
|
|
COMMAND "${CMAKE_COMMAND}" -E copy_if_different
|
|
|
|
"${CMAKE_CURRENT_SOURCE_DIR}/${PY_SRC_FILE}" "${PY_DEST_FILE}"
|
|
|
|
)
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Build python extension
|
|
|
|
################################################################################
|
|
|
|
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
# Normally on unix-like platforms, extensions are built as "MODULE" libraries
|
|
|
|
# and do not explicitly link to the python shared object. This allows for
|
|
|
|
# some greater deployment flexibility since the extension will bind to
|
|
|
|
# symbols in the python interpreter on load. However, it also keeps the
|
|
|
|
# linker from erroring on undefined symbols, leaving this to (usually obtuse)
|
|
|
|
# runtime errors. Building in "SHARED" mode with an explicit link to the
|
|
|
|
# python libraries allows us to build with the expectation of no undefined
|
2020-10-10 06:50:07 +08:00
|
|
|
# symbols, which is better for development. Note that not all python
|
|
|
|
# configurations provide build-time libraries to link against, in which
|
|
|
|
# case, we fall back to MODULE linking.
|
|
|
|
if(PYTHON_LIBRARIES STREQUAL "" OR NOT MLIR_PYTHON_BINDINGS_VERSION_LOCKED)
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
set(PYEXT_LINK_MODE MODULE)
|
|
|
|
set(PYEXT_LIBADD)
|
2020-10-10 06:50:07 +08:00
|
|
|
else()
|
|
|
|
set(PYEXT_LINK_MODE SHARED)
|
|
|
|
set(PYEXT_LIBADD ${PYTHON_LIBRARIES})
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# The actual extension library produces a shared-object or DLL and has
|
|
|
|
# sources that must be compiled in accordance with pybind11 needs (RTTI and
|
|
|
|
# exceptions).
|
2020-08-17 09:49:28 +08:00
|
|
|
# TODO: Link the libraries separately once a helper function is available
|
|
|
|
# to more generically add a pybind11 compliant library.
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
add_library(MLIRBindingsPythonExtension ${PYEXT_LINK_MODE}
|
|
|
|
MainModule.cpp
|
2020-08-17 09:49:28 +08:00
|
|
|
IRModules.cpp
|
2020-08-17 11:53:45 +08:00
|
|
|
PybindUtils.cpp
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
target_include_directories(MLIRBindingsPythonExtension PRIVATE
|
|
|
|
"${PYTHON_INCLUDE_DIRS}"
|
|
|
|
"${pybind11_INCLUDE_DIRS}")
|
|
|
|
|
|
|
|
# The extension itself must be compiled with RTTI and exceptions enabled.
|
|
|
|
# Also, some warning classes triggered by pybind11 are disabled.
|
|
|
|
target_compile_options(MLIRBindingsPythonExtension PRIVATE
|
|
|
|
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
|
|
|
|
# Enable RTTI and exceptions.
|
|
|
|
-frtti -fexceptions
|
|
|
|
# Noisy pybind warnings
|
|
|
|
-Wno-unused-value
|
|
|
|
-Wno-covered-switch-default
|
|
|
|
>
|
|
|
|
$<$<CXX_COMPILER_ID:MSVC>:
|
|
|
|
# Enable RTTI and exceptions.
|
|
|
|
/EHsc /GR>
|
|
|
|
)
|
|
|
|
|
|
|
|
# Configure the output to match python expectations.
|
|
|
|
set_target_properties(
|
|
|
|
MLIRBindingsPythonExtension PROPERTIES
|
2020-10-10 06:50:07 +08:00
|
|
|
# Build-time RPath layouts require to be a directory one up from the
|
|
|
|
# binary root.
|
|
|
|
# TODO: Don't reference the LLVM_BINARY_DIR here: the invariant is that
|
|
|
|
# the output directory must be at the same level of the lib directory
|
|
|
|
# where libMLIR.so is installed. This is presently not optimal from a
|
|
|
|
# project separation perspective and a discussion on how to better
|
|
|
|
# segment MLIR libraries needs to happen.
|
|
|
|
LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/python
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
OUTPUT_NAME "_mlir"
|
|
|
|
PREFIX "${PYTHON_MODULE_PREFIX}"
|
|
|
|
SUFFIX "${PYTHON_MODULE_SUFFIX}${PYTHON_MODULE_EXTENSION}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# pybind11 requires binding code to be compiled with -fvisibility=hidden
|
|
|
|
# For static linkage, better code can be generated if the entire project
|
|
|
|
# compiles that way, but that is not enforced here. Instead, include a linker
|
|
|
|
# script that explicitly hides anything but the PyInit_* symbols, allowing gc
|
|
|
|
# to take place.
|
|
|
|
# TODO: Add a Windows .def file and figure out the right thing to do on MacOS.
|
|
|
|
set_target_properties(
|
|
|
|
MLIRBindingsPythonExtension PROPERTIES CXX_VISIBILITY_PRESET "hidden")
|
2020-10-10 06:50:07 +08:00
|
|
|
|
|
|
|
set(PYEXT_DEPS)
|
|
|
|
list(APPEND PYEXT_DEPS
|
2020-11-05 10:29:40 +08:00
|
|
|
# Depend on libMLIR.so first so that deps primarily come from the shared
|
|
|
|
# library.
|
|
|
|
MLIR
|
2020-10-10 06:50:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
target_link_libraries(MLIRBindingsPythonExtension
|
|
|
|
PRIVATE
|
|
|
|
${PYEXT_DEPS}
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-07 14:05:46 +08:00
|
|
|
${PYEXT_LIBADD}
|
|
|
|
)
|
2020-10-10 06:50:07 +08:00
|
|
|
|
|
|
|
add_dependencies(MLIRBindingsPythonExtension MLIRBindingsPythonSources)
|
|
|
|
llvm_setup_rpath(MLIRBindingsPythonExtension)
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Install
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
install(TARGETS MLIRBindingsPythonExtension
|
|
|
|
COMPONENT MLIRBindingsPythonExtension
|
|
|
|
LIBRARY DESTINATION python
|
|
|
|
ARCHIVE DESTINATION python
|
|
|
|
# NOTE: Even on DLL-platforms, extensions go in the lib directory tree.
|
|
|
|
RUNTIME DESTINATION python)
|
|
|
|
|
|
|
|
# Note that we copy from the source tree just like for headers because
|
|
|
|
# it will not be polluted with py_cache runtime artifacts (from testing and
|
|
|
|
# such).
|
|
|
|
install(
|
|
|
|
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/mlir
|
|
|
|
DESTINATION python
|
|
|
|
COMPONENT MLIRBindingsPythonSources
|
|
|
|
FILES_MATCHING PATTERN "*.py"
|
|
|
|
)
|
|
|
|
|
|
|
|
if (NOT LLVM_ENABLE_IDE)
|
|
|
|
add_llvm_install_targets(
|
|
|
|
install-MLIRBindingsPythonExtension
|
|
|
|
DEPENDS MLIRBindingsPythonExtension
|
|
|
|
COMPONENT MLIRBindingsPythonExtension)
|
|
|
|
add_llvm_install_targets(
|
|
|
|
install-MLIRBindingsPythonSources
|
|
|
|
DEPENDS MLIRBindingsPythonSources
|
|
|
|
COMPONENT MLIRBindingsPythonSources)
|
|
|
|
endif()
|