Skip to content

Commit b696366

Browse files
committed
[mlir][Python] create MLIRPythonSupport
1 parent 86cc934 commit b696366

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

mlir/python/CMakeLists.txt

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ include(AddMLIRPython)
33
# Specifies that all MLIR packages are co-located under the `MLIR_PYTHON_PACKAGE_PREFIX.`
44
# top level package (the API has been embedded in a relocatable way).
55
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=${MLIR_PYTHON_PACKAGE_PREFIX}.")
6+
set(MLIRPythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
7+
set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
68

79
################################################################################
810
# Structural groupings.
@@ -524,28 +526,18 @@ declare_mlir_dialect_python_bindings(
524526
# dependencies.
525527
################################################################################
526528

527-
set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
528529
declare_mlir_python_extension(MLIRPythonExtension.Core
529530
MODULE_NAME _mlir
530531
ADD_TO_PARENT MLIRPythonSources.Core
531532
ROOT_DIR "${PYTHON_SOURCE_DIR}"
532533
PYTHON_BINDINGS_LIBRARY nanobind
533534
SOURCES
534535
MainModule.cpp
535-
IRAffine.cpp
536-
IRAttributes.cpp
537-
IRCore.cpp
538-
IRInterfaces.cpp
539-
IRModule.cpp
540-
IRTypes.cpp
541536
Pass.cpp
542537
Rewrite.cpp
543538

544539
# Headers must be included explicitly so they are installed.
545-
Globals.h
546-
IRModule.h
547540
Pass.h
548-
NanobindUtils.h
549541
Rewrite.h
550542
PRIVATE_LINK_LIBS
551543
LLVMSupport
@@ -768,8 +760,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.SMT.Pybind
768760
PYTHON_BINDINGS_LIBRARY nanobind
769761
SOURCES
770762
DialectSMT.cpp
771-
# Headers must be included explicitly so they are installed.
772-
NanobindUtils.h
773763
PRIVATE_LINK_LIBS
774764
LLVMSupport
775765
EMBED_CAPI_LINK_LIBS
@@ -876,7 +866,6 @@ endif()
876866
# once ready.
877867
################################################################################
878868

879-
set(MLIRPythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
880869
add_mlir_python_common_capi_library(MLIRPythonCAPI
881870
INSTALL_COMPONENT MLIRPythonModules
882871
INSTALL_DESTINATION "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/_mlir_libs"
@@ -1013,3 +1002,53 @@ if(MLIR_PYTHON_STUBGEN_ENABLED)
10131002
add_dependencies(MLIRPythonModules "${_mlirPythonTestNanobind_typestub_gen_target}")
10141003
endif()
10151004
endif()
1005+
1006+
get_property(NB_LIBRARY_TARGET_NAME TARGET MLIRPythonModules.extension._mlir.dso PROPERTY LINK_LIBRARIES)
1007+
list(GET NB_LIBRARY_TARGET_NAME 0 NB_LIBRARY_TARGET_NAME)
1008+
add_mlir_library_install(${NB_LIBRARY_TARGET_NAME})
1009+
add_mlir_library(MLIRPythonSupport
1010+
${PYTHON_SOURCE_DIR}/Globals.cpp
1011+
${PYTHON_SOURCE_DIR}/IRAffine.cpp
1012+
${PYTHON_SOURCE_DIR}/IRAttributes.cpp
1013+
${PYTHON_SOURCE_DIR}/IRCore.cpp
1014+
${PYTHON_SOURCE_DIR}/IRInterfaces.cpp
1015+
${PYTHON_SOURCE_DIR}/IRTypes.cpp
1016+
EXCLUDE_FROM_LIBMLIR
1017+
SHARED
1018+
LINK_COMPONENTS
1019+
Support
1020+
LINK_LIBS
1021+
${NB_LIBRARY_TARGET_NAME}
1022+
MLIRCAPIIR
1023+
)
1024+
target_link_libraries(MLIRPythonSupport PUBLIC ${NB_LIBRARY_TARGET_NAME})
1025+
nanobind_link_options(MLIRPythonSupport)
1026+
set_target_properties(MLIRPythonSupport PROPERTIES
1027+
LIBRARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1028+
BINARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1029+
# Needed for windows (and doesn't hurt others).
1030+
RUNTIME_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1031+
ARCHIVE_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1032+
)
1033+
set(eh_rtti_enable)
1034+
if(MSVC)
1035+
set(eh_rtti_enable /EHsc /GR)
1036+
elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
1037+
set(eh_rtti_enable -frtti -fexceptions)
1038+
endif()
1039+
target_compile_options(MLIRPythonSupport PRIVATE ${eh_rtti_enable})
1040+
if(APPLE)
1041+
# NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
1042+
# doesn't declare this API as undefined in its linker flags. So we need to declare it as such
1043+
# for downstream users that do not do something like `-undefined dynamic_lookup`.
1044+
# Same for the rest.
1045+
target_link_options(MLIRPythonSupport PUBLIC
1046+
"LINKER:-U,_PyClassMethod_New"
1047+
"LINKER:-U,_PyCode_Addr2Location"
1048+
"LINKER:-U,_PyFrame_GetLasti"
1049+
)
1050+
endif()
1051+
target_link_libraries(
1052+
MLIRPythonModules.extension._mlir.dso
1053+
PUBLIC MLIRPythonSupport)
1054+

0 commit comments

Comments
 (0)