Skip to content

Commit ec797b3

Browse files
committed
massage cmake
1 parent 55a5f20 commit ec797b3

File tree

3 files changed

+111
-92
lines changed

3 files changed

+111
-92
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,27 @@ endfunction()
228228
# aggregate dylib that is linked against.
229229
function(declare_mlir_python_extension name)
230230
cmake_parse_arguments(ARG
231-
""
232-
"ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
231+
"SUPPORT_LIB"
232+
"ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;SOURCES_TYPE"
233233
"SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
234234
${ARGN})
235235

236236
if(NOT ARG_ROOT_DIR)
237237
set(ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
238238
endif()
239+
if(ARG_SUPPORT_LIB)
240+
set(SOURCES_TYPE "support")
241+
else()
242+
set(SOURCES_TYPE "extension")
243+
endif()
239244
set(_install_destination "src/python/${name}")
240245

241246
add_library(${name} INTERFACE)
242247
set_target_properties(${name} PROPERTIES
243248
# Yes: Leading-lowercase property names are load bearing and the recommended
244249
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
245250
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
246-
mlir_python_SOURCES_TYPE extension
251+
mlir_python_SOURCES_TYPE "${SOURCES_TYPE}"
247252
mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
248253
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
249254
mlir_python_DEPENDS ""
@@ -318,8 +323,21 @@ function(add_mlir_python_modules name)
318323
"ROOT_PREFIX;INSTALL_PREFIX"
319324
"COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
320325
${ARGN})
326+
327+
# build nanobind shared lib first
328+
if (NB_ABI MATCHES "[0-9]t")
329+
set(_ft "-ft")
330+
endif()
331+
# nanobind does a string match on the suffix to figure out whether to build
332+
# the lib with free threading...
333+
set(NB_LIBRARY_TARGET_NAME "nanobind${_ft}-${MLIR_BINDINGS_PYTHON_NB_DOMAIN}")
334+
nanobind_build_library(${NB_LIBRARY_TARGET_NAME} AS_SYSINCLUDE)
335+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
336+
target_link_options(${NB_LIBRARY_TARGET_NAME} PRIVATE "-Wl,-z,undefs")
337+
endif()
338+
321339
# Helper to process an individual target.
322-
function(_process_target modules_target sources_target)
340+
function(_process_target modules_target sources_target support_libs)
323341
get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
324342

325343
if(_source_type STREQUAL "pure")
@@ -337,16 +355,19 @@ function(add_mlir_python_modules name)
337355
get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
338356
# Transform relative source to based on root dir.
339357
set(_extension_target "${modules_target}.extension.${_module_name}.dso")
340-
add_mlir_python_extension(${_extension_target} "${_module_name}"
358+
add_mlir_python_extension(${_extension_target} "${_module_name}" ${NB_LIBRARY_TARGET_NAME}
341359
INSTALL_COMPONENT ${modules_target}
342360
INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
343361
OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
344362
LINK_LIBS PRIVATE
345363
${sources_target}
346364
${ARG_COMMON_CAPI_LINK_LIBS}
365+
${support_libs}
347366
)
348367
add_dependencies(${modules_target} ${_extension_target})
349368
mlir_python_setup_extension_rpath(${_extension_target})
369+
elseif(_source_type STREQUAL "support")
370+
# do nothing because already built
350371
else()
351372
message(SEND_ERROR "Unrecognized source type '${_source_type}' for python source target ${sources_target}")
352373
return()
@@ -356,8 +377,33 @@ function(add_mlir_python_modules name)
356377
# Build the modules target.
357378
add_custom_target(${name} ALL)
358379
_flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES})
380+
381+
# Build all support libs first
382+
set(_mlir_python_support_libs)
359383
foreach(sources_target ${_flat_targets})
360-
_process_target(${name} ${sources_target})
384+
get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
385+
if(_source_type STREQUAL "support")
386+
get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
387+
set(_extension_target "${name}.extension.${_module_name}.dso")
388+
add_mlir_python_extension(${_extension_target} "${_module_name}" ${NB_LIBRARY_TARGET_NAME}
389+
INSTALL_COMPONENT ${name}
390+
INSTALL_DIR "${ARG_INSTALL_PREFIX}/_mlir_libs"
391+
OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX}/_mlir_libs"
392+
SUPPORT_LIB
393+
LINK_LIBS PRIVATE
394+
LLVMSupport
395+
Python::Module
396+
${sources_target}
397+
${ARG_COMMON_CAPI_LINK_LIBS}
398+
)
399+
add_dependencies(${name} ${_extension_target})
400+
mlir_python_setup_extension_rpath(${_extension_target})
401+
list(APPEND _mlir_python_support_libs "${_extension_target}")
402+
endif()
403+
endforeach()
404+
405+
foreach(sources_target ${_flat_targets})
406+
_process_target(${name} ${sources_target} ${_mlir_python_support_libs})
361407
endforeach()
362408

363409
# Create an install target.
@@ -741,9 +787,9 @@ endfunction()
741787
################################################################################
742788
# Build python extension
743789
################################################################################
744-
function(add_mlir_python_extension libname extname)
790+
function(add_mlir_python_extension libname extname nb_library_target_name)
745791
cmake_parse_arguments(ARG
746-
""
792+
"SUPPORT_LIB"
747793
"INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
748794
"SOURCES;LINK_LIBS"
749795
${ARGN})
@@ -760,41 +806,57 @@ function(add_mlir_python_extension libname extname)
760806
set(eh_rtti_enable -frtti -fexceptions)
761807
endif ()
762808

763-
nanobind_add_module(${libname}
764-
NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
765-
FREE_THREADED
766-
NB_SHARED
767-
${ARG_SOURCES}
768-
)
809+
if(NOT MLIR_BINDINGS_PYTHON_NB_DOMAIN)
810+
set(MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir" CACHE STRING "" FORCE)
811+
endif()
812+
813+
if(ARG_SUPPORT_LIB)
814+
add_library(${libname} SHARED ${ARG_SOURCES})
815+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
816+
target_link_options(${libname} PRIVATE "-Wl,-z,undefs")
817+
endif()
818+
nanobind_link_options(${libname})
819+
target_compile_definitions(${libname} PRIVATE NB_DOMAIN=${MLIR_BINDINGS_PYTHON_NB_DOMAIN})
820+
if (MSVC)
821+
set_property(TARGET ${libname} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
822+
endif ()
823+
else()
824+
nanobind_add_module(${libname}
825+
NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
826+
FREE_THREADED
827+
NB_SHARED
828+
${ARG_SOURCES}
829+
)
830+
endif()
831+
target_link_libraries(${libname} PRIVATE ${nb_library_target_name})
769832

770833
if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
771834
AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
772835
# Avoid some warnings from upstream nanobind.
773836
# If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
774837
# the super project handle compile options as it wishes.
775-
get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
776-
target_compile_options(${NB_LIBRARY_TARGET_NAME}
838+
target_compile_options(${nb_library_target_name}
777839
PRIVATE
778840
-Wno-c++98-compat-extra-semi
779-
-Wno-cast-qual
780-
-Wno-covered-switch-default
781-
-Wno-deprecated-literal-operator
782-
-Wno-nested-anon-types
783-
-Wno-unused-parameter
784-
-Wno-zero-length-array
785-
-Wno-missing-field-initializers
841+
-Wno-cast-qual
842+
-Wno-covered-switch-default
843+
-Wno-deprecated-literal-operator
844+
-Wno-nested-anon-types
845+
-Wno-unused-parameter
846+
-Wno-zero-length-array
847+
-Wno-missing-field-initializers
786848
${eh_rtti_enable})
787849

788850
target_compile_options(${libname}
789851
PRIVATE
790852
-Wno-c++98-compat-extra-semi
791-
-Wno-cast-qual
792-
-Wno-covered-switch-default
793-
-Wno-deprecated-literal-operator
794-
-Wno-nested-anon-types
795-
-Wno-unused-parameter
796-
-Wno-zero-length-array
797-
-Wno-missing-field-initializers
853+
-Wno-cast-qual
854+
-Wno-covered-switch-default
855+
-Wno-deprecated-literal-operator
856+
-Wno-nested-anon-types
857+
-Wno-unused-parameter
858+
-Wno-zero-length-array
859+
-Wno-missing-field-initializers
798860
${eh_rtti_enable})
799861
endif()
800862

@@ -813,11 +875,16 @@ function(add_mlir_python_extension libname extname)
813875
target_compile_options(${libname} PRIVATE ${eh_rtti_enable})
814876

815877
# Configure the output to match python expectations.
878+
if (ARG_SUPPORT_LIB)
879+
set(_no_soname OFF)
880+
else ()
881+
set(_no_soname ON)
882+
endif ()
816883
set_target_properties(
817884
${libname} PROPERTIES
818885
LIBRARY_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
819886
OUTPUT_NAME "${extname}"
820-
NO_SONAME ON
887+
NO_SONAME ${_no_soname}
821888
)
822889

823890
if(WIN32)

mlir/examples/standalone/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
6666
if(NOT MLIR_PYTHON_PACKAGE_PREFIX)
6767
set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
6868
endif()
69+
if(NOT MLIR_BINDINGS_PYTHON_NB_DOMAIN)
70+
set(MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir_standalone" CACHE STRING "" FORCE)
71+
endif()
6972
if(NOT MLIR_BINDINGS_PYTHON_INSTALL_PREFIX)
7073
set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
7174
endif()

mlir/python/CMakeLists.txt

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,6 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.AMDGPU.Nanobind
784784
MODULE_NAME _mlirDialectsAMDGPU
785785
ADD_TO_PARENT MLIRPythonSources.Dialects.amdgpu
786786
ROOT_DIR "${PYTHON_SOURCE_DIR}"
787-
PYTHON_BINDINGS_LIBRARY nanobind
788787
SOURCES
789788
DialectAMDGPU.cpp
790789
PRIVATE_LINK_LIBS
@@ -841,6 +840,16 @@ if(MLIR_INCLUDE_TESTS)
841840
)
842841
endif()
843842

843+
declare_mlir_python_extension(MLIRPythonExtension.MLIRPythonSupport
844+
SUPPORT_LIB
845+
MODULE_NAME MLIRPythonSupport
846+
ADD_TO_PARENT MLIRPythonSources.Core
847+
ROOT_DIR "${PYTHON_SOURCE_DIR}"
848+
SOURCES
849+
IRCore.cpp
850+
Globals.cpp
851+
)
852+
844853
################################################################################
845854
# Common CAPI dependency DSO.
846855
# All python extensions must link through one DSO which exports the CAPI, and
@@ -990,63 +999,3 @@ if(MLIR_PYTHON_STUBGEN_ENABLED)
990999
add_dependencies(MLIRPythonModules "${_mlirPythonTestNanobind_typestub_gen_target}")
9911000
endif()
9921001
endif()
993-
994-
get_property(NB_LIBRARY_TARGET_NAME TARGET MLIRPythonModules.extension._mlir.dso PROPERTY LINK_LIBRARIES)
995-
list(GET NB_LIBRARY_TARGET_NAME 0 NB_LIBRARY_TARGET_NAME)
996-
add_mlir_library_install(${NB_LIBRARY_TARGET_NAME})
997-
add_library(MLIRPythonSupport SHARED
998-
${PYTHON_SOURCE_DIR}/IRCore.cpp
999-
${PYTHON_SOURCE_DIR}/Globals.cpp
1000-
)
1001-
target_link_libraries(MLIRPythonSupport PRIVATE
1002-
LLVMSupport
1003-
Python::Module
1004-
${NB_LIBRARY_TARGET_NAME}
1005-
MLIRPythonCAPI
1006-
1007-
)
1008-
if((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND (NOT LLVM_USE_SANITIZER))
1009-
target_link_options(MLIRPythonSupport PRIVATE "-Wl,-z,undefs")
1010-
target_link_options(${NB_LIBRARY_TARGET_NAME} PRIVATE "-Wl,-z,undefs")
1011-
endif()
1012-
nanobind_link_options(MLIRPythonSupport)
1013-
set_target_properties(MLIRPythonSupport PROPERTIES
1014-
LIBRARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1015-
BINARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1016-
# Needed for windows (and doesn't hurt others).
1017-
RUNTIME_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1018-
ARCHIVE_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1019-
)
1020-
set_target_properties(${NB_LIBRARY_TARGET_NAME} PROPERTIES
1021-
LIBRARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1022-
BINARY_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1023-
# Needed for windows (and doesn't hurt others).
1024-
RUNTIME_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1025-
ARCHIVE_OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
1026-
)
1027-
set(eh_rtti_enable)
1028-
if(MSVC)
1029-
set(eh_rtti_enable /EHsc /GR)
1030-
set_property(TARGET MLIRPythonSupport PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
1031-
elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
1032-
set(eh_rtti_enable -frtti -fexceptions)
1033-
endif()
1034-
target_compile_options(MLIRPythonSupport PRIVATE ${eh_rtti_enable})
1035-
if(APPLE)
1036-
# NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
1037-
# doesn't declare this API as undefined in its linker flags. So we need to declare it as such
1038-
# for downstream users that do not do something like `-undefined dynamic_lookup`.
1039-
# Same for the rest.
1040-
target_link_options(MLIRPythonSupport PUBLIC
1041-
"LINKER:-U,_PyClassMethod_New"
1042-
"LINKER:-U,_PyCode_Addr2Location"
1043-
"LINKER:-U,_PyFrame_GetLasti"
1044-
)
1045-
endif()
1046-
target_link_libraries(
1047-
MLIRPythonModules.extension._mlir.dso
1048-
PUBLIC MLIRPythonSupport)
1049-
target_link_libraries(
1050-
MLIRPythonModules.extension._mlirPythonTestNanobind.dso
1051-
PUBLIC MLIRPythonSupport)
1052-
target_compile_definitions(MLIRPythonSupport PRIVATE NB_DOMAIN=${MLIR_BINDINGS_PYTHON_NB_DOMAIN})

0 commit comments

Comments
 (0)