Skip to content

Commit 889dc92

Browse files
committed
[mlir][bazel] Build MLIR bindings extensions with nanobind_bazel
Instead of maintaining custom nanobind and robin-map build files, and bootstrapping these archives with a custom module extension, use `nanobind_bazel` from the BCR. The advantage of this is that less maintenance is needed inside the LLVM Bazel tree, and usage of nanobind_bazel's wrapper macros save a few lines of code while directly expressing the extensions as `nanobind_extension`s, which is more indicative of the target's purpose than just a `cc_binary`.
1 parent f218573 commit 889dc92

File tree

5 files changed

+33
-124
lines changed

5 files changed

+33
-124
lines changed

utils/bazel/MODULE.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module(name = "llvm-project-overlay")
88

99
bazel_dep(name = "apple_support", version = "1.24.1", repo_name = "build_bazel_apple_support")
1010
bazel_dep(name = "bazel_skylib", version = "1.8.2")
11+
bazel_dep(name = "nanobind_bazel", version = "2.9.2")
1112
bazel_dep(name = "platforms", version = "1.0.0")
1213
bazel_dep(name = "protobuf", version = "31.1", repo_name = "com_google_protobuf")
1314
bazel_dep(name = "rules_android", version = "0.6.6")
@@ -25,11 +26,9 @@ use_repo(
2526
"llvm_zstd",
2627
"mpc",
2728
"mpfr",
28-
"nanobind",
2929
"pfm",
3030
"pybind11",
3131
"pyyaml",
32-
"robin_map",
3332
"vulkan_headers",
3433
"vulkan_sdk",
3534
)

utils/bazel/extensions.bzl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,6 @@ def _llvm_repos_extension_impl(module_ctx):
105105
build_file = "@llvm-raw//utils/bazel/third_party_build:pyyaml.BUILD",
106106
)
107107

108-
# TODO: bump to robin-map-1.4.0
109-
http_archive(
110-
name = "robin_map",
111-
build_file = "@llvm-raw//utils/bazel/third_party_build:robin_map.BUILD",
112-
sha256 = "a8424ad3b0affd4c57ed26f0f3d8a29604f0e1f2ef2089f497f614b1c94c7236",
113-
strip_prefix = "robin-map-1.3.0",
114-
url = "https://github.com/Tessil/robin-map/archive/refs/tags/v1.3.0.tar.gz",
115-
)
116-
117-
http_archive(
118-
name = "nanobind",
119-
build_file = "@llvm-raw//utils/bazel/third_party_build:nanobind.BUILD",
120-
sha256 = "8ce3667dce3e64fc06bfb9b778b6f48731482362fb89a43da156632266cd5a90",
121-
strip_prefix = "nanobind-2.9.2",
122-
url = "https://github.com/wjakob/nanobind/archive/refs/tags/v2.9.2.tar.gz",
123-
)
124-
125108
llvm_repos_extension = module_extension(
126109
implementation = _llvm_repos_extension_impl,
127110
)

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
99
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
10+
load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension", "nanobind_library")
1011
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
1112
load("@rules_python//python:defs.bzl", "py_binary")
1213
load("//llvm:targets.bzl", "llvm_targets")
@@ -1130,39 +1131,31 @@ cc_library(
11301131
],
11311132
)
11321133

1133-
cc_library(
1134+
nanobind_library(
11341135
name = "MLIRBindingsPythonNanobindHeaders",
11351136
includes = [
11361137
"include",
11371138
],
11381139
textual_hdrs = [":MLIRBindingsPythonHeaderFiles"],
11391140
deps = [
11401141
":CAPIIRHeaders",
1141-
"@nanobind",
11421142
"@rules_python//python/cc:current_py_cc_headers",
11431143
],
11441144
)
11451145

1146-
cc_library(
1146+
nanobind_library(
11471147
name = "MLIRBindingsPythonNanobindHeadersAndDeps",
11481148
includes = [
11491149
"include",
11501150
],
11511151
textual_hdrs = [":MLIRBindingsPythonHeaderFiles"],
11521152
deps = [
11531153
":CAPIIR",
1154-
"@nanobind",
11551154
"@rules_python//python/cc:current_py_cc_headers",
11561155
],
11571156
)
11581157

1159-
# These flags are needed for pybind11 to work.
1160-
PYBIND11_COPTS = [
1161-
"-fexceptions",
1162-
"-frtti",
1163-
]
1164-
1165-
PYBIND11_FEATURES = [
1158+
NB_FEATURES = [
11661159
# Cannot use header_modules (parse_headers feature fails).
11671160
"-use_header_modules",
11681161
]
@@ -1188,11 +1181,10 @@ filegroup(
11881181
]),
11891182
)
11901183

1191-
cc_library(
1184+
nanobind_library(
11921185
name = "MLIRBindingsPythonCore",
11931186
srcs = [":MLIRBindingsPythonSourceFiles"],
1194-
copts = PYBIND11_COPTS,
1195-
features = PYBIND11_FEATURES,
1187+
features = NB_FEATURES,
11961188
includes = [
11971189
"lib/Bindings/Python",
11981190
],
@@ -1207,16 +1199,14 @@ cc_library(
12071199
":Support",
12081200
":config",
12091201
"//llvm:Support",
1210-
"@nanobind",
12111202
"@rules_python//python/cc:current_py_cc_headers",
12121203
],
12131204
)
12141205

1215-
cc_library(
1206+
nanobind_library(
12161207
name = "MLIRBindingsPythonCoreNoCAPI",
12171208
srcs = [":MLIRBindingsPythonSourceFiles"],
1218-
copts = PYBIND11_COPTS,
1219-
features = PYBIND11_FEATURES,
1209+
features = NB_FEATURES,
12201210
includes = [
12211211
"lib/Bindings/Python",
12221212
],
@@ -1230,7 +1220,6 @@ cc_library(
12301220
":Support",
12311221
":config",
12321222
"//llvm:Support",
1233-
"@nanobind",
12341223
"@rules_python//python/cc:current_py_cc_headers",
12351224
],
12361225
)
@@ -1249,130 +1238,105 @@ cc_library(
12491238
)
12501239

12511240
# Dynamic library with the MLIR Python extension.
1252-
cc_binary(
1253-
name = "_mlir.so",
1241+
nanobind_extension(
1242+
name = "_mlir",
12541243
srcs = ["lib/Bindings/Python/MainModule.cpp"],
1255-
# These flags are needed for pybind11 to work.
1256-
copts = PYBIND11_COPTS,
1257-
features = PYBIND11_FEATURES,
1244+
features = NB_FEATURES,
12581245
includes = [
12591246
"lib/Bindings/Python",
12601247
],
1261-
linkshared = 1,
12621248
linkstatic = 0,
12631249
deps = [
12641250
":MLIRBindingsPythonCore",
12651251
":MLIRBindingsPythonHeadersAndDeps",
1266-
"@nanobind",
12671252
],
12681253
)
12691254

1270-
cc_binary(
1271-
name = "_mlirDialectsIRDL.so",
1255+
nanobind_extension(
1256+
name = "_mlirDialectsIRDL",
12721257
srcs = ["lib/Bindings/Python/DialectIRDL.cpp"],
1273-
copts = PYBIND11_COPTS,
1274-
features = PYBIND11_FEATURES,
1275-
linkshared = 1,
1258+
features = NB_FEATURES,
12761259
linkstatic = 0,
12771260
deps = [
12781261
":CAPIIR",
12791262
":MLIRBindingsPythonNanobindHeadersAndDeps",
1280-
"@nanobind",
12811263
],
12821264
)
12831265

1284-
cc_binary(
1285-
name = "_mlirDialectsLinalg.so",
1266+
nanobind_extension(
1267+
name = "_mlirDialectsLinalg",
12861268
srcs = ["lib/Bindings/Python/DialectLinalg.cpp"],
1287-
copts = PYBIND11_COPTS,
1288-
features = PYBIND11_FEATURES,
1269+
features = NB_FEATURES,
12891270
includes = [
12901271
"lib/Bindings/Python",
12911272
],
1292-
linkshared = 1,
12931273
linkstatic = 0,
12941274
deps = [
12951275
":CAPIIR",
12961276
":CAPILinalg",
12971277
":MLIRBindingsPythonNanobindHeadersAndDeps",
1298-
"@nanobind",
12991278
],
13001279
)
13011280

1302-
cc_binary(
1303-
name = "_mlirDialectsLLVM.so",
1281+
nanobind_extension(
1282+
name = "_mlirDialectsLLVM",
13041283
srcs = ["lib/Bindings/Python/DialectLLVM.cpp"],
1305-
copts = PYBIND11_COPTS,
1306-
features = PYBIND11_FEATURES,
1307-
linkshared = 1,
1284+
features = NB_FEATURES,
13081285
linkstatic = 0,
13091286
deps = [
13101287
":CAPIIR",
13111288
":CAPILLVM",
13121289
":CAPITarget",
13131290
":MLIRBindingsPythonNanobindHeadersAndDeps",
1314-
"@nanobind",
13151291
],
13161292
)
13171293

1318-
cc_binary(
1319-
name = "_mlirDialectsQuant.so",
1294+
nanobind_extension(
1295+
name = "_mlirDialectsQuant",
13201296
srcs = ["lib/Bindings/Python/DialectQuant.cpp"],
1321-
copts = PYBIND11_COPTS,
1322-
features = PYBIND11_FEATURES,
1323-
linkshared = 1,
1297+
features = NB_FEATURES,
13241298
linkstatic = 0,
13251299
deps = [
13261300
":CAPIIR",
13271301
":CAPIQuant",
13281302
":MLIRBindingsPythonNanobindHeadersAndDeps",
1329-
"@nanobind",
13301303
],
13311304
)
13321305

1333-
cc_binary(
1334-
name = "_mlirDialectsSparseTensor.so",
1306+
nanobind_extension(
1307+
name = "_mlirDialectsSparseTensor",
13351308
srcs = ["lib/Bindings/Python/DialectSparseTensor.cpp"],
1336-
copts = PYBIND11_COPTS,
1337-
features = PYBIND11_FEATURES,
1338-
linkshared = 1,
1309+
features = NB_FEATURES,
13391310
linkstatic = 0,
13401311
deps = [
13411312
":CAPIIR",
13421313
":CAPISparseTensor",
13431314
":MLIRBindingsPythonNanobindHeadersAndDeps",
1344-
"@nanobind",
13451315
],
13461316
)
13471317

13481318
# Dynamic library with the MLIR Conversions Python extension.
1349-
cc_binary(
1350-
name = "_mlirExecutionEngine.so",
1319+
nanobind_extension(
1320+
name = "_mlirExecutionEngine",
13511321
srcs = ["lib/Bindings/Python/ExecutionEngineModule.cpp"],
1352-
copts = PYBIND11_COPTS,
1353-
features = PYBIND11_FEATURES,
1354-
linkshared = 1,
1322+
features = NB_FEATURES,
13551323
linkstatic = 0,
13561324
deps = [
13571325
":CAPIExecutionEngine",
13581326
":MLIRBindingsPythonNanobindHeadersAndDeps",
1359-
"@nanobind",
13601327
"@rules_python//python/cc:current_py_cc_headers",
13611328
],
13621329
)
13631330

13641331
# Dynamic library with the MLIR Linalg dialect+passes Python extension.
1365-
cc_binary(
1366-
name = "_mlirLinalgPasses.so",
1332+
nanobind_extension(
1333+
name = "_mlirLinalgPasses",
13671334
srcs = ["lib/Bindings/Python/LinalgPasses.cpp"],
1368-
copts = PYBIND11_COPTS,
1369-
features = PYBIND11_FEATURES,
1370-
linkshared = 1,
1335+
features = NB_FEATURES,
13711336
linkstatic = 0,
13721337
deps = [
13731338
":CAPILinalg",
13741339
":MLIRBindingsPythonNanobindHeadersAndDeps",
1375-
"@nanobind",
13761340
"@rules_python//python/cc:current_py_cc_headers",
13771341
],
13781342
)

utils/bazel/third_party_build/nanobind.BUILD

Lines changed: 0 additions & 25 deletions
This file was deleted.

utils/bazel/third_party_build/robin_map.BUILD

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)