Skip to content

Commit 9c5b57a

Browse files
Use FetchContent for both pybind11 and nanobind. (#18872)
This avoids various pinning problems and ensures that the version that IREE specifies at the top of a build is used consistently throughout. The nanobind incantation was taken from shortfin. The pybind11 incantation was adapted from [a comment](pybind/pybind11#2817 (comment)) and extended to use find_package integration. The latter is sufficient for MLIR's pybind11 finding to delegate to the one set at the top level. The MLIR code for finding pybind11 is ancient and should be modernized to use FetchContent and find_package integration with a pinned version. This would ensure consistent interop with the rest of the ecosystem. --------- Signed-off-by: Stella Laurenzo <stellaraccident@gmail.com> Co-authored-by: Marius Brehler <marius.brehler@amd.com>
1 parent 00104b5 commit 9c5b57a

File tree

7 files changed

+32
-44
lines changed

7 files changed

+32
-44
lines changed

.gitmodules

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
[submodule "third_party/vulkan_headers"]
88
path = third_party/vulkan_headers
99
url = https://github.com/KhronosGroup/Vulkan-Headers.git
10-
[submodule "third_party/pybind11"]
11-
path = third_party/pybind11
12-
url = https://github.com/pybind/pybind11.git
13-
branch = stable
1410
[submodule "third_party/benchmark"]
1511
path = third_party/benchmark
1612
url = https://github.com/google/benchmark.git

CMakeLists.txt

+24-13
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,30 @@ endif()
772772
# MLIR/LLVM Dependency
773773
#-------------------------------------------------------------------------------
774774

775+
# Both the IREE and MLIR Python bindings require pybind11. We initialize it here
776+
# at the top level so that everything uses ours consistently.
777+
if(IREE_BUILD_PYTHON_BINDINGS AND IREE_BUILD_COMPILER)
778+
set(pybind11_VERSION 2.13.6)
779+
include(FetchContent)
780+
FetchContent_Declare(
781+
pybind11
782+
GIT_REPOSITORY https://github.com/pybind/pybind11
783+
GIT_TAG v${pybind11_VERSION}
784+
)
785+
set(PYBIND11_FINDPYTHON ON)
786+
FetchContent_MakeAvailable(pybind11)
787+
# pybind11 source fetches do not include find_package integration, which is
788+
# a shame since sub-projects can require that to work. If we were using
789+
# CMake 3.24, we could just add OVERRIDE_FIND_PACKAGE to the
790+
# FetchContent_Declare call above and it would take care of doing the
791+
# following to let subsequent sub-project find_package calls to resolve
792+
# successfully.
793+
set(pybind11_DIR "${pybind11_BINARY_DIR}")
794+
file(WRITE "${pybind11_BINARY_DIR}/pybind11Config.cmake" "")
795+
file(WRITE "${pybind11_BINARY_DIR}/pybind11ConfigVersion.cmake"
796+
"set(PACKAGE_VERSION ${pybind11_VERSION})\nset(PACKAGE_VERSION_COMPATIBLE TRUE)")
797+
endif()
798+
775799
if(NOT IREE_BUILD_COMPILER)
776800
message(STATUS "Not adding LLVM/MLIR because the configuration does not require it")
777801
else()
@@ -921,19 +945,6 @@ if(IREE_BUILD_TESTS)
921945
include(iree_configure_testing)
922946
endif()
923947

924-
if(IREE_BUILD_PYTHON_BINDINGS)
925-
# The compiler uses pybind11
926-
if(IREE_BUILD_COMPILER)
927-
if(NOT TARGET pybind11::module)
928-
message(STATUS "Using bundled pybind11")
929-
set(PYBIND11_FINDPYTHON ON)
930-
add_subdirectory(third_party/pybind11 EXCLUDE_FROM_ALL)
931-
else()
932-
message(STATUS "Not including bundled pybind11 (already configured)")
933-
endif()
934-
endif()
935-
endif()
936-
937948
if(IREE_TARGET_BACKEND_METAL_SPIRV)
938949
# SPIRV-Cross is needed to cross compile SPIR-V into MSL source code.
939950
iree_set_spirv_cross_cmake_options()

compiler/pyproject.toml

-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"cmake",
6-
# Note that the compiler wheel does not presently need nanobind, but
7-
# it's build is enabled by the same flag which enables the runtime
8-
# configuration, which does.
9-
"nanobind==2.2.0",
106
"ninja",
117
# MLIR build depends.
128
"numpy",
139
"packaging",
14-
"pybind11==2.13.6",
1510
"sympy",
1611
]
1712
build-backend = "setuptools.build_meta"

runtime/bindings/python/CMakeLists.txt

+8-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
# See https://llvm.org/LICENSE.txt for license information.
55
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66

7-
if(NOT nanobind_FOUND)
8-
find_package(nanobind CONFIG QUIET)
9-
if(NOT nanobind_FOUND)
10-
execute_process(
11-
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
12-
OUTPUT_STRIP_TRAILING_WHITESPACE
13-
OUTPUT_VARIABLE NB_DIR
14-
RESULT_VARIABLE RC)
15-
if(RC AND NOT RC EQUAL 0)
16-
message(WARNING "Probing for nanobind failed. Please install the project's Python dependencies or '${Python_EXECUTABLE} -m pip install nanobind'")
17-
endif()
18-
list(APPEND CMAKE_PREFIX_PATH "${NB_DIR}")
19-
endif()
20-
find_package(nanobind CONFIG REQUIRED)
21-
endif()
7+
# nanobind
8+
include(FetchContent)
9+
FetchContent_Declare(
10+
nanobind
11+
GIT_REPOSITORY https://github.com/wjakob/nanobind.git
12+
GIT_TAG 784efa2a0358a4dc5432c74f5685ee026e20f2b6 # 2.2.0
13+
)
14+
FetchContent_MakeAvailable(nanobind)
2215

2316
set(_EXTRA_INSTALL_TOOL_TARGETS)
2417
set(_TRACY_ENABLED OFF)

runtime/bindings/python/iree/runtime/build_requirements.txt

-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55

66
pip>=21.3
77
setuptools>=62.4.0
8-
nanobind==2.2.0
98
numpy>=2.0.0b1
109
requests>=2.28.0
1110
wheel>=0.36.2
1211
sympy==1.12.1
13-
14-
# TODO: nanobind is used in the runtime but the compiler uses pybind and
15-
# removing this breaks CI bots; remove this.
16-
pybind11==2.13.6

runtime/pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ requires = [
33
"setuptools>=42",
44
"wheel",
55
"cmake",
6-
"nanobind==2.2.0",
76
"ninja",
87
"numpy>=2.0.0b1",
98
"packaging",

third_party/pybind11

-1
This file was deleted.

0 commit comments

Comments
 (0)