Skip to content

Commit 25c54e5

Browse files
committed
[Bazel] Configure static compiler plugins
Replace the hardcoded Bazel StaticLinkedPlugins.inc contents with a generated registry driven by //compiler/plugins:enabled_plugins and the --iree_compiler_plugins alias. This mirrors the runtime driver selection model while preserving the existing default plugin set for plain Bazel invocation. Teach iree-bazel-configure/configure_bazel.py to resolve IREE_COMPILER_PLUGINS, including all/exclusion syntax, from buildable local inputs. The wrapper defaults to all buildable plugins so developer builds pick up optional inputs such as Torch when the submodule is initialized, while explicit plugin lists fail on unknown or unavailable entries. CUDA selection also records the repository env var consumed by @iree_cuda so plugin selection and external repository configuration agree. Make the compiler plugin registry root-loadable so a downstream Bazel root can provide its own default_compiler_plugins.bzl and register out-of-tree plugins without editing IREE. Align MLIR-adjacent source repositories with the existing LLVM ownership model: IREE creates bundled LLVM, StableHLO, and torch-mlir sources when it is the root module, while downstream roots can provide their own repositories and configure torch-mlir through its upstream Bazel overlay. Add Bazel wiring for the Torch input plugin. This lets Bazel-built tools link input_torch and process Torch dialect input through the same conversion paths covered by the existing lit tests. Those tests are only compatible with tools built with input_torch enabled, and the generated CMake preserves the same option guard. Teach bazel_to_cmake to preserve supported compiler-plugin compatibility selects, alongside the existing platform selects, so generated CMake does not silently drop option-gated lit tests. Declare the LLVMIR translation helper's dialect translation dependencies on the helper target itself, matching the headers it includes instead of relying on consumers to expose incidental MLIR translation libraries. Detect Vulkan SDK installs during Bazel configuration and persist the normalized VULKAN_SDK repository environment for LLVM's Vulkan SDK repository rule when available.
1 parent 7009ee1 commit 25c54e5

21 files changed

Lines changed: 1052 additions & 116 deletions

File tree

MODULE.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ use_repo(
6767
"vulkan_sdk",
6868
)
6969

70-
# IREE extension creates llvm-raw (when IREE is root) and other IREE-specific repos
70+
# IREE extension creates MLIR-adjacent source repos when IREE is root and other
71+
# IREE-specific repos unconditionally.
7172
iree_ext = use_extension("//build_tools/bazel:extensions.bzl", "iree_extension")
7273
use_repo(
7374
iree_ext,
@@ -83,6 +84,7 @@ use_repo(
8384
"rccl",
8485
"spirv_cross",
8586
"stablehlo",
87+
"torch-mlir-raw",
8688
"tracy_client",
8789
"vulkan_headers",
8890
"webgpu_headers",
@@ -103,3 +105,12 @@ llvm_configure(
103105
"X86",
104106
],
105107
)
108+
109+
# Configure torch-mlir using its upstream Bazel overlay. The configured
110+
# repository is only needed when the Torch input plugin is selected.
111+
torch_mlir_configure = use_repo_rule("@torch-mlir-raw//utils/bazel:configure.bzl", "torch_mlir_configure")
112+
113+
torch_mlir_configure(
114+
name = "torch-mlir",
115+
src_workspace = "@torch-mlir-raw//:CMakeLists.txt",
116+
)

build_tools/bazel/BZLMOD_LLVM.md

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# Using IREE with a Custom LLVM via Bzlmod
1+
# Using IREE with Custom MLIR-Adjacent Dependencies via Bzlmod
22

3-
This document explains how projects that depend on IREE can provide their own LLVM
4-
instead of using IREE's bundled version.
3+
This document explains how projects that depend on IREE can provide their own
4+
LLVM, StableHLO, torch-mlir, and compiler plugin registry instead of using
5+
IREE's bundled defaults.
56

67
## Terminology
78

@@ -38,9 +39,11 @@ use_repo(ext, "repo_a", "repo_b")
3839
Imports a repository rule from another module so it can be called directly in
3940
MODULE.bazel to create a repository.
4041

41-
### `llvm-raw`
42-
A repository containing the raw LLVM source code. This is the input to the LLVM
43-
build configuration.
42+
### Raw source repositories
43+
Repositories such as `llvm-raw` and `torch-mlir-raw` contain unconfigured
44+
upstream source trees. They are inputs to repository rules that overlay Bazel
45+
BUILD files and produce configured repositories such as `llvm-project` and
46+
`torch-mlir`.
4447

4548
### `llvm-project`
4649
The configured LLVM repository created by `llvm_configure`. It overlays Bazel BUILD
@@ -53,8 +56,8 @@ The bzlmod module name for LLVM's Bazel integration (located at
5356

5457
## How It Works
5558

56-
IREE's module extension (`iree_extension`) creates the `llvm-raw` repository
57-
**only when IREE is the root module**:
59+
IREE's module extension (`iree_extension`) creates MLIR-adjacent source
60+
repositories **only when IREE is the root module**:
5861

5962
```python
6063
# In build_tools/bazel/extensions.bzl
@@ -65,11 +68,21 @@ def _iree_extension_impl(module_ctx):
6568
build_file_content = "# empty",
6669
path = "third_party/llvm-project",
6770
)
71+
local_repository(
72+
name = "stablehlo",
73+
path = "third_party/stablehlo",
74+
)
75+
new_local_repository(
76+
name = "torch-mlir-raw",
77+
build_file_content = "# empty - BUILD files overlaid by torch_mlir_configure",
78+
path = "third_party/torch-mlir",
79+
)
6880
# ... other repos
6981
```
7082

7183
When your project depends on IREE, IREE is **not** the root module - your project is.
72-
Therefore, IREE's extension will not create `llvm-raw`, and you must provide it yourself.
84+
Therefore, IREE's extension will not create these MLIR-adjacent repositories,
85+
and you must provide the ones needed by the compiler plugins you enable.
7386

7487
## MODULE.bazel Ordering
7588

@@ -82,7 +95,7 @@ The order of statements in MODULE.bazel matters:
8295
5. `use_repo()` - must come after its corresponding `use_extension()`
8396
6. `use_repo_rule()` + invocation - can reference repos created by earlier extensions
8497

85-
## Example: Using Your Own LLVM
98+
## Example: Using Your Own LLVM, StableHLO, and torch-mlir
8699

87100
```python
88101
# my_project/MODULE.bazel
@@ -114,7 +127,7 @@ local_path_override(
114127
path = "my/custom/llvm-project/utils/bazel",
115128
)
116129

117-
# Create your own llvm-raw repository pointing to your LLVM
130+
# Create your own raw repositories pointing to the upstream projects you want.
118131
new_local_repository = use_repo_rule(
119132
"@bazel_tools//tools/build_defs/repo:local.bzl",
120133
"new_local_repository",
@@ -124,6 +137,19 @@ new_local_repository(
124137
path = "my/custom/llvm-project",
125138
build_file_content = "# empty",
126139
)
140+
new_local_repository(
141+
name = "torch-mlir-raw",
142+
path = "my/custom/torch-mlir",
143+
build_file_content = "# empty",
144+
)
145+
local_repository = use_repo_rule(
146+
"@bazel_tools//tools/build_defs/repo:local.bzl",
147+
"local_repository",
148+
)
149+
local_repository(
150+
name = "stablehlo",
151+
path = "my/custom/stablehlo",
152+
)
127153

128154
# Use LLVM's extension for third-party deps (gmp, mpfr, etc.)
129155
llvm_repos_ext = use_extension(
@@ -161,8 +187,34 @@ llvm_configure = use_repo_rule(
161187
"llvm_configure",
162188
)
163189
llvm_configure(name = "llvm-project")
190+
191+
# Configure torch-mlir from your raw source repository if you enable the Torch
192+
# input plugin.
193+
torch_mlir_configure = use_repo_rule(
194+
"@torch-mlir-raw//utils/bazel:configure.bzl",
195+
"torch_mlir_configure",
196+
)
197+
torch_mlir_configure(
198+
name = "torch-mlir",
199+
src_workspace = "@torch-mlir-raw//:CMakeLists.txt",
200+
)
201+
```
202+
203+
## Custom Compiler Plugin Registry
204+
205+
IREE's `//compiler/plugins` package loads the plugin registry from the root
206+
workspace:
207+
208+
```python
209+
load("@//build_tools/bazel:default_compiler_plugins.bzl", ...)
164210
```
165211

212+
When IREE is the root module, this resolves to IREE's default registry. A
213+
downstream root workspace can provide a file at the same path to register
214+
additional compiler plugins, replace registration targets, or change the
215+
default enabled plugin IDs. In-tree IREE plugin labels should be qualified with
216+
`@iree_core//...` from such a downstream file.
217+
166218
## Using LLVM from an HTTP Archive
167219

168220
If you want to fetch LLVM from a release tarball instead of a local path:
@@ -204,7 +256,15 @@ When providing your own LLVM, ensure compatibility with IREE:
204256

205257
### "repository 'llvm-raw' is not defined"
206258
You haven't created the `llvm-raw` repository. As the root module, you must
207-
define it yourself (see examples above).
259+
define it yourself if you configure LLVM (see examples above).
260+
261+
### "repository 'stablehlo' is not defined"
262+
You enabled the StableHLO input plugin without providing a `stablehlo`
263+
repository from your root module.
264+
265+
### "repository 'torch-mlir' is not defined"
266+
You enabled the Torch input plugin without configuring a `torch-mlir` repository
267+
from your root module.
208268

209269
### Build errors in LLVM code
210270
Your LLVM version may be incompatible with IREE. Check that your LLVM commit
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2026 The IREE Authors
2+
#
3+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
7+
"""Default compiler plugin registry for Bazel builds.
8+
9+
IREE's plugin aggregation BUILD file loads this file from the root workspace
10+
with @//build_tools/bazel:default_compiler_plugins.bzl. When IREE is built as
11+
part of a larger Bazel workspace, that root workspace may provide a file at the
12+
same path to register its own compiler plugins or override the default set.
13+
"""
14+
15+
def iree_default_compiler_plugin_ids():
16+
"""Returns plugin IDs enabled by plain Bazel invocation."""
17+
return [
18+
"input_stablehlo",
19+
"input_tosa",
20+
"hal_target_cuda",
21+
"hal_target_llvm_cpu",
22+
"hal_target_local",
23+
"hal_target_metal_spirv",
24+
"hal_target_rocm",
25+
"hal_target_vmvx",
26+
"hal_target_vulkan_spirv",
27+
"example",
28+
"simple_io_sample",
29+
]
30+
31+
def iree_default_compiler_plugins():
32+
"""Returns all known compiler plugin registration targets."""
33+
return {
34+
# Input plugins.
35+
"input_stablehlo": "//compiler/plugins/input/StableHLO:registration",
36+
"input_tosa": "//compiler/plugins/input/TOSA:registration",
37+
"input_torch": "//compiler/plugins/input/Torch:registration",
38+
# Target plugins.
39+
"hal_target_cuda": "//compiler/plugins/target/CUDA",
40+
"hal_target_llvm_cpu": "//compiler/plugins/target/LLVMCPU",
41+
"hal_target_local": "//compiler/plugins/target/Local",
42+
"hal_target_metal_spirv": "//compiler/plugins/target/MetalSPIRV",
43+
"hal_target_rocm": "//compiler/plugins/target/ROCM",
44+
"hal_target_vmvx": "//compiler/plugins/target/VMVX",
45+
"hal_target_vulkan_spirv": "//compiler/plugins/target/VulkanSPIRV",
46+
# Sample plugins.
47+
"example": "//samples/compiler_plugins/example:registration",
48+
"simple_io_sample": "//samples/compiler_plugins/simple_io_sample:registration",
49+
}

build_tools/bazel/extensions.bzl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,26 @@ load("//build_tools/bazel:workspace.bzl", "cuda_auto_configure")
1313
def _iree_extension_impl(module_ctx):
1414
"""Implementation of the IREE module extension."""
1515

16-
# Create llvm-raw only when IREE is the root module.
17-
# This allows downstream consumers to provide their own LLVM.
18-
if any([m.is_root and m.name == "iree_core" for m in module_ctx.modules]):
16+
iree_is_root = any([m.is_root and m.name == "iree_core" for m in module_ctx.modules])
17+
18+
# Create MLIR-adjacent source repositories only when IREE is the root
19+
# module. Downstream consumers can provide their own LLVM, StableHLO, and
20+
# torch-mlir repositories when IREE is part of a larger project.
21+
if iree_is_root:
1922
new_local_repository(
2023
name = "llvm-raw",
2124
build_file_content = "# empty",
2225
path = "third_party/llvm-project",
2326
)
27+
local_repository(
28+
name = "stablehlo",
29+
path = "third_party/stablehlo",
30+
)
31+
new_local_repository(
32+
name = "torch-mlir-raw",
33+
build_file_content = "# empty - BUILD files overlaid by torch_mlir_configure",
34+
path = "third_party/torch-mlir",
35+
)
2436

2537
# Googletest
2638
local_repository(
@@ -42,12 +54,6 @@ def _iree_extension_impl(module_ctx):
4254
path = "third_party/vulkan_headers",
4355
)
4456

45-
# StableHLO
46-
local_repository(
47-
name = "stablehlo",
48-
path = "third_party/stablehlo",
49-
)
50-
5157
# Benchmark
5258
local_repository(
5359
name = "com_google_benchmark",

build_tools/bazel/iree.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ build --force_pic
4848
# Build settings flag aliases. See https://bazel.build/rules/config
4949
###############################################################################
5050
build --flag_alias=iree_drivers=//runtime/src/iree/hal/drivers:enabled_drivers
51+
build --flag_alias=iree_runtime_drivers=//runtime/src/iree/hal/drivers:enabled_drivers
52+
build --flag_alias=iree_compiler_plugins=//compiler/plugins:enabled_plugins
5153
build --flag_alias=iree_link_compiler_shared=//compiler/src/iree/compiler/API:link_shared
5254
# Equivalent to CMake flag IREE_ENABLE_RUNTIME_TRACING + IREE_TRACING_PROVIDER.
5355
# Builds the runtime with tracing support enabled.

build_tools/bazel/workspace.bzl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
load("@bazel_skylib//lib:paths.bzl", "paths")
99
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1010

11+
IREE_INPUT_TORCH_ENV_KEY = "IREE_INPUT_TORCH"
1112
CUDA_TOOLKIT_ROOT_ENV_KEY = "IREE_CUDA_TOOLKIT_ROOT"
1213

1314
# Our CI docker images use a stripped down CUDA directory tree in some
@@ -67,6 +68,78 @@ cuda_auto_configure = repository_rule(
6768
},
6869
)
6970

71+
def torch_mlir_auto_configure_impl(repository_ctx):
72+
"""Conditionally configures torch-mlir based on IREE_INPUT_TORCH env var."""
73+
env = repository_ctx.os.environ
74+
iree_repo_alias = repository_ctx.attr.iree_repo_alias
75+
enabled = env.get(IREE_INPUT_TORCH_ENV_KEY, "OFF").upper() in ["ON", "TRUE", "1", "YES"]
76+
77+
if enabled:
78+
# Run torch-mlir's configure to create the overlay.
79+
# We need to find the torch-mlir source and run its overlay script.
80+
torch_mlir_path = repository_ctx.path(
81+
Label("%s//:third_party/torch-mlir/CMakeLists.txt" % iree_repo_alias),
82+
).dirname
83+
bazel_path = torch_mlir_path.get_child("utils").get_child("bazel")
84+
overlay_path = bazel_path.get_child("torch-mlir-overlay")
85+
script_path = bazel_path.get_child("overlay_directories.py")
86+
87+
python_bin = repository_ctx.which("python3")
88+
if not python_bin:
89+
python_bin = repository_ctx.which("python")
90+
if not python_bin:
91+
fail("Failed to find python3 binary for torch-mlir configuration")
92+
93+
cmd = [
94+
python_bin,
95+
script_path,
96+
"--src",
97+
torch_mlir_path,
98+
"--overlay",
99+
overlay_path,
100+
"--target",
101+
".",
102+
]
103+
exec_result = repository_ctx.execute(cmd, timeout = 60)
104+
105+
if exec_result.return_code != 0:
106+
fail(("Failed to configure torch-mlir: '{cmd}'\n" +
107+
"Exited with code {return_code}\n" +
108+
"stdout:\n{stdout}\n" +
109+
"stderr:\n{stderr}\n").format(
110+
cmd = " ".join([str(arg) for arg in cmd]),
111+
return_code = exec_result.return_code,
112+
stdout = exec_result.stdout,
113+
stderr = exec_result.stderr,
114+
))
115+
else:
116+
# Create stub repository when torch-mlir is disabled.
117+
repository_ctx.file(
118+
"BUILD.bazel",
119+
content = """# Stub: torch-mlir disabled (IREE_INPUT_TORCH != ON)
120+
package(default_visibility = ["//visibility:public"])
121+
122+
# Provide empty targets that dependent code can reference.
123+
# These will fail at build time if actually used.
124+
""",
125+
)
126+
127+
torch_mlir_auto_configure = repository_rule(
128+
environ = [IREE_INPUT_TORCH_ENV_KEY],
129+
implementation = torch_mlir_auto_configure_impl,
130+
local = True,
131+
attrs = {
132+
"iree_repo_alias": attr.string(default = "@iree_core"),
133+
},
134+
)
135+
136+
def configure_iree_torch_mlir_deps(iree_repo_alias = None):
137+
maybe(
138+
torch_mlir_auto_configure,
139+
name = "torch-mlir",
140+
iree_repo_alias = iree_repo_alias,
141+
)
142+
70143
def configure_iree_cuda_deps(iree_repo_alias = None):
71144
maybe(
72145
cuda_auto_configure,

0 commit comments

Comments
 (0)