diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -66,13 +66,12 @@ # setting -DMLIR_PYTHON_BINDINGS_VERSION_LOCKED=OFF #------------------------------------------------------------------------------- -option(MLIR_ENABLE_PYTHON_BINDINGS +set(MLIR_BINDINGS_PYTHON_ENABLED 0 CACHE BOOL "Enables building of Python bindings.") -option(MLIR_PYTHON_BINDINGS_VERSION_LOCKED - "Links to specific python libraries, resolving all symbols." - ON) +set(MLIR_PYTHON_BINDINGS_VERSION_LOCKED 1 CACHE BOOL + "Links to specific python libraries, resolving all symbols.") -if(MLIR_ENABLE_PYTHON_BINDINGS) +if(MLIR_BINDINGS_PYTHON_ENABLED) find_package(PythonInterp REQUIRED) find_package(PythonLibs REQUIRED) message(STATUS "Found python include dirs: ${PYTHON_INCLUDE_DIRS}") @@ -98,8 +97,6 @@ add_definitions(-DMLIR_INCLUDE_TESTS) add_subdirectory(unittests) add_subdirectory(test) - # Must come after test, since it extends it. - add_subdirectory(bindings_test) endif() if (MLIR_INCLUDE_INTEGRATION_TESTS) add_definitions(-DMLIR_INCLUDE_INTEGRATION_TESTS) diff --git a/mlir/bindings_test/CMakeLists.txt b/mlir/bindings_test/CMakeLists.txt deleted file mode 100644 --- a/mlir/bindings_test/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -if(MLIR_ENABLE_PYTHON_BINDINGS) - add_subdirectory(Python) -endif() diff --git a/mlir/bindings_test/Python/CMakeLists.txt b/mlir/bindings_test/Python/CMakeLists.txt deleted file mode 100644 --- a/mlir/bindings_test/Python/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -# Passed to lit.site.cfg.py.in to set up the path where to find the libraries. - -set(TEST_DEPENDS - MLIRBindingsPythonExtension - FileCheck count not - mlir-opt - ) - -configure_lit_site_cfg( - ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in - ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py - ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py - ) - -add_lit_testsuite(check-mlir-bindings-python "Running the MLIR Python bindings tests" - ${CMAKE_CURRENT_BINARY_DIR} - DEPENDS ${TEST_DEPENDS} - ) -set_target_properties(check-mlir-bindings-python PROPERTIES FOLDER "bindings_test/python") - -# Extend the check-mlir target if the python bindings are enabled. -add_dependencies(check-mlir check-mlir-bindings-python) - -add_lit_testsuites(MLIR_BINDINGS_PYTHON ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${TEST_DEPENDS} - ) diff --git a/mlir/bindings_test/Python/lit.cfg.py b/mlir/bindings_test/Python/lit.cfg.py deleted file mode 100644 --- a/mlir/bindings_test/Python/lit.cfg.py +++ /dev/null @@ -1,58 +0,0 @@ -# -*- Python -*- - -import os -import platform -import re -import subprocess -import tempfile - -import lit.formats -import lit.util - -from lit.llvm import llvm_config -from lit.llvm.subst import ToolSubst - -# Configuration file for the 'lit' integration test runner. - -# name: The name of this integration test suite. -config.name = 'MLIR_BINDINGS_PYTHON' - -config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) - -# suffixes: A list of file extensions to treat as integration test files. -config.suffixes = ['.py'] - -# test_source_root: The root path where integration tests are located. -config.test_source_root = os.path.dirname(__file__) - -# test_exec_root: The root path where integration tests should be run. -config.test_exec_root = os.path.join(config.mlir_obj_root, 'bindings_test/Python') - -config.substitutions.append(('%PATH%', config.environment['PATH'])) -config.substitutions.append(('%shlibext', config.llvm_shlib_ext)) -config.substitutions.append(('%mlir_src_root', config.mlir_src_root)) -config.substitutions.append(('%PYTHON', config.python_executable)) - -llvm_config.with_system_environment(['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP']) - -llvm_config.use_default_substitutions() - -# excludes: A list of directories to exclude from the integration testsuite. -config.excludes = ['CMakeLists.txt', 'README.txt', 'LICENSE.txt', - 'lit.cfg.py', 'lit.site.cfg.py'] - -# Tweak the PATH to include the tools dir. -llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True) - -# Add the python path for both the source and binary tree. -llvm_config.with_environment('PYTHONPATH', [ - os.path.join(config.mlir_src_root, "lib", "Bindings", "Python"), - os.path.join(config.mlir_obj_root, "lib", "Bindings", "Python"), -], append_path=True) - -tool_dirs = [config.mlir_tools_dir, config.llvm_tools_dir] -tools = [ - 'mlir-opt', -] - -llvm_config.add_tool_substitutions(tools, tool_dirs) diff --git a/mlir/bindings_test/Python/lit.site.cfg.py.in b/mlir/bindings_test/Python/lit.site.cfg.py.in deleted file mode 100644 --- a/mlir/bindings_test/Python/lit.site.cfg.py.in +++ /dev/null @@ -1,51 +0,0 @@ -@LIT_SITE_CFG_IN_HEADER@ - -import sys - -config.host_triple = "@LLVM_HOST_TRIPLE@" -config.target_triple = "@TARGET_TRIPLE@" -config.llvm_src_root = "@LLVM_SOURCE_DIR@" -config.llvm_obj_root = "@LLVM_BINARY_DIR@" -config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" -config.llvm_lib_dir = "@LLVM_LIBRARY_DIR@" -config.llvm_shlib_dir = "@SHLIBDIR@" -config.llvm_shlib_ext = "@SHLIBEXT@" -config.llvm_exe_ext = "@EXEEXT@" -config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" -config.python_executable = "@PYTHON_EXECUTABLE@" -config.gold_executable = "@GOLD_EXECUTABLE@" -config.ld64_executable = "@LD64_EXECUTABLE@" -config.enable_shared = @ENABLE_SHARED@ -config.enable_assertions = @ENABLE_ASSERTIONS@ -config.targets_to_build = "@TARGETS_TO_BUILD@" -config.native_target = "@LLVM_NATIVE_ARCH@" -config.llvm_bindings = "@LLVM_BINDINGS@".split(' ') -config.host_os = "@HOST_OS@" -config.host_cc = "@HOST_CC@" -config.host_cxx = "@HOST_CXX@" -config.host_cmake = "@CMAKE_COMMAND@" -# Note: ldflags can contain double-quoted paths, so must use single quotes here. -config.host_ldflags = '@HOST_LDFLAGS@' -config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@" -config.llvm_host_triple = '@LLVM_HOST_TRIPLE@' -config.host_arch = "@HOST_ARCH@" -config.mlir_src_root = "@MLIR_SOURCE_DIR@" -config.mlir_obj_root = "@MLIR_BINARY_DIR@" -config.mlir_tools_dir = "@MLIR_TOOLS_DIR@" -config.python_executable = "@PYTHON_EXECUTABLE@" - -# Support substitution of the tools_dir with user parameters. This is -# used when we can't determine the tool dir at configuration time. -try: - config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params - config.llvm_shlib_dir = config.llvm_shlib_dir % lit_config.params -except KeyError: - e = sys.exc_info()[1] - key, = e.args - lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) - -import lit.llvm -lit.llvm.initialize(lit_config, config) - -# Let the main config do the real work. -lit_config.load_config(config, "@MLIR_SOURCE_DIR@/bindings_test/Python/lit.cfg.py") diff --git a/mlir/lib/Bindings/CMakeLists.txt b/mlir/lib/Bindings/CMakeLists.txt --- a/mlir/lib/Bindings/CMakeLists.txt +++ b/mlir/lib/Bindings/CMakeLists.txt @@ -1,3 +1,3 @@ -if(MLIR_ENABLE_PYTHON_BINDINGS) +if(MLIR_BINDINGS_PYTHON_ENABLED) add_subdirectory(Python) endif() 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 @@ -54,9 +54,10 @@ # 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") -if(NOT MSVC) +if(NOT MSVC AND NOT APPLE) set_target_properties(MLIRBindingsPythonExtension PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/unix_version.lds") @@ -64,6 +65,6 @@ target_link_libraries(MLIRBindingsPythonExtension PRIVATE - + MLIRIR ${PYEXT_LIBADD} ) diff --git a/mlir/lib/Bindings/Python/MainModule.cpp b/mlir/lib/Bindings/Python/MainModule.cpp --- a/mlir/lib/Bindings/Python/MainModule.cpp +++ b/mlir/lib/Bindings/Python/MainModule.cpp @@ -6,11 +6,22 @@ // //===----------------------------------------------------------------------===// +#include + #include +#include "mlir/IR/MLIRContext.h" + +using namespace mlir; + PYBIND11_MODULE(_mlir, m) { m.doc() = "MLIR Python Native Extension"; - m.def("get_test_value", - []() { return std::string("From the native module"); }); + m.def("get_test_value", []() { + // This is just calling a method on the MLIRContext as a smoketest + // for linkage. + MLIRContext context; + return std::make_tuple(std::string("From the native module"), + context.isMultithreadingEnabled()); + }); } diff --git a/mlir/test/Bindings/Python/lit.local.cfg b/mlir/test/Bindings/Python/lit.local.cfg new file mode 100644 --- /dev/null +++ b/mlir/test/Bindings/Python/lit.local.cfg @@ -0,0 +1,2 @@ +if not config.enable_bindings_python: + config.unsupported = True diff --git a/mlir/bindings_test/Python/smoke_test.py b/mlir/test/Bindings/Python/smoke_test.py rename from mlir/bindings_test/Python/smoke_test.py rename to mlir/test/Bindings/Python/smoke_test.py diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -4,7 +4,13 @@ add_subdirectory(lib) llvm_canonicalize_cmake_booleans( + MLIR_BINDINGS_PYTHON_ENABLED LLVM_BUILD_EXAMPLES + MLIR_CUDA_CONVERSIONS_ENABLED + MLIR_CUDA_RUNNER_ENABLED + MLIR_ROCM_CONVERSIONS_ENABLED + MLIR_ROCM_RUNNER_ENABLED + MLIR_VULKAN_RUNNER_ENABLED ) # Passed to lit.site.cfg.py.in to set up the path where to find the libraries @@ -78,6 +84,12 @@ ) endif() +if(MLIR_BINDINGS_PYTHON_ENABLED) + list(APPEND MLIR_TEST_DEPENDS + MLIRBindingsPythonExtension + ) +endif() + add_lit_testsuite(check-mlir "Running the MLIR regression tests" ${CMAKE_CURRENT_BINARY_DIR} DEPENDS ${MLIR_TEST_DEPENDS} diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py --- a/mlir/test/lit.cfg.py +++ b/mlir/test/lit.cfg.py @@ -21,7 +21,7 @@ config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell) # suffixes: A list of file extensions to treat as test files. -config.suffixes = ['.td', '.mlir', '.toy', '.ll', '.tc'] +config.suffixes = ['.td', '.mlir', '.toy', '.ll', '.tc', '.py'] # test_source_root: The root path where tests are located. config.test_source_root = os.path.dirname(__file__) @@ -41,7 +41,8 @@ # excludes: A list of directories to exclude from the testsuite. The 'Inputs' # subdirectories contain auxiliary inputs for various tests in their parent # directories. -config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt'] +config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt', + 'lit.cfg.py', 'lit.site.cfg.py'] # test_source_root: The root path where tests are located. config.test_source_root = os.path.dirname(__file__) @@ -62,6 +63,7 @@ # The following tools are optional tools.extend([ + ToolSubst('%PYTHON', config.python_executable), ToolSubst('toy-ch1', unresolved='ignore'), ToolSubst('toy-ch2', unresolved='ignore'), ToolSubst('toy-ch3', unresolved='ignore'), @@ -71,7 +73,7 @@ ToolSubst('%linalg_test_lib_dir', config.linalg_test_lib_dir, unresolved='ignore'), ToolSubst('%mlir_runner_utils_dir', config.mlir_runner_utils_dir, unresolved='ignore'), ToolSubst('%rocm_wrapper_library_dir', config.rocm_wrapper_library_dir, unresolved='ignore'), - ToolSubst('%vulkan_wrapper_library_dir', config.vulkan_wrapper_library_dir, unresolved='ignore') + ToolSubst('%vulkan_wrapper_library_dir', config.vulkan_wrapper_library_dir, unresolved='ignore'), ]) llvm_config.add_tool_substitutions(tools, tool_dirs) @@ -89,3 +91,13 @@ # to be available for JIT tests. if config.target_triple: config.available_features.add('default_triple') + +# Add the python path for both the source and binary tree. +# Note that presently, the python sources come from the source tree and the +# binaries come from the build tree. This should be unified to the build tree +# by copying/linking sources to build. +if config.enable_bindings_python: + llvm_config.with_environment('PYTHONPATH', [ + os.path.join(config.mlir_src_root, "lib", "Bindings", "Python"), + os.path.join(config.mlir_obj_root, "lib", "Bindings", "Python"), + ], append_path=True) diff --git a/mlir/test/lit.site.cfg.py.in b/mlir/test/lit.site.cfg.py.in --- a/mlir/test/lit.site.cfg.py.in +++ b/mlir/test/lit.site.cfg.py.in @@ -43,6 +43,7 @@ config.enable_rocm_runner = @MLIR_ROCM_RUNNER_ENABLED@ config.vulkan_wrapper_library_dir = "@MLIR_VULKAN_WRAPPER_LIBRARY_DIR@" config.enable_vulkan_runner = @MLIR_VULKAN_RUNNER_ENABLED@ +config.enable_bindings_python = @MLIR_BINDINGS_PYTHON_ENABLED@ # Support substitution of the tools_dir with user parameters. This is # used when we can't determine the tool dir at configuration time.