diff --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake --- a/mlir/cmake/modules/AddMLIR.cmake +++ b/mlir/cmake/modules/AddMLIR.cmake @@ -162,10 +162,7 @@ set_property(GLOBAL APPEND PROPERTY MLIR_EXPORTS ${name}) endfunction() -# Declare an mlir library which is part of the public C-API and will be -# compiled and exported into libMLIRPublicAPI.so/MLIRPublicAPI.dll. -# This shared library is built regardless of the overall setting of building -# libMLIR.so (which exports the C++ implementation). +# Declare an mlir library which is part of the public C-API. function(add_mlir_public_c_api_library name) add_mlir_library(${name} ${ARGN} @@ -186,7 +183,6 @@ PRIVATE -DMLIR_CAPI_BUILDING_LIBRARY=1 ) - set_property(GLOBAL APPEND PROPERTY MLIR_PUBLIC_C_API_LIBS ${name}) endfunction() # Declare the library associated with a dialect. diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake --- a/mlir/cmake/modules/AddMLIRPython.cmake +++ b/mlir/cmake/modules/AddMLIRPython.cmake @@ -35,7 +35,8 @@ # 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). - add_library(${libname} ${PYEXT_LINK_MODE} + add_library(${libname} + ${PYEXT_LINK_MODE} ${ARG_SOURCES} ) @@ -99,8 +100,6 @@ # if further dependencies are added explicitly. target_link_libraries(${libname} PRIVATE - MLIRPublicAPI - LLVMSupport ${ARG_LINK_LIBS} ${PYEXT_LIBADD} ) diff --git a/mlir/include/mlir-c/Support.h b/mlir/include/mlir-c/Support.h --- a/mlir/include/mlir-c/Support.h +++ b/mlir/include/mlir-c/Support.h @@ -22,9 +22,17 @@ //===----------------------------------------------------------------------===// // Visibility annotations. // Use MLIR_CAPI_EXPORTED for exported functions. +// +// On Windows, if MLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC is defined, then +// __declspec(dllexport) and __declspec(dllimport) will be generated. This +// can only be enabled if actually building DLLs. It is generally, mutually +// exclusive with the use of other mechanisms for managing imports/exports +// (i.e. CMake's WINDOWS_EXPORT_ALL_SYMBOLS feature). //===----------------------------------------------------------------------===// -#if defined(MLIR_CAPI_DISABLE_VISIBILITY_ANNOTATIONS) +#if (defined(_WIN32) || defined(__CYGWIN__)) && \ + !defined(MLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC) +// Visibility annotations disabled. #define MLIR_CAPI_EXPORTED #elif defined(_WIN32) || defined(__CYGWIN__) // Windows visibility declarations. diff --git a/mlir/lib/Bindings/Python/CMakeLists.txt b/mlir/lib/Bindings/Python/CMakeLists.txt --- a/mlir/lib/Bindings/Python/CMakeLists.txt +++ b/mlir/lib/Bindings/Python/CMakeLists.txt @@ -1,6 +1,59 @@ include(AddMLIRPython) add_custom_target(MLIRBindingsPythonExtension) +################################################################################ +# All python extensions must link through one DSO which exports the CAPI, and +# this must have a globally unique name amongst all embeddors of the python +# library since it will effectively have global scope. +# +# The presence of this aggregate library is part of the long term plan, but its +# use needs to be made more flexible. +################################################################################ + +set(public_api_libs + MLIRCAPIConversion + MLIRCAPIDebug + MLIRCEXECUTIONENGINE + MLIRCAPIIR + MLIRCAPIRegistration + MLIRCAPITransforms + + # Dialects + MLIRCAPIAsync + MLIRCAPIGPU + MLIRCAPILinalg + MLIRCAPILLVM + MLIRCAPIShape + MLIRCAPISparseTensor + MLIRCAPIStandard + MLIRCAPISCF + MLIRCAPITensor +) + +foreach(lib ${public_api_libs}) + if(XCODE) + # Xcode doesn't support object libraries, so we have to trick it into + # linking the static libraries instead. + list(APPEND _DEPS "-force_load" ${lib}) + else() + list(APPEND _OBJECTS $) + endif() + # Accumulate transitive deps of each exported lib into _DEPS. + list(APPEND _DEPS $) +endforeach() + +add_mlir_library(MLIRPythonCAPI + PARTIAL_SOURCES_INTENDED + SHARED + ${_OBJECTS} + EXCLUDE_FROM_LIBMLIR + LINK_LIBS + ${_DEPS} +) +if(MSVC) + set_property(TARGET MLIRPythonCAPI PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) +endif() + ################################################################################ # Build core python extension ################################################################################ @@ -19,6 +72,9 @@ PybindUtils.cpp Pass.cpp ExecutionEngine.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRCoreBindingsPythonExtension) @@ -30,6 +86,9 @@ python SOURCES AllPassesRegistration.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRAllPassesRegistrationBindingsPythonExtension) @@ -38,6 +97,9 @@ python SOURCES AsyncPasses.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRAsyncPassesBindingsPythonExtension) @@ -46,6 +108,9 @@ python SOURCES SparseTensorPasses.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRSparseTensorPassesBindingsPythonExtension) @@ -54,6 +119,9 @@ python SOURCES GPUPasses.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRGPUPassesBindingsPythonExtension) @@ -62,5 +130,8 @@ python SOURCES LinalgPasses.cpp + LINK_LIBS PRIVATE + LLVMSupport + MLIRPythonCAPI ) add_dependencies(MLIRBindingsPythonExtension MLIRLinalgPassesBindingsPythonExtension) diff --git a/mlir/lib/Bindings/Python/Conversions/CMakeLists.txt b/mlir/lib/Bindings/Python/Conversions/CMakeLists.txt --- a/mlir/lib/Bindings/Python/Conversions/CMakeLists.txt +++ b/mlir/lib/Bindings/Python/Conversions/CMakeLists.txt @@ -7,4 +7,6 @@ python SOURCES Conversions.cpp + LINK_LIBS PRIVATE + MLIRPythonCAPI ) diff --git a/mlir/lib/Bindings/Python/Transforms/CMakeLists.txt b/mlir/lib/Bindings/Python/Transforms/CMakeLists.txt --- a/mlir/lib/Bindings/Python/Transforms/CMakeLists.txt +++ b/mlir/lib/Bindings/Python/Transforms/CMakeLists.txt @@ -7,4 +7,6 @@ python SOURCES Transforms.cpp -) \ No newline at end of file + LINK_LIBS PRIVATE + MLIRPythonCAPI +) diff --git a/mlir/lib/CAPI/CMakeLists.txt b/mlir/lib/CAPI/CMakeLists.txt --- a/mlir/lib/CAPI/CMakeLists.txt +++ b/mlir/lib/CAPI/CMakeLists.txt @@ -5,46 +5,3 @@ add_subdirectory(IR) add_subdirectory(Registration) add_subdirectory(Transforms) - - -################################################################################ -# libMLIRPublicAPI shared library/DLL. -################################################################################ - -get_property(public_api_libs GLOBAL PROPERTY MLIR_PUBLIC_C_API_LIBS) - -foreach(lib ${public_api_libs}) - if(XCODE) - # Xcode doesn't support object libraries, so we have to trick it into - # linking the static libraries instead. - list(APPEND _DEPS "-force_load" ${lib}) - else() - list(APPEND _OBJECTS $) - endif() - # Accumulate transitive deps of each exported lib into _DEPS. - list(APPEND _DEPS $) -endforeach() - -if(MINGW) - set(MLIR_LINK_MLIR_DYLIB 0) -else() - set(MLIR_LINK_MLIR_DYLIB ${LLVM_BUILD_LLVM_DYLIB}) -endif() - -add_mlir_library(MLIRPublicAPI - SHARED - ${_OBJECTS} - EXCLUDE_FROM_LIBMLIR - LINK_LIBS - # Dependency on the implementation shared library. - $<$:MLIR> - ${_DEPS} -) - -target_link_options( - MLIRPublicAPI - PRIVATE - # On Linux, disable re-export of any static linked libraries that - # came through. - $<$:LINKER:--exclude-libs,ALL> -) diff --git a/mlir/python/mlir/_cext_loader.py b/mlir/python/mlir/_cext_loader.py --- a/mlir/python/mlir/_cext_loader.py +++ b/mlir/python/mlir/_cext_loader.py @@ -24,7 +24,7 @@ _load_extension = _mlir_libs.load_extension _preload_dependency = _mlir_libs.preload_dependency -_preload_dependency("MLIRPublicAPI") +_preload_dependency("MLIRPythonCAPI") # Expose the corresponding C-Extension module with a well-known name at this # top-level module. This allows relative imports like the following to diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt --- a/mlir/test/CAPI/CMakeLists.txt +++ b/mlir/test/CAPI/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_parse_arguments(ARG "" "" - "" + "LINK_LIBS" ${ARGN}) set(LLVM_LINK_COMPONENTS ) @@ -11,28 +11,45 @@ ${ARG_UNPARSED_ARGUMENTS}) llvm_update_compile_flags(${name}) target_link_libraries(${name} - PRIVATE - MLIRPublicAPI) + ${ARG_LINK_LIBS}) endfunction(_add_capi_test_executable) _add_capi_test_executable(mlir-capi-execution-engine-test execution_engine.c -DEPENDS - MLIRConversionPassIncGen +LINK_LIBS PRIVATE + MLIRCAPIConversion + MLIRCEXECUTIONENGINE + MLIRCAPIRegistration ) _add_capi_test_executable(mlir-capi-ir-test ir.c + LINK_LIBS PRIVATE + MLIRCAPIIR + MLIRCAPIStandard + MLIRCAPIRegistration ) _add_capi_test_executable(mlir-capi-llvm-test llvm.c + LINK_LIBS PRIVATE + MLIRCAPIIR + MLIRCAPILLVM + MLIRCAPIRegistration ) _add_capi_test_executable(mlir-capi-pass-test pass.c + LINK_LIBS PRIVATE + MLIRCAPIIR + MLIRCAPIRegistration + MLIRCAPITransforms ) _add_capi_test_executable(mlir-capi-sparse-tensor-test sparse_tensor.c + LINK_LIBS PRIVATE + MLIRCAPIIR + MLIRCAPIRegistration + MLIRCAPISparseTensor )