xref: /llvm-project/utils/bazel/llvm-project-overlay/mlir/linalggen.bzl (revision 65cead410be042d7f627c806b985c30082bed704)
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"""BUILD extensions for MLIR linalg generation."""
6
7def genlinalg(name, linalggen, src, linalg_outs):
8    """genlinalg() generates code from a tc spec file.
9
10    Args:
11      name: The name of the build rule for use in dependencies.
12      linalggen: The binary used to produce the output.
13      src: The tc spec file.
14      linalg_outs: A list of tuples (opts, out), where each opts is a string of
15        options passed to linalggen, and the out is the corresponding output file
16        produced.
17    """
18
19    for (opts, out) in linalg_outs:
20        # All arguments to generate the output except output destination.
21        base_args = [
22            "$(location %s)" % linalggen,
23            "%s" % opts,
24            "$(location %s)" % src,
25        ]
26        rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" "))
27
28        # Rule to generate code using generated shell script.
29        native.genrule(
30            name = "%s_%s_genrule" % (name, rule_suffix),
31            srcs = [src],
32            outs = [out],
33            tools = [linalggen],
34            cmd = (" ".join(base_args)),
35        )
36
37    hdrs = [f for (opts, f) in linalg_outs]
38    native.cc_library(
39        name = name,
40        hdrs = hdrs,
41        textual_hdrs = hdrs,
42    )
43