Skip to content

Commit 59e74a0

Browse files
authored
Reland "[MLIR][Python] add Python wheel build demo/test" (llvm#160481) (llvm#160488)
Reland standalone wheel build. The fix is to gate the test behind `BUILD_SHARED_LIBS=OFF` (because bundling all libs in the wheel requires valid rpaths which is not the case under `BUILD_SHARED_LIBS=ON`).
1 parent 3c4f611 commit 59e74a0

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

mlir/examples/standalone/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
6363
include(MLIRDetectPythonEnv)
6464
mlir_configure_python_dev_packages()
6565
# Note: for EXTERNAL_PROJECT_BUILD this must be set from the command line.
66-
set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
67-
set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
66+
if(NOT MLIR_PYTHON_PACKAGE_PREFIX)
67+
set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
68+
endif()
69+
if(NOT MLIR_BINDINGS_PYTHON_INSTALL_PREFIX)
70+
set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
71+
endif()
6872
add_subdirectory(python)
6973
endif()
7074
add_subdirectory(test)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
# Copyright (c) 2025.
5+
6+
[project]
7+
name = "standalone-python-bindings"
8+
dynamic = ["version"]
9+
requires-python = ">=3.8,<=3.14"
10+
dependencies = [
11+
"numpy>=1.19.5, <=2.1.2",
12+
"PyYAML>=5.4.0, <=6.0.1",
13+
"ml_dtypes>=0.1.0, <=0.6.0; python_version<'3.13'",
14+
"ml_dtypes>=0.5.0, <=0.6.0; python_version>='3.13'",
15+
]
16+
17+
[project.urls]
18+
Homepage = "https://github.com/llvm/llvm-project"
19+
Discussions = "https://discourse.llvm.org/"
20+
"Issue Tracker" = "https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3Amlir%3Apython%20"
21+
"Source Code" = "https://github.com/llvm/llvm-project/tree/main/mlir/python"
22+
23+
[build-system]
24+
requires = [
25+
"scikit-build-core>=0.10.7",
26+
"typing_extensions>=4.12.2",
27+
"nanobind>=2.9, <3.0",
28+
"pybind11>=2.10.0, <=2.13.6",
29+
]
30+
build-backend = "scikit_build_core.build"
31+
32+
[tool.scikit-build]
33+
# This is the minimum version of scikit-build-core.
34+
minimum-version = "0.10.7"
35+
# This pyproject.toml must be adjacent to the root CMakeLists.txt (wherever project(...) is specified).
36+
cmake.source-dir = "."
37+
# This is for installing/distributing the python bindings target and only the python bindings target.
38+
build.targets = ["StandalonePythonModules"]
39+
install.components = ["StandalonePythonModules"]
40+
41+
[tool.scikit-build.cmake.define]
42+
# Optional
43+
CMAKE_C_COMPILER = { env = "CMAKE_C_COMPILER", default = "" }
44+
CMAKE_CXX_COMPILER = { env = "CMAKE_CXX_COMPILER", default = "" }
45+
CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
46+
CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
47+
CMAKE_GENERATOR = { env = "CMAKE_GENERATOR", default = "Ninja" }
48+
LLVM_USE_LINKER = { env = "LLVM_USE_LINKER", default = "" }
49+
# Optional but highly recommended (this makes the bindings compatible with other bindings packages
50+
# by preventing symbol collisions).
51+
CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
52+
CMAKE_C_VISIBILITY_PRESET = "hidden"
53+
CMAKE_CXX_VISIBILITY_PRESET = "hidden"
54+
55+
# Non-optional (alternatively you could use CMAKE_PREFIX_PATH here).
56+
MLIR_DIR = { env = "MLIR_DIR", default = "" }
57+
# Non-optional
58+
CMAKE_BUILD_TYPE = { env = "CMAKE_BUILD_TYPE", default = "Release" }
59+
MLIR_ENABLE_BINDINGS_PYTHON = "ON"
60+
# Effectively non-optional (any downstream project should specify this).
61+
MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
62+
# This specifies the directory in the install directory (i.e., /tmp/pip-wheel/platlib) where _mlir_libs, dialects, etc.
63+
# are installed. Thus, this will be the package location (and the name of the package) that pip assumes is
64+
# the root package.
65+
MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"

mlir/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ llvm_canonicalize_cmake_booleans(
8484
MLIR_RUN_CUDA_SM80_TESTS
8585
MLIR_RUN_CUDA_SM80_LT_TESTS
8686
MLIR_RUN_CUDA_SM90_TESTS
87+
BUILD_SHARED_LIBS
8788
)
8889

8990
configure_lit_site_cfg(

mlir/test/Examples/standalone/lit.local.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ config.substitutions.append(("%cmake_exe", config.host_cmake))
77
config.substitutions.append(("%cmake_generator", config.host_cmake_generator))
88
config.substitutions.append(("%host_cxx", config.host_cxx))
99
config.substitutions.append(("%host_cc", config.host_cc))
10+
config.substitutions.append(("%hostc_compiler_launcher", config.host_c_compiler_launcher))
11+
config.substitutions.append(("%hostcxx_compiler_launcher", config.host_cxx_compiler_launcher))
1012
config.substitutions.append(("%enable_libcxx", config.enable_libcxx))
1113
config.substitutions.append(("%mlir_cmake_dir", config.mlir_cmake_dir))
14+
config.substitutions.append(("%mlir_obj_root", config.mlir_obj_root))
1215
config.substitutions.append(("%llvm_use_linker", config.llvm_use_linker))
1316
config.substitutions.append(("%cmake_build_type", config.cmake_build_type))
17+
18+
if not config.llvm_shared_libs_build:
19+
config.available_features.add("non-shared-libs-build")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# There's no real issue with windows here, it's just that some CMake generated paths for targets end up being longer
2+
# than 255 chars when combined with the fact that pip wants to install into a tmp directory buried under
3+
# C/Users/ContainerAdministrator/AppData/Local/Temp.
4+
# UNSUPPORTED: target={{.*(windows).*}}
5+
# REQUIRES: non-shared-libs-build
6+
7+
# RUN: export CMAKE_BUILD_TYPE=%cmake_build_type
8+
# RUN: export CMAKE_CXX_COMPILER=%host_cxx
9+
# RUN: export CMAKE_CXX_COMPILER_LAUNCHER=%hostcxx_compiler_launcher
10+
# RUN: export CMAKE_C_COMPILER=%host_cc
11+
# RUN: export CMAKE_C_COMPILER_LAUNCHER=%hostc_compiler_launcher
12+
# RUN: export CMAKE_GENERATOR=%cmake_generator
13+
# RUN: export LLVM_USE_LINKER=%llvm_use_linker
14+
# RUN: export MLIR_DIR="%mlir_cmake_dir"
15+
16+
# RUN: %python -m pip wheel "%mlir_src_root/examples/standalone" -w "%mlir_obj_root/wheelhouse" -v | tee %t
17+
18+
# RUN: rm -rf "%mlir_obj_root/standalone-python-bindings-install"
19+
# RUN: %python -m pip install standalone_python_bindings -f "%mlir_obj_root/wheelhouse" --target "%mlir_obj_root/standalone-python-bindings-install" -v | tee -a %t
20+
21+
# RUN: export PYTHONPATH="%mlir_obj_root/standalone-python-bindings-install"
22+
# RUN: %python "%mlir_src_root/examples/standalone/test/python/smoketest.py" nanobind | tee -a %t
23+
24+
# RUN: FileCheck --input-file=%t %s
25+
26+
# CHECK: Successfully built standalone-python-bindings
27+
28+
# CHECK: module {
29+
# CHECK: %[[C2:.*]] = arith.constant 2 : i32
30+
# CHECK: %[[V0:.*]] = standalone.foo %[[C2]] : i32
31+
# CHECK: }
32+

mlir/test/lit.site.cfg.py.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ config.native_target = "@LLVM_NATIVE_ARCH@"
1515
config.host_os = "@HOST_OS@"
1616
config.host_cc = "@HOST_CC@"
1717
config.host_cxx = "@HOST_CXX@"
18+
config.host_c_compiler_launcher = "@CMAKE_C_COMPILER_LAUNCHER@"
19+
config.host_cxx_compiler_launcher = "@CMAKE_CXX_COMPILER_LAUNCHER@"
1820
config.enable_libcxx = "@LLVM_ENABLE_LIBCXX@"
1921
config.host_cmake = "@CMAKE_COMMAND@"
2022
config.host_cmake_generator = "@CMAKE_GENERATOR@"
@@ -58,6 +60,7 @@ config.mlir_run_cuda_sm80_tests = @MLIR_RUN_CUDA_SM80_TESTS@
5860
config.mlir_run_cuda_sm80_lt_tests = @MLIR_RUN_CUDA_SM80_LT_TESTS@
5961
config.mlir_run_cuda_sm90_tests = @MLIR_RUN_CUDA_SM90_TESTS@
6062
config.mlir_include_integration_tests = @MLIR_INCLUDE_INTEGRATION_TESTS@
63+
config.llvm_shared_libs_build = @BUILD_SHARED_LIBS@
6164
config.arm_emulator_executable = "@ARM_EMULATOR_EXECUTABLE@"
6265
# Some tests marked with 'UNSUPPORTED: target=aarch64{{.*}}' are still run when
6366
# configured with ARM_EMULATOR_EXECUTABLE and the default target is not aarch64.

0 commit comments

Comments
 (0)