Skip to content

Commit 5815846

Browse files
committed
[MLIR][Python] enable precise registration
1 parent 538325f commit 5815846

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
# grouping. Source groupings form a DAG.
2424
# SOURCES: List of specific source files relative to ROOT_DIR to include.
2525
# SOURCES_GLOB: List of glob patterns relative to ROOT_DIR to include.
26+
# EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
27+
# on. These will be collected for all extensions and put into an
28+
# aggregate dylib that is linked against.
2629
function(declare_mlir_python_sources name)
2730
cmake_parse_arguments(ARG
2831
""
2932
"ROOT_DIR;ADD_TO_PARENT"
30-
"SOURCES;SOURCES_GLOB"
33+
"SOURCES;SOURCES_GLOB;EMBED_CAPI_LINK_LIBS"
3134
${ARGN})
3235

3336
if(NOT ARG_ROOT_DIR)
@@ -53,9 +56,10 @@ function(declare_mlir_python_sources name)
5356
set_target_properties(${name} PROPERTIES
5457
# Yes: Leading-lowercase property names are load bearing and the recommended
5558
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
56-
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_DEPENDS"
59+
EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_DEPENDS;mlir_python_EMBED_CAPI_LINK_LIBS"
5760
mlir_python_SOURCES_TYPE pure
5861
mlir_python_DEPENDS ""
62+
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
5963
)
6064

6165
# Use the interface include directories and sources on the target to carry the
@@ -374,6 +378,9 @@ endfunction()
374378
# This file is where the *EnumAttrs are defined, not where the *Enums are defined.
375379
# **WARNING**: This arg will shortly be removed when the just-below TODO is satisfied. Use at your
376380
# risk.
381+
# EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
382+
# on. These will be collected for all extensions and put into an
383+
# aggregate dylib that is linked against.
377384
#
378385
# TODO: Right now `TD_FILE` can't be the actual dialect tablegen file, since we
379386
# use its path to determine where to place the generated python file. If
@@ -383,7 +390,7 @@ function(declare_mlir_dialect_python_bindings)
383390
cmake_parse_arguments(ARG
384391
"GEN_ENUM_BINDINGS"
385392
"ROOT_DIR;ADD_TO_PARENT;TD_FILE;DIALECT_NAME"
386-
"SOURCES;SOURCES_GLOB;DEPENDS;GEN_ENUM_BINDINGS_TD_FILE"
393+
"SOURCES;SOURCES_GLOB;DEPENDS;GEN_ENUM_BINDINGS_TD_FILE;EMBED_CAPI_LINK_LIBS"
387394
${ARGN})
388395
# Sources.
389396
set(_dialect_target "${ARG_ADD_TO_PARENT}.${ARG_DIALECT_NAME}")
@@ -424,6 +431,7 @@ function(declare_mlir_dialect_python_bindings)
424431
ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
425432
ADD_TO_PARENT "${_dialect_target}"
426433
SOURCES ${_sources}
434+
EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS}"
427435
)
428436
endif()
429437
endfunction()

mlir/python/CMakeLists.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ declare_mlir_dialect_python_bindings(
326326
SOURCES
327327
dialects/arith.py
328328
DIALECT_NAME arith
329-
GEN_ENUM_BINDINGS)
329+
GEN_ENUM_BINDINGS
330+
EMBED_CAPI_LINK_LIBS
331+
MLIRCAPIArith
332+
)
330333

331334
declare_mlir_dialect_python_bindings(
332335
ADD_TO_PARENT MLIRPythonSources.Dialects
@@ -846,8 +849,20 @@ endif()
846849
# once ready.
847850
################################################################################
848851

852+
set(MLIR_PYTHON_CAPI_DYLIB_NAME MLIRPythonCAPI)
853+
configure_file(
854+
"${CMAKE_CURRENT_LIST_DIR}/mlir/_mlir_libs/_capi.py.in"
855+
"${CMAKE_CURRENT_BINARY_DIR}/_mlir_libs/_capi.py"
856+
@ONLY
857+
)
858+
declare_mlir_python_sources(
859+
MLIRPythonCAPICTypesBinding
860+
ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
861+
SOURCES _mlir_libs/_capi.py
862+
)
863+
849864
set(MLIRPythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
850-
add_mlir_python_common_capi_library(MLIRPythonCAPI
865+
add_mlir_python_common_capi_library(${MLIR_PYTHON_CAPI_DYLIB_NAME}
851866
INSTALL_COMPONENT MLIRPythonModules
852867
INSTALL_DESTINATION "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/_mlir_libs"
853868
OUTPUT_DIRECTORY "${MLIRPythonModules_ROOT_PREFIX}/_mlir_libs"
@@ -966,6 +981,7 @@ add_mlir_python_modules(MLIRPythonModules
966981
MLIRPythonSources
967982
MLIRPythonExtension.RegisterEverything
968983
MLIRPythonExtension.Core.type_stub_gen
984+
MLIRPythonCAPICTypesBinding
969985
${_ADDL_TEST_SOURCES}
970986
COMMON_CAPI_LINK_LIBS
971987
MLIRPythonCAPI
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
import ctypes
6+
from pathlib import Path
7+
8+
_capi = ctypes.CDLL(str(Path(__file__).parent / "@CMAKE_SHARED_LIBRARY_PREFIX@@MLIR_PYTHON_CAPI_DYLIB_NAME@@CMAKE_SHARED_LIBRARY_SUFFIX@"))

mlir/test/python/ir/capi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# RUN: %PYTHON %s | FileCheck %s
2+
3+
from mlir._mlir_libs._capi import _capi
4+
5+
print("success")
6+
# CHECK: success

0 commit comments

Comments
 (0)