Skip to content

[Bazel] Add Bazel rules for Python bindings#4609

Open
kalvdans wants to merge 1 commit into
llvm:mainfrom
kalvdans:bazel-python-bindings
Open

[Bazel] Add Bazel rules for Python bindings#4609
kalvdans wants to merge 1 commit into
llvm:mainfrom
kalvdans:bazel-python-bindings

Conversation

@kalvdans

Copy link
Copy Markdown

Summary

Adds Bazel build support for the torch-mlir Python bindings. Until now the
Bazel build (utils/bazel/) only covered torch-mlir-opt and the C++
libraries; the Python package could only be built via CMake. This brings the
Bazel build closer to parity with CMake for projects that consume torch-mlir
through Bazel.

What's included

  • C API libraries in torch-mlir-overlay/BUILD.bazel: CAPITorch and
    CAPITorchRegisterEverything (via mlir_c_api_cc_library), plus an
    exports_files for include/torch-mlir-c/Registration.h.
  • python/BUILD.bazel (new):
    • generates the Torch op bindings (_torch_ops_gen.py) with
      gentbl_filegroup;
    • builds the _torchMlir.so (pybind11) and _mlirRegisterEverything.so
      (nanobind) extension modules;
    • assembles everything into a torch-mlir py_library, mirroring the
      package tree CMake produces (torch_mlir/, torch_mlir/dialects,
      torch_mlir/_mlir_libs).
  • python/fileutils.bzl (new): a small copy_into_directory rule that
    symlinks generated files into that package layout.

Notes

  • Per the Bazel support policy,
    this is peripheral-tier and opt-in. The default Bazel CI target
    (@torch-mlir//:torch-mlir-opt) is unaffected.
  • Like MLIR's own Bazel Python bindings, building the Python targets requires
    the consuming project to provide nanobind and a Python toolchain
    (@rules_python//python/cc libs) through its module graph; torch-mlir's
    standalone Bazel build does not register these.
  • BUILD/.bzl files were formatted with buildifier as requested by the
    contributing guidelines.

Testing

  • bazel query @torch-mlir//python:torch-mlir + @torch-mlir//:CAPITorch + @torch-mlir//:CAPITorchRegisterEverything resolves.
  • Analysis (bazel build --nobuild) of @torch-mlir//python:torch-mlir
    configures the full dependency graph against the pinned LLVM revision.
  • The targets build and import as a py_library dependency in a downstream
    Bazel (bzlmod) project that provides the nanobind/Python-toolchain deps.

Adds Bazel support for building the torch-mlir Python bindings, which
previously had no Bazel coverage:

- BUILD.bazel: add `mlir_c_api_cc_library` rules for the `CAPITorch` and
  `CAPITorchRegisterEverything` C API targets, and export
  `include/torch-mlir-c/Registration.h`.
- python/fileutils.bzl: add a `copy_into_directory` rule that symlinks
  generated files into the package subdirectory layout CMake produces.
- python/BUILD.bazel: generate the Torch op bindings, build the
  `_torchMlir.so` (pybind11) and `_mlirRegisterEverything.so` (nanobind)
  extension modules, and assemble everything into a `torch-mlir`
  py_library.
@kalvdans kalvdans marked this pull request as ready for review June 16, 2026 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant