Skip to content

Commit 46d35c2

Browse files
GMNGeoffreymemfrob
authored andcommitted
[Bazel] Rework LLVM target selection
This patch introduces a custom rule for expanding the LLVM target enumeration .def files. This provides a slightly cleaner API for these rules, but is mostly to permit selects to be used when determining which LLVM targets to build. Right now the target list is generated at Bazel configure time, but this will allows us to add functionality to also control which targets are built based on config settings. Tested: Ran `bazel test --config=rbe ... @llvm-project//...` Reviewed By: chandlerc Differential Revision: https://reviews.llvm.org/D104969
1 parent 260d24d commit 46d35c2

File tree

2 files changed

+77
-16
lines changed

2 files changed

+77
-16
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load(":template_rule.bzl", "template_rule")
66
load(":tblgen.bzl", "gentbl")
77
load(":config.bzl", "llvm_config_defines")
88
load(":targets.bzl", "llvm_targets")
9+
load(":enum_targets_gen.bzl", "enum_targets_gen")
910
load(":binary_alias.bzl", "binary_alias")
1011

1112
package(
@@ -24,22 +25,20 @@ llvm_copts = [
2425
"$(STACK_FRAME_UNLIMITED)",
2526
]
2627

27-
template_rule(
28+
enum_targets_gen(
2829
name = "targets_def_gen",
2930
src = "include/llvm/Config/Targets.def.in",
3031
out = "include/llvm/Config/Targets.def",
31-
substitutions = {
32-
"@LLVM_ENUM_TARGETS@": "\n".join(["LLVM_TARGET({})".format(t) for t in llvm_targets]),
33-
},
32+
macro_name = "TARGET",
33+
targets = llvm_targets,
3434
)
3535

36-
template_rule(
36+
enum_targets_gen(
3737
name = "asm_printers_def_gen",
3838
src = "include/llvm/Config/AsmPrinters.def.in",
3939
out = "include/llvm/Config/AsmPrinters.def",
40-
substitutions = {
41-
"@LLVM_ENUM_ASM_PRINTERS@": "\n".join(["LLVM_ASM_PRINTER({})".format(t) for t in llvm_targets]),
42-
},
40+
macro_name = "ASM_PRINTER",
41+
targets = llvm_targets,
4342
)
4443

4544
# List of targets with ASM parsers, filtered to our list of overall targets.
@@ -57,13 +56,12 @@ llvm_target_asm_parsers = [t for t in [
5756
"X86",
5857
] if t in llvm_targets]
5958

60-
template_rule(
59+
enum_targets_gen(
6160
name = "asm_parsers_def_gen",
6261
src = "include/llvm/Config/AsmParsers.def.in",
6362
out = "include/llvm/Config/AsmParsers.def",
64-
substitutions = {
65-
"@LLVM_ENUM_ASM_PARSERS@": "\n".join(["LLVM_ASM_PARSER({})".format(t) for t in llvm_target_asm_parsers]),
66-
},
63+
macro_name = "ASM_PARSER",
64+
targets = llvm_target_asm_parsers,
6765
)
6866

6967
# List of targets with disassemblers, filtered to our list of overall targets.
@@ -81,13 +79,12 @@ llvm_target_disassemblers = [t for t in [
8179
"X86",
8280
] if t in llvm_targets]
8381

84-
template_rule(
82+
enum_targets_gen(
8583
name = "disassemblers_def_gen",
8684
src = "include/llvm/Config/Disassemblers.def.in",
8785
out = "include/llvm/Config/Disassemblers.def",
88-
substitutions = {
89-
"@LLVM_ENUM_DISASSEMBLERS@": "\n".join(["LLVM_DISASSEMBLER({})".format(t) for t in llvm_target_disassemblers]),
90-
},
86+
macro_name = "DISASSEMBLER",
87+
targets = llvm_target_disassemblers,
9188
)
9289

9390
# TODO: Need to replace this with something that actually extracts the git
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
"""A rule to expand LLVM target enumerations.
6+
7+
Replaces in a text file a single variable of the style `@LLVM_ENUM_FOOS@` with a
8+
list of macro invocations, one for each target on its own line:
9+
10+
```
11+
LLVM_FOO(TARGET1)
12+
LLVM_FOO(TARGET2)
13+
// ...
14+
```
15+
16+
Example:
17+
load(":enum_targets_gen.bzl", "enum_targets_gen")
18+
19+
enum_targets_gen(
20+
name = "disassemblers_def_gen",
21+
src = "include/llvm/Config/Disassemblers.def.in",
22+
out = "include/llvm/Config/Disassemblers.def",
23+
macro_name = "DISASSEMBLER",
24+
targets = llvm_target_disassemblers,
25+
)
26+
27+
This rule provides a slightly more semantic API than template_rule, but the main
28+
reason it exists is to permit a list with selects to be passed for `targets` as
29+
a select is not allowed to be passed to a rule within another data structure.
30+
"""
31+
32+
def enum_targets_gen_impl(ctx):
33+
to_replace = "@LLVM_ENUM_{}S@".format(ctx.attr.macro_name)
34+
replacement = "\n".join([
35+
"LLVM_{}({})\n".format(ctx.attr.macro_name, t)
36+
for t in ctx.attr.targets
37+
])
38+
39+
ctx.actions.expand_template(
40+
template = ctx.file.src,
41+
output = ctx.outputs.out,
42+
substitutions = {to_replace: replacement},
43+
)
44+
45+
enum_targets_gen = rule(
46+
attrs = {
47+
"src": attr.label(
48+
mandatory = True,
49+
allow_single_file = True,
50+
),
51+
"out": attr.output(mandatory = True),
52+
"targets": attr.string_list(mandatory = True),
53+
"macro_name": attr.string(
54+
mandatory = True,
55+
doc = "The name of the enumeration. This is the suffix of the" +
56+
" placeholder being replaced `@LLVM_ENUM_{}S@` and of the" +
57+
" macro invocations generated `LLVM_{}(TARGET)`. Should be" +
58+
" all caps and singular, e.g. 'DISASSEMBLER'",
59+
),
60+
},
61+
# output_to_genfiles is required for header files.
62+
output_to_genfiles = True,
63+
implementation = enum_targets_gen_impl,
64+
)

0 commit comments

Comments
 (0)