Skip to content

Commit 5b2e7f5

Browse files
[MLIR][python] Export CAPI headers.
* Adds source targets (not included in the full set that downstreams use by default) to bundle mlir-c/ headers into the mlir/_mlir_libs/include directory. * Adds a minimal entry point to get include and library directories. * Used by npcomp to export a full CAPI (which is then used by the Torch extension to link npcomp). Reviewed By: mikeurbach Differential Revision: https://reviews.llvm.org/D107090
1 parent 532c458 commit 5b2e7f5

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
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+
# DEST_PREFIX: Destination prefix to prepend to files in the python
27+
# package directory namespace.
2628
function(declare_mlir_python_sources name)
2729
cmake_parse_arguments(ARG
2830
""
29-
"ROOT_DIR;ADD_TO_PARENT"
31+
"ROOT_DIR;ADD_TO_PARENT;DEST_PREFIX"
3032
"SOURCES;SOURCES_GLOB"
3133
${ARGN})
3234

@@ -54,6 +56,7 @@ function(declare_mlir_python_sources name)
5456
set_target_properties(${name} PROPERTIES
5557
PYTHON_SOURCES_TYPE pure
5658
PYTHON_ROOT_DIR "${ARG_ROOT_DIR}"
59+
PYTHON_DEST_PREFIX "${ARG_DEST_PREFIX}"
5760
PYTHON_SOURCES "${ARG_SOURCES}"
5861
PYTHON_FILE_DEPENDS "${_file_depends}"
5962
PYTHON_DEPENDS ""
@@ -130,9 +133,14 @@ function(add_mlir_python_modules name)
130133
# Pure python sources to link into the tree.
131134
get_target_property(_python_root_dir ${sources_target} PYTHON_ROOT_DIR)
132135
get_target_property(_python_sources ${sources_target} PYTHON_SOURCES)
136+
get_target_property(_specified_dest_prefix ${sources_target} PYTHON_DEST_PREFIX)
137+
set(_dest_prefix "${ARG_ROOT_PREFIX}")
138+
if(_specified_dest_prefix)
139+
set(_dest_prefix "${_dest_prefix}/${_specified_dest_prefix}")
140+
endif()
133141
foreach(_source_relative_path ${_python_sources})
134142
set(_src_path "${_python_root_dir}/${_source_relative_path}")
135-
set(_dest_path "${ARG_ROOT_PREFIX}/${_source_relative_path}")
143+
set(_dest_path "${_dest_prefix}/${_source_relative_path}")
136144

137145
get_filename_component(_dest_dir "${_dest_path}" DIRECTORY)
138146
get_filename_component(_install_path "${ARG_INSTALL_PREFIX}/${_source_relative_path}" DIRECTORY)

mlir/python/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ declare_mlir_python_sources(MLIRPythonSources.Passes
4646
transforms/*.py
4747
)
4848

49+
declare_mlir_python_sources(MLIRPythonCAPIHeaderSources
50+
ROOT_DIR "${MLIR_SOURCE_DIR}/include"
51+
SOURCES_GLOB "mlir-c/*.h"
52+
DEST_PREFIX "_mlir_libs/include"
53+
)
54+
4955
################################################################################
5056
# Dialect bindings
5157
################################################################################
@@ -304,6 +310,7 @@ add_mlir_python_modules(MLIRPythonModules
304310
DECLARED_SOURCES
305311
MLIRPythonSources
306312
MLIRPythonExtension.AllPassesRegistration
313+
MLIRPythonCAPIHeaderSources
307314
COMMON_CAPI_LINK_LIBS
308315
MLIRPythonCAPI
309316
)

mlir/python/mlir/_mlir_libs/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5+
from typing import Sequence
6+
57
import importlib
68
import os
79

@@ -19,3 +21,21 @@ def load_extension(name):
1921
def preload_dependency(public_name):
2022
# TODO: Implement this hook to pre-load DLLs with ctypes on Windows.
2123
pass
24+
25+
26+
def get_lib_dirs() -> Sequence[str]:
27+
"""Gets the lib directory for linking to shared libraries.
28+
29+
On some platforms, the package may need to be built specially to export
30+
development libraries.
31+
"""
32+
return [_this_dir]
33+
34+
35+
def get_include_dirs() -> Sequence[str]:
36+
"""Gets the include directory for compiling against exported C libraries.
37+
38+
Depending on how the package was build, development C libraries may or may
39+
not be present.
40+
"""
41+
return [os.path.join(_this_dir, "include")]

mlir/test/python/develoment_files.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: %PYTHON %s 2>&1
2+
3+
import os
4+
5+
from mlir._mlir_libs import get_include_dirs, get_lib_dirs
6+
7+
8+
header_file = os.path.join(get_include_dirs()[0], "mlir-c", "IR.h")
9+
assert os.path.isfile(header_file), f"Header does not exist: {header_file}"
10+
11+
# Since actual library names are platform specific, just scan the directory
12+
# for a filename that contains the library name.
13+
expected_lib_name = "MLIRPythonCAPI"
14+
all_libs = os.listdir(get_lib_dirs()[0])
15+
found_lib = False
16+
for file_name in all_libs:
17+
if expected_lib_name in file_name: found_lib = True
18+
assert found_lib, f"Did not find '{expected_lib_name}' lib in {all_libs}"

0 commit comments

Comments
 (0)