Skip to content

Commit 1b97d91

Browse files
committed
[mlir][Python] create MLIRPythonSupport
1 parent ad92174 commit 1b97d91

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,27 +526,17 @@ 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
SOURCES
533534
MainModule.cpp
534-
IRAffine.cpp
535-
IRAttributes.cpp
536-
IRCore.cpp
537-
IRInterfaces.cpp
538-
IRModule.cpp
539-
IRTypes.cpp
540535
Pass.cpp
541536
Rewrite.cpp
542537

543538
# Headers must be included explicitly so they are installed.
544-
Globals.h
545-
IRModule.h
546539
Pass.h
547-
NanobindUtils.h
548540
Rewrite.h
549541
PRIVATE_LINK_LIBS
550542
LLVMSupport
@@ -752,8 +744,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.SMT.Nanobind
752744
ROOT_DIR "${PYTHON_SOURCE_DIR}"
753745
SOURCES
754746
DialectSMT.cpp
755-
# Headers must be included explicitly so they are installed.
756-
NanobindUtils.h
757747
PRIVATE_LINK_LIBS
758748
LLVMSupport
759749
EMBED_CAPI_LINK_LIBS
@@ -860,7 +850,6 @@ endif()
860850
# once ready.
861851
################################################################################
862852

863-
set(MLIRPythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
864853
add_mlir_python_common_capi_library(MLIRPythonCAPI
865854
INSTALL_COMPONENT MLIRPythonModules
866855
INSTALL_DESTINATION "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/_mlir_libs"
@@ -997,3 +986,53 @@ if(MLIR_PYTHON_STUBGEN_ENABLED)
997986
add_dependencies(MLIRPythonModules "${_mlirPythonTestNanobind_typestub_gen_target}")
998987
endif()
999988
endif()
989+
990+
get_property(NB_LIBRARY_TARGET_NAME TARGET MLIRPythonModules.extension._mlir.dso PROPERTY LINK_LIBRARIES)
991+
list(GET NB_LIBRARY_TARGET_NAME 0 NB_LIBRARY_TARGET_NAME)
992+
add_mlir_library_install(${NB_LIBRARY_TARGET_NAME})
993+
add_mlir_library(MLIRPythonSupport
994+
${PYTHON_SOURCE_DIR}/Globals.cpp
995+
${PYTHON_SOURCE_DIR}/IRAffine.cpp
996+
${PYTHON_SOURCE_DIR}/IRAttributes.cpp
997+
${PYTHON_SOURCE_DIR}/IRCore.cpp
998+
${PYTHON_SOURCE_DIR}/IRInterfaces.cpp
999+
${PYTHON_SOURCE_DIR}/IRTypes.cpp
1000+
EXCLUDE_FROM_LIBMLIR
1001+
SHARED
1002+
LINK_COMPONENTS
1003+
Support
1004+
LINK_LIBS
1005+
${NB_LIBRARY_TARGET_NAME}
1006+
MLIRCAPIIR
1007+
)
1008+
target_link_libraries(MLIRPythonSupport PUBLIC ${NB_LIBRARY_TARGET_NAME})
1009+
nanobind_link_options(MLIRPythonSupport)
1010+
set_target_properties(MLIRPythonSupport PROPERTIES
1011+
LIBRARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1012+
BINARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1013+
# Needed for windows (and doesn't hurt others).
1014+
RUNTIME_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1015+
ARCHIVE_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1016+
)
1017+
set(eh_rtti_enable)
1018+
if(MSVC)
1019+
set(eh_rtti_enable /EHsc /GR)
1020+
elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
1021+
set(eh_rtti_enable -frtti -fexceptions)
1022+
endif()
1023+
target_compile_options(MLIRPythonSupport PRIVATE ${eh_rtti_enable})
1024+
if(APPLE)
1025+
# NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
1026+
# doesn't declare this API as undefined in its linker flags. So we need to declare it as such
1027+
# for downstream users that do not do something like `-undefined dynamic_lookup`.
1028+
# Same for the rest.
1029+
target_link_options(MLIRPythonSupport PUBLIC
1030+
"LINKER:-U,_PyClassMethod_New"
1031+
"LINKER:-U,_PyCode_Addr2Location"
1032+
"LINKER:-U,_PyFrame_GetLasti"
1033+
)
1034+
endif()
1035+
target_link_libraries(
1036+
MLIRPythonModules.extension._mlir.dso
1037+
PUBLIC MLIRPythonSupport)
1038+

0 commit comments

Comments
 (0)