Skip to content
Draft
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion xls/build_rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ bzl_library(
bzl_library(
name = "xls_toolchains_bzl",
srcs = ["xls_toolchains.bzl"],
deps = [":xls_providers_bzl"],
deps = [
":xls_providers_bzl",
"@rules_cc//cc/common",
],
)

bzl_library(
Expand Down Expand Up @@ -345,6 +348,21 @@ bzl_library(
],
)

bzl_library(
name = "xls_codegen_pass_rules_bzl",
srcs = ["xls_codegen_pass_rules.bzl"],
parse_tests = False,
visibility = ["//visibility:private"],
deps = [
":xls_cc_embed_data_rules_bzl",
":xls_providers_bzl",
":xls_utilities_bzl",
"@bazel_skylib//lib:dicts",
"@rules_cc//cc:core_rules",
"@rules_cc//cc/common",
],
)

xls_toolchain(
name = "default_xls_toolchain",
visibility = ["//xls:xls_public"],
Expand Down
347 changes: 347 additions & 0 deletions xls/build_rules/xls_codegen_pass_rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
# Copyright 2026 The XLS Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module containing rules for defining codegen passes."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//xls/build_rules:xls_cc_embed_data_rules.bzl", "embed_data_attrs", _get_embedded_data = "get_embedded_data")
load(
"//xls/build_rules:xls_providers.bzl",
"XlsCodegenPassRegistryInfo",
)
load("//xls/build_rules:xls_utilities.bzl", "proto_data_tool_attrs", _textproto_to_binary = "text_proto_to_binary")

CodegenPassInfo = provider(
doc = "Information about a codegen pass and its registration.",
fields = {
"pass_impl": "The library containing the pass implementation.",
"pass_registration": "The library containing the pass registration.",
},
)

def _generate_registration_impl(ctx):
output_file = ctx.actions.declare_file(ctx.attr.name + ".cc")
headers = ""
for h in ctx.attr.lib[CcInfo].compilation_context.direct_public_headers:
headers += "\n"
headers += '#include "%s"' % h.path
ctx.actions.expand_template(
template = ctx.file._registration_template,
output = output_file,
substitutions = {
"{REGISTRATION_NAME}": ctx.attr.name,
"{SHORT_NAME}": ctx.attr.pass_class + "::kName",
"{NAME}": ctx.attr.pass_class,
"{WARNING}": "Generated file. Do not edit.",
"{HEADERS}": headers,
"{FIRST_HEADER_FILE}": ctx.attr.lib[CcInfo].compilation_context.direct_public_headers[0].path,
},
)
return [DefaultInfo(files = depset([output_file]))]

_generate_registration = rule(
implementation = _generate_registration_impl,
attrs = {
"_registration_template": attr.label(
default = Label("//xls/codegen_v_1_5/tools:codegen_pass_registration.cc.tmpl"),
allow_single_file = True,
),
"pass_class": attr.string(mandatory = True),
"lib": attr.label(mandatory = True, providers = [CcInfo]),
},
)

def _xls_codegen_pass_and_registration_impl(ctx):
return [
ctx.attr.pass_lib[CcInfo],
ctx.attr.pass_lib[DefaultInfo],
CodegenPassInfo(
pass_impl = ctx.attr.pass_lib,
pass_registration = ctx.attr.pass_reg,
),
]

_xls_codegen_pass_and_registration = rule(
implementation = _xls_codegen_pass_and_registration_impl,
attrs = {
"pass_lib": attr.label(mandatory = True, providers = [CcInfo]),
"pass_reg": attr.label(mandatory = True, providers = [CcInfo]),
},
)

# TODO(allight): We could make this a 'rule' but we need a macro solely to make
# sure that build-rule automation still works so might as well leave it as a
# macro.
# TODO(allight): Use new macro() system when available.
def xls_codegen_pass(name, pass_class, tags = [], **kwargs):
"""A rule to build an xls codegen pass library and the registration library.

Example:
```
xls_codegen_pass(
name = "clock_gating_pass",
pass_class = "ClockGatingPass",
srcs = ["clock_gating_pass.cc"],
hdrs = ["clock_gating_pass.h"],
deps = [...],
)
```

Args:
name: The name of the pass library.
pass_class: String name (with namespace) of the pass being created.
tags: Any tags to put on generated libraries.
**kwargs: Keyword arguments to pass to the cc_library.
"""
if "visibility" in kwargs:
visibility = kwargs["visibility"]
kwargs.pop("visibility")
else:
visibility = None

cc_library(
name = "%s_impl" % name,
visibility = ["//visibility:private"],
tags = tags + ["avoid_dep"],
**kwargs
)

_generate_registration(
name = "%s_registration_cc" % name,
lib = "%s_impl" % name,
visibility = ["//visibility:private"],
pass_class = pass_class,
)

cc_library(
name = "%s_registration" % name,
srcs = ["%s_registration_cc" % name],
visibility = ["//visibility:private"],
tags = tags,
deps = [
"%s_impl" % name,
"//xls/codegen_v_1_5:codegen_pass_registry",
"//xls/common:module_initializer",
"@com_google_absl//absl/log:check",
],
alwayslink = True,
)

_xls_codegen_pass_and_registration(
name = name,
pass_lib = "%s_impl" % name,
pass_reg = "%s_registration" % name,
visibility = visibility,
)

def _xls_codegen_pass_registry_impl(ctx):
out_files = []
if ctx.file.pipeline_binpb and ctx.file.pipeline:
fail("At most one of pipeline and pipeline_binpb may be present.")
cc_toolchain = find_cpp_toolchain(ctx)
cc_features = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
if ctx.file.pipeline_binpb:
pipeline_binpb = ctx.file.pipeline_binpb
pipeline_filename = pipeline_binpb.path
elif ctx.file.pipeline:
pipeline_filename = ctx.file.pipeline.path
pipeline_binpb = ctx.actions.declare_file(
ctx.file.pipeline.basename + "_" + ctx.attr.name + ".binpb",
)
out_files.append(pipeline_binpb)
_textproto_to_binary(
ctx = ctx,
src_file = ctx.file.pipeline,
output_file = pipeline_binpb,
proto_name = "xls.codegen.CodegenPipelineProto",
)
else:
pipeline_binpb = None
pipeline_filename = None

files_out = []
inputs = []
passes = []
pass_infos = []

linking_ctxs = []
for c in ctx.attr.passes:
if CodegenPassInfo in c:
inputs.append(c[CodegenPassInfo].pass_registration[CcInfo].linking_context)
passes.append(c[CodegenPassInfo].pass_registration[CcInfo])
pass_infos.append(CodegenPassInfo(
pass_impl = c[CodegenPassInfo].pass_impl[CcInfo],
pass_registration = c[CodegenPassInfo].pass_registration[CcInfo],
))
elif XlsCodegenPassRegistryInfo in c:
for p in c[XlsCodegenPassRegistryInfo].passes:
inputs.append(p.linking_context)
passes.append(p)
pass_infos.extend(c[XlsCodegenPassRegistryInfo].pass_infos)
else:
inputs.append(c[CcInfo].linking_context)
passes.append(c[CcInfo])
pass_infos.append(CodegenPassInfo(
pass_impl = c[CcInfo],
pass_registration = c[CcInfo],
))

linking_ctxs.extend(inputs)
if pipeline_binpb:
mangled_label = str(abs(hash("@@{}//{}:{}".format(
ctx.label.workspace_name,
ctx.label.package,
ctx.label.name,
))))
accessor = "get_%s_pipeline" % mangled_label
proto_data_fn = "xls::codegen::pass_registry::%s" % accessor
emb_header = ctx.actions.declare_file("%s_embedded_pipeline.h" % ctx.attr.name)
emb_cc = ctx.actions.declare_file("%s_embedded_pipeline.cc" % ctx.attr.name)
files_out.append(emb_header)
files_out.append(emb_cc)
embedded_binproto_data = _get_embedded_data(
ctx = ctx,
name = ctx.attr.name + "_embedded_pipeline_binpb",
hdr_file = emb_header,
cpp_file = emb_cc,
namespace = "xls::codegen::pass_registry",
accessor = accessor,
data_file = pipeline_binpb,
)
reg_pipeline_cc = ctx.actions.declare_file("%s_declare_pipeline.cc" % ctx.attr.name)
files_out.append(reg_pipeline_cc)
ctx.actions.expand_template(
template = ctx.file._register_pipeline_template,
output = reg_pipeline_cc,
substitutions = {
"{WARNING}": "Generated file. Do not edit",
"{HEADER}": emb_header.path,
"{ACCESS_FN}": proto_data_fn,
"{MANGLED_LABEL}": accessor,
"{FILE}": pipeline_filename,
},
)
deps = [embedded_binproto_data.compilation_context]
link_deps = [embedded_binproto_data.linking_context]
for lib in ctx.attr._register_pipeline_deps:
deps.append(lib[CcInfo].compilation_context)
link_deps.append(lib[CcInfo].linking_context)
(comp_ctx, comp_out) = cc_common.compile(
name = ctx.label.name + "_pipeline",
actions = ctx.actions,
feature_configuration = cc_features,
cc_toolchain = cc_toolchain,
srcs = [reg_pipeline_cc],
compilation_contexts = deps,
)
(link_ctx, _link_out) = cc_common.create_linking_context_from_compilation_outputs(
name = ctx.label.name + "_pipeline",
actions = ctx.actions,
feature_configuration = cc_features,
cc_toolchain = cc_toolchain,
compilation_outputs = comp_out,
linking_contexts = link_deps,
alwayslink = True,
)
linking_ctxs.append(link_ctx)

comp_out = cc_common.create_compilation_outputs()
comp_ctx = cc_common.create_compilation_context()
(link, out) = cc_common.create_linking_context_from_compilation_outputs(
name = ctx.label.name,
actions = ctx.actions,
feature_configuration = cc_features,
cc_toolchain = cc_toolchain,
compilation_outputs = comp_out,
linking_contexts = linking_ctxs,
alwayslink = True,
)
files_out.extend(out.library_to_link.pic_objects)
cc_info = CcInfo(compilation_context = comp_ctx, linking_context = link)

default_info = DefaultInfo(files = depset(
direct = files_out,
transitive = [c[DefaultInfo].files for c in ctx.attr.passes],
))
return [
cc_info,
default_info,
XlsCodegenPassRegistryInfo(
cc_library = cc_info,
passes = passes,
pipeline_binpb = pipeline_binpb,
pass_infos = pass_infos,
default_info = default_info,
pipeline_src = ctx.file.pipeline,
),
]

xls_codegen_pass_registry = rule(
implementation = _xls_codegen_pass_registry_impl,
provides = [CcInfo],
fragments = ["cpp"],
toolchains = use_cpp_toolchain(),
attrs = dicts.add(
{
"passes": attr.label_list(
providers = [[CodegenPassInfo], [CcInfo]],
),
"pipeline": attr.label(
allow_single_file = True,
mandatory = False,
),
"pipeline_binpb": attr.label(
allow_single_file = True,
mandatory = False,
),
"_register_pipeline_template": attr.label(
default = Label("//xls/codegen_v_1_5/tools:codegen_pipeline_registration.cc.tmpl"),
allow_single_file = True,
),
"_register_pipeline_deps": attr.label_list(
default = [
Label("//xls/codegen_v_1_5:codegen_pass_registry"),
Label("//xls/common:module_initializer"),
Label("@com_google_absl//absl/log:check"),
],
),
},
proto_data_tool_attrs,
embed_data_attrs,
),
)

def _xls_codegen_default_pass_registry(ctx):
config = ctx.toolchains["//xls/common/toolchains:toolchain_type"].configuration
return [
config.codegen_pass_registry,
config.codegen_pass_registry.cc_library,
config.codegen_pass_registry.default_info,
]

xls_codegen_default_pass_registry = rule(
implementation = _xls_codegen_default_pass_registry,
doc = """A pass registry with the default codegen pipeline.""",
provides = [CcInfo],
toolchains = ["//xls/common/toolchains:toolchain_type"],
)
Loading
Loading