Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .ci/all_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ ml-dtypes==0.5.1 ; python_version < "3.13" \
--hash=sha256:d13755f8e8445b3870114e5b6240facaa7cb0c3361e54beba3e07fa912a6e12b \
--hash=sha256:fd918d4e6a4e0c110e2e05be7a7814d10dc1b95872accbf6512b80a109b71ae1
# via -r mlir/python/requirements.txt
nanobind==2.7.0 \
--hash=sha256:73b12d0e751d140d6c1bf4b215e18818a8debfdb374f08dc3776ad208d808e74 \
--hash=sha256:f9f1b160580c50dcf37b6495a0fd5ec61dc0d95dae5f8004f87dd9ad7eb46b34
nanobind==2.9.2 \
--hash=sha256:c37957ffd5eac7eda349cff3622ecd32e5ee1244ecc912c99b5bc8188bafd16e \
--hash=sha256:e7608472de99d375759814cab3e2c94aba3f9ec80e62cfef8ced495ca5c27d6e
# via -r mlir/python/requirements.txt
numpy==2.0.2 \
--hash=sha256:0123ffdaa88fa4ab64835dcbde75dcdf89c453c922f18dced6e27c90d1d0ec5a \
Expand Down Expand Up @@ -383,6 +383,10 @@ swig==4.3.1 \
--hash=sha256:efec16327029f682f649a26da726bb0305be8800bd0f1fa3e81bf0769cf5b476 \
--hash=sha256:fc496c0d600cf1bb2d91e28d3d6eae9c4301e5ea7a0dec5a4281b5efed4245a8
# via -r lldb/test/requirements.txt
typing-extensions==4.15.0 \
--hash=sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466 \
--hash=sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548
# via -r mlir/python/requirements.txt
urllib3==2.5.0 \
--hash=sha256:3fc47733c7e419d4bc3f6b3dc2b4f890bb743906a30d56ba4a5bfa4bbff92760 \
--hash=sha256:e6b01673c0fa6a13e374b50871808eb3bf7046c4b125b216f6bf1cc604cff0dc
Expand Down
114 changes: 94 additions & 20 deletions mlir/cmake/modules/AddMLIRPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,57 @@ function(declare_mlir_python_sources name)
endif()
endfunction()

# Function: generate_type_stubs
# Turns on automatic type stub generation (via nanobind's stubgen) for extension modules.
# Arguments:
# MODULE_NAME: The name of the extension module as specified in declare_mlir_python_extension.
# DEPENDS_TARGET: The dso target corresponding to the extension module
# (e.g., something like StandalonePythonModules.extension._standaloneDialectsNanobind.dso)
# MLIR_DEPENDS_TARGET: The dso target corresponding to the main/core extension module
# (e.g., something like StandalonePythonModules.extension._mlir.dso)
# OUTPUT_DIR: The root output directory to emit the type stubs into.
# Outputs:
# NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on.
function(generate_type_stubs MODULE_NAME DEPENDS_TARGET MLIR_DEPENDS_TARGET OUTPUT_DIR)
if(EXISTS ${nanobind_DIR}/../src/stubgen.py)
set(NB_STUBGEN "${nanobind_DIR}/../src/stubgen.py")
elseif(EXISTS ${nanobind_DIR}/../stubgen.py)
set(NB_STUBGEN "${nanobind_DIR}/../stubgen.py")
else()
message(FATAL_ERROR "generate_type_stubs(): could not locate 'stubgen.py'!")
endif()
file(REAL_PATH "${NB_STUBGEN}" NB_STUBGEN)

set(_module "${MLIR_PYTHON_PACKAGE_PREFIX}._mlir_libs.${MODULE_NAME}")
file(REAL_PATH "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/.." _import_path)

set(NB_STUBGEN_CMD
"${Python_EXECUTABLE}"
"${NB_STUBGEN}"
--module
"${_module}"
-i
"${_import_path}"
--recursive
--include-private
--output-dir
"${OUTPUT_DIR}")

set(NB_STUBGEN_OUTPUT "${OUTPUT_DIR}/${MODULE_NAME}.pyi")
add_custom_command(
OUTPUT ${NB_STUBGEN_OUTPUT}
COMMAND ${NB_STUBGEN_CMD}
WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}"
DEPENDS
"${MLIR_DEPENDS_TARGET}.extension._mlir.dso"
"${MLIR_DEPENDS_TARGET}.sources.MLIRPythonSources.Core.Python"
"${DEPENDS_TARGET}"
)
set(_name "MLIRPythonModuleStubs_${_module}")
add_custom_target("${_name}" ALL DEPENDS ${NB_STUBGEN_OUTPUT})
set(NB_STUBGEN_CUSTOM_TARGET "${_name}" PARENT_SCOPE)
endfunction()

# Function: declare_mlir_python_extension
# Declares a buildable python extension from C++ source files. The built
# module is considered a python source file and included as everything else.
Expand All @@ -115,9 +166,10 @@ endfunction()
# on. These will be collected for all extensions and put into an
# aggregate dylib that is linked against.
# PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
# NO_GENERATE_TYPE_STUBS: Disable type stub generation.
function(declare_mlir_python_extension name)
cmake_parse_arguments(ARG
""
"NO_GENERATE_TYPE_STUBS"
"ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY"
"SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
${ARGN})
Expand All @@ -130,17 +182,21 @@ function(declare_mlir_python_extension name)
if(NOT ARG_PYTHON_BINDINGS_LIBRARY)
set(ARG_PYTHON_BINDINGS_LIBRARY "pybind11")
endif()
if(ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11")
set(ARG_NO_GENERATE_TYPE_STUBS TRUE)
endif()

add_library(${name} INTERFACE)
set_target_properties(${name} PROPERTIES
# Yes: Leading-lowercase property names are load bearing and the recommended
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY"
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY;mlir_python_NO_GENERATE_TYPE_STUBS"
mlir_python_SOURCES_TYPE extension
mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME}"
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
mlir_python_DEPENDS ""
mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY}"
mlir_python_NO_GENERATE_TYPE_STUBS "${ARG_NO_GENERATE_TYPE_STUBS}"
)

# Set the interface source and link_libs properties of the target
Expand Down Expand Up @@ -243,6 +299,22 @@ function(add_mlir_python_modules name)
)
add_dependencies(${modules_target} ${_extension_target})
mlir_python_setup_extension_rpath(${_extension_target})
get_target_property(_no_generate_type_stubs ${sources_target} mlir_python_NO_GENERATE_TYPE_STUBS)
if(NOT _no_generate_type_stubs)
generate_type_stubs(
${_module_name}
${_extension_target}
${name}
"${CMAKE_CURRENT_SOURCE_DIR}/mlir/_mlir_libs/_mlir"
)
declare_mlir_python_sources(
"${MLIR_PYTHON_PACKAGE_PREFIX}.${_module_name}_type_stub_gen"
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
ADD_TO_PARENT "${sources_target}"
SOURCES_GLOB "_mlir_libs/${_module_name}/**/*.pyi"
)
add_dependencies("${modules_target}" "${NB_STUBGEN_CUSTOM_TARGET}")
endif()
else()
message(SEND_ERROR "Unrecognized source type '${_source_type}' for python source target ${sources_target}")
return()
Expand Down Expand Up @@ -678,26 +750,28 @@ function(add_mlir_python_extension libname extname)
# the super project handle compile options as it wishes.
get_property(NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES)
target_compile_options(${NB_LIBRARY_TARGET_NAME}
PRIVATE
-Wall -Wextra -Wpedantic
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
${eh_rtti_enable})
PRIVATE
-Wall -Wextra -Wpedantic
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-deprecated-literal-operator
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
${eh_rtti_enable})

target_compile_options(${libname}
PRIVATE
-Wall -Wextra -Wpedantic
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
${eh_rtti_enable})
PRIVATE
-Wall -Wextra -Wpedantic
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-deprecated-literal-operator
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
${eh_rtti_enable})
endif()

if(APPLE)
Expand Down
21 changes: 8 additions & 13 deletions mlir/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
include(AddMLIRPython)

# Specifies that all MLIR packages are co-located under the `mlir_standalone`
# top level package (the API has been embedded in a relocatable way).
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=${MLIR_PYTHON_PACKAGE_PREFIX}.")

################################################################################
# Structural groupings.
################################################################################
Expand All @@ -23,11 +27,6 @@ declare_mlir_python_sources(MLIRPythonSources.Core.Python
passmanager.py
rewrite.py
dialects/_ods_common.py

# The main _mlir module has submodules: include stubs from each.
_mlir_libs/_mlir/__init__.pyi
_mlir_libs/_mlir/ir.pyi
_mlir_libs/_mlir/passmanager.pyi
)

declare_mlir_python_sources(MLIRPythonSources.Core.Python.Extras
Expand All @@ -43,7 +42,6 @@ declare_mlir_python_sources(MLIRPythonSources.ExecutionEngine
ADD_TO_PARENT MLIRPythonSources
SOURCES
execution_engine.py
_mlir_libs/_mlirExecutionEngine.pyi
SOURCES_GLOB
runtime/*.py
)
Expand Down Expand Up @@ -195,7 +193,6 @@ declare_mlir_dialect_python_bindings(
TD_FILE dialects/TransformOps.td
SOURCES
dialects/transform/__init__.py
_mlir_libs/_mlir/dialects/transform/__init__.pyi
DIALECT_NAME transform
GEN_ENUM_BINDINGS_TD_FILE
"../../include/mlir/Dialect/Transform/IR/TransformAttrs.td"
Expand Down Expand Up @@ -367,8 +364,7 @@ declare_mlir_python_sources(
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
GEN_ENUM_BINDINGS
SOURCES
dialects/quant.py
_mlir_libs/_mlir/dialects/quant.pyi)
dialects/quant.py)

declare_mlir_dialect_python_bindings(
ADD_TO_PARENT MLIRPythonSources.Dialects
Expand All @@ -384,7 +380,6 @@ declare_mlir_dialect_python_bindings(
TD_FILE dialects/PDLOps.td
SOURCES
dialects/pdl.py
_mlir_libs/_mlir/dialects/pdl.pyi
DIALECT_NAME pdl)

declare_mlir_dialect_python_bindings(
Expand Down Expand Up @@ -778,6 +773,7 @@ if(MLIR_INCLUDE_TESTS)
LLVMSupport
EMBED_CAPI_LINK_LIBS
MLIRCAPIPythonTestDialect
NO_GENERATE_TYPE_STUBS
)
declare_mlir_python_extension(MLIRPythonTestSources.PythonTestExtensionNanobind
MODULE_NAME _mlirPythonTestNanobind
Expand Down Expand Up @@ -809,7 +805,7 @@ endif()
add_mlir_python_common_capi_library(MLIRPythonCAPI
INSTALL_COMPONENT MLIRPythonModules
INSTALL_DESTINATION "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/_mlir_libs"
OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir/_mlir_libs"
OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/_mlir_libs"
RELATIVE_INSTALL_ROOT "../../../.."
DECLARED_HEADERS
MLIRPythonCAPI.HeaderSources
Expand Down Expand Up @@ -838,7 +834,7 @@ endif()
################################################################################

add_mlir_python_modules(MLIRPythonModules
ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir"
ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}"
INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}"
DECLARED_SOURCES
MLIRPythonSources
Expand All @@ -847,4 +843,3 @@ add_mlir_python_modules(MLIRPythonModules
COMMON_CAPI_LINK_LIBS
MLIRPythonCAPI
)

1 change: 1 addition & 0 deletions mlir/python/mlir/_mlir_libs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_mlir/**/*.pyi
12 changes: 0 additions & 12 deletions mlir/python/mlir/_mlir_libs/_mlir/__init__.pyi

This file was deleted.

63 changes: 0 additions & 63 deletions mlir/python/mlir/_mlir_libs/_mlir/dialects/pdl.pyi

This file was deleted.

Loading