Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bazel configs for some C APIs and pybinds #3879

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 95 additions & 1 deletion utils/bazel/torch-mlir-overlay/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")
load("@llvm-project//mlir:build_defs.bzl", "mlir_c_api_cc_library")
load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "gentbl_filegroup", "td_library")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

package(
Expand Down Expand Up @@ -923,3 +924,96 @@ cc_binary(
"@llvm-project//mlir:MlirOptLib",
],
)

# C API bindings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For isolation and ease of maintenance, I'd suggest creating a separate python/BUILD.bazel to host everything below.

mlir_c_api_cc_library(
name = "CAPITorchRegisterEverything",
srcs = ["lib/CAPI/Registration.cpp"],
hdrs = ["include/torch-mlir-c/Registration.h"],
capi_deps = ["@llvm-project//mlir:CAPIIR"],
deps = [
":TorchMLIRInitAll",
"@llvm-project//mlir:AllPassesAndDialects",
],
)

mlir_c_api_cc_library(
name = "CAPITorch",
srcs = ["lib/CAPI/Dialects.cpp"],
hdrs = ["include/torch-mlir-c/Dialects.h"],
capi_deps = ["@llvm-project//mlir:CAPIIR"],
deps = [":TorchMLIRTorchDialect"],
)

# These flags are needed for pybind11 to work.
PYBIND11_COPTS = [
"-fexceptions",
"-frtti",
]

PYBIND11_FEATURES = [
# Cannot use header_modules (parse_headers feature fails).
"-use_header_modules",
]

# pybind11 extension module
cc_binary(
name = "_torchMlir.so",
srcs = [
"include/torch-mlir-c/Registration.h",
"python/TorchMLIRModule.cpp",
],
copts = PYBIND11_COPTS,
features = PYBIND11_FEATURES,
linkshared = 1,
linkstatic = 0,
deps = [
":CAPITorch",
":CAPITorchRegisterEverything",
"@llvm-project//mlir:CAPIIR",
"@llvm-project//mlir:MLIRBindingsPythonHeadersAndDeps",
],
)

# python files
td_library(
name = "TorchOpsPyTdFiles",
srcs = [":MLIRTorchOpsIncGenTdFiles"],
includes = ["include"],
deps = [
"@llvm-project//mlir:BuiltinDialectTdFiles",
"@llvm-project//mlir:OpBaseTdFiles",
],
)

gentbl_filegroup(
name = "TorchOpsPyGen",
includes = ["include"],
tbl_outs = [
(
[
"-gen-python-op-bindings",
"-bind-dialect=torch",
],
"python/torch_mlir/dialects/_torch_ops_gen.py",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "python/torch_mlir/dialects/TorchBinding.td",
deps = [
":TorchOpsPyTdFiles",
"@llvm-project//mlir:AttrTdFiles",
],
)

filegroup(
name = "TorchOpsPyFiles",
srcs = [
":TorchOpsPyGen",
],
)

filegroup(
name = "TorchPyFiles",
srcs = glob(["python/**/*.py"]),
)
Loading