[Mlir-commits] [mlir] [MLIR][Python] Standalone wheel (PR #160388)

Maksim Levental llvmlistbot at llvm.org
Tue Sep 23 13:29:42 PDT 2025


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/160388

>From a2c0a3d477daed322cab9ddbe7c386d708dd64d1 Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Tue, 23 Sep 2025 13:16:00 -0700
Subject: [PATCH] [MLIR][Python] Standalone wheel

---
 mlir/examples/standalone/CMakeLists.txt       |  8 ++-
 .../examples/standalone/python/pyproject.toml | 64 +++++++++++++++++++
 mlir/examples/standalone/python/version.py    | 44 +++++++++++++
 mlir/test/Examples/standalone/lit.local.cfg   |  1 +
 mlir/test/Examples/standalone/test.wheel.toy  |  4 ++
 5 files changed, 119 insertions(+), 2 deletions(-)
 create mode 100644 mlir/examples/standalone/python/pyproject.toml
 create mode 100644 mlir/examples/standalone/python/version.py
 create mode 100644 mlir/test/Examples/standalone/test.wheel.toy

diff --git a/mlir/examples/standalone/CMakeLists.txt b/mlir/examples/standalone/CMakeLists.txt
index e2bcda7fa6f0b..c6c49fde12d2e 100644
--- a/mlir/examples/standalone/CMakeLists.txt
+++ b/mlir/examples/standalone/CMakeLists.txt
@@ -63,8 +63,12 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
   include(MLIRDetectPythonEnv)
   mlir_configure_python_dev_packages()
   # Note: for EXTERNAL_PROJECT_BUILD this must be set from the command line.
-  set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
-  set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
+  if(NOT MLIR_PYTHON_PACKAGE_PREFIX)
+    set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
+  endif()
+  if(NOT MLIR_BINDINGS_PYTHON_INSTALL_PREFIX)
+    set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
+  endif()
   add_subdirectory(python)
 endif()
 add_subdirectory(test)
diff --git a/mlir/examples/standalone/python/pyproject.toml b/mlir/examples/standalone/python/pyproject.toml
new file mode 100644
index 0000000000000..69e98d653483d
--- /dev/null
+++ b/mlir/examples/standalone/python/pyproject.toml
@@ -0,0 +1,64 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+# Copyright (c) 2025.
+
+[project]
+name = "standalone-python-bindings"
+dynamic = ["version"]
+requires-python = ">=3.8,<=3.13"
+dependencies = [
+    "numpy>=1.19.5, <=2.1.2",
+    "PyYAML>=5.4.0, <=6.0.2",
+    # required but no package for it...
+    # "ml_dtypes>=0.5.0, <=0.6.0"
+]
+
+[project.urls]
+Homepage = "https://github.com/llvm/llvm-project"
+Discussions = "https://discourse.llvm.org/"
+"Issue Tracker" = "https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3Amlir%3Apython%20"
+"Source Code" = "https://github.com/llvm/llvm-project/tree/main/mlir/python"
+
+[build-system]
+requires = [
+    "scikit-build-core==0.10.7",
+    # nanobind requires it for stubgen but we don't use stubgen?
+    "typing_extensions==4.12.2",
+    "nanobind>=2.9, <3.0",
+    "pybind11>=2.10.0, <=2.13.6",
+]
+build-backend = "scikit_build_core.build"
+
+[tool.scikit-build]
+experimental = true
+metadata.version.provider = "version"
+metadata.version.provider-path = "."
+
+minimum-version = "0.10"
+build-dir = "build"
+cmake.source-dir = "../"
+wheel.exclude = ["bin", "include", "lib", "src", "share"]
+# for installing/distributing only the python stuff
+build.targets = ["StandalonePythonModules"]
+install.components = ["StandalonePythonModules"]
+
+[tool.scikit-build.cmake.define]
+CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
+CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
+CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
+CMAKE_C_VISIBILITY_PRESET = "hidden"
+CMAKE_CXX_VISIBILITY_PRESET = "hidden"
+CMAKE_VERBOSE_MAKEFILE = "ON"
+
+# for installing/distributing only the python stuff
+LLVM_DISTRIBUTIONS = "StandalonePython"
+LLVM_StandalonePython_DISTRIBUTION_COMPONENTS = "StandalonePythonModules"
+LLVM_ENABLE_PROJECTS = "mlir"
+
+MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
+MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"
+MLIR_ENABLE_BINDINGS_PYTHON = "ON"
+MLIR_DIR = { env = "MLIR_DIR", default = "" }
+
+LLVM_NATIVE_TOOL_DIR = { env = "LLVM_NATIVE_TOOL_DIR", default = "" }
diff --git a/mlir/examples/standalone/python/version.py b/mlir/examples/standalone/python/version.py
new file mode 100644
index 0000000000000..8b323285b95a0
--- /dev/null
+++ b/mlir/examples/standalone/python/version.py
@@ -0,0 +1,44 @@
+from __future__ import annotations
+from pathlib import Path
+from datetime import datetime
+import os
+import re
+
+__all__ = ["dynamic_metadata"]
+
+
+def __dir__() -> list[str]:
+    return __all__
+
+
+def dynamic_metadata(
+    field: str,
+    settings: dict[str, object] | None = None,
+    _project: dict[str, object] = None,
+) -> str:
+
+    if field != "version":
+        msg = "Only the 'version' field is supported"
+        raise ValueError(msg)
+
+    if settings:
+        msg = "No inline configuration is supported"
+        raise ValueError(msg)
+
+    now = datetime.now()
+    llvm_datetime = os.environ.get(
+        "DATETIME", f"{now.year}{now.month:02}{now.day:02}{now.hour:02}"
+    )
+
+    llvm_src_root = Path(__file__).parent.parent.parent.parent.parent
+    cmake_version_path = llvm_src_root / "cmake/Modules/LLVMVersion.cmake"
+    if not cmake_version_path.exists():
+        cmake_version_path = llvm_src_root / "llvm/CMakeLists.txt"
+    cmake_txt = open(cmake_version_path).read()
+    llvm_version = []
+    for v in ["LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH"]:
+        vn = re.findall(rf"set\({v} (\d+)\)", cmake_txt)
+        assert vn, f"couldn't find {v} in cmake txt"
+        llvm_version.append(vn[0])
+
+    return f"{llvm_version[0]}.{llvm_version[1]}.{llvm_version[2]}.{llvm_datetime}"
diff --git a/mlir/test/Examples/standalone/lit.local.cfg b/mlir/test/Examples/standalone/lit.local.cfg
index 3b12dcbd99e83..5bf631964b770 100644
--- a/mlir/test/Examples/standalone/lit.local.cfg
+++ b/mlir/test/Examples/standalone/lit.local.cfg
@@ -9,5 +9,6 @@ config.substitutions.append(("%host_cxx", config.host_cxx))
 config.substitutions.append(("%host_cc", config.host_cc))
 config.substitutions.append(("%enable_libcxx", config.enable_libcxx))
 config.substitutions.append(("%mlir_cmake_dir", config.mlir_cmake_dir))
+config.substitutions.append(("%mlir_tools_dir", config.mlir_tools_dir))
 config.substitutions.append(("%llvm_use_linker", config.llvm_use_linker))
 config.substitutions.append(("%cmake_build_type", config.cmake_build_type))
diff --git a/mlir/test/Examples/standalone/test.wheel.toy b/mlir/test/Examples/standalone/test.wheel.toy
new file mode 100644
index 0000000000000..05c4a0e4aa752
--- /dev/null
+++ b/mlir/test/Examples/standalone/test.wheel.toy
@@ -0,0 +1,4 @@
+# RUN: LLVM_NATIVE_TOOL_DIR="%mlir_tools_dir" MLIR_DIR=%mlir_cmake_dir pip wheel . -v | tee %t
+# RUN: FileCheck --input-file=%t %s
+
+# CHECK: *** Created standalone_python_bindings-{{.*}}.whl



More information about the Mlir-commits mailing list