| # Copyright 2025 The IREE Authors | |
| # | |
| # Licensed 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 | |
| """Bzlmod support for registering external HAL drivers with IREE. | |
| Any module that depends on IREE can opt in by adding an `external_hal_driver` | |
| tag to `iree_extension` in its MODULE.bazel. The extension collects the tags of | |
| all modules and generates the `@iree_external_hal_drivers` repository. IREE's | |
| runtime/src/iree/hal/drivers/BUILD.bazel loads the driver list from there. | |
| Without any tags, the list is empty. | |
| See EXTERNAL_DRIVERS.md for documentation. | |
| """ | |
| external_hal_driver_tag = tag_class( | |
| doc = "Registers an external HAL driver with the IREE runtime.", | |
| attrs = { | |
| "target": attr.label( | |
| mandatory = True, | |
| doc = "Driver registration library. It must define register_fn.", | |
| ), | |
| "register_fn": attr.string( | |
| mandatory = True, | |
| doc = "C registration function, called as " + | |
| "`iree_status_t register_fn(iree_hal_driver_registry_t*)`.", | |
| ), | |
| "enabled_by": attr.label( | |
| doc = "Optional config_setting. If set, the driver is only built " + | |
| "and registered when this config_setting matches. If not " + | |
| "set, the driver is always enabled.", | |
| ), | |
| }, | |
| ) | |
| def _iree_external_hal_drivers_repo_impl(repository_ctx): | |
| entries = [] | |
| for target, register_fn, enabled_by in zip( | |
| repository_ctx.attr.targets, | |
| repository_ctx.attr.register_fns, | |
| repository_ctx.attr.enabled_by, | |
| ): | |
| fields = [ | |
| "target = {}".format(repr(target)), | |
| "register_fn = {}".format(repr(register_fn)), | |
| ] | |
| if enabled_by: | |
| fields.append("enabled_by = {}".format(repr(enabled_by))) | |
| entries.append(" struct({}),\n".format(", ".join(fields))) | |
| repository_ctx.file("BUILD.bazel", "# Generated by iree_extension.\n") | |
| repository_ctx.file("drivers.bzl", "".join([ | |
| '"""External HAL drivers. Generated by iree_extension."""\n', | |
| "\n", | |
| "def get_external_hal_drivers():\n", | |
| " return [\n", | |
| ] + entries + [ | |
| " ]\n", | |
| ])) | |
| _iree_external_hal_drivers_repo = repository_rule( | |
| implementation = _iree_external_hal_drivers_repo_impl, | |
| attrs = { | |
| # Parallel lists, one entry per driver. Labels are canonical strings. | |
| "targets": attr.string_list(), | |
| "register_fns": attr.string_list(), | |
| # Empty string: the driver is always enabled. | |
| "enabled_by": attr.string_list(), | |
| }, | |
| ) | |
| def iree_external_hal_drivers_repo(module_ctx, name): | |
| """Generates the external HAL driver repository from all modules' tags. | |
| Args: | |
| module_ctx: The module_ctx of the calling module extension. | |
| name: Name of the repository to generate. | |
| """ | |
| targets = [] | |
| register_fns = [] | |
| enabled_by = [] | |
| owners = {} | |
| for mod in module_ctx.modules: | |
| for tag in mod.tags.external_hal_driver: | |
| if tag.register_fn in owners: | |
| fail(("External HAL driver register_fn '{}' is registered " + | |
| "by both module '{}' and module '{}'.").format( | |
| tag.register_fn, | |
| owners[tag.register_fn], | |
| mod.name, | |
| )) | |
| owners[tag.register_fn] = mod.name | |
| # str() of a Label gives the canonical form (@@repo//pkg:name), | |
| # which resolves the same way from IREE's BUILD files. | |
| targets.append(str(tag.target)) | |
| register_fns.append(tag.register_fn) | |
| enabled_by.append(str(tag.enabled_by) if tag.enabled_by else "") | |
| _iree_external_hal_drivers_repo( | |
| name = name, | |
| targets = targets, | |
| register_fns = register_fns, | |
| enabled_by = enabled_by, | |
| ) |